1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class UserSessionsApiTest < ActionDispatch::IntegrationTest
9 'https://wb.example.com'
12 def mock_auth_with(email: nil, username: nil, identity_url: nil)
14 'provider' => 'josh_id',
15 'uid' => 'https://edward.example.com',
17 'identity_url' => 'https://edward.example.com',
18 'name' => 'Edward Example',
19 'first_name' => 'Edward',
20 'last_name' => 'Example',
23 mock['info']['email'] = email unless email.nil?
24 mock['info']['username'] = username unless username.nil?
25 mock['info']['identity_url'] = identity_url unless identity_url.nil?
26 post('/auth/josh_id/callback',
27 {return_to: client_url},
28 {'omniauth.auth' => mock})
29 assert_response :redirect, 'Did not redirect to client with token'
32 test 'assign username from sso' do
33 mock_auth_with(email: 'foo@example.com', username: 'bar')
35 assert_equal 'bar', u.username
38 test 'no assign username from sso' do
39 mock_auth_with(email: 'foo@example.com')
41 assert_equal 'foo', u.username
44 test 'existing user login' do
45 mock_auth_with(identity_url: "https://active-user.openid.local")
47 assert_equal 'zzzzz-tpzed-xurymjxw79nv3jz', u.uuid
50 test 'user redirect_to_user_uuid' do
51 mock_auth_with(identity_url: "https://redirects-to-active-user.openid.local")
53 assert_equal 'zzzzz-tpzed-xurymjxw79nv3jz', u.uuid
56 test 'user double redirect_to_user_uuid' do
57 mock_auth_with(identity_url: "https://double-redirects-to-active-user.openid.local")
59 assert_equal 'zzzzz-tpzed-xurymjxw79nv3jz', u.uuid
62 test 'create new user during omniauth callback' do
63 mock_auth_with(email: 'edward@example.com')
64 assert_equal(0, @response.redirect_url.index(client_url),
65 'Redirected to wrong address after succesful login: was ' +
66 @response.redirect_url + ', expected ' + client_url + '[...]')
67 assert_not_nil(@response.redirect_url.index('api_token='),
68 'Expected api_token in query string of redirect url ' +
69 @response.redirect_url)
72 # Test various combinations of auto_setup configuration and email
73 # address provided during a new user's first session setup.
74 [{result: :nope, email: nil, cfg: {auto: true, repo: true, vm: true}},
75 {result: :yup, email: nil, cfg: {auto: true}},
76 {result: :nope, email: '@example.com', cfg: {auto: true, repo: true, vm: true}},
77 {result: :yup, email: '@example.com', cfg: {auto: true}},
78 {result: :nope, email: 'root@', cfg: {auto: true, repo: true, vm: true}},
79 {result: :nope, email: 'root@', cfg: {auto: true, repo: true}},
80 {result: :nope, email: 'root@', cfg: {auto: true, vm: true}},
81 {result: :yup, email: 'root@', cfg: {auto: true}},
82 {result: :nope, email: 'gitolite@', cfg: {auto: true, repo: true}},
83 {result: :nope, email: '*_*@', cfg: {auto: true, vm: true}},
84 {result: :yup, email: 'toor@', cfg: {auto: true, vm: true, repo: true}},
85 {result: :yup, email: 'foo@', cfg: {auto: true, vm: true},
87 {result: :yup, email: 'foo@', cfg: {auto: true, repo: true},
89 {result: :yup, email: 'auto_setup_vm_login@', cfg: {auto: true, repo: true},
90 uniqprefix: 'auto_setup_vm_login'},
92 test "user auto-activate #{testcase.inspect}" do
93 # Configure auto_setup behavior according to testcase[:cfg]
94 Rails.configuration.auto_setup_new_users = testcase[:cfg][:auto]
95 Rails.configuration.auto_setup_new_users_with_vm_uuid =
96 (testcase[:cfg][:vm] ? virtual_machines(:testvm).uuid : false)
97 Rails.configuration.auto_setup_new_users_with_repository =
100 mock_auth_with(email: testcase[:email])
102 vm_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
103 'permission', u.uuid,
104 '%-' + VirtualMachine.uuid_prefix + '-%')
105 repo_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
106 'permission', u.uuid,
107 '%-' + Repository.uuid_prefix + '-%')
108 repos = Repository.where('uuid in (?)', repo_links.collect(&:head_uuid))
111 assert_equal false, u.is_invited, "should not have been set up"
112 assert_empty vm_links, "should not have VM login permission"
113 assert_empty repo_links, "should not have repo permission"
115 assert_equal true, u.is_invited
116 if testcase[:cfg][:vm]
117 assert_equal 1, vm_links.count, "wrong number of VM perm links"
119 assert_empty vm_links, "should not have VM login permission"
121 if testcase[:cfg][:repo]
122 assert_equal 1, repo_links.count, "wrong number of repo perm links"
123 assert_equal 1, repos.count, "wrong number of repos"
124 assert_equal 'can_manage', repo_links.first.name, "wrong perm type"
126 assert_empty repo_links, "should not have repo permission"
129 if (prefix = testcase[:uniqprefix])
130 # This email address conflicts with a test fixture. Make sure
131 # every VM login and repository name got digits added to make
133 (repos.collect(&:name) +
134 vm_links.collect { |link| link.properties['username'] }
136 r = name.match(/^(.{#{prefix.length}})(\d+)$/)
137 assert_not_nil r, "#{name.inspect} does not match {prefix}\\d+"
138 assert_equal(prefix, r[1],
139 "#{name.inspect} was not {#{prefix.inspect} plus digits}")