diff --git a/assets/src/bundles/add_forge/create-request.js b/assets/src/bundles/add_forge/create-request.js --- a/assets/src/bundles/add_forge/create-request.js +++ b/assets/src/bundles/add_forge/create-request.js @@ -7,9 +7,17 @@ import {handleFetchError, removeUrlFragment, csrfPost, getHumanReadableDate} from 'utils/functions'; +import userRequestsFilterCheckboxFn from 'utils/requests-filter-checkbox.ejs'; +import {swhSpinnerSrc} from 'utils/constants'; let requestBrowseTable; +const addForgeCheckboxId = 'swh-add-forge-user-filter'; +const userRequestsFilterCheckbox = userRequestsFilterCheckboxFn({ + 'inputId': addForgeCheckboxId, + 'checked': true // by default, display only user requests +}); + export function onCreateRequestPageLoad() { $(document).ready(() => { $('#requestCreateForm').submit(async function(event) { @@ -56,6 +64,7 @@ }); $('#swh-add-forge-requests-list-tab').on('shown.bs.tab', () => { + requestBrowseTable.draw(); window.location.hash = '#browse-requests'; }); @@ -93,13 +102,32 @@ .DataTable({ serverSide: true, processing: true, + language: { + processing: `` + }, retrieve: true, searching: true, info: false, - dom: '<<"d-flex justify-content-between align-items-center"f' + - '<"#list-exclude">l>rt<"bottom"ip>>', + // Layout configuration, see [1] for more details + // [1] https://datatables.net/reference/option/dom + dom: '<"row"<"col-sm-3"l><"col-sm-6 text-left user-requests-filter"><"col-sm-3"f>>' + + '<"row"<"col-sm-12"tr>>' + + '<"row"<"col-sm-5"i><"col-sm-7"p>>', ajax: { - 'url': Urls.add_forge_request_list_datatables() + 'url': Urls.add_forge_request_list_datatables(), + data: (d) => { + if (swh.webapp.isUserLoggedIn() && $(`#${addForgeCheckboxId}`).prop('checked')) { + d.user_requests_only = '1'; + } + } + }, + fnInitComplete: function() { + if (swh.webapp.isUserLoggedIn()) { + $('div.user-requests-filter').html(userRequestsFilterCheckbox); + $(`#${addForgeCheckboxId}`).on('change', () => { + requestBrowseTable.draw(); + }); + } }, columns: [ { @@ -124,5 +152,4 @@ } ] }); - requestBrowseTable.draw(); } diff --git a/cypress/integration/add-forge-now-request-create.spec.js b/cypress/integration/add-forge-now-request-create.spec.js --- a/cypress/integration/add-forge-now-request-create.spec.js +++ b/cypress/integration/add-forge-now-request-create.spec.js @@ -7,13 +7,85 @@ function populateForm(type, url, contact, email, consent, comment) { cy.get('#swh-input-forge-type').select(type); - cy.get('#swh-input-forge-url').type(url); - cy.get('#swh-input-forge-contact-name').type(contact); - cy.get('#swh-input-forge-contact-email').type(email); - cy.get('#swh-input-forge-comment').type(comment); + 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-email').clear().type(email); + if (comment) { + cy.get('#swh-input-forge-comment').clear().type(comment); + } cy.get('#swh-input-consent-check').click({force: consent === 'on'}); } +describe('Browse requests list tests', function() { + beforeEach(function() { + this.addForgeNowUrl = this.Urls.forge_add(); + this.listAddForgeRequestsUrl = this.Urls.add_forge_request_list_datatables(); + }); + + it('should not show user requests filter checkbox for anonymous users', function() { + cy.visit(this.addForgeNowUrl); + cy.get('#swh-add-forge-requests-list-tab').click(); + cy.get('#swh-add-forge-user-filter').should('not.exist'); + }); + + it('should show user requests filter checkbox for authenticated users', function() { + cy.userLogin(); + cy.visit(this.addForgeNowUrl); + cy.get('#swh-add-forge-requests-list-tab').click(); + cy.get('#swh-add-forge-user-filter').should('exist').should('be.checked'); + }); + + it('should only display user requests when filter is activated', function() { + // Clean up previous state + cy.task('db:add_forge_now:delete'); + // 'user2' logs in and create requests + cy.user2Login(); + cy.visit(this.addForgeNowUrl); + + // create requests for the user 'user' + populateForm('gitlab', 'gitlab.org', 'admin', 'admin@example.org', 'on', ''); + cy.get('#requestCreateForm').submit(); + + // user requests filter checkbox should be in the DOM + cy.get('#swh-add-forge-requests-list-tab').click(); + cy.get('#swh-add-forge-user-filter').should('exist').should('be.checked'); + + // check unfiltered user requests + cy.get('tbody tr').then(rows => { + expect(rows.length).to.eq(1); + }); + + // user1 logout + cy.contains('a', 'logout').click(); + + // user logs in + cy.userLogin(); + cy.visit(this.addForgeNowUrl); + + populateForm('gitea', 'gitea.org', 'admin', 'admin@example.org', 'on', ''); + cy.get('#requestCreateForm').submit(); + populateForm('cgit', 'cgit.org', 'admin', 'admin@example.org', 'on', ''); + cy.get('#requestCreateForm').submit(); + + // user requests filter checkbox should be in the DOM + cy.get('#swh-add-forge-requests-list-tab').click(); + cy.get('#swh-add-forge-user-filter').should('exist').should('be.checked'); + + // check unfiltered user requests + cy.get('tbody tr').then(rows => { + expect(rows.length).to.eq(2); + }); + + cy.get('#swh-add-forge-user-filter') + .uncheck({force: true}); + + // Users now sees everything + cy.get('tbody tr').then(rows => { + expect(rows.length).to.eq(2 + 1); + }); + }); +}); + describe('Test add-forge-request creation', function() { beforeEach(function() { this.addForgeNowUrl = this.Urls.forge_add(); diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js --- a/cypress/plugins/index.js +++ b/cypress/plugins/index.js @@ -146,6 +146,14 @@ }); db.close(); return true; + }, + 'db:add_forge_now:delete': () => { + const db = getDatabase(); + db.serialize(function() { + db.run('DELETE FROM add_forge_now_request'); + }); + db.close(); + return true; } }); return config; diff --git a/cypress/support/index.js b/cypress/support/index.js --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -52,6 +52,10 @@ return loginUser('user', 'user'); }); +Cypress.Commands.add('user2Login', () => { + return loginUser('user2', 'user2'); +}); + Cypress.Commands.add('ambassadorLogin', () => { return loginUser('ambassador', 'ambassador'); }); diff --git a/swh/web/tests/create_test_users.py b/swh/web/tests/create_test_users.py --- a/swh/web/tests/create_test_users.py +++ b/swh/web/tests/create_test_users.py @@ -10,8 +10,8 @@ from swh.web.auth.utils import ( ADD_FORGE_MODERATOR_PERMISSION, ADMIN_LIST_DEPOSIT_PERMISSION, - SWH_AMBASSADOR_PERMISSION, MAILMAP_ADMIN_PERMISSION, + SWH_AMBASSADOR_PERMISSION, ) from swh.web.tests.utils import create_django_permission @@ -20,6 +20,7 @@ users: Dict[str, Tuple[str, str, List[str]]] = { "user": ("user", "user@example.org", []), + "user2": ("user2", "user2@example.org", []), "ambassador": ( "ambassador", "ambassador@example.org",