Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[arvados.git] / services / api / test / integration / user_sessions_test.rb
1 require 'test_helper'
2
3 class UserSessionsApiTest < ActionDispatch::IntegrationTest
4   def client_url
5     'https://wb.example.com'
6   end
7
8   def mock_auth_with_email email
9     mock = {
10       'provider' => 'josh_id',
11       'uid' => 'https://edward.example.com',
12       'info' => {
13         'identity_url' => 'https://edward.example.com',
14         'name' => 'Edward Example',
15         'first_name' => 'Edward',
16         'last_name' => 'Example',
17         'email' => email,
18       },
19     }
20     post('/auth/josh_id/callback',
21          {return_to: client_url},
22          {'omniauth.auth' => mock})
23     assert_response :redirect, 'Did not redirect to client with token'
24   end
25
26   test 'create new user during omniauth callback' do
27     mock_auth_with_email 'edward@example.com'
28     assert_equal(0, @response.redirect_url.index(client_url),
29                  'Redirected to wrong address after succesful login: was ' +
30                  @response.redirect_url + ', expected ' + client_url + '[...]')
31     assert_not_nil(@response.redirect_url.index('api_token='),
32                    'Expected api_token in query string of redirect url ' +
33                    @response.redirect_url)
34   end
35
36   # Test various combinations of auto_setup configuration and email
37   # address provided during a new user's first session setup.
38   [{result: :nope, email: nil, cfg: {auto: true, repo: true, vm: true}},
39    {result: :yup, email: nil, cfg: {auto: true}},
40    {result: :nope, email: '@example.com', cfg: {auto: true, repo: true, vm: true}},
41    {result: :yup, email: '@example.com', cfg: {auto: true}},
42    {result: :nope, email: 'root@', cfg: {auto: true, repo: true, vm: true}},
43    {result: :nope, email: 'root@', cfg: {auto: true, repo: true}},
44    {result: :nope, email: 'root@', cfg: {auto: true, vm: true}},
45    {result: :yup, email: 'root@', cfg: {auto: true}},
46    {result: :nope, email: 'gitolite@', cfg: {auto: true, repo: true}},
47    {result: :nope, email: '*_*@', cfg: {auto: true, vm: true}},
48    {result: :yup, email: 'toor@', cfg: {auto: true, vm: true, repo: true}},
49    {result: :yup, email: 'foo@', cfg: {auto: true, vm: true},
50      uniqprefix: 'foo'},
51    {result: :yup, email: 'foo@', cfg: {auto: true, repo: true},
52      uniqprefix: 'foo'},
53    {result: :yup, email: 'auto_setup_vm_login@', cfg: {auto: true, repo: true},
54      uniqprefix: 'auto_setup_vm_login'},
55    ].each do |testcase|
56     test "user auto-activate #{testcase.inspect}" do
57       # Configure auto_setup behavior according to testcase[:cfg]
58       Rails.configuration.auto_setup_new_users = testcase[:cfg][:auto]
59       Rails.configuration.auto_setup_new_users_with_vm_uuid =
60         (testcase[:cfg][:vm] ? virtual_machines(:testvm).uuid : false)
61       Rails.configuration.auto_setup_new_users_with_repository =
62         testcase[:cfg][:repo]
63
64       mock_auth_with_email testcase[:email]
65       u = assigns(:user)
66       vm_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
67                             'permission', u.uuid,
68                             '%-' + VirtualMachine.uuid_prefix + '-%')
69       repo_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
70                               'permission', u.uuid,
71                               '%-' + Repository.uuid_prefix + '-%')
72       repos = Repository.where('uuid in (?)', repo_links.collect(&:head_uuid))
73       case u[:result]
74       when :nope
75         assert_equal false, u.is_invited, "should not have been set up"
76         assert_empty vm_links, "should not have VM login permission"
77         assert_empty repo_links, "should not have repo permission"
78       when :yup
79         assert_equal true, u.is_invited
80         if testcase[:cfg][:vm]
81           assert_equal 1, vm_links.count, "wrong number of VM perm links"
82         else
83           assert_empty vm_links, "should not have VM login permission"
84         end
85         if testcase[:cfg][:repo]
86           assert_equal 1, repo_links.count, "wrong number of repo perm links"
87           assert_equal 1, repos.count, "wrong number of repos"
88           assert_equal 'can_manage', repo_links.first.name, "wrong perm type"
89         else
90           assert_empty repo_links, "should not have repo permission"
91         end
92       end
93       if (prefix = testcase[:uniqprefix])
94         # This email address conflicts with a test fixture. Make sure
95         # every VM login and repository name got digits added to make
96         # it unique.
97         (repos.collect(&:name) +
98          vm_links.collect { |link| link.properties['username'] }
99          ).each do |name|
100           r = name.match /^(.{#{prefix.length}})(\d+)$/
101           assert_not_nil r, "#{name.inspect} does not match {prefix}\\d+"
102           assert_equal(prefix, r[1],
103                        "#{name.inspect} was not {#{prefix.inspect} plus digits}")
104         end
105       end
106     end
107   end
108 end