18559: Add user profile tests.
[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] [data-cy=firstName] [data-cy=value]').contains(firstName);
39         cy.get('[data-cy=profile-form] [data-cy=lastName] [data-cy=value]').contains(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"]').click({force: true});
80
81         cy.goToPath('/user/' + activeUser.user.uuid);
82         enterProfileValues({
83             org: '',
84             org_email: '',
85             role: '',
86             website: '',
87         });
88         cy.get('[data-cy=profile-form] button[type="submit"]').click({force: true});
89     });
90
91     it('non-admin can edit own profile', function() {
92         cy.loginAs(activeUser);
93
94         cy.get('header button[title="Account Management"]').click();
95         cy.get('#account-menu').contains('My account').click();
96
97         // Admin tab should be hidden
98         cy.get('div [role="tab"]').should('not.contain', 'ADMIN');
99
100         // Check initial values
101         assertProfileValues({
102             firstName: 'Active',
103             lastName: 'User',
104             email: 'user@example.local',
105             username: 'user',
106             org: '',
107             org_email: '',
108             role: '',
109             website: '',
110         });
111
112         // Change values
113         enterProfileValues({
114             org: 'Org name',
115             org_email: 'email@example.com',
116             role: 'Data Scientist',
117             website: 'example.com',
118         });
119
120         // Submit
121         cy.get('[data-cy=profile-form] button[type="submit"]').click();
122
123         // Check new values
124         assertProfileValues({
125             firstName: 'Active',
126             lastName: 'User',
127             email: 'user@example.local',
128             username: 'user',
129             org: 'Org name',
130             org_email: 'email@example.com',
131             role: 'Data Scientist',
132             website: 'example.com',
133         });
134     });
135
136     it('non-admin cannot edit other profile', function() {
137         cy.loginAs(activeUser);
138         cy.goToPath('/user/' + adminUser.user.uuid);
139
140         assertProfileValues({
141             firstName: 'Admin',
142             lastName: 'User',
143             email: 'admin@example.local',
144             username: 'admin',
145             org: '',
146             org_email: '',
147             role: '',
148             website: '',
149         });
150
151         // Inputs should be disabled
152         cy.get('[data-cy=profile-form] input[name="prefs.profile.organization"]').should('be.disabled');
153         cy.get('[data-cy=profile-form] input[name="prefs.profile.organization_email"]').should('be.disabled');
154         cy.get('[data-cy=profile-form] select[name="prefs.profile.role"]').should('be.disabled');
155         cy.get('[data-cy=profile-form] input[name="prefs.profile.website_url"]').should('be.disabled');
156
157         // Submit should be disabled
158         cy.get('[data-cy=profile-form] button[type="submit"]').should('be.disabled');
159
160         // Admin tab should be hidden
161         cy.get('div [role="tab"]').should('not.contain', 'ADMIN');
162     });
163
164     it('admin can edit own profile', function() {
165         cy.loginAs(adminUser);
166
167         cy.get('header button[title="Account Management"]').click();
168         cy.get('#account-menu').contains('My account').click();
169
170         // Admin tab should be visible
171         cy.get('div [role="tab"]').should('contain', 'ADMIN');
172
173         // Check initial values
174         assertProfileValues({
175             firstName: 'Admin',
176             lastName: 'User',
177             email: 'admin@example.local',
178             username: 'admin',
179             org: '',
180             org_email: '',
181             role: '',
182             website: '',
183         });
184
185         // Change values
186         enterProfileValues({
187             org: 'Admin org name',
188             org_email: 'admin@example.com',
189             role: 'Researcher',
190             website: 'admin.local',
191         });
192         cy.get('[data-cy=profile-form] button[type="submit"]').click();
193
194         // Check new values
195         assertProfileValues({
196             firstName: 'Admin',
197             lastName: 'User',
198             email: 'admin@example.local',
199             username: 'admin',
200             org: 'Admin org name',
201             org_email: 'admin@example.com',
202             role: 'Researcher',
203             website: 'admin.local',
204         });
205     });
206
207     it('admin can edit other profile', function() {
208         cy.loginAs(adminUser);
209         cy.goToPath('/user/' + activeUser.user.uuid);
210
211         // Check new values
212         assertProfileValues({
213             firstName: 'Active',
214             lastName: 'User',
215             email: 'user@example.local',
216             username: 'user',
217             org: '',
218             org_email: '',
219             role: '',
220             website: '',
221         });
222
223         enterProfileValues({
224             org: 'Changed org name',
225             org_email: 'changed@example.com',
226             role: 'Researcher',
227             website: 'changed.local',
228         });
229         cy.get('[data-cy=profile-form] button[type="submit"]').click();
230
231         // Check new values
232         assertProfileValues({
233             firstName: 'Active',
234             lastName: 'User',
235             email: 'user@example.local',
236             username: 'user',
237             org: 'Changed org name',
238             org_email: 'changed@example.com',
239             role: 'Researcher',
240             website: 'changed.local',
241         });
242     });
243
244     it('displays role groups on user profile', function() {
245         cy.loginAs(adminUser);
246
247         cy.createGroup(adminUser.token, {
248             name: roleGroupName,
249             group_class: 'role',
250         }).as('roleGroup').then(function() {
251             cy.createLink(adminUser.token, {
252                 name: 'can_write',
253                 link_class: 'permission',
254                 head_uuid: this.roleGroup.uuid,
255                 tail_uuid: adminUser.user.uuid
256             });
257             cy.createLink(adminUser.token, {
258                 name: 'can_write',
259                 link_class: 'permission',
260                 head_uuid: this.roleGroup.uuid,
261                 tail_uuid: activeUser.user.uuid
262             });
263         });
264
265         cy.createGroup(adminUser.token, {
266             name: projectGroupName,
267             group_class: 'project',
268         }).as('projectGroup').then(function() {
269             cy.createLink(adminUser.token, {
270                 name: 'can_write',
271                 link_class: 'permission',
272                 head_uuid: this.projectGroup.uuid,
273                 tail_uuid: adminUser.user.uuid
274             });
275             cy.createLink(adminUser.token, {
276                 name: 'can_write',
277                 link_class: 'permission',
278                 head_uuid: this.projectGroup.uuid,
279                 tail_uuid: activeUser.user.uuid
280             });
281         });
282
283         cy.goToPath('/user/' + activeUser.user.uuid);
284         cy.get('div [role="tab"]').contains('GROUPS').click();
285         cy.get('[data-cy=user-profile-groups-data-explorer]').contains(roleGroupName);
286         cy.get('[data-cy=user-profile-groups-data-explorer]').should('not.contain', projectGroupName);
287
288         cy.goToPath('/user/' + adminUser.user.uuid);
289         cy.get('div [role="tab"]').contains('GROUPS').click();
290         cy.get('[data-cy=user-profile-groups-data-explorer]').contains(roleGroupName);
291         cy.get('[data-cy=user-profile-groups-data-explorer]').should('not.contain', projectGroupName);
292     });
293
294 });