3 class UserSessionsApiTest < ActionDispatch::IntegrationTest
5 'https://wb.example.com'
8 def mock_auth_with(email: nil, username: nil)
10 'provider' => 'josh_id',
11 'uid' => 'https://edward.example.com',
13 'identity_url' => 'https://edward.example.com',
14 'name' => 'Edward Example',
15 'first_name' => 'Edward',
16 'last_name' => 'Example',
19 mock['info']['email'] = email unless email.nil?
20 mock['info']['username'] = username unless username.nil?
21 post('/auth/josh_id/callback',
22 {return_to: client_url},
23 {'omniauth.auth' => mock})
24 assert_response :redirect, 'Did not redirect to client with token'
27 test 'assign username from sso' do
28 mock_auth_with(email: 'foo@example.com', username: 'bar')
30 assert_equal 'bar', u.username
33 test 'no assign username from sso' do
34 mock_auth_with(email: 'foo@example.com')
36 assert_equal 'foo', u.username
39 test 'create new user during omniauth callback' do
40 mock_auth_with(email: 'edward@example.com')
41 assert_equal(0, @response.redirect_url.index(client_url),
42 'Redirected to wrong address after succesful login: was ' +
43 @response.redirect_url + ', expected ' + client_url + '[...]')
44 assert_not_nil(@response.redirect_url.index('api_token='),
45 'Expected api_token in query string of redirect url ' +
46 @response.redirect_url)
49 # Test various combinations of auto_setup configuration and email
50 # address provided during a new user's first session setup.
51 [{result: :nope, email: nil, cfg: {auto: true, repo: true, vm: true}},
52 {result: :yup, email: nil, cfg: {auto: true}},
53 {result: :nope, email: '@example.com', cfg: {auto: true, repo: true, vm: true}},
54 {result: :yup, email: '@example.com', cfg: {auto: true}},
55 {result: :nope, email: 'root@', cfg: {auto: true, repo: true, vm: true}},
56 {result: :nope, email: 'root@', cfg: {auto: true, repo: true}},
57 {result: :nope, email: 'root@', cfg: {auto: true, vm: true}},
58 {result: :yup, email: 'root@', cfg: {auto: true}},
59 {result: :nope, email: 'gitolite@', cfg: {auto: true, repo: true}},
60 {result: :nope, email: '*_*@', cfg: {auto: true, vm: true}},
61 {result: :yup, email: 'toor@', cfg: {auto: true, vm: true, repo: true}},
62 {result: :yup, email: 'foo@', cfg: {auto: true, vm: true},
64 {result: :yup, email: 'foo@', cfg: {auto: true, repo: true},
66 {result: :yup, email: 'auto_setup_vm_login@', cfg: {auto: true, repo: true},
67 uniqprefix: 'auto_setup_vm_login'},
69 test "user auto-activate #{testcase.inspect}" do
70 # Configure auto_setup behavior according to testcase[:cfg]
71 Rails.configuration.auto_setup_new_users = testcase[:cfg][:auto]
72 Rails.configuration.auto_setup_new_users_with_vm_uuid =
73 (testcase[:cfg][:vm] ? virtual_machines(:testvm).uuid : false)
74 Rails.configuration.auto_setup_new_users_with_repository =
77 mock_auth_with(email: testcase[:email])
79 vm_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
81 '%-' + VirtualMachine.uuid_prefix + '-%')
82 repo_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
84 '%-' + Repository.uuid_prefix + '-%')
85 repos = Repository.where('uuid in (?)', repo_links.collect(&:head_uuid))
88 assert_equal false, u.is_invited, "should not have been set up"
89 assert_empty vm_links, "should not have VM login permission"
90 assert_empty repo_links, "should not have repo permission"
92 assert_equal true, u.is_invited
93 if testcase[:cfg][:vm]
94 assert_equal 1, vm_links.count, "wrong number of VM perm links"
96 assert_empty vm_links, "should not have VM login permission"
98 if testcase[:cfg][:repo]
99 assert_equal 1, repo_links.count, "wrong number of repo perm links"
100 assert_equal 1, repos.count, "wrong number of repos"
101 assert_equal 'can_manage', repo_links.first.name, "wrong perm type"
103 assert_empty repo_links, "should not have repo permission"
106 if (prefix = testcase[:uniqprefix])
107 # This email address conflicts with a test fixture. Make sure
108 # every VM login and repository name got digits added to make
110 (repos.collect(&:name) +
111 vm_links.collect { |link| link.properties['username'] }
113 r = name.match(/^(.{#{prefix.length}})(\d+)$/)
114 assert_not_nil r, "#{name.inspect} does not match {prefix}\\d+"
115 assert_equal(prefix, r[1],
116 "#{name.inspect} was not {#{prefix.inspect} plus digits}")