Skip to content

Commit 81c9f41

Browse files
committed
CV2-6599: fix omniauth tests
1 parent fb3caae commit 81c9f41

File tree

4 files changed

+12
-48
lines changed

4 files changed

+12
-48
lines changed

app/controllers/api/v1/omniauth_callbacks_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def failure
2020
end
2121

2222
def setup
23-
setup_twitter if request.env['omniauth.strategy'].is_a?(OmniAuth::Strategies::Twitter)
2423
setup_facebook if request.env['omniauth.strategy'].is_a?(OmniAuth::Strategies::Facebook)
2524
render plain: 'Setup complete.', status: 404
2625
end

lib/sample_data.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def create_omniauth_user(options = {})
169169
provider = options.has_key?(:provider) ? options[:provider] : %w(google slack).sample
170170
auth[:uid] = options.has_key?(:uid) ? options[:uid] : random_string
171171
auth[:url] = url
172+
options[:info][:email] = options[:email] if options.has_key?(:info) && options[:info][:email].nil?
172173
auth[:info] = options.has_key?(:info) ? options[:info] : {name: random_string, email: options[:email]}
173174
auth[:credentials] = options.has_key?(:credentials) ? options[:credentials] : {token: random_string, secret: random_string}
174175
auth[:extra] = options.has_key?(:extra) ? options[:extra] : {}

test/controllers/base_api_controller_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ def setup
4141
end
4242

4343
test "should get current user from session" do
44-
u = create_omniauth_user info: {name: 'Test User'}
44+
u = create_omniauth_user email: '[email protected]', info: {name: 'Test User'}
4545
authenticate_with_user(u)
4646
get :me, params: {}
4747
assert_response :success
4848
response = JSON.parse(@response.body)
49-
assert_equal 'Test User', response['data']['name']
49+
assert_equal '[email protected]', response['data']['email']
5050
assert_equal 'session', response['data']['source']
5151
end
5252

5353
test "should get current user from token" do
54-
u = create_omniauth_user info: {name: 'Test User'}
54+
u = create_omniauth_user email: '[email protected]', info: { name: 'Test User', email: '[email protected]' }
5555
header = CheckConfig.get('authorization_header') || 'X-Token'
5656
@request.headers.merge!({ header => u.token })
5757
get :me, params: {}
5858
assert_response :success
5959
response = JSON.parse(@response.body)
60-
assert_equal 'Test User', response['data']['name']
60+
assert_equal '[email protected]', response['data']['email']
6161
assert_equal 'token', response['data']['source']
6262
end
6363

test/controllers/omniauth_callbacks_controller_test.rb

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@ def setup
55
super
66
@controller = Api::V1::OmniauthCallbacksController.new
77
OmniAuth.config.test_mode = true
8-
OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new({
9-
provider: 'twitter',
10-
uid: '654321',
11-
info: {
12-
name: 'Test',
13-
image: 'http://twitter.com/test/image.png',
14-
nickname: 'test'
15-
},
16-
credentials: {
17-
token: '123456',
18-
secret: 'top_secret'
19-
}
20-
})
218
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
229
provider: 'facebook',
2310
uid: '654321',
@@ -77,8 +64,11 @@ def setup
7764
secret: 'top_secret'
7865
}
7966
})
67+
[{email: '[email protected]', name: 'Test'}, {email: '[email protected]', name: 'Mohamed El-Sawy'}].each do |info|
68+
invite_new_user info
69+
end
8070
request.env['devise.mapping'] = Devise.mappings[:api_user]
81-
['https://twitter.com/test', 'https://facebook.com/654321', 'https://www.googleapis.com/plus/v1/people/654321'].each do |url|
71+
['https://facebook.com/654321', 'https://www.googleapis.com/plus/v1/people/654321'].each do |url|
8272
WebMock.stub_request(:get, CheckConfig.get('pender_url_private') + '/api/medias').with({ query: { url: url } }).to_return(body: '{"type":"media","data":{"type":"profile"}}')
8373
end
8474
User.current = nil
@@ -89,25 +79,6 @@ def teardown
8979
User.current = nil
9080
end
9181

92-
test "should redirect to root after Twitter authentication" do
93-
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:twitter]
94-
get :twitter, params: {}
95-
assert_redirected_to '/close.html'
96-
end
97-
98-
test "should set information in session after Twitter authentication" do
99-
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:twitter]
100-
assert_nil session['checkdesk.user']
101-
get :twitter, params: {}
102-
assert_not_nil session['checkdesk.current_user_id']
103-
end
104-
105-
test "should redirect to destination after Twitter authentication" do
106-
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:twitter]
107-
get :twitter, params: { destination: '/close.html' }
108-
assert_redirected_to '/close.html'
109-
end
110-
11182
test "should set information in session after Facebook authentication" do
11283
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:facebook]
11384
assert_nil session['checkdesk.user']
@@ -190,12 +161,11 @@ def teardown
190161
end
191162

192163
test "should connect when current user set" do
193-
p1 = random_complex_password
194-
u = create_user login: 'test', password: p1, password_confirmation: p1, email: '[email protected]'
164+
u = User.where(email: '[email protected]').first
195165
u.confirm
196166
authenticate_with_user(u)
197-
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:twitter]
198-
get :twitter, params: {}
167+
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:facebook]
168+
get :facebook, params: {}
199169
u = User.find(u.id)
200170
assert_equal 1, u.source.accounts.count
201171
end
@@ -219,12 +189,6 @@ def teardown
219189
assert_redirected_to '/close.html'
220190
end
221191

222-
test "should setup Twitter authentication" do
223-
request.env['omniauth.strategy'] = OmniAuth::Strategies::Twitter.new({})
224-
get :setup, params: {}
225-
assert_response 404
226-
end
227-
228192
test "should setup Facebook authentication" do
229193
request.env['omniauth.strategy'] = OmniAuth::Strategies::Facebook.new({})
230194
get :setup, params: {}

0 commit comments

Comments
 (0)