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)
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 post('/auth/josh_id/callback',
26 {return_to: client_url},
27 {'omniauth.auth' => mock})
28 assert_response :redirect, 'Did not redirect to client with token'
31 test 'assign username from sso' do
32 mock_auth_with(email: 'foo@example.com', username: 'bar')
34 assert_equal 'bar', u.username
37 test 'no assign username from sso' do
38 mock_auth_with(email: 'foo@example.com')
40 assert_equal 'foo', u.username
43 test 'create new user during omniauth callback' do
44 mock_auth_with(email: 'edward@example.com')
45 assert_equal(0, @response.redirect_url.index(client_url),
46 'Redirected to wrong address after succesful login: was ' +
47 @response.redirect_url + ', expected ' + client_url + '[...]')
48 assert_not_nil(@response.redirect_url.index('api_token='),
49 'Expected api_token in query string of redirect url ' +
50 @response.redirect_url)
53 # Test various combinations of auto_setup configuration and email
54 # address provided during a new user's first session setup.
55 [{result: :nope, email: nil, cfg: {auto: true, repo: true, vm: true}},
56 {result: :yup, email: nil, cfg: {auto: true}},
57 {result: :nope, email: '@example.com', cfg: {auto: true, repo: true, vm: true}},
58 {result: :yup, email: '@example.com', cfg: {auto: true}},
59 {result: :nope, email: 'root@', cfg: {auto: true, repo: true, vm: true}},
60 {result: :nope, email: 'root@', cfg: {auto: true, repo: true}},
61 {result: :nope, email: 'root@', cfg: {auto: true, vm: true}},
62 {result: :yup, email: 'root@', cfg: {auto: true}},
63 {result: :nope, email: 'gitolite@', cfg: {auto: true, repo: true}},
64 {result: :nope, email: '*_*@', cfg: {auto: true, vm: true}},
65 {result: :yup, email: 'toor@', cfg: {auto: true, vm: true, repo: true}},
66 {result: :yup, email: 'foo@', cfg: {auto: true, vm: true},
68 {result: :yup, email: 'foo@', cfg: {auto: true, repo: true},
70 {result: :yup, email: 'auto_setup_vm_login@', cfg: {auto: true, repo: true},
71 uniqprefix: 'auto_setup_vm_login'},
73 test "user auto-activate #{testcase.inspect}" do
74 # Configure auto_setup behavior according to testcase[:cfg]
75 Rails.configuration.auto_setup_new_users = testcase[:cfg][:auto]
76 Rails.configuration.auto_setup_new_users_with_vm_uuid =
77 (testcase[:cfg][:vm] ? virtual_machines(:testvm).uuid : false)
78 Rails.configuration.auto_setup_new_users_with_repository =
81 mock_auth_with(email: testcase[:email])
83 vm_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
85 '%-' + VirtualMachine.uuid_prefix + '-%')
86 repo_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
88 '%-' + Repository.uuid_prefix + '-%')
89 repos = Repository.where('uuid in (?)', repo_links.collect(&:head_uuid))
92 assert_equal false, u.is_invited, "should not have been set up"
93 assert_empty vm_links, "should not have VM login permission"
94 assert_empty repo_links, "should not have repo permission"
96 assert_equal true, u.is_invited
97 if testcase[:cfg][:vm]
98 assert_equal 1, vm_links.count, "wrong number of VM perm links"
100 assert_empty vm_links, "should not have VM login permission"
102 if testcase[:cfg][:repo]
103 assert_equal 1, repo_links.count, "wrong number of repo perm links"
104 assert_equal 1, repos.count, "wrong number of repos"
105 assert_equal 'can_manage', repo_links.first.name, "wrong perm type"
107 assert_empty repo_links, "should not have repo permission"
110 if (prefix = testcase[:uniqprefix])
111 # This email address conflicts with a test fixture. Make sure
112 # every VM login and repository name got digits added to make
114 (repos.collect(&:name) +
115 vm_links.collect { |link| link.properties['username'] }
117 r = name.match(/^(.{#{prefix.length}})(\d+)$/)
118 assert_not_nil r, "#{name.inspect} does not match {prefix}\\d+"
119 assert_equal(prefix, r[1],
120 "#{name.inspect} was not {#{prefix.inspect} plus digits}")