Skip to content

Commit 2072147

Browse files
Merge pull request #6 from cadenza-tech/refactoring
Refactoring
2 parents 239f2eb + adcf418 commit 2072147

File tree

2 files changed

+12
-135
lines changed

2 files changed

+12
-135
lines changed

lib/omniauth/strategies/qiita_v2.rb

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,8 @@ class QiitaV2 < OmniAuth::Strategies::OAuth2
6161
hash
6262
end
6363

64-
def raw_info # rubocop:disable Metrics/AbcSize
65-
@raw_info ||= begin
66-
access_token.get(USER_INFO_URL).parsed || {}
67-
rescue ::OAuth2::Error => e
68-
case e.response.status
69-
when 401
70-
log :error, '401 Unauthorized - Invalid access token'
71-
raise ::OmniAuth::NoSessionError.new('Invalid access token')
72-
when 403
73-
log :error, '403 Forbidden - Insufficient permissions'
74-
raise ::OmniAuth::NoSessionError.new('Insufficient permissions')
75-
when 404
76-
log :error, '404 Not Found - User not found'
77-
raise ::OmniAuth::NoSessionError.new('User not found')
78-
else
79-
log :error, "API Error: #{e.response.status} - #{e.message}"
80-
raise e
81-
end
82-
rescue ::Errno::ETIMEDOUT
83-
log :error, 'Connection timed out'
84-
raise ::OmniAuth::NoSessionError.new('Connection timed out')
85-
rescue ::SocketError => e
86-
log :error, "Network error: #{e.message}"
87-
raise ::OmniAuth::NoSessionError.new('Network error')
88-
end
64+
def raw_info
65+
@raw_info ||= access_token.get(USER_INFO_URL).parsed
8966
end
9067

9168
def callback_url
@@ -105,7 +82,7 @@ def authorize_params
10582
protected
10683

10784
def build_access_token
108-
client.get_token(base_params.merge(token_params_from_options).merge(deep_symbolize(options.auth_token_params)))
85+
client.get_token(base_params.merge(token_params.to_hash(symbolize_keys: true)).merge(deep_symbolize(options.auth_token_params)))
10986
end
11087

11188
private
@@ -128,17 +105,6 @@ def base_params
128105
code: request.params['code']
129106
}
130107
end
131-
132-
def token_params_from_options
133-
return {} unless options.token_params
134-
135-
if options.token_params.is_a?(Hash)
136-
params = options.token_params
137-
else
138-
params = options.token_params.to_hash
139-
end
140-
params.transform_keys(&:to_sym)
141-
end
142108
end
143109
end
144110
end

spec/omniauth/strategies/qiita_v2_spec.rb

Lines changed: 9 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -211,108 +211,18 @@
211211
let(:access_token) { instance_double(OAuth2::AccessToken) }
212212
let(:response) { instance_double(OAuth2::Response, parsed: { 'id' => 'test123' }) }
213213

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)
285217
end
286218

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' })
296221
end
297222

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
316226
end
317227
end
318228

@@ -434,6 +344,7 @@
434344
client_id: 'client_id',
435345
client_secret: 'secret',
436346
token_params: {},
347+
token_options: {},
437348
auth_token_params: {}
438349
)
439350
)

0 commit comments

Comments
 (0)