Changeset View
Changeset View
Standalone View
Standalone View
cypress/integration/admin.spec.js
/** | /** | ||||
* Copyright (C) 2019 The Software Heritage developers | * Copyright (C) 2019 The Software Heritage developers | ||||
* See the AUTHORS file at the top-level directory of this distribution | * See the AUTHORS file at the top-level directory of this distribution | ||||
* License: GNU Affero General Public License version 3, or any later version | * License: GNU Affero General Public License version 3, or any later version | ||||
* See top-level LICENSE file for more information | * See top-level LICENSE file for more information | ||||
*/ | */ | ||||
const username = 'admin'; | const username = 'admin'; | ||||
const password = 'admin'; | const password = 'admin'; | ||||
const defaultRedirect = '/admin/origin/save/'; | const defaultRedirect = '/admin/origin/save/'; | ||||
let url; | let url; | ||||
function login(username, password) { | function login(username, password) { | ||||
cy.visit(url); | |||||
cy.get('input[name="username"]') | cy.get('input[name="username"]') | ||||
.type(username) | .type(username) | ||||
.get('input[name="password"]') | .get('input[name="password"]') | ||||
.type(password) | .type(password) | ||||
.get('form') | .get('form') | ||||
.submit(); | .submit(); | ||||
} | } | ||||
function logout() { | function logout() { | ||||
cy.contains('a', 'logout') | cy.contains('a', 'logout') | ||||
.click(); | .click(); | ||||
} | } | ||||
describe('Test Admin Features', function() { | describe('Test Admin Features', function() { | ||||
before(function() { | before(function() { | ||||
url = this.Urls.admin(); | url = this.Urls.admin(); | ||||
}); | }); | ||||
beforeEach(function() { | it('should redirect to default page', function() { | ||||
cy.visit(url); | |||||
login(username, password); | login(username, password); | ||||
}); | |||||
it('should redirect to default page', function() { | |||||
cy.location('pathname') | cy.location('pathname') | ||||
.should('be.equal', defaultRedirect); | .should('be.equal', defaultRedirect); | ||||
logout(); | logout(); | ||||
}); | }); | ||||
it('should display admin-origin-save and deposit in sidebar', function() { | 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()}"]`) | cy.get(`.sidebar a[href="${this.Urls.admin_origin_save()}"]`) | ||||
.should('be.visible'); | .should('be.visible'); | ||||
cy.get(`.sidebar a[href="${this.Urls.admin_deposit()}"]`) | cy.get(`.sidebar a[href="${this.Urls.admin_deposit()}"]`) | ||||
.should('be.visible'); | .should('be.visible'); | ||||
logout(); | logout(); | ||||
}); | }); | ||||
it('should display username on top-right', function() { | it('should display username on top-right', function() { | ||||
cy.visit(url); | |||||
login(username, password); | |||||
cy.get('.swh-position-right') | cy.get('.swh-position-right') | ||||
.should('contain', username); | .should('contain', username); | ||||
logout(); | logout(); | ||||
}); | }); | ||||
it('should prevent unauthorized access after logout', function() { | it('should prevent unauthorized access after logout', function() { | ||||
logout(); | |||||
cy.visit(this.Urls.admin_origin_save()) | cy.visit(this.Urls.admin_origin_save()) | ||||
.location('pathname') | .location('pathname') | ||||
.should('be.equal', '/admin/login/'); | .should('be.equal', '/admin/login/'); | ||||
cy.visit(this.Urls.admin_deposit()) | cy.visit(this.Urls.admin_deposit()) | ||||
.location('pathname') | .location('pathname') | ||||
.should('be.equal', '/admin/login/'); | .should('be.equal', '/admin/login/'); | ||||
}); | }); | ||||
it('should redirect to correct page after login', function() { | |||||
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(); | |||||
}); | |||||
}); | }); |