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