refactor(middleware-jwt): integration with
MODIFY
packages/middleware-body/package.json
packages/middleware-body/package.json
"type-is": "~1.6.16"
},
"devDependencies": {
- "@marblejs/core": "^3.2.0"
+ "@marblejs/core": "^3.2.0",
+ "@marblejs/testing": "^3.2.0"
},
"publishConfig": {
"access": "public"

MODIFY
packages/middleware-body/test/bodyParser.integration.spec.ts
packages/middleware-body/test/bodyParser.integration.spec.ts
import { ContentType } from '@marblejs/core/dist/+internal/http';
-import { createHttpServerTestBed } from '@marblejs/core/dist/+internal/testing';
-import { createServer } from '@marblejs/core';
-import * as request from 'supertest';
+import { bufferFrom, stringifyJson } from '@marblejs/core/dist/+internal/utils';
+import { pipe } from 'fp-ts/lib/pipeable';
+import { createHttpTestBed, createTestBedSetup } from '@marblejs/testing';
import { listener } from './bodyParser.integration';
+const testBed = createHttpTestBed({ listener });
+const useTestBedSetup = createTestBedSetup({ testBed });
+
describe('@marblejs/middleware-body - integration', () => {
- const server = createServer({ listener });
- const httpTestBed = createHttpServerTestBed(server);
-
- describe('POST /default-parser', () => {
- test(`parses ${ContentType.APPLICATION_JSON} content-type`, async () =>
- request(httpTestBed.getInstance())
- .post('/default-parser')
- .set({ 'Content-Type': ContentType.APPLICATION_JSON })
- .send({ id: 'id', name: 'name', age: 100 })
- .expect(200, { id: 'id', name: 'name', age: 100 })
+ const testBedSetup = useTestBedSetup();
+
+ afterEach(async () => {
+ await testBedSetup.cleanup();
+ });
+
+ describe('default parser', () => {
+ test(`parses ${ContentType.APPLICATION_JSON} content-type`, async () => {
+ const { request } = await testBedSetup.useTestBed();
+ const body = { id: 'id', name: 'name', age: 100 };
+
+ const response = await pipe(
+ request('POST'),
+ request.withPath('/default-parser'),
+ request.withHeaders({ 'Content-Type': ContentType.APPLICATION_JSON }),
+ request.withBody(body),
+ request.send,
);
- test(`parses ${ContentType.APPLICATION_X_WWW_FORM_URLENCODED} content-type`, async () =>
- request(httpTestBed.getInstance())
- .post('/default-parser')
- .set({ 'Content-Type': ContentType.APPLICATION_X_WWW_FORM_URLENCODED })
- .send({ id: 'id', name: 'name', age: 100 })
- .expect(200, { id: 'id', name: 'name', age: '100' })
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual(body);
+ });
+
+ test(`parses ${ContentType.APPLICATION_X_WWW_FORM_URLENCODED} content-type`, async () => {
+ const { request } = await testBedSetup.useTestBed();
+ const body = { id: 'id', name: 'name', age: '100' };
+
+ const response = await pipe(
+ request('POST'),
+ request.withPath('/default-parser'),
+ request.withHeaders({ 'Content-Type': ContentType.APPLICATION_X_WWW_FORM_URLENCODED }),
+ request.withBody(body),
+ request.send,
);
+
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual(body);
+ });
});
- describe('POST /multiple-parsers', () => {
+ describe('multiple parsers', () => {
+ test(`parses ${ContentType.APPLICATION_JSON} content-type`, async () => {
+ const { request } = await testBedSetup.useTestBed();
const body = { id: 'id', name: 'name', age: 100 };
- const text = 'test message';
-
- test(`parses ${ContentType.APPLICATION_JSON} content-type`, async () =>
- request(httpTestBed.getInstance())
- .post('/multiple-parsers')
- .set({ 'Content-Type': ContentType.APPLICATION_JSON })
- .send(body)
- .expect(200, body),
- );
- test(`parses custom "test/json" content-type`, async () =>
- request(httpTestBed.getInstance())
- .post('/multiple-parsers')
- .set({ 'Content-Type': 'test/json' })
- .send(body)
- .expect(200, body)
+ const response = await pipe(
+ request('POST'),
+ request.withPath('/multiple-parsers'),
+ request.withHeaders({ 'Content-Type': ContentType.APPLICATION_JSON }),
+ request.withBody(body),
+ request.send,
);
- test(`parses ${ContentType.APPLICATION_VND_API_JSON} content-type`, async () =>
- request(httpTestBed.getInstance())
- .post('/multiple-parsers')
- .set({ 'Content-Type': ContentType.APPLICATION_VND_API_JSON })
- .send(body)
- .expect(200, body)
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual(body);
+ });
+
+ test('parses custom "test/json" content-type', async () => {
+ const { request } = await testBedSetup.useTestBed();
+ const body = { id: 'id', name: 'name', age: 100 };
+
+ const response = await pipe(
+ request('POST'),
+ request.withPath('/multiple-parsers'),
+ request.withHeaders({ 'Content-Type': 'test/json' }),
+ request.withBody(body),
+ request.send,
);
- test(`parses ${ContentType.TEXT_PLAIN} content-type`, async () =>
- request(httpTestBed.getInstance())
- .post('/multiple-parsers')
- .set({ 'Content-Type': ContentType.TEXT_PLAIN })
- .send(text)
- .expect(200, text)
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual(body);
+ });
+
+ test(`parses ${ContentType.APPLICATION_VND_API_JSON} content-type`, async () => {
+ const { request } = await testBedSetup.useTestBed();
+ const body = { id: 'id', name: 'name', age: 100 };
+
+ const response = await pipe(
+ request('POST'),
+ request.withPath('/multiple-parsers'),
+ request.withHeaders({ 'Content-Type': ContentType.APPLICATION_VND_API_JSON }),
+ request.withBody(body),
+ request.send,
);
- test(`parses ${ContentType.APPLICATION_OCTET_STREAM} content-type`, async () =>
- request(httpTestBed.getInstance())
- .post('/multiple-parsers')
- .set({ 'Content-Type': ContentType.APPLICATION_OCTET_STREAM })
- .send(text)
- .expect(200)
- .then(({ body }) => expect(body).toEqual(Buffer.from(body)))
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual(body);
+ });
+
+ test(`parses ${ContentType.TEXT_PLAIN} content-type`, async () => {
+ const { request } = await testBedSetup.useTestBed();
+ const body = 'test message';
+
+ const response = await pipe(
+ request('POST'),
+ request.withPath('/multiple-parsers'),
+ request.withHeaders({ 'Content-Type': ContentType.TEXT_PLAIN }),
+ request.withBody(body),
+ request.send,
);
+
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual(body);
});
+ test(`parses ${ContentType.APPLICATION_OCTET_STREAM} content-type`, async () => {
+ const { request } = await testBedSetup.useTestBed();
+ const body = { id: 'id', name: 'name', age: 100 };
+
+ const response = await pipe(
+ request('POST'),
+ request.withPath('/multiple-parsers'),
+ request.withHeaders({ 'Content-Type': ContentType.APPLICATION_OCTET_STREAM }),
+ request.withBody(body),
+ request.send,
+ );
+
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual(pipe(body, stringifyJson, bufferFrom));
+ });
+ });
});

MODIFY
packages/middleware-jwt/package.json
packages/middleware-jwt/package.json
},
"devDependencies": {
"@marblejs/core": "^3.2.0",
- "@marblejs/middleware-body": "^3.2.0"
+ "@marblejs/middleware-body": "^3.2.0",
+ "@marblejs/testing": "3.2.0"
},
"publishConfig": {
"access": "public"

MODIFY
packages/middleware-jwt/test/jwt.integration.spec.ts
packages/middleware-jwt/test/jwt.integration.spec.ts
-import * as request from 'supertest';
-import { createServer } from '@marblejs/core';
-import { createHttpServerTestBed } from '@marblejs/core/dist/+internal/testing';
+import { createHttpTestBed, createTestBedSetup } from '@marblejs/testing';
+import { pipe } from 'fp-ts/lib/pipeable';
import { verifyToken } from '../src';
import { listener, SECRET_KEY } from './jwt.integration';
+const testBed = createHttpTestBed({ listener });
+const useTestBedSetup = createTestBedSetup({ testBed });
const LOGIN_CREDENTIALS = { email: 'admin@admin.com', password: 'admin' };
describe('@marblejs/middleware-jwt - HTTP integration', () => {
- const server = createServer({ listener });
- const httpTestBed = createHttpServerTestBed(server);
-
- test('POST /api/login returns token and verifies its correctness', async () =>
- request(httpTestBed.getInstance())
- .post('/api/login')
- .send(LOGIN_CREDENTIALS)
- .expect(200)
- .then(req => {
- const { token } = req.body;
- expect(token).toBeDefined();
- expect(typeof token).toBe('string');
+ const testBedSetup = useTestBedSetup();
+ afterEach(async () => {
+ await testBedSetup.cleanup();
+ });
+
+ test('POST "/api/login" returns token and verifies its correctness', async () => {
+ const { request } = await testBedSetup.useTestBed();
+
+ const response = await pipe(
+ request('POST'),
+ request.withPath('/api/login'),
+ request.withBody(LOGIN_CREDENTIALS),
+ request.send,
+ );
+
+ expect(response.body).toEqual({ token: expect.any(String) });
+
+ const { token } = response.body;
const payload = verifyToken<any>({ secret: SECRET_KEY })(token);
+
expect(payload.id).toEqual('test_id');
expect(payload.email).toEqual('admin@admin.com');
- }));
-
- test('GET /api/secured authorizes request and checks the `req.user` object', async () => {
- const token = await request(httpTestBed.getInstance())
- .post('/api/login')
- .send(LOGIN_CREDENTIALS)
- .expect(200)
- .then(req => req.body.token as string);
-
- return request(httpTestBed.getInstance())
- .get('/api/secured')
- .set('Authorization', `Bearer ${token}`)
- .expect(200)
- .then(req => {
- const user = req.body;
- expect(user.id).toEqual('test_id');
- expect(user.email).toEqual(LOGIN_CREDENTIALS.email);
- expect(user.password).toBeUndefined();
});
+
+ test('GET "/api/secured" authorizes request and checks the `req.user` object', async () => {
+ const { request } = await testBedSetup.useTestBed();
+
+ const responseWithToken = await pipe(
+ request('POST'),
+ request.withPath('/api/login'),
+ request.withBody(LOGIN_CREDENTIALS),
+ request.send,
+ );
+
+ const { token } = responseWithToken.body;
+
+ const responseAuthorized = await pipe(
+ request('GET'),
+ request.withPath('/api/secured'),
+ request.withHeaders({ 'Authorization': `Beared ${token}` }),
+ request.send,
+ );
+
+ expect(responseAuthorized.statusCode).toEqual(200);
+ expect(responseAuthorized.body).toEqual({
+ id: 'test_id',
+ email: LOGIN_CREDENTIALS.email,
+ iat: expect.any(Number),
});
+ });
+
+ test('GET "/api/secured" return 401 if payload doesn\'t pass verification', async () => {
+ const { request } = await testBedSetup.useTestBed();
+
+ const responseWithToken = await pipe(
+ request('POST'),
+ request.withPath('/api/login'),
+ request.withBody({ ...LOGIN_CREDENTIALS, email: 'doesnt_exists@admin.com' }),
+ request.send,
+ );
+
+ const { token } = responseWithToken.body;
+
+ const responseNotAuthorized = await pipe(
+ request('GET'),
+ request.withPath('/api/secured'),
+ request.withHeaders({ 'Authorization': `Beared ${token}` }),
+ request.send,
+ );
- test('GET /api/secured return 401 if payload doesn\'t pass verification', async () => {
- const token = await request(httpTestBed.getInstance())
- .post('/api/login')
- .send({ ...LOGIN_CREDENTIALS, email: 'doesnt_exists@admin.com' })
- .expect(200)
- .then(req => req.body.token as string);
-
- return request(httpTestBed.getInstance())
- .get('/api/secured')
- .set('Authorization', `Bearer ${token}`)
- .expect(401, { error: { status: 401, message: 'Unauthorized' } });
+ expect(responseNotAuthorized.statusCode).toEqual(401);
+ expect(responseNotAuthorized.body).toEqual({
+ error: {
+ status: 401,
+ message: 'Unauthorized',
+ }
});
+ });
+
+ test('GET "/api/secured" returns 401 if token is wrong', async () => {
+ const { request } = await testBedSetup.useTestBed();
+
+ const response = await pipe(
+ request('GET'),
+ request.withPath('/api/secured'),
+ request.withHeaders({ 'Authorization': 'Beared WRONG_TOKEN' }),
+ request.send,
+ );
- test('GET /api/secured returns 401 if token is wrong', async () =>
- request(httpTestBed.getInstance())
- .get('/api/secured')
- .set('Authorization', `Bearer WRONG_TOKEN`)
- .expect(401, { error: { status: 401, message: 'Unauthorized' } }));
+ expect(response.statusCode).toEqual(401);
+ expect(response.body).toEqual({
+ error: {
+ status: 401,
+ message: 'Unauthorized',
+ }
+ });
+ });
});

refactor(middleware-jwt): integration with @marblejs/testing