16118: Adds collection's integration test suite (WIP)
[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(
57     "getUser", (username, first_name='', last_name='', is_admin=false, is_active=true) => {
58         // Create user if not already created
59         return cy.do_request('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.do_request('GET', '/arvados/v1/users', null, {
75                 filters: `[["username", "=", "${username}"]]`
76             })
77             .its('body.items.0')
78             .as('aUser')
79             .then(function() {
80                 cy.do_request('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     "createCollection", (token, collection) => {
98         return cy.do_request('POST', '/arvados/v1/collections', {
99             collection: JSON.stringify(collection),
100             ensure_unique_name: true
101         }, null, token, true)
102         .its('body').as('collection')
103         .then(function() {
104             return this.collection;
105         })
106     }
107 )
108
109 Cypress.Commands.add(
110     "loginAs", (user) => {
111         cy.visit(`/token/?api_token=${user.token}`);
112         cy.url().should('contain', '/projects/');
113         cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
114         cy.get('div#root').should('not.contain', 'Your account is inactive');
115     }
116 )