diff --git a/cypress/integration/admin.spec.js b/cypress/integration/admin.spec.js new file mode 100644 --- /dev/null +++ b/cypress/integration/admin.spec.js @@ -0,0 +1,71 @@ +/** + * Copyright (C) 2019 The Software Heritage developers + * See the AUTHORS file at the top-level directory of this distribution + * License: GNU Affero General Public License version 3, or any later version + * See top-level LICENSE file for more information + */ + +const username = 'admin'; +const password = 'admin'; +const defaultRedirect = '/admin/origin/save/'; + +let url; + +function login(username, password) { + cy.visit(url); + cy.get('input[name="username"]') + .type(username) + .get('input[name="password"]') + .type(password) + .get('form') + .submit(); +} + +function logout() { + cy.contains('a', 'logout') + .click(); +} + +describe('Test Admin Features', function() { + before(function() { + url = this.Urls.admin(); + }); + + beforeEach(function() { + login(username, password); + }); + + it('should redirect to default page', function() { + cy.location('pathname') + .should('be.equal', defaultRedirect); + + logout(); + }); + + it('should display admin-origin-save and deposit in sidebar', function() { + cy.get(`.sidebar a[href="${this.Urls.admin_origin_save()}"]`) + .should('be.visible'); + + cy.get(`.sidebar a[href="${this.Urls.admin_deposit()}"]`) + .should('be.visible'); + + logout(); + }); + + it('should display username on top-right', function() { + cy.get('.swh-position-right') + .should('contain', username); + + logout(); + }); + + it('should prevent unauthorized access after logout', function() { + logout(); + cy.visit(this.Urls.admin_origin_save()) + .location('pathname') + .should('be.equal', '/admin/login/'); + cy.visit(this.Urls.admin_deposit()) + .location('pathname') + .should('be.equal', '/admin/login/'); + }); +}); diff --git a/cypress/support/index.js b/cypress/support/index.js --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -66,25 +66,28 @@ for (let content of origin.content) { - const contentPathApiUrl = this.Urls.api_1_directory(origin.rootDirectory, content.path); - const contentMetaData = await httpGetJson(contentPathApiUrl); - - content.name = contentMetaData.name; - content.sha1git = contentMetaData.target; - content.directory = contentMetaData.dir_id; - - content.rawFilePath = this.Urls.browse_content_raw(`sha1_git:${content.sha1git}`) + - `?filename=${encodeURIComponent(content.name)}`; - - cy.request(content.rawFilePath) - .then((response) => { - const fileText = response.body; - const fileLines = fileText.split('\n'); - content.numberLines = fileLines.length; - - // If last line is empty its not shown - if (!fileLines[content.numberLines - 1]) content.numberLines -= 1; + cy.visit(this.Urls.browse_origin_content(origin.url, content.path)) + .window().then(win => { + const contentMetaData = win.swh.webapp.getBrowsedSwhObjectMetadata(); + + content.name = contentMetaData.filename; + content.sha1git = contentMetaData.sha1_git; + content.directory = contentMetaData.directory; + + content.rawFilePath = this.Urls.browse_content_raw(`sha1_git:${content.sha1git}`) + + `?filename=${encodeURIComponent(content.name)}`; + + cy.request(content.rawFilePath) + .then((response) => { + const fileText = response.body; + const fileLines = fileText.split('\n'); + content.numberLines = fileLines.length; + + // If last line is empty its not shown + if (!fileLines[content.numberLines - 1]) content.numberLines -= 1; + }); }); + } }