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