Merge branch 'master' into 16848-token-handling-improvements
[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     "trashGroup", (token, uuid) => {
115         return cy.deleteResource(token, 'groups', uuid);
116     }
117 )
118
119
120 Cypress.Commands.add(
121     "createWorkflow", (token, data) => {
122         return cy.createResource(token, 'workflows', {
123             workflow: JSON.stringify(data),
124             ensure_unique_name: true
125         })
126     }
127 )
128
129 Cypress.Commands.add(
130     "createCollection", (token, data) => {
131         return cy.createResource(token, 'collections', {
132             collection: JSON.stringify(data),
133             ensure_unique_name: true
134         })
135     }
136 )
137
138 Cypress.Commands.add(
139     "updateCollection", (token, uuid, data) => {
140         return cy.updateResource(token, 'collections', uuid, {
141             collection: JSON.stringify(data)
142         })
143     }
144 )
145
146 Cypress.Commands.add(
147     "createResource", (token, suffix, data) => {
148         return cy.doRequest('POST', '/arvados/v1/' + suffix, data, null, token, true)
149             .its('body').as('resource')
150             .then(function () {
151                 return this.resource;
152             })
153     }
154 )
155
156 Cypress.Commands.add(
157     "deleteResource", (token, suffix, uuid) => {
158         return cy.doRequest('DELETE', '/arvados/v1/' + suffix + '/' + uuid)
159             .its('body').as('resource')
160             .then(function () {
161                 return this.resource;
162             })
163     }
164 )
165
166 Cypress.Commands.add(
167     "updateResource", (token, suffix, uuid, data) => {
168         return cy.doRequest('PUT', '/arvados/v1/' + suffix + '/' + uuid, data, null, token, true)
169             .its('body').as('resource')
170             .then(function () {
171                 return this.resource;
172             })
173     }
174 )
175
176 Cypress.Commands.add(
177     "loginAs", (user) => {
178         cy.visit(`/token/?api_token=${user.token}`);
179         cy.url().should('contain', '/projects/');
180         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
181         cy.get('div#root').should('not.contain', 'Your account is inactive');
182     }
183 )
184
185 Cypress.Commands.add(
186     "doSearch", (searchTerm) => {
187         cy.get('[data-cy=searchbar-input-field]').type(`{selectall}${searchTerm}{enter}`);
188     }
189 )
190
191 Cypress.Commands.add('getAll', (...elements) => {
192     const promise = cy.wrap([], { log: false })
193
194     for (let element of elements) {
195         promise.then(arr => cy.get(element).then(got => cy.wrap([...arr, got])))
196     }
197
198     return promise
199 })