Merge branch '8784-dir-listings'
[arvados.git] / services / api / test / integration / user_sessions_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class UserSessionsApiTest < ActionDispatch::IntegrationTest
8   def client_url
9     'https://wb.example.com'
10   end
11
12   def mock_auth_with(email: nil, username: nil)
13     mock = {
14       'provider' => 'josh_id',
15       'uid' => 'https://edward.example.com',
16       'info' => {
17         'identity_url' => 'https://edward.example.com',
18         'name' => 'Edward Example',
19         'first_name' => 'Edward',
20         'last_name' => 'Example',
21       },
22     }
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'
29   end
30
31   test 'assign username from sso' do
32     mock_auth_with(email: 'foo@example.com', username: 'bar')
33     u = assigns(:user)
34     assert_equal 'bar', u.username
35   end
36
37   test 'no assign username from sso' do
38     mock_auth_with(email: 'foo@example.com')
39     u = assigns(:user)
40     assert_equal 'foo', u.username
41   end
42
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)
51   end
52
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},
67      uniqprefix: 'foo'},
68    {result: :yup, email: 'foo@', cfg: {auto: true, repo: true},
69      uniqprefix: 'foo'},
70    {result: :yup, email: 'auto_setup_vm_login@', cfg: {auto: true, repo: true},
71      uniqprefix: 'auto_setup_vm_login'},
72    ].each do |testcase|
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 =
79         testcase[:cfg][:repo]
80
81       mock_auth_with(email: testcase[:email])
82       u = assigns(:user)
83       vm_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
84                             'permission', u.uuid,
85                             '%-' + VirtualMachine.uuid_prefix + '-%')
86       repo_links = Link.where('link_class=? and tail_uuid=? and head_uuid like ?',
87                               'permission', u.uuid,
88                               '%-' + Repository.uuid_prefix + '-%')
89       repos = Repository.where('uuid in (?)', repo_links.collect(&:head_uuid))
90       case u[:result]
91       when :nope
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"
95       when :yup
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"
99         else
100           assert_empty vm_links, "should not have VM login permission"
101         end
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"
106         else
107           assert_empty repo_links, "should not have repo permission"
108         end
109       end
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
113         # it unique.
114         (repos.collect(&:name) +
115          vm_links.collect { |link| link.properties['username'] }
116          ).each do |name|
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}")
121         end
122       end
123     end
124   end
125 end