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