Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / 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         randomUser.username = `randomuser${Math.floor(Math.random() * 999999)}`;
32         randomUser.password = {
33             crypt: 'zpAReoZzPnwmQ',
34             clear: 'topsecret',
35         };
36         cy.exec(`useradd ${randomUser.username} -p ${randomUser.password.crypt}`);
37     })
38
39     after(function() {
40         cy.exec(`userdel ${randomUser.username}`);
41     })
42
43     beforeEach(function() {
44         cy.clearCookies()
45         cy.clearLocalStorage()
46     })
47
48     it('shows login page on first visit', function() {
49         cy.visit('/')
50         cy.get('div#root').should('contain', 'Please log in')
51         cy.url().should('not.contain', '/projects/')
52     })
53
54     it('shows login page with no token', function() {
55         cy.visit('/token/?api_token=')
56         cy.get('div#root').should('contain', 'Please log in')
57         cy.url().should('not.contain', '/projects/')
58     })
59
60     it('shows inactive page to inactive user', function() {
61         cy.visit(`/token/?api_token=${inactiveUser.token}`)
62         cy.get('div#root').should('contain', 'Your account is inactive');
63     })
64
65     it('shows login page with invalid token', function() {
66         cy.visit('/token/?api_token=nope')
67         cy.get('div#root').should('contain', 'Please log in')
68         cy.url().should('not.contain', '/projects/')
69     })
70
71     it('logs in successfully with valid user token', function() {
72         cy.visit(`/token/?api_token=${activeUser.token}`);
73         cy.url().should('contain', '/projects/');
74         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
75         cy.get('div#root').should('not.contain', 'Your account is inactive');
76         cy.get('button[title="Account Management"]').click();
77         cy.get('ul[role=menu] > li[role=menuitem]').contains(
78             `${activeUser.user.first_name} ${activeUser.user.last_name}`);
79     })
80
81     it('logs out when token no longer valid', function() {
82         cy.createProject({
83             owningUser: activeUser,
84             projectName: `Test Project ${Math.floor(Math.random() * 999999)}`,
85             addToFavorites: false
86         }).as('testProject1');
87         // Log in
88         cy.visit(`/token/?api_token=${activeUser.token}`);
89         cy.url().should('contain', '/projects/');
90         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
91         cy.get('div#root').should('not.contain', 'Your account is inactive');
92         cy.waitForDom();
93
94         // Invalidate own token.
95         const tokenUuid = activeUser.token.split('/')[1];
96         cy.doRequest('PUT', `/arvados/v1/api_client_authorizations/${tokenUuid}`, {
97             id: tokenUuid,
98             api_client_authorization: JSON.stringify({
99                 api_token: `randomToken${Math.floor(Math.random() * 999999)}`
100             })
101         }, null, activeUser.token, true);
102         // Should log the user out.
103
104         cy.getAll('@testProject1').then(([testProject1]) => {
105             cy.get('main').contains(testProject1.name).click();
106             cy.get('div#root').should('contain', 'Please log in');
107             // Should retain last visited url when auth is invalidated
108             cy.url().should('contain', `/projects/${testProject1.uuid}`);
109         })
110     })
111
112     it('logs in successfully with valid admin token', function() {
113         cy.visit(`/token/?api_token=${adminUser.token}`);
114         cy.url().should('contain', '/projects/');
115         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
116         cy.get('div#root').should('not.contain', 'Your account is inactive');
117         cy.get('button[title="Admin Panel"]').click();
118         cy.get('ul[role=menu] > li[role=menuitem]')
119             .contains('Repositories')
120             .type('{esc}');
121         cy.get('button[title="Account Management"]').click();
122         cy.get('ul[role=menu] > li[role=menuitem]').contains(
123             `${adminUser.user.first_name} ${adminUser.user.last_name}`);
124     })
125
126     it('fails to authenticate using the login form with wrong password', function() {
127         cy.visit('/');
128         cy.get('#username').type(randomUser.username);
129         cy.get('#password').type('wrong password');
130         cy.get("button span:contains('Log in')").click();
131         cy.get('p#password-helper-text').should('contain', 'PAM: Authentication failure');
132         cy.url().should('not.contain', '/projects/');
133     })
134
135     it('successfully authenticates using the login form', function() {
136         cy.visit('/');
137         cy.get('#username').type(randomUser.username);
138         cy.get('#password').type(randomUser.password.clear);
139         cy.get("button span:contains('Log in')").click();
140         cy.url().should('contain', '/projects/');
141         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
142         cy.get('div#root').should('contain', 'Your account is inactive');
143         cy.get('button[title="Account Management"]').click();
144         cy.get('ul[role=menu] > li[role=menuitem]').contains(randomUser.username);
145     })
146 })