17500: Cleans up previously created objects before running each test.
[arvados-workbench2.git] / cypress / support / commands.js
index 929ffb83d849a357c18d74680d9e762f0b833bdc..36358991f0d1241d8acf211a44ee6d8c95ec626a 100644 (file)
 
 const controllerURL = Cypress.env('controller_url');
 const systemToken = Cypress.env('system_token');
+let createdResources = [];
+
+// Clean up on a 'before' hook to allow post-mortem analysis on individual tests.
+beforeEach(function () {
+    if (createdResources.length === 0) {
+        return;
+    }
+    cy.log(`Cleaning ${createdResources.length} previously created resource(s)`);
+    createdResources.forEach(function({suffix, uuid}) {
+        // Don't fail when a resource isn't already there, some objects may have
+        // been removed, directly or indirectly, from the test that created them.
+        cy.deleteResource(systemToken, suffix, uuid, false);
+    });
+    createdResources = [];
+});
 
 Cypress.Commands.add(
     "doRequest", (method = 'GET', path = '', data = null, qs = null,
-        token = systemToken, auth = false, followRedirect = true) => {
+        token = systemToken, auth = false, followRedirect = true, failOnStatusCode = true) => {
     return cy.request({
         method: method,
         url: `${controllerURL.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`,
         body: data,
         qs: auth ? qs : Object.assign({ api_token: token }, qs),
         auth: auth ? { bearer: `${token}` } : undefined,
-        followRedirect: followRedirect
-    })
-}
-)
-
-// This resets the DB removing all content and seeding it with the fixtures.
-// TODO: Maybe we can add an optional param to avoid the loading part?
-Cypress.Commands.add(
-    "resetDB", () => {
-        cy.request('POST', `${controllerURL}/database/reset?api_token=${systemToken}`);
-    }
-)
+        followRedirect: followRedirect,
+        failOnStatusCode: failOnStatusCode
+    });
+});
 
 Cypress.Commands.add(
     "getUser", (username, first_name = '', last_name = '', is_admin = false, is_active = true) => {
@@ -148,14 +155,15 @@ Cypress.Commands.add(
         return cy.doRequest('POST', '/arvados/v1/' + suffix, data, null, token, true)
             .its('body').as('resource')
             .then(function () {
+                createdResources.push({suffix, uuid: this.resource.uuid});
                 return this.resource;
             })
     }
 )
 
 Cypress.Commands.add(
-    "deleteResource", (token, suffix, uuid) => {
-        return cy.doRequest('DELETE', '/arvados/v1/' + suffix + '/' + uuid)
+    "deleteResource", (token, suffix, uuid, failOnStatusCode = true) => {
+        return cy.doRequest('DELETE', '/arvados/v1/' + suffix + '/' + uuid, null, null, token, false, true, failOnStatusCode)
             .its('body').as('resource')
             .then(function () {
                 return this.resource;