1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 describe('Login tests', 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;
21 cy.getUser('active', 'Active', 'User', false, true)
22 .as('activeUser').then(function() {
23 activeUser = this.activeUser;
26 cy.getUser('inactive', 'Inactive', 'User', false, false)
27 .as('inactiveUser').then(function() {
28 inactiveUser = this.inactiveUser;
31 // Username/password match Login.Test section of arvados_config.yml
32 randomUser.username = 'randomuser1234';
33 randomUser.password = 'topsecret';
36 beforeEach(function() {
38 cy.clearLocalStorage()
41 it('shows login page on first visit', function() {
43 cy.get('div#root').should('contain', 'Please log in')
44 cy.url().should('not.contain', '/projects/')
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/')
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');
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/')
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}`);
74 it('logs out when token no longer valid', function() {
76 owningUser: activeUser,
77 projectName: `Test Project ${Math.floor(Math.random() * 999999)}`,
79 }).as('testProject1');
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');
87 // Invalidate own token.
88 const tokenUuid = activeUser.token.split('/')[1];
89 cy.doRequest('PUT', `/arvados/v1/api_client_authorizations/${tokenUuid}`, {
91 api_client_authorization: JSON.stringify({
92 api_token: `randomToken${Math.floor(Math.random() * 999999)}`
94 }, null, activeUser.token, true);
95 // Should log the user out.
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}`);
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')
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}`);
119 it('fails to authenticate using the login form with wrong password', function() {
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/');
128 it('successfully authenticates using the login form', function() {
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);