Skip to content

Commit

Permalink
Fix cy.requests tests
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Oct 28, 2024
1 parent ab8155c commit c9ad5de
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 17 deletions.
2 changes: 1 addition & 1 deletion frontend/.env.local.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ FRONTEND_SENTRY_TRACING_ORIGINS=
FRONTEND_SENTRY_ENV=

################################################################################
# UseMatomo
# Matomo

FRONTEND_MATOMO_URL=
FRONTEND_MATOMO_ID=
Expand Down
3 changes: 3 additions & 0 deletions frontend/config/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default defineConfig({
"auth_base_url": `http://${IS_CI ? '0.0.0.0:8880' : 'localhost:8085'}`,
"auth_realm": "monitor",
"auth_client_id": "monitorfish",
"FRONTEND_OIDC_AUTHORITY": `http://${IS_CI ? '0.0.0.0:8880' : 'localhost:8880'}/realms/monitor`,
"FRONTEND_OIDC_CLIENT_ID": "monitorfish",
"LOCALSTORAGE_URL": `http://${IS_CI ? '0.0.0.0:8880' : 'localhost:3000'}`,
'cypress-plugin-snapshots': {
imageConfig: {
threshold: 20,
Expand Down
14 changes: 10 additions & 4 deletions frontend/cypress/e2e/main_window/vessel_sidebar/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { faker } from '@faker-js/faker'

import { getAuthorizationHeader } from '../../../support/commands/getAuthorizationHeader'

import type { Reporting } from '@features/Reporting/types'

/**
Expand All @@ -9,7 +11,6 @@ export const createReportingFromVesselSidebar = (vesselName: string) => {
cy.intercept('GET', '/bff/v1/vessels/reportings?*').as('getVesselReportings')
cy.intercept('POST', '/bff/v1/reportings').as('createReporting')

cy.login('superuser')
cy.visit('/#@-824534.42,6082993.21,8.70')
cy.wait(500)

Expand Down Expand Up @@ -42,9 +43,14 @@ export const createReportingFromVesselSidebar = (vesselName: string) => {
}

export const deleteReporting = (reportId: number) => {
cy.request({
method: 'DELETE',
url: `/bff/v1/reportings/${reportId}`
getAuthorizationHeader().then(authorization => {
cy.request({
headers: {
authorization
},
method: 'DELETE',
url: `/bff/v1/reportings/${reportId}`
})
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
editSideWindowPriorNotification,
getPriorNotificationSentMessagesFakeResponse
} from './utils'
import { getAuthorizationHeader } from '../../../support/commands/getAuthorizationHeader'
import { openSideWindowPriorNotificationListAsSuperUser } from '../prior_notification_list/utils'

context('Side Window > Logbook Prior Notification Form > Behavior', () => {
Expand Down Expand Up @@ -36,10 +37,17 @@ context('Side Window > Logbook Prior Notification Form > Behavior', () => {

// Reset
const operationDate = dayjs().subtract(6, 'hours').toISOString()
cy.request('PUT', `/bff/v1/prior_notifications/logbook/FAKE_OPERATION_116?operationDate=${operationDate}`, {
body: {
note: null
}
getAuthorizationHeader().then(authorization => {
cy.request({
body: {
note: null
},
headers: {
authorization
},
method: 'PUT',
url: `/bff/v1/prior_notifications/logbook/FAKE_OPERATION_116?operationDate=${operationDate}`
})
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import { PriorNotification } from '@features/PriorNotification/PriorNotification
import dayjs from 'dayjs'

import { editSideWindowPriorNotification } from './utils'
import { getAuthorizationHeader } from '../../../support/commands/getAuthorizationHeader'
import { openSideWindowPriorNotificationListAsSuperUser } from '../prior_notification_list/utils'

context('Side Window > Logbook Prior Notification Form > Form', () => {
it('Should update a logbook prior notification', () => {
// Reset
const operationDate = dayjs().subtract(6, 'hours').toISOString()
cy.request('PUT', `/bff/v1/prior_notifications/logbook/FAKE_OPERATION_115?operationDate=${operationDate}`, {
body: {
note: null
}
getAuthorizationHeader().then(authorization => {
cy.request({
body: {
note: null
},
headers: {
authorization
},
method: 'PUT',
url: `/bff/v1/prior_notifications/logbook/FAKE_OPERATION_115?operationDate=${operationDate}`
})
})

// Given
Expand All @@ -37,10 +45,17 @@ context('Side Window > Logbook Prior Notification Form > Form', () => {
cy.get('[name="note"]').should('have.value', "Un point d'attention.")

// Reset
cy.request('PUT', `/bff/v1/prior_notifications/logbook/FAKE_OPERATION_115?operationDate=${operationDate}`, {
body: {
note: null
}
getAuthorizationHeader().then(authorization => {
cy.request({
body: {
note: null
},
headers: {
authorization
},
method: 'PUT',
url: `/bff/v1/prior_notifications/logbook/FAKE_OPERATION_115?operationDate=${operationDate}`
})
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createReportingFromVesselSidebar } from '../../main_window/vessel_sidebar/utils'

context('Side Window > Reporting List > Actions', () => {
beforeEach(() => {
cy.login('superuser')
})

it('Reportings Should be archived', () => {
createReportingFromVesselSidebar('COURANT MAIN PROFESSEUR').then(createdReportingId => {
cy.intercept('PUT', '/bff/v1/reportings/archive').as('archiveReportings')
Expand Down
34 changes: 34 additions & 0 deletions frontend/cypress/support/commands/getAuthorizationHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { User } from 'oidc-client-ts'

export function getAuthorizationHeader() {
return getOIDCUser().then(user => {
const token = user?.access_token

// If we have a token set in state, we pass it.
if (token) {
return Promise.resolve(`Bearer ${token}`)
}

return Promise.resolve(undefined)
})
}

function getOIDCUser() {
const OIDC_AUTHORITY = Cypress.env('FRONTEND_OIDC_AUTHORITY')
const OIDC_CLIENT_ID = Cypress.env('FRONTEND_OIDC_CLIENT_ID')
const LOCALSTORAGE_URL = Cypress.env('LOCALSTORAGE_URL')

return cy.getAllLocalStorage().then(localStorages => {
const testLocalStorage = localStorages[LOCALSTORAGE_URL]
if (!testLocalStorage) {
return Promise.resolve(undefined)
}

const oidcStorage = testLocalStorage[`oidc.user:${OIDC_AUTHORITY}:${OIDC_CLIENT_ID}`]
if (!oidcStorage) {
return Promise.resolve(undefined)
}

return Promise.resolve(User.fromStorageString(oidcStorage as string))
})
}

0 comments on commit c9ad5de

Please sign in to comment.