1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 // ***********************************************
6 // This example commands.js shows you how to
7 // create various custom commands and overwrite
10 // For more comprehensive examples of custom
11 // commands please read more here:
12 // https://on.cypress.io/custom-commands
13 // ***********************************************
16 // -- This is a parent command --
17 // Cypress.Commands.add("login", (email, password) => { ... })
20 // -- This is a child command --
21 // Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
24 // -- This is a dual command --
25 // Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
28 // -- This will overwrite an existing command --
29 // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
31 const controllerURL = Cypress.env('controller_url');
32 const systemToken = Cypress.env('system_token');
33 let createdResources = [];
35 // Clean up on a 'before' hook to allow post-mortem analysis on individual tests.
36 beforeEach(function () {
37 if (createdResources.length === 0) {
40 cy.log(`Cleaning ${createdResources.length} previously created resource(s)`);
41 createdResources.forEach(function({suffix, uuid}) {
42 // Don't fail when a resource isn't already there, some objects may have
43 // been removed, directly or indirectly, from the test that created them.
44 cy.deleteResource(systemToken, suffix, uuid, false);
46 createdResources = [];
50 "doRequest", (method = 'GET', path = '', data = null, qs = null,
51 token = systemToken, auth = false, followRedirect = true, failOnStatusCode = true) => {
54 url: `${controllerURL.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`,
56 qs: auth ? qs : Object.assign({ api_token: token }, qs),
57 auth: auth ? { bearer: `${token}` } : undefined,
58 followRedirect: followRedirect,
59 failOnStatusCode: failOnStatusCode
64 "getUser", (username, first_name = '', last_name = '', is_admin = false, is_active = true) => {
65 // Create user if not already created
66 return cy.doRequest('POST', '/auth/controller/callback', {
67 auth_info: JSON.stringify({
68 email: `${username}@example.local`,
70 first_name: first_name,
74 return_to: ',https://example.local'
75 }, null, systemToken, true, false) // Don't follow redirects so we can catch the token
76 .its('headers.location').as('location')
77 // Get its token and set the account up as admin and/or active
79 this.userToken = this.location.split("=")[1]
80 assert.isString(this.userToken)
81 return cy.doRequest('GET', '/arvados/v1/users', null, {
82 filters: `[["username", "=", "${username}"]]`
87 cy.doRequest('PUT', `/arvados/v1/users/${this.aUser.uuid}`, {
96 return { user: this.theUser, token: this.userToken };
103 Cypress.Commands.add(
104 "createLink", (token, data) => {
105 return cy.createResource(token, 'links', {
106 link: JSON.stringify(data)
111 Cypress.Commands.add(
112 "createGroup", (token, data) => {
113 return cy.createResource(token, 'groups', {
114 group: JSON.stringify(data),
115 ensure_unique_name: true
120 Cypress.Commands.add(
121 "trashGroup", (token, uuid) => {
122 return cy.deleteResource(token, 'groups', uuid);
127 Cypress.Commands.add(
128 "createWorkflow", (token, data) => {
129 return cy.createResource(token, 'workflows', {
130 workflow: JSON.stringify(data),
131 ensure_unique_name: true
136 Cypress.Commands.add(
137 "createCollection", (token, data) => {
138 return cy.createResource(token, 'collections', {
139 collection: JSON.stringify(data),
140 ensure_unique_name: true
145 Cypress.Commands.add(
146 "updateCollection", (token, uuid, data) => {
147 return cy.updateResource(token, 'collections', uuid, {
148 collection: JSON.stringify(data)
153 Cypress.Commands.add(
154 "createVirtualMachine", (token, data) => {
155 return cy.createResource(token, 'virtual_machines', {
156 virtual_machine: JSON.stringify(data),
157 ensure_unique_name: true
162 Cypress.Commands.add(
163 "createResource", (token, suffix, data) => {
164 return cy.doRequest('POST', '/arvados/v1/' + suffix, data, null, token, true)
165 .its('body').as('resource')
167 createdResources.push({suffix, uuid: this.resource.uuid});
168 return this.resource;
173 Cypress.Commands.add(
174 "deleteResource", (token, suffix, uuid, failOnStatusCode = true) => {
175 return cy.doRequest('DELETE', '/arvados/v1/' + suffix + '/' + uuid, null, null, token, false, true, failOnStatusCode)
176 .its('body').as('resource')
178 return this.resource;
183 Cypress.Commands.add(
184 "updateResource", (token, suffix, uuid, data) => {
185 return cy.doRequest('PUT', '/arvados/v1/' + suffix + '/' + uuid, data, null, token, true)
186 .its('body').as('resource')
188 return this.resource;
193 Cypress.Commands.add(
194 "loginAs", (user) => {
196 cy.clearLocalStorage()
197 cy.visit(`/token/?api_token=${user.token}`);
198 cy.url({timeout: 10000}).should('contain', '/projects/');
199 cy.get('div#root').should('contain', 'Arvados Workbench (zzzzz)');
200 cy.get('div#root').should('not.contain', 'Your account is inactive');
204 Cypress.Commands.add(
205 "testEditProjectOrCollection", (container, oldName, newName, newDescription, isProject = true) => {
206 cy.get(container).contains(oldName).rightclick();
207 cy.get('[data-cy=context-menu]').contains(isProject ? 'Edit project' : 'Edit collection').click();
208 cy.get('[data-cy=form-dialog]').within(() => {
209 cy.get('input[name=name]').clear().type(newName);
210 cy.get(isProject ? 'div[contenteditable=true]' : 'input[name=description]').clear().type(newDescription);
211 cy.get('[data-cy=form-submit-btn]').click();
214 cy.get(container).contains(newName).rightclick();
215 cy.get('[data-cy=context-menu]').contains(isProject ? 'Edit project' : 'Edit collection').click();
216 cy.get('[data-cy=form-dialog]').within(() => {
217 cy.get('input[name=name]').should('have.value', newName);
220 cy.get('span[data-text=true]').contains(newDescription);
222 cy.get('input[name=description]').should('have.value', newDescription);
225 cy.get('[data-cy=form-cancel-btn]').click();
230 Cypress.Commands.add(
231 "doSearch", (searchTerm) => {
232 cy.get('[data-cy=searchbar-input-field]').type(`{selectall}${searchTerm}{enter}`);
236 Cypress.Commands.add(
237 "goToPath", (path) => {
238 return cy.window().its('appHistory').invoke('push', path);
242 Cypress.Commands.add('getAll', (...elements) => {
243 const promise = cy.wrap([], { log: false })
245 for (let element of elements) {
246 promise.then(arr => cy.get(element).then(got => cy.wrap([...arr, got])))
252 Cypress.Commands.add('shareWith', (srcUserToken, targetUserUUID, itemUUID, permission = 'can_write') => {
253 cy.createLink(srcUserToken, {
255 link_class: 'permission',
257 tail_uuid: targetUserUUID
261 Cypress.Commands.add('addToFavorites', (userToken, userUUID, itemUUID) => {
262 cy.createLink(userToken, {
266 owner_uuid: userUUID,
271 Cypress.Commands.add('createProject', ({
278 const writePermission = canWrite ? 'can_write' : 'can_read';
280 cy.createGroup(owningUser.token, {
281 name: `${projectName} ${Math.floor(Math.random() * 999999)}`,
282 group_class: 'project',
283 }).as(`${projectName}`).then((project) => {
284 if (targetUser && targetUser !== owningUser) {
285 cy.shareWith(owningUser.token, targetUser.user.uuid, project.uuid, writePermission);
287 if (addToFavorites) {
288 const user = targetUser ? targetUser : owningUser;
289 cy.addToFavorites(user.token, user.user.uuid, project.uuid);
294 Cypress.Commands.add(
297 prevSubject: 'element',
299 (subject, file, fileName) => {
300 cy.window().then(window => {
301 const blob = b64toBlob(file, '', 512);
302 const testFile = new window.File([blob], fileName);
304 cy.wrap(subject).trigger('drop', {
305 dataTransfer: { files: [testFile] },
311 function b64toBlob(b64Data, contentType = '', sliceSize = 512) {
312 const byteCharacters = atob(b64Data)
313 const byteArrays = []
315 for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
316 const slice = byteCharacters.slice(offset, offset + sliceSize);
318 const byteNumbers = new Array(slice.length);
319 for (let i = 0; i < slice.length; i++) {
320 byteNumbers[i] = slice.charCodeAt(i);
323 const byteArray = new Uint8Array(byteNumbers);
325 byteArrays.push(byteArray);
328 const blob = new Blob(byteArrays, { type: contentType });