18559: Update user profile cypress tests and fix minor issues
[arvados-workbench2.git] / cypress / integration / user-profile.spec.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 describe('User profile tests', function() {
6     let activeUser;
7     let adminUser;
8     const roleGroupName = `Test role group (${Math.floor(999999 * Math.random())})`;
9     const projectGroupName = `Test project group (${Math.floor(999999 * Math.random())})`;
10
11     before(function() {
12         // Only set up common users once. These aren't set up as aliases because
13         // aliases are cleaned up after every test. Also it doesn't make sense
14         // to set the same users on beforeEach() over and over again, so we
15         // separate a little from Cypress' 'Best Practices' here.
16         cy.getUser('admin', 'Admin', 'User', true, true)
17             .as('adminUser').then(function() {
18                 adminUser = this.adminUser;
19             }
20         );
21         cy.getUser('user', 'Active', 'User', false, true)
22             .as('activeUser').then(function() {
23                 activeUser = this.activeUser;
24             }
25         );
26     });
27
28     function assertProfileValues({
29         firstName,
30         lastName,
31         email,
32         username,
33         org,
34         org_email,
35         role,
36         website,
37     }) {
38         cy.get('[data-cy=profile-form] input[name="firstName"]').invoke('val').should('equal', firstName);
39         cy.get('[data-cy=profile-form] input[name="lastName"]').invoke('val').should('equal', lastName);
40         cy.get('[data-cy=profile-form] [data-cy=email] [data-cy=value]').contains(email);
41         cy.get('[data-cy=profile-form] [data-cy=username] [data-cy=value]').contains(username);
42
43         cy.get('[data-cy=profile-form] input[name="prefs.profile.organization"]').invoke('val').should('equal', org);
44         cy.get('[data-cy=profile-form] input[name="prefs.profile.organization_email"]').invoke('val').should('equal', org_email);
45         cy.get('[data-cy=profile-form] select[name="prefs.profile.role"]').invoke('val').should('equal', role);
46         cy.get('[data-cy=profile-form] input[name="prefs.profile.website_url"]').invoke('val').should('equal', website);
47     }
48
49     function enterProfileValues({
50         org,
51         org_email,
52         role,
53         website,
54     }) {
55         cy.get('[data-cy=profile-form] input[name="prefs.profile.organization"]').clear();
56         if (org) {
57             cy.get('[data-cy=profile-form] input[name="prefs.profile.organization"]').type(org);
58         }
59         cy.get('[data-cy=profile-form] input[name="prefs.profile.organization_email"]').clear();
60         if (org_email) {
61             cy.get('[data-cy=profile-form] input[name="prefs.profile.organization_email"]').type(org_email);
62         }
63         cy.get('[data-cy=profile-form] select[name="prefs.profile.role"]').select(role);
64         cy.get('[data-cy=profile-form] input[name="prefs.profile.website_url"]').clear();
65         if (website) {
66             cy.get('[data-cy=profile-form] input[name="prefs.profile.website_url"]').type(website);
67         }
68     }
69
70     beforeEach(function() {
71         cy.loginAs(adminUser);
72         cy.goToPath('/my-account');
73         enterProfileValues({
74             org: '',
75             org_email: '',
76             role: '',
77             website: '',
78         });
79         cy.get('[data-cy=profile-form] button[type="submit"]').then((btn) => {
80             if (!btn.is(':disabled')) {
81                 btn.click();
82             }
83         });
84
85
86         cy.goToPath('/user/' + activeUser.user.uuid);
87         enterProfileValues({
88             org: '',
89             org_email: '',
90             role: '',
91             website: '',
92         });
93         cy.get('[data-cy=profile-form] button[type="submit"]').then((btn) => {
94             if (!btn.is(':disabled')) {
95                 btn.click();
96             }
97         });
98     });
99
100     it('non-admin can edit own profile', function() {
101         cy.loginAs(activeUser);
102
103         cy.get('header button[title="Account Management"]').click();
104         cy.get('#account-menu').contains('My account').click();
105
106         // Admin tab should be hidden
107         cy.get('div [role="tab"]').should('not.contain', 'ADMIN');
108
109         // Check initial values
110         assertProfileValues({
111             firstName: 'Active',
112             lastName: 'User',
113             email: 'user@example.local',
114             username: 'user',
115             org: '',
116             org_email: '',
117             role: '',
118             website: '',
119         });
120
121         // Change values
122         enterProfileValues({
123             org: 'Org name',
124             org_email: 'email@example.com',
125             role: 'Data Scientist',
126             website: 'example.com',
127         });
128
129         // Submit
130         cy.get('[data-cy=profile-form] button[type="submit"]').click();
131
132         // Check new values
133         assertProfileValues({
134             firstName: 'Active',
135             lastName: 'User',
136             email: 'user@example.local',
137             username: 'user',
138             org: 'Org name',
139             org_email: 'email@example.com',
140             role: 'Data Scientist',
141             website: 'example.com',
142         });
143     });
144
145     it('non-admin cannot edit other profile', function() {
146         cy.loginAs(activeUser);
147         cy.goToPath('/user/' + adminUser.user.uuid);
148
149         assertProfileValues({
150             firstName: 'Admin',
151             lastName: 'User',
152             email: 'admin@example.local',
153             username: 'admin',
154             org: '',
155             org_email: '',
156             role: '',
157             website: '',
158         });
159
160         // Inputs should be disabled
161         cy.get('[data-cy=profile-form] input[name="prefs.profile.organization"]').should('be.disabled');
162         cy.get('[data-cy=profile-form] input[name="prefs.profile.organization_email"]').should('be.disabled');
163         cy.get('[data-cy=profile-form] select[name="prefs.profile.role"]').should('be.disabled');
164         cy.get('[data-cy=profile-form] input[name="prefs.profile.website_url"]').should('be.disabled');
165
166         // Submit should be disabled
167         cy.get('[data-cy=profile-form] button[type="submit"]').should('be.disabled');
168
169         // Admin context items should be hidden
170         cy.get('[data-cy=user-profile-panel-options-btn]').click();
171         cy.get('[data-cy=context-menu]').within(() => {
172             cy.get('[role=button]').should('not.contain', 'Activate User')
173             cy.get('[role=button]').should('not.contain', 'Deactivate User')
174             cy.get('[role=button]').should('not.contain', 'Login As User')
175             cy.get('[role=button]').should('not.contain', 'Setup User');
176         });
177         cy.get('div[role=presentation]').click();
178     });
179
180     it('admin can edit own profile', function() {
181         cy.loginAs(adminUser);
182
183         cy.get('header button[title="Account Management"]').click();
184         cy.get('#account-menu').contains('My account').click();
185
186         // Admin context items should be visible
187         cy.get('[data-cy=user-profile-panel-options-btn]').click();
188         cy.get('[data-cy=context-menu]').within(() => {
189             cy.get('[role=button]').contains('Activate User')
190             cy.get('[role=button]').contains('Deactivate User')
191             cy.get('[role=button]').contains('Login As User')
192             cy.get('[role=button]').contains('Setup User');
193         });
194         cy.get('div[role=presentation]').click();
195
196         // Check initial values
197         assertProfileValues({
198             firstName: 'Admin',
199             lastName: 'User',
200             email: 'admin@example.local',
201             username: 'admin',
202             org: '',
203             org_email: '',
204             role: '',
205             website: '',
206         });
207
208         // Change values
209         enterProfileValues({
210             org: 'Admin org name',
211             org_email: 'admin@example.com',
212             role: 'Researcher',
213             website: 'admin.local',
214         });
215         cy.get('[data-cy=profile-form] button[type="submit"]').click();
216
217         // Check new values
218         assertProfileValues({
219             firstName: 'Admin',
220             lastName: 'User',
221             email: 'admin@example.local',
222             username: 'admin',
223             org: 'Admin org name',
224             org_email: 'admin@example.com',
225             role: 'Researcher',
226             website: 'admin.local',
227         });
228     });
229
230     it('admin can edit other profile', function() {
231         cy.loginAs(adminUser);
232         cy.goToPath('/user/' + activeUser.user.uuid);
233
234         // Check new values
235         assertProfileValues({
236             firstName: 'Active',
237             lastName: 'User',
238             email: 'user@example.local',
239             username: 'user',
240             org: '',
241             org_email: '',
242             role: '',
243             website: '',
244         });
245
246         enterProfileValues({
247             org: 'Changed org name',
248             org_email: 'changed@example.com',
249             role: 'Researcher',
250             website: 'changed.local',
251         });
252         cy.get('[data-cy=profile-form] button[type="submit"]').click();
253
254         // Check new values
255         assertProfileValues({
256             firstName: 'Active',
257             lastName: 'User',
258             email: 'user@example.local',
259             username: 'user',
260             org: 'Changed org name',
261             org_email: 'changed@example.com',
262             role: 'Researcher',
263             website: 'changed.local',
264         });
265     });
266
267     it('displays role groups on user profile', function() {
268         cy.loginAs(adminUser);
269
270         cy.createGroup(adminUser.token, {
271             name: roleGroupName,
272             group_class: 'role',
273         }).as('roleGroup').then(function() {
274             cy.createLink(adminUser.token, {
275                 name: 'can_write',
276                 link_class: 'permission',
277                 head_uuid: this.roleGroup.uuid,
278                 tail_uuid: adminUser.user.uuid
279             });
280             cy.createLink(adminUser.token, {
281                 name: 'can_write',
282                 link_class: 'permission',
283                 head_uuid: this.roleGroup.uuid,
284                 tail_uuid: activeUser.user.uuid
285             });
286         });
287
288         cy.createGroup(adminUser.token, {
289             name: projectGroupName,
290             group_class: 'project',
291         }).as('projectGroup').then(function() {
292             cy.createLink(adminUser.token, {
293                 name: 'can_write',
294                 link_class: 'permission',
295                 head_uuid: this.projectGroup.uuid,
296                 tail_uuid: adminUser.user.uuid
297             });
298             cy.createLink(adminUser.token, {
299                 name: 'can_write',
300                 link_class: 'permission',
301                 head_uuid: this.projectGroup.uuid,
302                 tail_uuid: activeUser.user.uuid
303             });
304         });
305
306         cy.goToPath('/user/' + activeUser.user.uuid);
307         cy.get('div [role="tab"]').contains('GROUPS').click();
308         cy.get('[data-cy=user-profile-groups-data-explorer]').contains(roleGroupName);
309         cy.get('[data-cy=user-profile-groups-data-explorer]').should('not.contain', projectGroupName);
310
311         cy.goToPath('/user/' + adminUser.user.uuid);
312         cy.get('div [role="tab"]').contains('GROUPS').click();
313         cy.get('[data-cy=user-profile-groups-data-explorer]').contains(roleGroupName);
314         cy.get('[data-cy=user-profile-groups-data-explorer]').should('not.contain', projectGroupName);
315     });
316
317     it('allows performing admin functions', function() {
318         cy.loginAs(adminUser);
319         cy.goToPath('/user/' + activeUser.user.uuid);
320
321         // Check that user is active
322         cy.get('[data-cy=account-status]').contains('Active');
323         cy.get('div [role="tab"]').contains('GROUPS').click();
324         cy.get('[data-cy=user-profile-groups-data-explorer]').should('contain', 'All users');
325         cy.get('div [role="tab"]').contains('PROFILE').click();
326
327         // Deactivate user
328         cy.get('[data-cy=user-profile-panel-options-btn]').click();
329         cy.get('[data-cy=context-menu]').contains('Deactivate User').click();
330         cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
331
332         // Check that user is deactivated
333         cy.get('[data-cy=account-status]').contains('Inactive');
334         cy.get('div [role="tab"]').contains('GROUPS').click();
335         cy.get('[data-cy=user-profile-groups-data-explorer]').should('not.contain', 'All users');
336         cy.get('div [role="tab"]').contains('PROFILE').click();
337
338         // Setup user
339         cy.get('[data-cy=user-profile-panel-options-btn]').click();
340         cy.get('[data-cy=context-menu]').contains('Setup User').click();
341         cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
342
343         // Check that user is setup
344         cy.get('[data-cy=account-status]').contains('Setup');
345         cy.get('div [role="tab"]').contains('GROUPS').click();
346         cy.get('[data-cy=user-profile-groups-data-explorer]').should('contain', 'All users');
347         cy.get('div [role="tab"]').contains('PROFILE').click();
348
349         // Activate user
350         cy.get('[data-cy=user-profile-panel-options-btn]').click();
351         cy.get('[data-cy=context-menu]').contains('Activate User').click();
352         cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
353
354         // Check that user is active
355         cy.get('[data-cy=account-status]').contains('Active');
356         cy.get('div [role="tab"]').contains('GROUPS').click();
357         cy.get('[data-cy=user-profile-groups-data-explorer]').should('contain', 'All users');
358         cy.get('div [role="tab"]').contains('PROFILE').click();
359
360         // Deactivate and activate user skipping setup
361         cy.get('[data-cy=user-profile-panel-options-btn]').click();
362         cy.get('[data-cy=context-menu]').contains('Deactivate User').click();
363         cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
364         //
365         cy.get('[data-cy=account-status]').contains('Inactive');
366         cy.get('div [role="tab"]').contains('GROUPS').click();
367         cy.get('[data-cy=user-profile-groups-data-explorer]').should('not.contain', 'All users');
368         cy.get('div [role="tab"]').contains('PROFILE').click();
369         //
370         cy.get('[data-cy=user-profile-panel-options-btn]').click();
371         cy.get('[data-cy=context-menu]').contains('Activate User').click();
372         cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
373
374         // Check that user is active
375         cy.get('[data-cy=account-status]').contains('Active');
376         cy.get('div [role="tab"]').contains('GROUPS').click();
377         cy.get('[data-cy=user-profile-groups-data-explorer]').should('contain', 'All users');
378     });
379
380 });