diff --git a/cypress/integration/admin.spec.js b/cypress/integration/admin.spec.js index f9ccd5b4..54c40b6d 100644 --- a/cypress/integration/admin.spec.js +++ b/cypress/integration/admin.spec.js @@ -1,101 +1,213 @@ /** * 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 $ = Cypress.$; + const username = 'admin'; const password = 'admin'; const defaultRedirect = '/admin/origin/save/'; let url; function login(username, password) { 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() { +describe('Test Admin Login/logout', function() { before(function() { url = this.Urls.admin(); }); it('should redirect to default page', function() { cy.visit(url); login(username, password); cy.location('pathname') .should('be.equal', defaultRedirect); logout(); }); it('should display admin-origin-save and deposit in sidebar', function() { cy.visit(url); login(username, password); 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.visit(url); login(username, password); cy.get('.swh-position-right') .should('contain', username); logout(); }); it('should prevent unauthorized access after logout', function() { 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/'); }); it('should redirect to correct page after login', function() { // mock calls to deposit list api to avoid possible errors // while running the test cy.server(); cy.route({ method: 'GET', url: `${this.Urls.admin_deposit_list()}**`, response: { data: [], recordsTotal: 0, recordsFiltered: 0, draw: 1 } }); cy.visit(this.Urls.admin_deposit()) .location('search') .should('contain', `next=${this.Urls.admin_deposit()}`); login(username, password); cy.location('pathname') .should('be.equal', this.Urls.admin_deposit()); logout(); }); }); + +const existingRowToSelect = 'https://bitbucket.org/'; + +const originUrlListTestData = [ + { + listType: 'authorized', + originToAdd: 'git://git.archlinux.org/', + originToRemove: 'https://github.com/' + }, + { + listType: 'unauthorized', + originToAdd: 'https://random.org', + originToRemove: 'https://gitlab.com' + } +]; + +const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1); + +describe('Test Admin Origin Save Urls Filtering', function() { + + beforeEach(function() { + cy.visit(this.Urls.admin_origin_save()); + + login(username, password); + + cy.contains('a', 'Origin urls filtering') + .click(); + }); + + it(`should select or unselect a table row by clicking on it`, function() { + cy.contains(`#swh-authorized-origin-urls tr`, existingRowToSelect) + .click() + .should('have.class', 'selected') + .click() + .should('not.have.class', 'selected'); + }); + + originUrlListTestData.forEach(testData => { + + it(`should add a new origin url prefix in the ${testData.listType} list`, function() { + + const tabName = capitalize(testData.listType) + ' urls'; + + cy.contains('a', tabName) + .click() + .wait(500); + + cy.get(`#swh-${testData.listType}-origin-urls tr`).each(elt => { + if ($(elt).text() === testData.originToAdd) { + cy.get(elt).click(); + cy.get(`#swh-remove-${testData.listType}-origin-url`).click(); + } + }); + + cy.get(`#swh-${testData.listType}-url-prefix`) + .type(testData.originToAdd); + + cy.get(`#swh-add-${testData.listType}-origin-url`) + .click(); + + cy.contains(`#swh-${testData.listType}-origin-urls tr`, testData.originToAdd) + .should('be.visible'); + + cy.contains('.alert-success', `The origin url prefix has been successfully added in the ${testData.listType} list.`) + .should('be.visible'); + + cy.get(`#swh-add-${testData.listType}-origin-url`) + .click(); + + cy.contains('.alert-warning', `The provided origin url prefix is already registered in the ${testData.listType} list.`) + .should('be.visible'); + + }); + + it(`should remove an origin url prefix from the ${testData.listType} list`, function() { + + const tabName = capitalize(testData.listType) + ' urls'; + + cy.contains('a', tabName) + .click(); + + let originUrlMissing = true; + cy.get(`#swh-${testData.listType}-origin-urls tr`).each(elt => { + if ($(elt).text() === testData.originToRemove) { + originUrlMissing = false; + } + }); + + if (originUrlMissing) { + cy.get(`#swh-${testData.listType}-url-prefix`) + .type(testData.originToRemove); + + cy.get(`#swh-add-${testData.listType}-origin-url`) + .click(); + + cy.get('.alert-dismissible button').click(); + } + + cy.contains(`#swh-${testData.listType}-origin-urls tr`, testData.originToRemove) + .click(); + + cy.get(`#swh-remove-${testData.listType}-origin-url`).click(); + + cy.contains(`#swh-${testData.listType}-origin-urls tr`, testData.originToRemove) + .should('not.exist'); + + }); + }); + +}); diff --git a/swh/web/templates/admin/origin-save.html b/swh/web/templates/admin/origin-save.html index 3bcbfb78..0f615cdf 100644 --- a/swh/web/templates/admin/origin-save.html +++ b/swh/web/templates/admin/origin-save.html @@ -1,183 +1,183 @@ {% extends "layout.html" %} {% comment %} Copyright (C) 2018-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 {% endcomment %} {% load swh_templatetags %} {% load render_bundle from webpack_loader %} {% block header %} {{ block.super }} {% render_bundle 'admin' %} {% endblock %} {% block title %} Save origin administration {% endblock %} {% block navbar-content %}

Save origin administration

{% endblock %} {% block content %}
Date Type Url
- +
- +
- +
Date Type Url Status
- +
Date Type Url
Url
- +
- +
Url
- +
- +
{% endblock %}