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