8784: Fix test for latest firefox.
[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: nil, username: nil)
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       },
18     }
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'
25   end
26
27   test 'assign username from sso' do
28     mock_auth_with(email: 'foo@example.com', username: 'bar')
29     u = assigns(:user)
30     assert_equal 'bar', u.username
31   end
32
33   test 'no assign username from sso' do
34     mock_auth_with(email: 'foo@example.com')
35     u = assigns(:user)
36     assert_equal 'foo', u.username
37   end
38
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)
47   end
48
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},
63      uniqprefix: 'foo'},
64    {result: :yup, email: 'foo@', cfg: {auto: true, repo: true},
65      uniqprefix: 'foo'},
66    {result: :yup, email: 'auto_setup_vm_login@', cfg: {auto: true, repo: true},
67      uniqprefix: 'auto_setup_vm_login'},
68    ].each do |testcase|
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 =
75         testcase[:cfg][:repo]
76
77       mock_auth_with(email: testcase[:email])
78       u = assigns(:user)
79       vm_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
80                             'permission', u.uuid,
81                             '%-' + VirtualMachine.uuid_prefix + '-%')
82       repo_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
83                               'permission', u.uuid,
84                               '%-' + Repository.uuid_prefix + '-%')
85       repos = Repository.where('uuid in (?)', repo_links.collect(&:head_uuid))
86       case u[:result]
87       when :nope
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"
91       when :yup
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"
95         else
96           assert_empty vm_links, "should not have VM login permission"
97         end
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"
102         else
103           assert_empty repo_links, "should not have repo permission"
104         end
105       end
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
109         # it unique.
110         (repos.collect(&:name) +
111          vm_links.collect { |link| link.properties['username'] }
112          ).each do |name|
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}")
117         end
118       end
119     end
120   end
121 end