19153: Fix project cypress test
[arvados.git] / cypress / integration / virtual-machine-admin.spec.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 describe('Virtual machine login manage tests', function() {
6     let activeUser;
7     let adminUser;
8
9     const vmHost = `vm-${Math.floor(999999 * Math.random())}.host`;
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', 'VMAdmin', 'User', true, true)
17             .as('adminUser').then(function() {
18                 adminUser = this.adminUser;
19             }
20         );
21         cy.getUser('user', 'VMActive', 'User', false, true)
22             .as('activeUser').then(function() {
23                 activeUser = this.activeUser;
24             }
25         );
26     });
27
28     it('adds and removes vm logins', function() {
29         cy.loginAs(adminUser);
30         cy.createVirtualMachine(adminUser.token, {hostname: vmHost});
31
32         // Navigate to VM admin
33         cy.get('header button[title="Admin Panel"]').click();
34         cy.get('#admin-menu').contains('Virtual Machines').click();
35
36         // Add login permission to admin
37         cy.get('[data-cy=vm-admin-table]')
38             .contains(vmHost)
39             .parents('tr')
40             .within(() => {
41                 cy.get('button[title="Add Login Permission"]').click();
42             });
43         cy.get('[data-cy=form-dialog]')
44             .should('contain', 'Add login permission')
45             .within(() => {
46                 cy.get('label')
47                   .contains('Search for user')
48                   .parent()
49                   .within(() => {
50                     cy.get('input').type('VMAdmin');
51                   })
52             });
53         cy.get('[role=tooltip]').click();
54         cy.get('[data-cy=form-dialog]')
55             .should('contain', 'Add login permission')
56             .within(() => {
57                 cy.get('label')
58                   .contains('Add groups')
59                   .parent()
60                   .within(() => {
61                     cy.get('input').type('docker sudo{enter}');
62                   })
63             });
64         cy.get('[data-cy=form-dialog]').within(() => {
65             cy.get('[data-cy=form-submit-btn]').click();
66         });
67         cy.get('[data-cy=snackbar]').contains('Permission updated');
68         cy.get('[data-cy=vm-admin-table]')
69             .contains(vmHost)
70             .parents('tr')
71             .within(() => {
72                 cy.get('td').contains('admin');
73         });
74
75         // Add login permission to activeUser
76         cy.get('[data-cy=vm-admin-table]')
77             .contains(vmHost)
78             .parents('tr')
79             .within(() => {
80                 cy.get('button[title="Add Login Permission"]').click();
81             });
82         cy.get('[data-cy=form-dialog]')
83             .should('contain', 'Add login permission')
84             .within(() => {
85                 cy.get('label')
86                   .contains('Search for user')
87                   .parent()
88                   .within(() => {
89                     cy.get('input').type('VMActive user');
90                   })
91             });
92         cy.get('[role=tooltip]').click();
93         cy.get('[data-cy=form-dialog]').within(() => {
94             cy.get('[data-cy=form-submit-btn]').click();
95         });
96         cy.get('[data-cy=snackbar]').contains('Permission updated');
97         cy.get('[data-cy=vm-admin-table]')
98             .contains(vmHost)
99             .parents('tr')
100             .within(() => {
101                 cy.get('td').contains('user');
102         });
103
104         // Check admin's vm page for login
105         cy.get('header button[title="Account Management"]').click();
106         cy.get('#account-menu').contains('Virtual Machines').click();
107
108         cy.get('[data-cy=vm-user-table]')
109             .contains(vmHost)
110             .parents('tr')
111             .within(() => {
112                 cy.get('td').contains('admin');
113                 cy.get('td').contains('docker');
114                 cy.get('td').contains('sudo');
115                 cy.get('td').contains('ssh admin@' + vmHost);
116         });
117
118         // Check activeUser's vm page for login
119         cy.loginAs(activeUser);
120         cy.get('header button[title="Account Management"]').click();
121         cy.get('#account-menu').contains('Virtual Machines').click();
122
123         cy.get('[data-cy=vm-user-table]')
124             .contains(vmHost)
125             .parents('tr')
126             .within(() => {
127                 cy.get('td').contains('user');
128                 cy.get('td').should('not.contain', 'docker');
129                 cy.get('td').should('not.contain', 'sudo');
130                 cy.get('td').contains('ssh user@' + vmHost);
131         });
132
133         // Edit login permissions
134         cy.loginAs(adminUser);
135         cy.get('header button[title="Admin Panel"]').click();
136         cy.get('#admin-menu').contains('Virtual Machines').click();
137
138         cy.get('[data-cy=vm-admin-table]')
139             .contains('admin'); // Wait for page to finish
140
141         cy.get('[data-cy=vm-admin-table]')
142             .contains(vmHost)
143             .parents('tr')
144             .contains('admin')
145             .click();
146
147         cy.get('[data-cy=form-dialog]')
148             .should('contain', 'Update login permission')
149             .within(() => {
150                 cy.get('label')
151                     .contains('Add groups')
152                     .parent()
153                     .as('groupInput');
154             });
155
156         cy.get('@groupInput').within(() => {
157             cy.get('div[role=button]').contains('sudo').parent().find('svg').click();
158             cy.get('div[role=button]').contains('docker').parent().find('svg').click();
159         });
160
161         cy.get('[data-cy=form-dialog]').within(() => {
162             cy.get('[data-cy=form-submit-btn]').click();
163         });
164
165         // Wait for page to finish loading
166         cy.get('[data-cy=snackbar]').contains('Permission updated');
167         cy.get('[data-cy=vm-admin-table]')
168             .contains(vmHost)
169             .parents('tr')
170             .within(() => {
171                 cy.get('div[role=button]')
172                     .parent()
173                     .first()
174                     .contains('admin')
175             });
176
177         cy.get('[data-cy=vm-admin-table]')
178             .contains(vmHost)
179             .parents('tr')
180             .contains('user')
181             .click();
182
183         cy.get('[data-cy=form-dialog]')
184             .should('contain', 'Update login permission')
185             .within(() => {
186                 cy.get('label')
187                     .contains('Add groups')
188                     .parent()
189                     .within(() => {
190                         cy.get('input').type('docker{enter}');
191                     })
192             });
193
194         cy.get('[data-cy=form-dialog]').within(() => {
195             cy.get('[data-cy=form-submit-btn]').click();
196         });
197         cy.get('[data-cy=snackbar]').contains('Permission updated');
198
199         // Verify new login permissions
200         // Check admin's vm page for login
201         cy.get('header button[title="Account Management"]').click();
202         cy.get('#account-menu').contains('Virtual Machines').click();
203
204         cy.get('[data-cy=vm-user-table]')
205             .contains(vmHost)
206             .parents('tr')
207             .within(() => {
208                 cy.get('td').contains('admin');
209                 cy.get('td').should('not.contain', 'docker');
210                 cy.get('td').should('not.contain', 'sudo');
211                 cy.get('td').contains('ssh admin@' + vmHost);
212         });
213
214         // Verify new login permissions
215         // Check activeUser's vm page for login
216         cy.loginAs(activeUser);
217         cy.get('header button[title="Account Management"]').click();
218         cy.get('#account-menu').contains('Virtual Machines').click();
219
220         cy.get('[data-cy=vm-user-table]')
221             .contains(vmHost)
222             .parents('tr')
223             .within(() => {
224                 cy.get('td').contains('user');
225                 cy.get('td').contains('docker');
226                 cy.get('td').should('not.contain', 'sudo');
227                 cy.get('td').contains('ssh user@' + vmHost);
228         });
229
230         // Remove login permissions
231         cy.loginAs(adminUser);
232         cy.get('header button[title="Admin Panel"]').click();
233         cy.get('#admin-menu').contains('Virtual Machines').click();
234
235         cy.get('[data-cy=vm-admin-table]')
236             .contains('user'); // Wait for page to finish
237
238         cy.get('[data-cy=vm-admin-table]')
239             .contains(vmHost)
240             .parents('tr')
241             .as('vmRow')
242             .contains('user')
243             .parents('[role=button]')
244             .find('svg')
245             .as('removeButton');
246         cy.get('@removeButton').click();
247         cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
248
249         cy.get('@vmRow')
250             .within(() => {
251                 cy.get('div[role=button]').should('not.contain', 'user');
252                 cy.get('div[role=button]').should('have.length', 1)
253             });
254
255         cy.get('@vmRow')
256             .find('div[role=button]')
257             .contains('admin')
258             .parents('[role=button]')
259             .find('svg')
260             .as('removeButton');
261         cy.get('@removeButton').click();
262         cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
263
264         cy.get('[data-cy=vm-admin-table]')
265             .contains(vmHost)
266             .parents('tr')
267             .within(() => {
268                 cy.get('div[role=button]').should('not.contain', 'admin');
269             });
270
271         // Check admin's vm page for login
272         cy.get('header button[title="Account Management"]').click();
273         cy.get('#account-menu').contains('Virtual Machines').click();
274
275         cy.get('[data-cy=vm-user-panel]')
276             .should('not.contain', vmHost);
277
278         // Check activeUser's vm page for login
279         cy.loginAs(activeUser);
280         cy.get('header button[title="Account Management"]').click();
281         cy.get('#account-menu').contains('Virtual Machines').click();
282
283         cy.get('[data-cy=vm-user-panel]')
284             .should('not.contain', vmHost);
285     });
286 });