18995: Merge branch 'main' into 18995-code-cleanup-5
[arvados.git] / apps / workbench / test / integration / user_profile_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'integration_helper'
6
7 class UserProfileTest < ActionDispatch::IntegrationTest
8   setup do
9     need_javascript
10     @user_profile_form_fields = Rails.configuration.Workbench.UserProfileFormFields
11   end
12
13   teardown do
14     Rails.configuration.Workbench.UserProfileFormFields = @user_profile_form_fields
15   end
16
17   def verify_homepage_with_profile user, invited, has_profile
18     profile_config = Rails.configuration.Workbench.UserProfileFormFields
19
20     if !user
21       assert_text('Please log in')
22     elsif user['is_active']
23       if !profile_config.empty? && !has_profile
24         assert_text('Save profile')
25         add_profile user
26       else
27         assert_text('Recent processes')
28         assert_no_text('Save profile')
29       end
30     elsif invited
31       assert_text('Please check the box below to indicate that you have read and accepted the user agreement')
32       assert_no_text('Save profile')
33     else
34       assert_text('Your account is inactive')
35       assert_no_text('Save profile')
36     end
37
38     # If the user has not already seen getting_started modal, it will be shown on first visit.
39     if user and user['is_active'] and !user['prefs']['getting_started_shown']
40       within '.modal-content' do
41         assert_text 'Getting Started'
42         assert_selector 'button', text: 'Next'
43         assert_selector 'button', text: 'Prev'
44         first('button', text: 'x').click
45       end
46     end
47
48     within('.navbar-fixed-top') do
49       if !user
50         assert page.has_link?('Log in'), 'Not found link - Log in'
51       else
52         # my account menu
53         assert_selector("#notifications-menu")
54         page.find("#notifications-menu").click
55         within('.dropdown-menu') do
56           if user['is_active']
57             assert_no_selector('a', text: 'Not active')
58             assert_no_selector('a', text: 'Sign agreements')
59
60             assert_selector('a', text: 'Virtual machines')
61             assert_selector('a', text: 'Repositories')
62             assert_selector('a', text: 'Current token')
63             assert_selector('a', text: 'SSH keys')
64
65             if !profile_config.empty?
66               assert_selector('a', text: 'Manage profile')
67             else
68               assert_no_selector('a', text: 'Manage profile')
69             end
70           end
71           assert_selector('a', text: 'Log out')
72         end
73       end
74     end
75   end
76
77   # Check manage profile page and add missing profile to the user
78   def add_profile user
79     assert_no_text('My projects')
80     assert_no_text('Projects shared with me')
81
82     assert_text('Profile')
83     assert_text('First Name')
84     assert_text('Last Name')
85     assert_text('Identity URL')
86     assert_text('E-mail')
87     assert_text(user['email'])
88
89     # Using the default profile which has message and one required field
90
91     # Save profile without filling in the required field. Expect to be back in this profile page again
92     click_button "Save profile"
93     assert_text('Profile')
94     assert_text('First Name')
95     assert_text('Last Name')
96     assert_text('Save profile')
97
98     # This time fill in required field and then save. Expect to go to requested page after that.
99     profile_message = Rails.configuration.Workbench.UserProfileFormMessage
100     required_field_title = ''
101     required_field_key = ''
102     profile_config = Rails.configuration.Workbench.UserProfileFormFields
103     profile_config.each do |k, entry|
104       if entry['Required']
105         required_field_key = k.to_s
106         required_field_title = entry['FormFieldTitle']
107         break
108       end
109     end
110
111     assert page.has_text? profile_message.gsub(/<.*?>/,'')
112     assert_text(required_field_title)
113
114     page.find_field('user[prefs][profile]['+required_field_key+']').set 'value to fill required field'
115
116     click_button "Save profile"
117     # profile saved and in profile page now with success
118     assert_text('Thank you for filling in your profile')
119     assert_selector('input' +
120                     '[name="user[prefs][profile]['+required_field_key+']"]' +
121                     '[value="value to fill required field"]')
122     if user['prefs']['getting_started_shown']
123       click_link 'Back to work!'
124     else
125       click_link 'Get started'
126     end
127
128     # profile saved and in home page now
129     assert_text('Recent processes')
130   end
131
132   [
133     [nil, false, false],
134     ['inactive', true, false],
135     ['inactive_uninvited', false, false],
136     ['active', true, true],
137     ['admin', true, true],
138     ['active_no_prefs', true, false],
139     ['active_no_prefs_profile_no_getting_started_shown', true, false],
140     ['active_no_prefs_profile_with_getting_started_shown', true, false],
141   ].each do |token, invited, has_profile|
142     [true, false].each do |profile_required|
143       test "visit #{token} home page when profile is #{'not ' if !profile_required}configured" do
144         if !profile_required
145           Rails.configuration.Workbench.UserProfileFormFields = []
146         else
147           # Our test config enabled profile by default. So, no need to update config
148         end
149         Rails.configuration.Workbench.EnableGettingStartedPopup = true
150
151         if !token
152           visit ('/')
153         else
154           visit page_with_token(token)
155         end
156
157         user = token && api_fixture('users')[token]
158         verify_homepage_with_profile user, invited, has_profile
159       end
160     end
161   end
162 end