3686: when a repository is manageable by a user, make repository name a link in manag...
[arvados.git] / apps / workbench / test / integration / user_manage_account_test.rb
1 require 'integration_helper'
2
3 class UserManageAccountTest < ActionDispatch::IntegrationTest
4   setup do
5     need_javascript
6   end
7
8   # test manage_account page
9   def verify_manage_account user
10     if user['is_active']
11       within('.navbar-fixed-top') do
12         find('a', text: "#{user['email']}").click
13         within('.dropdown-menu') do
14           find('a', text: 'Manage account').click
15         end
16       end
17
18       # now in manage account page
19       assert page.has_text?('Virtual Machines'), 'No text - Virtual Machines'
20       assert page.has_text?('Repositories'), 'No text - Repositories'
21       assert page.has_text?('SSH Keys'), 'No text - SSH Keys'
22       assert page.has_text?('Current Token'), 'No text - Current Token'
23       assert page.has_text?('The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados'), 'No text - Arvados API token'
24       add_and_verify_ssh_key
25       verify_repositories user
26     else  # inactive user
27       within('.navbar-fixed-top') do
28         find('a', text: "#{user['email']}").click
29         within('.dropdown-menu') do
30           assert page.has_no_link?('Manage profile'), 'Found link - Manage profile'
31         end
32       end
33     end
34   end
35
36   def add_and_verify_ssh_key
37       click_link 'Add new SSH key'
38
39       within '.modal-content' do
40         assert page.has_text?('Public Key'), 'No text - Public Key'
41         assert page.has_button?('Cancel'), 'No button - Cancel'
42         assert page.has_button?('Submit'), 'No button - Submit'
43
44         page.find_field('public_key').set 'first test with an incorrect ssh key value'
45         click_button 'Submit'
46         assert page.has_text?('Public key does not appear to be a valid ssh-rsa or dsa public key'), 'No text - Public key does not appear to be a valid'
47
48         public_key_str = api_fixture('authorized_keys')['active']['public_key']
49         page.find_field('public_key').set public_key_str
50         page.find_field('name').set 'added_in_test'
51         click_button 'Submit'
52         assert page.has_text?('Public key already exists in the database, use a different key.'), 'No text - Public key already exists'
53
54         new_key = SSHKey.generate
55         page.find_field('public_key').set new_key.ssh_public_key
56         page.find_field('name').set 'added_in_test'
57         click_button 'Submit'
58       end
59
60       # key must be added. look for it in the refreshed page
61       assert page.has_text?('added_in_test'), 'No text - added_in_test'
62   end
63
64   def verify_repositories user
65     Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
66     repo_links = Link.filter([['head_uuid', 'is_a', 'arvados#repository'],
67                               ['tail_uuid', '=', user['uuid']],
68                               ['link_class', '=', 'permission']])
69     repos = Repository.where uuid: repo_links.collect(&:head_uuid)
70     repositories = {}
71     repos.each do |repo|
72       repositories[repo.uuid] = repo
73     end
74
75     repo_writables = {}
76     repo_links.each do |link|
77       if link.name.in? ['can_write','can_manage']
78         repo_writables[link.head_uuid] = link.name
79       end
80     end
81
82     repo_links.each do |link|
83       if repo_writables[link.head_uuid] == 'can_manage'
84         assert_selector 'a', text: repositories[link.head_uuid]['name']
85         within('tr', text: repositories[link.head_uuid]['fetch_url']) do
86           assert_text 'writable'
87         end
88       else
89         assert_no_selector 'a', text: repositories[link.head_uuid]['name']
90         assert_text repositories[link.head_uuid]['name']
91         within('tr', text: repositories[link.head_uuid]['fetch_url']) do
92           assert_text 'read-only'
93         end
94       end
95     end
96   end
97
98   [
99     ['inactive', api_fixture('users')['inactive']],
100     ['inactive_uninvited', api_fixture('users')['inactive_uninvited']],
101     ['active', api_fixture('users')['active']],
102     ['admin', api_fixture('users')['admin']],
103   ].each do |token, user|
104     test "test manage account for user #{token}" do
105       visit page_with_token(token)
106       verify_manage_account user
107     end
108   end
109
110   [
111     ['inactive_but_signed_user_agreement', true],
112     ['active', false],
113   ].each do |user, notifications|
114     test "test manage account for #{user} with notifications #{notifications}" do
115       visit page_with_token(user)
116       click_link 'notifications-menu'
117       if notifications
118         assert_selector('a', text: 'Click here to set up an SSH public key for use with Arvados')
119         assert_selector('a', text: 'Click here to learn how to run an Arvados Crunch pipeline')
120         click_link('Click here to set up an SSH public key for use with Arvados')
121         assert_selector('a', text: 'Add new SSH key')
122
123         add_and_verify_ssh_key
124
125         # No more SSH notification
126         click_link 'notifications-menu'
127         assert_no_selector('a', text: 'Click here to set up an SSH public key for use with Arvados')
128         assert_selector('a', text: 'Click here to learn how to run an Arvados Crunch pipeline')
129       else
130         assert_no_selector('a', text: 'Click here to set up an SSH public key for use with Arvados')
131         assert_no_selector('a', text: 'Click here to learn how to run an Arvados Crunch pipeline')
132       end
133     end
134   end
135 end