From 6daf683a1f5b5e847664e4ebc454bdd43143fcf2 Mon Sep 17 00:00:00 2001 From: Stephan Kulla Date: Fri, 1 Dec 2023 17:27:14 +0100 Subject: [PATCH 1/7] test(pacts): Fix pact tests Fixes #1171 --- __config__/jest.setup-pacts-serlo-org-database-layer.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/__config__/jest.setup-pacts-serlo-org-database-layer.ts b/__config__/jest.setup-pacts-serlo-org-database-layer.ts index 2d90122be..e06c63c59 100644 --- a/__config__/jest.setup-pacts-serlo-org-database-layer.ts +++ b/__config__/jest.setup-pacts-serlo-org-database-layer.ts @@ -26,7 +26,11 @@ global.pact = new Pact({ }) beforeAll(async () => { - await createBeforeAll({ onUnhandledRequest: 'bypass' }) + createBeforeAll({ + onUnhandledRequest(req) { + return bypass(req) + }, + }) await global.pact.setup() }) From 0f225102ab50b3ac4e9923622c90e51e3d6af51e Mon Sep 17 00:00:00 2001 From: Stephan Kulla Date: Fri, 1 Dec 2023 17:27:37 +0100 Subject: [PATCH 2/7] test: Update pact setup file --- __config__/jest.setup-pacts-serlo-org-database-layer.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/__config__/jest.setup-pacts-serlo-org-database-layer.ts b/__config__/jest.setup-pacts-serlo-org-database-layer.ts index e06c63c59..7fc77e383 100644 --- a/__config__/jest.setup-pacts-serlo-org-database-layer.ts +++ b/__config__/jest.setup-pacts-serlo-org-database-layer.ts @@ -1,5 +1,5 @@ import { Pact } from '@pact-foundation/pact' -import { http } from 'msw' +import { bypass, http } from 'msw' import path from 'path' import { @@ -58,7 +58,7 @@ afterEach(async () => { try { await global.pact.verify() } finally { - createAfterEach() + await createAfterEach() } }) @@ -66,7 +66,7 @@ afterAll(async () => { try { await global.pact.finalize() } finally { - await createAfterAll() + createAfterAll() } }) From f5f19e058baaec710c6ba76626d08e37e58b4d7f Mon Sep 17 00:00:00 2001 From: Stephan Kulla Date: Fri, 1 Dec 2023 17:30:57 +0100 Subject: [PATCH 3/7] test(pacts): Simplify bypass config --- .../jest.setup-pacts-serlo-org-database-layer.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/__config__/jest.setup-pacts-serlo-org-database-layer.ts b/__config__/jest.setup-pacts-serlo-org-database-layer.ts index 7fc77e383..e6e44895f 100644 --- a/__config__/jest.setup-pacts-serlo-org-database-layer.ts +++ b/__config__/jest.setup-pacts-serlo-org-database-layer.ts @@ -1,5 +1,5 @@ import { Pact } from '@pact-foundation/pact' -import { bypass, http } from 'msw' +import { bypass, http, passthrough } from 'msw' import path from 'path' import { @@ -26,11 +26,7 @@ global.pact = new Pact({ }) beforeAll(async () => { - createBeforeAll({ - onUnhandledRequest(req) { - return bypass(req) - }, - }) + createBeforeAll({}) await global.pact.setup() }) @@ -41,7 +37,7 @@ beforeEach(async () => { new RegExp(process.env.SERLO_ORG_DATABASE_LAYER_HOST.replace('.', '\\.')), async ({ request }) => { const url = new URL(request.url) - return fetch(`http://127.0.0.1:${port}${url.pathname}`, request) + return fetch(bypass(`http://127.0.0.1:${port}${url.pathname}`, request)) }, ), ) From 1540f3f0777829cc413a9058eefdb62015cb1c60 Mon Sep 17 00:00:00 2001 From: Stephan Kulla Date: Fri, 1 Dec 2023 17:33:50 +0100 Subject: [PATCH 4/7] test: Simplify test setup --- ...st.setup-pacts-serlo-org-database-layer.ts | 2 +- __config__/jest.setup.ts | 19 +---------------- __config__/setup.ts | 21 ++++++++++++++++--- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/__config__/jest.setup-pacts-serlo-org-database-layer.ts b/__config__/jest.setup-pacts-serlo-org-database-layer.ts index e6e44895f..dc50686d1 100644 --- a/__config__/jest.setup-pacts-serlo-org-database-layer.ts +++ b/__config__/jest.setup-pacts-serlo-org-database-layer.ts @@ -26,7 +26,7 @@ global.pact = new Pact({ }) beforeAll(async () => { - createBeforeAll({}) + createBeforeAll() await global.pact.setup() }) diff --git a/__config__/jest.setup.ts b/__config__/jest.setup.ts index a9100478b..39e2e722b 100644 --- a/__config__/jest.setup.ts +++ b/__config__/jest.setup.ts @@ -25,24 +25,7 @@ jest.mock('@google-cloud/storage', () => { } }) -beforeAll(() => { - createBeforeAll({ - onUnhandledRequest(req) { - if ( - req.method === 'POST' && - req.url.includes(process.env.SERLO_ORG_DATABASE_LAYER_HOST) - ) { - console.error('Found an unhandled request for message %s', req.text()) - } else { - console.error( - 'Found an unhandled %s request to %s', - req.method, - req.url, - ) - } - }, - }) -}) +beforeAll(createBeforeAll) beforeEach(createBeforeEach) diff --git a/__config__/setup.ts b/__config__/setup.ts index 02bd70aea..08e99e810 100644 --- a/__config__/setup.ts +++ b/__config__/setup.ts @@ -1,7 +1,7 @@ import { flush as flushSentry } from '@sentry/node' import crypto from 'crypto' import { http, HttpResponse } from 'msw' -import { SetupServer, setupServer } from 'msw/node' +import { setupServer } from 'msw/node' import { defaultSpreadsheetApi, @@ -35,7 +35,7 @@ export class MockTimer implements Timer { } } -export function createBeforeAll(options: Parameters[0]) { +export function createBeforeAll() { initializeSentry({ dsn: 'https://public@127.0.0.1/0', environment: 'testing', @@ -50,7 +50,22 @@ export function createBeforeAll(options: Parameters[0]) { global.timer = timer global.kratos = kratos - global.server.listen(options) + global.server.listen({ + onUnhandledRequest(req) { + if ( + req.method === 'POST' && + req.url.includes(process.env.SERLO_ORG_DATABASE_LAYER_HOST) + ) { + console.error('Found an unhandled request for message %s', req.text()) + } else { + console.error( + 'Found an unhandled %s request to %s', + req.method, + req.url, + ) + } + }, + }) } export async function createBeforeEach() { From 15deb015cfc03f538e85bac36d6eee6a6d6df3cd Mon Sep 17 00:00:00 2001 From: Stephan Kulla Date: Fri, 1 Dec 2023 17:35:01 +0100 Subject: [PATCH 5/7] test: Simplify reporting of unhandled request --- __config__/setup.ts | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/__config__/setup.ts b/__config__/setup.ts index 08e99e810..7285dfe4b 100644 --- a/__config__/setup.ts +++ b/__config__/setup.ts @@ -51,19 +51,14 @@ export function createBeforeAll() { global.kratos = kratos global.server.listen({ - onUnhandledRequest(req) { - if ( - req.method === 'POST' && - req.url.includes(process.env.SERLO_ORG_DATABASE_LAYER_HOST) - ) { - console.error('Found an unhandled request for message %s', req.text()) - } else { - console.error( - 'Found an unhandled %s request to %s', - req.method, - req.url, - ) - } + async onUnhandledRequest(req) { + // eslint-disable-next-line no-console + console.error( + 'Found an unhandled %s request to %s with body %s', + req.method, + req.url, + await req.text(), + ) }, }) } From ec9ca36659db6133feb38cdbbbe773dde02d0ef5 Mon Sep 17 00:00:00 2001 From: Stephan Kulla Date: Fri, 1 Dec 2023 18:02:13 +0100 Subject: [PATCH 6/7] test: Fix tests kratos-middleware.ts --- __tests__/internals/kratos-middleware.ts | 41 +++++++++++++----------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/__tests__/internals/kratos-middleware.ts b/__tests__/internals/kratos-middleware.ts index acbc0bc78..2396746fe 100644 --- a/__tests__/internals/kratos-middleware.ts +++ b/__tests__/internals/kratos-middleware.ts @@ -1,5 +1,6 @@ import express, { Express } from 'express' import type { Server } from 'http' +import { bypass } from 'msw' import { given } from '../__utils__' import { Kratos } from '~/internals/authentication' @@ -135,28 +136,30 @@ function fetchKratosRegister({ withKratosKey?: boolean body?: unknown }) { - return fetch(`http://localhost:${port}/kratos/register`, { - method: 'POST', - headers: { - 'x-msw-bypass': 'true', - 'content-type': `application/json`, - ...(withKratosKey - ? { 'x-kratos-key': process.env.SERVER_KRATOS_SECRET } - : {}), - }, - ...(body != null ? { body: JSON.stringify(body) } : {}), - }) + return fetch( + bypass(`http://localhost:${port}/kratos/register`, { + method: 'POST', + headers: { + 'content-type': `application/json`, + ...(withKratosKey + ? { 'x-kratos-key': process.env.SERVER_KRATOS_SECRET } + : {}), + }, + ...(body != null ? { body: JSON.stringify(body) } : {}), + }), + ) } function fetchKratosSingleLogout(body?: string | undefined) { - return fetch(`http://localhost:${port}/kratos/single-logout`, { - method: 'POST', - headers: { - 'x-msw-bypass': 'true', - 'content-type': `application/x-www-form-urlencoded`, - }, - ...(body != null ? { body } : {}), - }) + return fetch( + bypass(`http://localhost:${port}/kratos/single-logout`, { + method: 'POST', + headers: { + 'content-type': `application/x-www-form-urlencoded`, + }, + ...(body != null ? { body } : {}), + }), + ) } function createKratosMiddlewareBeforeEach(done: jest.DoneCallback) { From 52b342a15990a29ba1a88823a232176335ebf696 Mon Sep 17 00:00:00 2001 From: Stephan Kulla Date: Fri, 1 Dec 2023 18:03:25 +0100 Subject: [PATCH 7/7] test: Return error on unhandled requests --- __config__/setup.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/__config__/setup.ts b/__config__/setup.ts index 7285dfe4b..434718bba 100644 --- a/__config__/setup.ts +++ b/__config__/setup.ts @@ -59,6 +59,7 @@ export function createBeforeAll() { req.url, await req.text(), ) + return 'error' }, }) }