Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
majakomel committed Sep 21, 2023
1 parent 5922994 commit 295db11
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 23 deletions.
94 changes: 94 additions & 0 deletions cypress/e2e/incidents.e2e.cy.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
14 changes: 9 additions & 5 deletions cypress/e2e/measurement.e2e.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]')
cy.findByRole('textbox').click()
cy.findByRole('textbox').type('[email protected]')
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()

Expand Down
18 changes: 14 additions & 4 deletions cypress/mocks/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
)
})
28 changes: 14 additions & 14 deletions cypress/support/msw.js
Original file line number Diff line number Diff line change
@@ -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)
// })

0 comments on commit 295db11

Please sign in to comment.