From da7ca478a938aacd17504e42c290757941719952 Mon Sep 17 00:00:00 2001 From: davydocsurg Date: Thu, 18 May 2023 15:49:37 +0100 Subject: [PATCH] chore: update authorization middleware and getDeveloper controller #627 --- __tests__/developers.test.js | 14 ++----------- __tests__/shared/commands.js | 39 +++++++++++++++++++++--------------- jest.frontend.config.js | 3 ++- src/routers/router.ts | 2 +- 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/__tests__/developers.test.js b/__tests__/developers.test.js index 982d4adb..81cc3a98 100644 --- a/__tests__/developers.test.js +++ b/__tests__/developers.test.js @@ -1,4 +1,4 @@ -import { createDeveloper, getExample, getExamples, getWord, getWords } from './shared/commands'; +import { createDeveloper, getDeveloper, getExample, getExamples, getWord, getWords } from './shared/commands'; import { developerData, malformedDeveloperData, wordId, exampleId } from './__mocks__/documentData'; describe('Developers', () => { @@ -106,17 +106,7 @@ describe('Developers', () => { const developerDetails = await getDeveloper({ apiKey: developerRes.body.apiKey }); expect(developerDetails.status).toEqual(200); expect(developerDetails.body.developer).toMatchObject({ - usage: expect.objectContaining({ - date: expect.any(String), - count: expect.any(Number), - }), - name: expect.any(String), - apiKey: expect.any(String), - email: expect.any(String), - password: expect.any(String), - createdAt: expect.any(String), - updatedAt: expect.any(String), - id: expect.any(String), + ...developerRes.body.developer, }); }); diff --git a/__tests__/shared/commands.js b/__tests__/shared/commands.js index 1084b593..dccb0705 100644 --- a/__tests__/shared/commands.js +++ b/__tests__/shared/commands.js @@ -60,25 +60,25 @@ export const getExamplesV2 = (query = {}, options = {}) => .query(query) .set('X-API-Key', options.apiKey || FALLBACK_API_KEY); -/* Searches for Nsibidi characters using the data in MongoDB V1 */ + /* Searches for Nsibidi characters using the data in MongoDB V1 */ export const getNsibidiCharacter = (id, query = {}, options = {}) => - server - .get(`${API_ROUTE}/nsibidi/${id}`) - .query(query) - .set('X-API-Key', options.apiKey || FALLBACK_API_KEY); +server + .get(`${API_ROUTE}/nsibidi/${id}`) + .query(query) + .set('X-API-Key', options.apiKey || FALLBACK_API_KEY); /* Searches for Nsibidi characters using the data in MongoDB V2 */ export const getNsibidiCharacterV2 = (id, query = {}, options = {}) => - server - .get(`${API_ROUTE_V2}/nsibidi/${id}`) - .query(query) - .set('X-API-Key', options.apiKey || FALLBACK_API_KEY); +server + .get(`${API_ROUTE_V2}/nsibidi/${id}`) + .query(query) + .set('X-API-Key', options.apiKey || FALLBACK_API_KEY); export const getNsibidiCharactersV2 = (query = {}, options = {}) => - server - .get(`${API_ROUTE_V2}/nsibidi`) - .query(query) - .set('X-API-Key', options.apiKey || FALLBACK_API_KEY); +server + .get(`${API_ROUTE_V2}/nsibidi`) + .query(query) + .set('X-API-Key', options.apiKey || FALLBACK_API_KEY); /* Hits the POST /populate route to seed the local MongoDB database */ export const populateAPI = () => server.post(`${TEST_ROUTE}/populate`); @@ -94,6 +94,13 @@ export const searchMockedTerm = (term) => { return resultsFromDictionarySearch(regexTerm, term, mockedData); }; -/* fetch developer details */ -export const getDeveloper = (id, options = {}) => - server.get(`${API_ROUTE}/developers/${id}`).set('X-API-Key', options.apiKey || FALLBACK_API_KEY); +/* Fetch developer details */ +export const getDeveloper = (options = {}) => + server.get(`${API_ROUTE}/developers/account`).set('X-API-Key', options.apiKey || FALLBACK_API_KEY); + +/** Login a developer */ +export const loginDeveloper = (data) => server.post(`${API_ROUTE}/login`).send(data); + +/** Logout a developer */ +export const logoutDeveloper = (options = {}) => + server.post(`${API_ROUTE}/logout`).set('Authorization', `Bearer ${options.token || ''}`); diff --git a/jest.frontend.config.js b/jest.frontend.config.js index 2ddd0d57..7fda904a 100644 --- a/jest.frontend.config.js +++ b/jest.frontend.config.js @@ -1,3 +1,4 @@ +// Frontend Jest Config module.exports = { displayName: 'igbo_api', testMatch: ['./**/__tests__/**/*.test.tsx'], @@ -9,7 +10,7 @@ module.exports = { '^[./a-zA-Z0-9$_-]+\\.(svg|gif|png|less|css)$': '/src/__data__/assetStub.ts', }, transform: { - '^.+\\.(js|ts|tsx)$': 'ts-jest', + '^.+\\.(js|ts|tsx)$': ['ts-jest', { tsconfig: './tsconfig.test.json' }], }, setupFilesAfterEnv: ['./src/__tests__/shared/script.ts'], }; diff --git a/src/routers/router.ts b/src/routers/router.ts index 8d56a81a..01ce9ee6 100644 --- a/src/routers/router.ts +++ b/src/routers/router.ts @@ -3,7 +3,7 @@ import rateLimit from 'express-rate-limit'; import { MiddleWare } from '../types'; import { getWords, getWord } from '../controllers/words'; import { getExamples, getExample } from '../controllers/examples'; -import { getDeveloper, postDeveloper } from '../controllers/developers/developers'; +import { getDeveloper, postDeveloper } from '../controllers/developers'; import { getStats } from '../controllers/stats'; import validId from '../middleware/validId'; import validateDeveloperBody from '../middleware/validateDeveloperBody';