8baa2db6e58398b969ecd97fdb3bc6a481ef5d3e
[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     "doRequest", (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(
57     "getUser", (username, first_name='', last_name='', is_admin=false, is_active=true) => {
58         // Create user if not already created
59         return cy.doRequest('POST', '/auth/controller/callback', {
60             auth_info: JSON.stringify({
61                 email: `${username}@example.local`,
62                 username: username,
63                 first_name: first_name,
64                 last_name: last_name,
65                 alternate_emails: []
66             }),
67             return_to: ',https://example.local'
68         }, null, systemToken, true, false) // Don't follow redirects so we can catch the token
69         .its('headers.location').as('location')
70         // Get its token and set the account up as admin and/or active
71         .then(function() {
72             this.userToken = this.location.split("=")[1]
73             assert.isString(this.userToken)
74             return cy.doRequest('GET', '/arvados/v1/users', null, {
75                 filters: `[["username", "=", "${username}"]]`
76             })
77             .its('body.items.0')
78             .as('aUser')
79             .then(function() {
80                 cy.doRequest('PUT', `/arvados/v1/users/${this.aUser.uuid}`, {
81                     user: {
82                         is_admin: is_admin,
83                         is_active: is_active
84                     }
85                 })
86                 .its('body')
87                 .as('theUser')
88                 .then(function() {
89                     return {user: this.theUser, token: this.userToken};
90                 })
91             })
92         })
93     }
94 )
95
96 Cypress.Commands.add(
97     "createLink", (token, data) => {
98         return cy.createResource(token, 'links', {
99             link: JSON.stringify(data)
100         })
101     }
102 )
103
104 Cypress.Commands.add(
105     "createGroup", (token, data) => {
106         return cy.createResource(token, 'groups', {
107             group: JSON.stringify(data),
108             ensure_unique_name: true
109         })
110     }
111 )
112
113 Cypress.Commands.add(
114     "createCollection", (token, data) => {
115         return cy.createResource(token, 'collections', {
116             collection: JSON.stringify(data),
117             ensure_unique_name: true
118         })
119     }
120 )
121
122 Cypress.Commands.add(
123     "createResource", (token, suffix, data) => {
124         return cy.doRequest('POST', '/arvados/v1/'+suffix, data, null, token, true)
125         .its('body').as('resource')
126         .then(function() {
127             return this.resource;
128         })
129     }
130 )
131
132 Cypress.Commands.add(
133     "loginAs", (user) => {
134         cy.visit(`/token/?api_token=${user.token}`);
135         cy.url().should('contain', '/projects/');
136         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
137         cy.get('div#root').should('not.contain', 'Your account is inactive');
138     }
139 )