|
211 | 211 | let(:access_token) { instance_double(OAuth2::AccessToken) }
|
212 | 212 | let(:response) { instance_double(OAuth2::Response, parsed: { 'id' => 'test123' }) }
|
213 | 213 |
|
214 |
| - before { allow(strategy).to receive(:access_token).and_return(access_token) } |
215 |
| - |
216 |
| - context 'when API request is successful' do |
217 |
| - before { allow(access_token).to receive(:get).with('/api/v2/authenticated_user').and_return(response) } |
218 |
| - |
219 |
| - it 'fetches user info from API' do |
220 |
| - expect(strategy.raw_info).to eq({ 'id' => 'test123' }) |
221 |
| - end |
222 |
| - |
223 |
| - it 'memoizes the result' do |
224 |
| - 2.times { strategy.raw_info } |
225 |
| - expect(access_token).to have_received(:get).once |
226 |
| - end |
227 |
| - end |
228 |
| - |
229 |
| - context 'when API returns 401 Unauthorized' do |
230 |
| - let(:error_response) { instance_double(OAuth2::Response, status: 401) } |
231 |
| - let(:oauth_error) { OAuth2::Error.new(error_response) } |
232 |
| - |
233 |
| - before do |
234 |
| - allow(access_token).to receive(:get).with('/api/v2/authenticated_user').and_raise(oauth_error) |
235 |
| - allow(strategy).to receive(:log) |
236 |
| - end |
237 |
| - |
238 |
| - it 'raises OmniAuth::NoSessionError' do |
239 |
| - expect { strategy.raw_info }.to raise_error(OmniAuth::NoSessionError, 'Invalid access token') |
240 |
| - end |
241 |
| - |
242 |
| - it 'logs the error' do |
243 |
| - expect { strategy.raw_info }.to raise_error(OmniAuth::NoSessionError) |
244 |
| - expect(strategy).to have_received(:log).with(:error, '401 Unauthorized - Invalid access token') |
245 |
| - end |
246 |
| - end |
247 |
| - |
248 |
| - context 'when API returns 403 Forbidden' do |
249 |
| - let(:error_response) { instance_double(OAuth2::Response, status: 403) } |
250 |
| - let(:oauth_error) { OAuth2::Error.new(error_response) } |
251 |
| - |
252 |
| - before do |
253 |
| - allow(access_token).to receive(:get).and_raise(oauth_error) |
254 |
| - allow(strategy).to receive(:log) |
255 |
| - end |
256 |
| - |
257 |
| - it 'raises OmniAuth::NoSessionError with appropriate message' do |
258 |
| - expect { strategy.raw_info }.to raise_error(OmniAuth::NoSessionError, 'Insufficient permissions') |
259 |
| - end |
260 |
| - end |
261 |
| - |
262 |
| - context 'when API returns 404 Not Found' do |
263 |
| - let(:error_response) { instance_double(OAuth2::Response, status: 404) } |
264 |
| - let(:oauth_error) { OAuth2::Error.new(error_response) } |
265 |
| - |
266 |
| - before do |
267 |
| - allow(access_token).to receive(:get).and_raise(oauth_error) |
268 |
| - allow(strategy).to receive(:log) |
269 |
| - end |
270 |
| - |
271 |
| - it 'raises OmniAuth::NoSessionError with appropriate message' do |
272 |
| - expect { strategy.raw_info }.to raise_error(OmniAuth::NoSessionError, 'User not found') |
273 |
| - end |
274 |
| - end |
275 |
| - |
276 |
| - context 'when connection times out' do |
277 |
| - before do |
278 |
| - allow(access_token).to receive(:get).and_raise(Errno::ETIMEDOUT) |
279 |
| - allow(strategy).to receive(:log) |
280 |
| - end |
281 |
| - |
282 |
| - it 'raises OmniAuth::NoSessionError' do |
283 |
| - expect { strategy.raw_info }.to raise_error(OmniAuth::NoSessionError, 'Connection timed out') |
284 |
| - end |
| 214 | + before do |
| 215 | + allow(strategy).to receive(:access_token).and_return(access_token) |
| 216 | + allow(access_token).to receive(:get).with('/api/v2/authenticated_user').and_return(response) |
285 | 217 | end
|
286 | 218 |
|
287 |
| - context 'when network error occurs' do |
288 |
| - before do |
289 |
| - allow(access_token).to receive(:get).and_raise(SocketError.new('getaddrinfo: nodename nor servname provided')) |
290 |
| - allow(strategy).to receive(:log) |
291 |
| - end |
292 |
| - |
293 |
| - it 'raises OmniAuth::NoSessionError' do |
294 |
| - expect { strategy.raw_info }.to raise_error(OmniAuth::NoSessionError, 'Network error') |
295 |
| - end |
| 219 | + it 'fetches user info from API' do |
| 220 | + expect(strategy.raw_info).to eq({ 'id' => 'test123' }) |
296 | 221 | end
|
297 | 222 |
|
298 |
| - context 'when API returns other errors' do |
299 |
| - let(:error_response) { instance_double(OAuth2::Response, status: 500) } |
300 |
| - let(:oauth_error) { OAuth2::Error.new(error_response) } |
301 |
| - |
302 |
| - before do |
303 |
| - allow(oauth_error).to receive(:message).and_return('Internal Server Error') |
304 |
| - allow(access_token).to receive(:get).with('/api/v2/authenticated_user').and_raise(oauth_error) |
305 |
| - allow(strategy).to receive(:log) |
306 |
| - end |
307 |
| - |
308 |
| - it 're-raises the original error' do |
309 |
| - expect { strategy.raw_info }.to raise_error(OAuth2::Error) |
310 |
| - end |
311 |
| - |
312 |
| - it 'logs the error with status and message' do |
313 |
| - expect { strategy.raw_info }.to raise_error(OAuth2::Error) |
314 |
| - expect(strategy).to have_received(:log).with(:error, 'API Error: 500 - Internal Server Error') |
315 |
| - end |
| 223 | + it 'memoizes the result' do |
| 224 | + 2.times { strategy.raw_info } |
| 225 | + expect(access_token).to have_received(:get).once |
316 | 226 | end
|
317 | 227 | end
|
318 | 228 |
|
|
434 | 344 | client_id: 'client_id',
|
435 | 345 | client_secret: 'secret',
|
436 | 346 | token_params: {},
|
| 347 | + token_options: {}, |
437 | 348 | auth_token_params: {}
|
438 | 349 | )
|
439 | 350 | )
|
|
0 commit comments