From 295db115c8abf5699c71e3d911703518070d311e Mon Sep 17 00:00:00 2001 From: majakomel Date: Thu, 21 Sep 2023 11:42:56 +0200 Subject: [PATCH] wip --- cypress/e2e/incidents.e2e.cy.js | 94 +++++++++++++++++++++++++++++++ cypress/e2e/measurement.e2e.cy.js | 14 +++-- cypress/mocks/handlers.js | 18 ++++-- cypress/support/msw.js | 28 ++++----- 4 files changed, 131 insertions(+), 23 deletions(-) create mode 100644 cypress/e2e/incidents.e2e.cy.js diff --git a/cypress/e2e/incidents.e2e.cy.js b/cypress/e2e/incidents.e2e.cy.js new file mode 100644 index 00000000..c1fd086e --- /dev/null +++ b/cypress/e2e/incidents.e2e.cy.js @@ -0,0 +1,94 @@ +const incident = { + incident: { + ASNs: [], + CCs: [], + domains: [], + end_time: null, + event_type: 'incident', + id: 1234, + links: [], + mine: 0, + published: true, + reported_by: 'ooni', + short_description: 'China recently started blocking access to our website (ooni.org) and censorship measurement app (OONI Probe).', + start_time: '2023-07-07T00:00:00Z', + tags: [], + test_names: [], + text: 'China recently started blocking access to our website (ooni.org) and censorship measurement app (OONI Probe).', + title: 'China is blocking OONI', + update_time: '2023-09-14T10:59:31Z' + } +} + +describe('Incidents Dashboard', () => { + it('admin can see the dashboard', () => { + cy.intercept('/api/_/account_metadata', {logged_in: true, role: 'admin'}).as('accountMetadata') + const dashboardUrl = '/incidents/dashboard' + cy.visit(dashboardUrl) + cy.wait('@accountMetadata') + cy.findByText('Incidents Dashboard').should('exist') + }) + + it('redirects user if not logged in', () => { + cy.intercept('/api/_/account_metadata', {statusCode: 401}).as('accountMetadata') + const dashboardUrl = '/incidents/dashboard' + cy.visit(dashboardUrl) + cy.wait('@accountMetadata') + cy.findByText('Incidents Dashboard').should('not.exist') + cy.url({timeout: 6000}).should('eq', 'http://localhost:3100/incidents') + }) + + it('redirects user if not admin', () => { + cy.intercept('/api/_/account_metadata', {logged_in: true, role: 'user'}).as('accountMetadata') + const dashboardUrl = '/incidents/dashboard' + cy.visit(dashboardUrl) + cy.wait('@accountMetadata') + cy.findByText('Incidents Dashboard').should('not.exist') + cy.url({timeout: 6000}).should('eq', 'http://localhost:3100/incidents') + }) +}) + +describe('Incidents Edit', () => { + it('admin can see edit incident page', () => { + cy.intercept('/api/_/account_metadata', {logged_in: true, role: 'admin'}).as('accountMetadata') + cy.intercept('/api/v1/incidents/show/1234', incident).as('showIncident') + + const dashboardUrl = '/incidents/edit/1234' + cy.visit(dashboardUrl) + cy.wait('@accountMetadata') + cy.wait('@showIncident') + cy.findByText('Edit Incident Report').should('exist') + }) + + it('report creator can see edit incident page', () => { + cy.intercept('/api/_/account_metadata', {logged_in: true, role: 'user'}).as('accountMetadata') + cy.intercept('/api/v1/incidents/show/1234', incident).as('showIncident') + + const dashboardUrl = '/incidents/edit/1234' + cy.visit(dashboardUrl) + cy.wait('@accountMetadata') + cy.wait('@showIncident') + cy.findByText('Edit Incident Report').should('exist') + }) + + it('redirects user if not logged in', () => { + cy.intercept('/api/_/account_metadata', {statusCode: 401}).as('accountMetadata') + const dashboardUrl = '/incidents/edit/1234' + cy.visit(dashboardUrl) + cy.wait('@accountMetadata') + cy.findByText('Edit Incident Report').should('not.exist') + cy.url({timeout: 6000}).should('eq', 'http://localhost:3100/incidents') + }) + + it('redirects user if not admin', () => { + cy.intercept('/api/_/account_metadata', {logged_in: true, role: 'user'}).as('accountMetadata') + cy.intercept('/api/v1/incidents/show/1234', incident).as('showIncident') + + const dashboardUrl = '/incidents/edit/1234' + cy.visit(dashboardUrl) + cy.wait('@accountMetadata') + cy.wait('@showIncident') + cy.findByText('Edit Incident Report').should('not.exist') + cy.url({timeout: 6000}).should('eq', 'http://localhost:3100/incidents') + }) +}) \ No newline at end of file diff --git a/cypress/e2e/measurement.e2e.cy.js b/cypress/e2e/measurement.e2e.cy.js index bff026df..5b49bc68 100644 --- a/cypress/e2e/measurement.e2e.cy.js +++ b/cypress/e2e/measurement.e2e.cy.js @@ -320,21 +320,25 @@ describe('Measurement Page Tests', () => { }) it('can login', () => { - cy.interceptRequest(failedAccountMetadata) - + cy.intercept('/api/_/account_metadata', {statusCode: 401}) + cy.intercept('/api/v1/user_register', {statusCode: 200}) + const measurementUrl = '/m/20230307142542.625294_US_webconnectivity_9215f30cf2412f49' cy.visit(measurementUrl) cy.findByText('VERIFY').click() - cy.findByRole('textbox').click().type('randomEmail@randomEmail.com') + cy.findByRole('textbox').click() + cy.findByRole('textbox').type('randomEmail@randomEmail.com') cy.findByText('Login').click() cy.findByText('Login link sent') }) - it('can submit feedback', () => { - const measurementUrl = '/m/20230307142542.625294_US_webconnectivity_9215f30cf2412f49' + cy.intercept('/api/_/account_metadata', {logged_in: true, role: 'user'}) + cy.intercept('/api/_/measurement_feedback', {statusCode: 200}) + const measurementUrl = '/m/20230307142542.625294_US_webconnectivity_9215f30cf2412f49' + cy.visit(measurementUrl) cy.findByText('VERIFY').click() diff --git a/cypress/mocks/handlers.js b/cypress/mocks/handlers.js index a0d0a992..56b399e1 100644 --- a/cypress/mocks/handlers.js +++ b/cypress/mocks/handlers.js @@ -42,7 +42,17 @@ export const handlers = [ ] export const failedAccountMetadata = rest.get('https://ams-pg-test.ooni.org/api/_/account_metadata', (req, res, ctx) => { - return res( - ctx.status(401), - ) - }) + return res( + ctx.status(401), + ) +}) + +export const userAccountMetadata = rest.get('https://ams-pg-test.ooni.org/api/_/account_metadata', (req, res, ctx) => { + return res( + ctx.status(200), + ctx.json({ + logged_in: true, + role: 'user', + }), + ) +}) diff --git a/cypress/support/msw.js b/cypress/support/msw.js index 1df4bc1e..ea66485d 100644 --- a/cypress/support/msw.js +++ b/cypress/support/msw.js @@ -1,18 +1,18 @@ -import { setupWorker, rest} from 'msw' -import { handlers } from '/cypress/mocks/handlers' +// import { setupWorker} from 'msw' +// import { handlers } from '/cypress/mocks/handlers' -let worker +// let worker -before(() => { - worker = setupWorker(...handlers) - cy.wrap(worker.start({ onUnhandledRequest: 'bypass' }), { log: false }) -}) -Cypress.on('test:before:run', () => { - if (!worker) return - worker.resetHandlers() -}) +// before(() => { +// worker = setupWorker(...handlers) +// cy.wrap(worker.start({ onUnhandledRequest: 'bypass' })) +// }) +// Cypress.on('test:before:run', () => { +// if (!worker) return +// worker.resetHandlers() +// }) -Cypress.Commands.add('interceptRequest', (...params) => { - worker.use(...params) -}) +// Cypress.Commands.add('interceptRequest', (...params) => { +// worker.use(...params) +// })