Changeset View
Changeset View
Standalone View
Standalone View
cypress/e2e/add-forge-now-request-create.cy.js
/** | /** | ||||
* Copyright (C) 2022 The Software Heritage developers | * Copyright (C) 2022 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 | ||||
*/ | */ | ||||
function populateForm(type, url, contact, email, consent, comment) { | function populateForm(type, url, contact, email, consent, comment) { | ||||
cy.get('#swh-input-forge-type').select(type); | cy.get('#swh-input-forge-type').select(type); | ||||
if (url) { | |||||
cy.get('#swh-input-forge-url').clear().type(url); | cy.get('#swh-input-forge-url').clear().type(url); | ||||
} | |||||
cy.get('#swh-input-forge-contact-name').clear().type(contact); | cy.get('#swh-input-forge-contact-name').clear().type(contact); | ||||
cy.get('#swh-input-forge-contact-email').clear().type(email); | cy.get('#swh-input-forge-contact-email').clear().type(email); | ||||
if (comment) { | if (comment) { | ||||
cy.get('#swh-input-forge-comment').clear().type(comment); | cy.get('#swh-input-forge-comment').clear().type(comment); | ||||
} | } | ||||
cy.get('#swh-input-consent-check').click({force: consent === 'on'}); | cy.get('#swh-input-consent-check').click({force: consent === 'on'}); | ||||
} | } | ||||
function submitForm() { | |||||
cy.get('#requestCreateForm input[type=submit]').click(); | |||||
cy.get('#requestCreateForm').then($form => { | |||||
if ($form[0].checkValidity()) { | |||||
cy.wait('@addForgeRequestCreate'); | |||||
} | |||||
}); | |||||
} | |||||
function initTest(testEnv) { | |||||
testEnv.addForgeNowUrl = testEnv.Urls.forge_add_create(); | |||||
testEnv.addForgeNowRequestCreateUrl = testEnv.Urls.api_1_add_forge_request_create(); | |||||
testEnv.listAddForgeRequestsUrl = testEnv.Urls.add_forge_request_list_datatables(); | |||||
cy.intercept('POST', testEnv.addForgeNowRequestCreateUrl + '**') | |||||
.as('addForgeRequestCreate'); | |||||
cy.intercept(testEnv.listAddForgeRequestsUrl + '**') | |||||
.as('addForgeRequestsList'); | |||||
} | |||||
describe('Browse requests list tests', function() { | describe('Browse requests list tests', function() { | ||||
beforeEach(function() { | beforeEach(function() { | ||||
this.addForgeNowUrl = this.Urls.forge_add_create(); | initTest(this); | ||||
this.listAddForgeRequestsUrl = this.Urls.add_forge_request_list_datatables(); | |||||
}); | }); | ||||
it('should not show user requests filter checkbox for anonymous users', function() { | it('should not show user requests filter checkbox for anonymous users', function() { | ||||
cy.visit(this.addForgeNowUrl); | cy.visit(this.addForgeNowUrl); | ||||
cy.get('#swh-add-forge-requests-list-tab').click(); | cy.get('#swh-add-forge-requests-list-tab').click(); | ||||
cy.get('#swh-add-forge-user-filter').should('not.exist'); | cy.get('#swh-add-forge-user-filter').should('not.exist'); | ||||
}); | }); | ||||
it('should show user requests filter checkbox for authenticated users', function() { | it('should show user requests filter checkbox for authenticated users', function() { | ||||
cy.userLogin(); | cy.userLogin(); | ||||
cy.visit(this.addForgeNowUrl); | cy.visit(this.addForgeNowUrl); | ||||
cy.get('#swh-add-forge-requests-list-tab').click(); | cy.get('#swh-add-forge-requests-list-tab').click(); | ||||
cy.get('#swh-add-forge-user-filter').should('exist').should('be.checked'); | cy.get('#swh-add-forge-user-filter').should('exist').should('be.checked'); | ||||
}); | }); | ||||
it('should only display user requests when filter is activated', function() { | it('should only display user requests when filter is activated', function() { | ||||
// Clean up previous state | // Clean up previous state | ||||
cy.task('db:add_forge_now:delete'); | cy.task('db:add_forge_now:delete'); | ||||
// 'user2' logs in and create requests | // 'user2' logs in and create requests | ||||
cy.user2Login(); | cy.user2Login(); | ||||
cy.visit(this.addForgeNowUrl); | cy.visit(this.addForgeNowUrl); | ||||
// create requests for the user 'user' | // create requests for the user 'user' | ||||
populateForm('gitlab', 'https://gitlab.org', 'admin', 'admin@example.org', 'on', ''); | populateForm('gitlab', 'https://gitlab.org', 'admin', 'admin@example.org', 'on', ''); | ||||
cy.get('#requestCreateForm').submit(); | submitForm(); | ||||
// user requests filter checkbox should be in the DOM | // user requests filter checkbox should be in the DOM | ||||
cy.get('#swh-add-forge-requests-list-tab').click(); | cy.get('#swh-add-forge-requests-list-tab').click(); | ||||
cy.get('#swh-add-forge-user-filter').should('exist').should('be.checked'); | cy.get('#swh-add-forge-user-filter').should('exist').should('be.checked'); | ||||
// check unfiltered user requests | // check unfiltered user requests | ||||
cy.get('tbody tr').then(rows => { | cy.get('tbody tr').then(rows => { | ||||
expect(rows.length).to.eq(1); | expect(rows.length).to.eq(1); | ||||
}); | }); | ||||
// user1 logout | // user1 logout | ||||
cy.contains('a', 'logout').click(); | cy.contains('a', 'logout').click(); | ||||
// user logs in | // user logs in | ||||
cy.userLogin(); | cy.userLogin(); | ||||
cy.visit(this.addForgeNowUrl); | cy.visit(this.addForgeNowUrl); | ||||
populateForm('gitea', 'https://gitea.org', 'admin', 'admin@example.org', 'on', ''); | populateForm('gitea', 'https://gitea.org', 'admin', 'admin@example.org', 'on', ''); | ||||
cy.get('#requestCreateForm').submit(); | submitForm(); | ||||
populateForm('cgit', 'https://cgit.org', 'admin', 'admin@example.org', 'on', ''); | populateForm('cgit', 'https://cgit.org', 'admin', 'admin@example.org', 'on', ''); | ||||
cy.get('#requestCreateForm').submit(); | submitForm(); | ||||
cy.intercept(this.Urls.add_forge_request_list_datatables() + '**') | |||||
.as('addForgeRequestsList'); | |||||
// user requests filter checkbox should be in the DOM | // user requests filter checkbox should be in the DOM | ||||
cy.get('#swh-add-forge-requests-list-tab').click(); | cy.get('#swh-add-forge-requests-list-tab').click(); | ||||
cy.get('#swh-add-forge-user-filter').should('exist').should('be.checked'); | cy.get('#swh-add-forge-user-filter').should('exist').should('be.checked'); | ||||
cy.wait('@addForgeRequestsList'); | cy.wait('@addForgeRequestsList'); | ||||
// check unfiltered user requests | // check unfiltered user requests | ||||
Show All 10 Lines | it('should only display user requests when filter is activated', function() { | ||||
cy.get('tbody tr').then(rows => { | cy.get('tbody tr').then(rows => { | ||||
expect(rows.length).to.eq(2 + 1); | expect(rows.length).to.eq(2 + 1); | ||||
}); | }); | ||||
}); | }); | ||||
}); | }); | ||||
describe('Test add-forge-request creation', function() { | describe('Test add-forge-request creation', function() { | ||||
beforeEach(function() { | beforeEach(function() { | ||||
this.addForgeNowUrl = this.Urls.forge_add_create(); | initTest(this); | ||||
}); | }); | ||||
it('should show all the tabs for every user', function() { | it('should show all the tabs for every user', function() { | ||||
cy.visit(this.addForgeNowUrl); | cy.visit(this.addForgeNowUrl); | ||||
cy.get('#swh-add-forge-tab') | cy.get('#swh-add-forge-tab') | ||||
.should('have.class', 'nav-link'); | .should('have.class', 'nav-link'); | ||||
▲ Show 20 Lines • Show All 104 Lines • ▼ Show 20 Lines | it('should show browse requests table for every user', function() { | ||||
cy.get('#loginLink') | cy.get('#loginLink') | ||||
.should('not.exist'); | .should('not.exist'); | ||||
}); | }); | ||||
it('should update browse list on successful submission', function() { | it('should update browse list on successful submission', function() { | ||||
cy.userLogin(); | cy.userLogin(); | ||||
cy.visit(this.addForgeNowUrl); | cy.visit(this.addForgeNowUrl); | ||||
populateForm('bitbucket', 'https://gitlab.com', 'test', 'test@example.com', 'on', 'test comment'); | populateForm('bitbucket', 'https://gitlab.com', 'test', 'test@example.com', 'on', 'test comment'); | ||||
cy.get('#requestCreateForm').submit(); | submitForm(); | ||||
cy.visit(this.addForgeNowUrl); | cy.visit(this.addForgeNowUrl); | ||||
cy.get('#swh-add-forge-requests-list-tab').click(); | cy.get('#swh-add-forge-requests-list-tab').click(); | ||||
cy.wait('@addForgeRequestsList'); | |||||
cy.get('#add-forge-request-browse') | cy.get('#add-forge-request-browse') | ||||
.should('be.visible') | .should('be.visible') | ||||
.should('contain', 'gitlab.com'); | .should('contain', 'gitlab.com'); | ||||
cy.get('#add-forge-request-browse') | cy.get('#add-forge-request-browse') | ||||
.should('be.visible') | .should('be.visible') | ||||
.should('contain', 'Pending'); | .should('contain', 'Pending'); | ||||
}); | }); | ||||
it('should show error message on conflict', function() { | it('should show error message on conflict', function() { | ||||
cy.userLogin(); | cy.userLogin(); | ||||
cy.visit(this.addForgeNowUrl); | cy.visit(this.addForgeNowUrl); | ||||
populateForm('bitbucket', 'https://gitlab.com', 'test', 'test@example.com', 'on', 'test comment'); | populateForm('bitbucket', 'https://gitlab.com', 'test', 'test@example.com', 'on', 'test comment'); | ||||
cy.get('#requestCreateForm').submit(); | submitForm(); | ||||
cy.get('#requestCreateForm').submit(); // Submitting the same data again | submitForm(); // Submitting the same data again | ||||
cy.get('#userMessage') | cy.get('#userMessage') | ||||
.should('have.class', 'badge-danger') | .should('have.class', 'badge-danger') | ||||
.should('contain', 'already exists'); | .should('contain', 'already exists'); | ||||
}); | }); | ||||
it('should show error message', function() { | it('should show error message', function() { | ||||
cy.userLogin(); | cy.userLogin(); | ||||
cy.intercept('POST', `${this.Urls.api_1_add_forge_request_create()}**`, | |||||
{ | |||||
body: { | |||||
'exception': 'BadInputExc', | |||||
'reason': '{"add-forge-comment": ["This field is required"]}' | |||||
}, | |||||
statusCode: 400 | |||||
}).as('errorRequest'); | |||||
cy.visit(this.addForgeNowUrl); | cy.visit(this.addForgeNowUrl); | ||||
populateForm( | populateForm( | ||||
'bitbucket', 'https://gitlab.com', 'test', 'test@example.com', 'off', 'comment' | 'bitbucket', '', 'test', 'test@example.com', 'off', 'comment' | ||||
); | |||||
submitForm(); | |||||
cy.get('#requestCreateForm').then( | |||||
$form => expect($form[0].checkValidity()).to.be.false | |||||
); | ); | ||||
cy.get('#requestCreateForm').submit(); | |||||
cy.wait('@errorRequest').then((xhr) => { | |||||
cy.get('#userMessage') | |||||
.should('have.class', 'badge-danger') | |||||
.should('contain', 'field is required'); | |||||
}); | |||||
}); | }); | ||||
it('should bot validate form when forge URL is invalid', function() { | it('should not validate form when forge URL is invalid', function() { | ||||
cy.userLogin(); | cy.userLogin(); | ||||
cy.visit(this.addForgeNowUrl); | cy.visit(this.addForgeNowUrl); | ||||
populateForm('bitbucket', 'bitbucket.org', 'test', 'test@example.com', 'on', 'test comment'); | populateForm('bitbucket', 'bitbucket.org', 'test', 'test@example.com', 'on', 'test comment'); | ||||
cy.get('#requestCreateForm').submit(); | submitForm(); | ||||
cy.get('#swh-input-forge-url') | cy.get('#swh-input-forge-url') | ||||
.then(input => { | .then(input => { | ||||
assert.isFalse(input[0].checkValidity()); | assert.isFalse(input[0].checkValidity()); | ||||
}); | }); | ||||
}); | }); | ||||
}); | }); |