Merge branch '16029-cypress-testing'
[arvados-workbench2.git] / cypress / support / commands.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 // ***********************************************
6 // This example commands.js shows you how to
7 // create various custom commands and overwrite
8 // existing commands.
9 //
10 // For more comprehensive examples of custom
11 // commands please read more here:
12 // https://on.cypress.io/custom-commands
13 // ***********************************************
14 //
15 //
16 // -- This is a parent command --
17 // Cypress.Commands.add("login", (email, password) => { ... })
18 //
19 //
20 // -- This is a child command --
21 // Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
22 //
23 //
24 // -- This is a dual command --
25 // Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
26 //
27 //
28 // -- This will overwrite an existing command --
29 // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
30
31 const controllerURL = Cypress.env('controller_url');
32 const systemToken = Cypress.env('system_token');
33
34 Cypress.Commands.add(
35     "do_request", (method='GET', path='', data=null, qs=null,
36                    token=systemToken, auth=false, followRedirect=true) => {
37         return cy.request({
38             method: method,
39             url: `${controllerURL}/${path}`,
40             body: data,
41             qs: auth ? qs : Object.assign({api_token: token}, qs),
42             auth: auth ? {bearer: `${token}`} : undefined,
43             followRedirect: followRedirect
44         })
45     }
46 )
47
48 // This resets the DB removing all content and seeding it with the fixtures.
49 // TODO: Maybe we can add an optional param to avoid the loading part?
50 Cypress.Commands.add(
51     "resetDB", () => {
52         cy.request('POST', `${controllerURL}/database/reset?api_token=${systemToken}`);
53     }
54 )
55
56 Cypress.Commands.add("getUser", (username, first_name='', last_name='', is_admin=false, is_active=true) => {
57     // Create user if not already created
58     return cy.do_request('POST', '/auth/controller/callback', {
59         auth_info: JSON.stringify({
60             email: `${username}@example.local`,
61             username: username,
62             first_name: first_name,
63             last_name: last_name,
64             alternate_emails: []
65         }),
66         return_to: ',https://example.local'
67     }, null, systemToken, true, false) // Don't follow redirects so we can catch the token
68     .its('headers.location').as('location')
69     // Get its token and set the account up as admin and/or active
70     .then(function() {
71         this.userToken = this.location.split("=")[1]
72         assert.isString(this.userToken)
73         return cy.do_request('GET', '/arvados/v1/users', null, {
74             filters: `[["username", "=", "${username}"]]`
75         })
76         .its('body.items.0')
77         .as('aUser')
78         .then(function() {
79             cy.do_request('PUT', `/arvados/v1/users/${this.aUser.uuid}`, {
80                 user: {
81                     is_admin: is_admin,
82                     is_active: is_active
83                 }
84             })
85             .its('body')
86             .as('theUser')
87             .then(function() {
88                 return {user: this.theUser, token: this.userToken};
89             })
90         })
91     })
92 })