21126: Merge branch 'main' into 21126-trash-when-ro
[arvados.git] / services / workbench2 / cypress / integration / login.spec.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 describe('Login tests', function() {
6     let activeUser;
7     let inactiveUser;
8     let adminUser;
9     let randomUser = {};
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('active', 'Active', 'User', false, true)
22             .as('activeUser').then(function() {
23                 activeUser = this.activeUser;
24             }
25         );
26         cy.getUser('inactive', 'Inactive', 'User', false, false)
27             .as('inactiveUser').then(function() {
28                 inactiveUser = this.inactiveUser;
29             }
30                                     );
31         // Username/password match Login.Test section of arvados_config.yml
32         randomUser.username = 'randomuser1234';
33         randomUser.password = 'topsecret';
34     })
35
36     beforeEach(function() {
37         cy.clearCookies()
38         cy.clearLocalStorage()
39     })
40
41     it('shows login page on first visit', function() {
42         cy.visit('/')
43         cy.get('div#root').should('contain', 'Please log in')
44         cy.url().should('not.contain', '/projects/')
45     })
46
47     it('shows login page with no token', function() {
48         cy.visit('/token/?api_token=')
49         cy.get('div#root').should('contain', 'Please log in')
50         cy.url().should('not.contain', '/projects/')
51     })
52
53     it('shows inactive page to inactive user', function() {
54         cy.visit(`/token/?api_token=${inactiveUser.token}`)
55         cy.get('div#root').should('contain', 'Your account is inactive');
56     })
57
58     it('shows login page with invalid token', function() {
59         cy.visit('/token/?api_token=nope')
60         cy.get('div#root').should('contain', 'Please log in')
61         cy.url().should('not.contain', '/projects/')
62     })
63
64     it('logs in successfully with valid user token', function() {
65         cy.visit(`/token/?api_token=${activeUser.token}`);
66         cy.url().should('contain', '/projects/');
67         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
68         cy.get('div#root').should('not.contain', 'Your account is inactive');
69         cy.get('button[title="Account Management"]').click();
70         cy.get('ul[role=menu] > li[role=menuitem]').contains(
71             `${activeUser.user.first_name} ${activeUser.user.last_name}`);
72     })
73
74     it('logs out when token no longer valid', function() {
75         cy.createProject({
76             owningUser: activeUser,
77             projectName: `Test Project ${Math.floor(Math.random() * 999999)}`,
78             addToFavorites: false
79         }).as('testProject1');
80         // Log in
81         cy.visit(`/token/?api_token=${activeUser.token}`);
82         cy.url().should('contain', '/projects/');
83         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
84         cy.get('div#root').should('not.contain', 'Your account is inactive');
85         cy.waitForDom();
86
87         // Invalidate own token.
88         const tokenUuid = activeUser.token.split('/')[1];
89         cy.doRequest('PUT', `/arvados/v1/api_client_authorizations/${tokenUuid}`, {
90             id: tokenUuid,
91             api_client_authorization: JSON.stringify({
92                 api_token: `randomToken${Math.floor(Math.random() * 999999)}`
93             })
94         }, null, activeUser.token, true);
95         // Should log the user out.
96
97         cy.getAll('@testProject1').then(([testProject1]) => {
98             cy.get('main').contains(testProject1.name).click();
99             cy.get('div#root').should('contain', 'Please log in');
100             // Should retain last visited url when auth is invalidated
101             cy.url().should('contain', `/projects/${testProject1.uuid}`);
102         })
103     })
104
105     it('logs in successfully with valid admin token', function() {
106         cy.visit(`/token/?api_token=${adminUser.token}`);
107         cy.url().should('contain', '/projects/');
108         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
109         cy.get('div#root').should('not.contain', 'Your account is inactive');
110         cy.get('button[title="Admin Panel"]').click();
111         cy.get('ul[role=menu] > li[role=menuitem]')
112             .contains('Repositories')
113             .type('{esc}');
114         cy.get('button[title="Account Management"]').click();
115         cy.get('ul[role=menu] > li[role=menuitem]').contains(
116             `${adminUser.user.first_name} ${adminUser.user.last_name}`);
117     })
118
119     it('fails to authenticate using the login form with wrong password', function() {
120         cy.visit('/');
121         cy.get('#username').type(randomUser.username);
122         cy.get('#password').type('wrong password');
123         cy.get("button span:contains('Log in')").click();
124         cy.get('p#password-helper-text').should('contain', 'authentication failed');
125         cy.url().should('not.contain', '/projects/');
126     })
127
128     it('successfully authenticates using the login form', function() {
129         cy.visit('/');
130         cy.get('#username').type(randomUser.username);
131         cy.get('#password').type(randomUser.password);
132         cy.get("button span:contains('Log in')").click();
133         cy.url().should('contain', '/projects/');
134         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
135         cy.get('div#root').should('contain', 'Your account is inactive');
136         cy.get('button[title="Account Management"]').click();
137         cy.get('ul[role=menu] > li[role=menuitem]').contains(randomUser.username);
138     })
139 })