From a70d563a7dc92818c9d545e0cdd06f41a44a9cad Mon Sep 17 00:00:00 2001 From: "Xunnamius (Romulus)" Date: Thu, 1 Jun 2023 03:11:57 -0700 Subject: [PATCH] refactor: 181/184 unit test suite passing --- src/pages/api/v1/blogs/[blogName]/index.ts | 4 +- .../pages/[pageName]/sessions/[session_id].ts | 10 +- .../pages/[pageName]/sessions/index.ts | 10 +- test/api/unit-app-blogs.test.ts | 141 ++++++++----- test/api/unit-app-info.test.ts | 195 +----------------- test/api/unit-app-users.test.ts | 79 ++----- 6 files changed, 126 insertions(+), 313 deletions(-) diff --git a/src/pages/api/v1/blogs/[blogName]/index.ts b/src/pages/api/v1/blogs/[blogName]/index.ts index bc447c1..ae8bbb0 100644 --- a/src/pages/api/v1/blogs/[blogName]/index.ts +++ b/src/pages/api/v1/blogs/[blogName]/index.ts @@ -1,5 +1,5 @@ import { withMiddleware } from 'universe/backend/middleware'; -import { getBlogPagesMetadata, updateBlog } from 'universe/backend'; +import { getBlog, updateBlog } from 'universe/backend'; import { sendHttpOk } from 'multiverse/next-api-respond'; // ? This is a NextJS special "config" export @@ -16,7 +16,7 @@ export default withMiddleware( switch (req.method) { case 'GET': { sendHttpOk(res, { - blog: await getBlogPagesMetadata({ blogName }) + blog: await getBlog({ blogName }) }); break; } diff --git a/src/pages/api/v1/blogs/[blogName]/pages/[pageName]/sessions/[session_id].ts b/src/pages/api/v1/blogs/[blogName]/pages/[pageName]/sessions/[session_id].ts index 111b9c9..d081be9 100644 --- a/src/pages/api/v1/blogs/[blogName]/pages/[pageName]/sessions/[session_id].ts +++ b/src/pages/api/v1/blogs/[blogName]/pages/[pageName]/sessions/[session_id].ts @@ -16,13 +16,13 @@ export default withMiddleware( switch (req.method) { case 'PUT': { - await renewSession({ session_id }); + await renewSession({ sessionId: session_id }); sendHttpOk(res); break; } case 'DELETE': { - await deleteSession({ session_id }); + await deleteSession({ sessionId: session_id }); sendHttpOk(res); break; } @@ -30,6 +30,10 @@ export default withMiddleware( }, { descriptor: metadata.descriptor, - options: { allowedMethods: ['PUT', 'DELETE'], apiVersion: '1' } + options: { + allowedContentTypes: { PUT: ['application/json', 'none'], DELETE: 'none' }, + allowedMethods: ['PUT', 'DELETE'], + apiVersion: '1' + } } ); diff --git a/src/pages/api/v1/blogs/[blogName]/pages/[pageName]/sessions/index.ts b/src/pages/api/v1/blogs/[blogName]/pages/[pageName]/sessions/index.ts index d2044f4..84b2c69 100644 --- a/src/pages/api/v1/blogs/[blogName]/pages/[pageName]/sessions/index.ts +++ b/src/pages/api/v1/blogs/[blogName]/pages/[pageName]/sessions/index.ts @@ -1,5 +1,5 @@ import { withMiddleware } from 'universe/backend/middleware'; -import { createSession, getBlogPagesMetadata } from 'universe/backend'; +import { createSession, getPageSessionsCount } from 'universe/backend'; import { authorizationHeaderToOwnerAttribute } from 'universe/backend/api'; import { sendHttpOk } from 'multiverse/next-api-respond'; @@ -18,7 +18,7 @@ export default withMiddleware( switch (req.method) { case 'GET': { sendHttpOk(res, { - active: await getBlogPagesMetadata({ blogName }) + active: await getPageSessionsCount({ blogName, pageName }) }); break; } @@ -39,6 +39,10 @@ export default withMiddleware( }, { descriptor: metadata.descriptor, - options: { allowedMethods: ['GET', 'POST'], apiVersion: '1' } + options: { + allowedContentTypes: { POST: ['application/json', 'none'], GET: 'none' }, + allowedMethods: ['GET', 'POST'], + apiVersion: '1' + } } ); diff --git a/test/api/unit-app-blogs.test.ts b/test/api/unit-app-blogs.test.ts index bb7e1fd..805ab65 100644 --- a/test/api/unit-app-blogs.test.ts +++ b/test/api/unit-app-blogs.test.ts @@ -3,6 +3,13 @@ import { testApiHandler } from 'next-test-api-route-handler'; import { api, setupMockBackend } from 'testverse/util'; jest.mock('universe/backend'); +jest.mock('universe/backend/api', (): typeof import('universe/backend/api') => { + return { + ...jest.requireActual('universe/backend/api'), + authorizationHeaderToOwnerAttribute: jest.fn(() => Promise.resolve('mock-owner')) + }; +}); + jest.mock( 'universe/backend/middleware', (): typeof import('universe/backend/middleware') => { @@ -17,15 +24,16 @@ jest.mock( } ); -const { mockedAuthAppUser } = setupMockBackend(); +setupMockBackend(); -describe('api/v1/users', () => { +describe('api/v1/blogs/:blogName', () => { describe('/ [GET]', () => { it('accepts GET requests', async () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.users, + handler: api.v1.blogsBlogname, + params: { blogName: 'blog-name' }, test: async ({ fetch }) => { const [status, json] = await fetch({ method: 'GET' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] @@ -33,40 +41,40 @@ describe('api/v1/users', () => { expect(status).toBe(200); expect(json.success).toBeTrue(); - expect(json.users).toBeArray(); + expect(json.blog).toBeObject(); expect(Object.keys(json)).toHaveLength(2); } }); }); }); - describe('/ [POST]', () => { - it('accepts POST requests', async () => { + describe('/ [PATCH]', () => { + it('accepts PATCH requests', async () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.users, + handler: api.v1.blogsBlogname, + params: { blogName: 'blog-name' }, test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'POST' }).then( + const [status, json] = await fetch({ method: 'PATCH' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] ); expect(status).toBe(200); expect(json.success).toBeTrue(); - expect(json.user).toBeObject(); - expect(Object.keys(json)).toHaveLength(2); + expect(Object.keys(json)).toHaveLength(1); } }); }); }); - describe('/:username [GET]', () => { + describe('/pages [GET]', () => { it('accepts GET requests', async () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsername, - params: { username: 'User1' }, + handler: api.v1.blogsBlognamePages, + params: { blogName: 'blog-name' }, test: async ({ fetch }) => { const [status, json] = await fetch({ method: 'GET' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] @@ -74,79 +82,84 @@ describe('api/v1/users', () => { expect(status).toBe(200); expect(json.success).toBeTrue(); - expect(json.user).toBeObject(); + expect(json.pages).toBeArray(); expect(Object.keys(json)).toHaveLength(2); } }); }); }); - describe('/:username [PATCH]', () => { - it('accepts PATCH requests', async () => { + describe('/pages [POST]', () => { + it('accepts POST requests', async () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsername, - params: { username: 'User1' }, + handler: api.v1.blogsBlognamePages, + params: { blogName: 'blog-name' }, test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'PATCH' }).then( + const [status, json] = await fetch({ method: 'POST' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] ); expect(status).toBe(200); expect(json.success).toBeTrue(); - expect(Object.keys(json)).toHaveLength(1); + expect(json.page).toBeObject(); + expect(Object.keys(json)).toHaveLength(2); } }); }); }); - describe('/:username [DELETE]', () => { - it('accepts DELETE requests', async () => { + describe('/pages/:pageName [GET]', () => { + it('accepts GET requests', async () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsername, - params: { username: 'User1' }, + handler: api.v1.blogsBlognamePagesPagename, + params: { blogName: 'blog-name', pageName: 'page-name' }, test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'DELETE' }).then( + const [status, json] = await fetch({ method: 'GET' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] ); expect(status).toBe(200); expect(json.success).toBeTrue(); - expect(Object.keys(json)).toHaveLength(1); + expect(json.page).toBeObject(); + expect(Object.keys(json)).toHaveLength(2); } }); }); }); - describe('/:username/auth [POST]', () => { - it('accepts POST requests', async () => { + describe('/pages/:pageName [PATCH]', () => { + it('accepts PATCH requests', async () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsernameAuth, - params: { username: 'User1' }, + handler: api.v1.blogsBlognamePagesPagename, + params: { blogName: 'blog-name', pageName: 'page-name' }, test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'POST' }).then( + const [status, json] = await fetch({ method: 'PATCH' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] ); - expect(status).toBe(403); - expect(json.success).toBeFalse(); - expect(json.error).toBeString(); - expect(Object.keys(json)).toHaveLength(2); + expect(status).toBe(200); + expect(json.success).toBeTrue(); + expect(Object.keys(json)).toHaveLength(1); } }); + }); + }); - mockedAuthAppUser.mockReturnValue(Promise.resolve(true)); + describe('/pages/:pageName [DELETE]', () => { + it('accepts DELETE requests', async () => { + expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsernameAuth, - params: { username: 'User1' }, + handler: api.v1.blogsBlognamePagesPagename, + params: { blogName: 'blog-name', pageName: 'page-name' }, test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'POST' }).then( + const [status, json] = await fetch({ method: 'DELETE' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] ); @@ -158,13 +171,13 @@ describe('api/v1/users', () => { }); }); - describe('/:username/questions [GET]', () => { + describe('/pages/:pageName/sessions [GET]', () => { it('accepts GET requests', async () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsernameQuestions, - params: { username: 'User1' }, + handler: api.v1.blogsBlognamePagesPagenameSessions, + params: { blogName: 'blog-name', pageName: 'page-name' }, test: async ({ fetch }) => { const [status, json] = await fetch({ method: 'GET' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] @@ -172,43 +185,63 @@ describe('api/v1/users', () => { expect(status).toBe(200); expect(json.success).toBeTrue(); - expect(json.questions).toBeArray(); + expect(json.active).toBeNumber(); expect(Object.keys(json)).toHaveLength(2); } }); }); }); - describe('/:username/answers [GET]', () => { - it('accepts GET requests', async () => { + describe('/pages/:pageName/sessions [POST]', () => { + it('accepts POST requests', async () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsernameAnswers, - params: { username: 'User1' }, + handler: api.v1.blogsBlognamePagesPagenameSessions, + params: { blogName: 'blog-name', pageName: 'page-name' }, test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'GET' }).then( + const [status, json] = await fetch({ method: 'POST' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] ); expect(status).toBe(200); expect(json.success).toBeTrue(); - expect(json.answers).toBeArray(); + expect(json.session_id).toBeString(); expect(Object.keys(json)).toHaveLength(2); } }); }); }); - describe('/:username/points [PATCH]', () => { - it('accepts PATCH requests', async () => { + describe('/pages/:pageName/sessions/:session_id [PUT]', () => { + it('accepts PUT requests', async () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsernamePoints, - params: { username: 'User1' }, + handler: api.v1.blogsBlognamePagesPagenameSessionsSessionid, + params: { blogName: 'blog-name', pageName: 'page-name', session_id: 'id' }, test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'PATCH' }).then( + const [status, json] = await fetch({ method: 'PUT' }).then( + async (r) => [r.status, await r.json()] as [status: number, json: any] + ); + + expect(status).toBe(200); + expect(json.success).toBeTrue(); + expect(Object.keys(json)).toHaveLength(1); + } + }); + }); + }); + + describe('/pages/:pageName/sessions/:session_id [DELETE]', () => { + it('accepts DELETE requests', async () => { + expect.hasAssertions(); + + await testApiHandler({ + handler: api.v1.blogsBlognamePagesPagenameSessionsSessionid, + params: { blogName: 'blog-name', pageName: 'page-name', session_id: 'id' }, + test: async ({ fetch }) => { + const [status, json] = await fetch({ method: 'DELETE' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] ); diff --git a/test/api/unit-app-info.test.ts b/test/api/unit-app-info.test.ts index bb7e1fd..15e96ce 100644 --- a/test/api/unit-app-info.test.ts +++ b/test/api/unit-app-info.test.ts @@ -3,6 +3,13 @@ import { testApiHandler } from 'next-test-api-route-handler'; import { api, setupMockBackend } from 'testverse/util'; jest.mock('universe/backend'); +jest.mock('universe/backend/api', (): typeof import('universe/backend/api') => { + return { + ...jest.requireActual('universe/backend/api'), + authorizationHeaderToOwnerAttribute: jest.fn(() => Promise.resolve('mock-owner')) + }; +}); + jest.mock( 'universe/backend/middleware', (): typeof import('universe/backend/middleware') => { @@ -17,154 +24,15 @@ jest.mock( } ); -const { mockedAuthAppUser } = setupMockBackend(); +setupMockBackend(); -describe('api/v1/users', () => { +describe('api/v1/info', () => { describe('/ [GET]', () => { it('accepts GET requests', async () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.users, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'GET' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(json.users).toBeArray(); - expect(Object.keys(json)).toHaveLength(2); - } - }); - }); - }); - - describe('/ [POST]', () => { - it('accepts POST requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.users, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'POST' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(json.user).toBeObject(); - expect(Object.keys(json)).toHaveLength(2); - } - }); - }); - }); - - describe('/:username [GET]', () => { - it('accepts GET requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.usersUsername, - params: { username: 'User1' }, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'GET' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(json.user).toBeObject(); - expect(Object.keys(json)).toHaveLength(2); - } - }); - }); - }); - - describe('/:username [PATCH]', () => { - it('accepts PATCH requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.usersUsername, - params: { username: 'User1' }, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'PATCH' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(Object.keys(json)).toHaveLength(1); - } - }); - }); - }); - - describe('/:username [DELETE]', () => { - it('accepts DELETE requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.usersUsername, - params: { username: 'User1' }, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'DELETE' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(Object.keys(json)).toHaveLength(1); - } - }); - }); - }); - - describe('/:username/auth [POST]', () => { - it('accepts POST requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.usersUsernameAuth, - params: { username: 'User1' }, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'POST' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(403); - expect(json.success).toBeFalse(); - expect(json.error).toBeString(); - expect(Object.keys(json)).toHaveLength(2); - } - }); - - mockedAuthAppUser.mockReturnValue(Promise.resolve(true)); - - await testApiHandler({ - handler: api.v1.usersUsernameAuth, - params: { username: 'User1' }, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'POST' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(Object.keys(json)).toHaveLength(1); - } - }); - }); - }); - - describe('/:username/questions [GET]', () => { - it('accepts GET requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.usersUsernameQuestions, - params: { username: 'User1' }, + handler: api.v1.info, test: async ({ fetch }) => { const [status, json] = await fetch({ method: 'GET' }).then( async (r) => [r.status, await r.json()] as [status: number, json: any] @@ -172,51 +40,10 @@ describe('api/v1/users', () => { expect(status).toBe(200); expect(json.success).toBeTrue(); - expect(json.questions).toBeArray(); + expect(json.info).toBeObject(); expect(Object.keys(json)).toHaveLength(2); } }); }); }); - - describe('/:username/answers [GET]', () => { - it('accepts GET requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.usersUsernameAnswers, - params: { username: 'User1' }, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'GET' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(json.answers).toBeArray(); - expect(Object.keys(json)).toHaveLength(2); - } - }); - }); - }); - - describe('/:username/points [PATCH]', () => { - it('accepts PATCH requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.usersUsernamePoints, - params: { username: 'User1' }, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'PATCH' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(Object.keys(json)).toHaveLength(1); - } - }); - }); - }); }); diff --git a/test/api/unit-app-users.test.ts b/test/api/unit-app-users.test.ts index bb7e1fd..2f58d17 100644 --- a/test/api/unit-app-users.test.ts +++ b/test/api/unit-app-users.test.ts @@ -3,6 +3,13 @@ import { testApiHandler } from 'next-test-api-route-handler'; import { api, setupMockBackend } from 'testverse/util'; jest.mock('universe/backend'); +jest.mock('universe/backend/api', (): typeof import('universe/backend/api') => { + return { + ...jest.requireActual('universe/backend/api'), + authorizationHeaderToOwnerAttribute: jest.fn(() => Promise.resolve('mock-owner')) + }; +}); + jest.mock( 'universe/backend/middleware', (): typeof import('universe/backend/middleware') => { @@ -65,7 +72,7 @@ describe('api/v1/users', () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsername, + handler: api.v1.usersUsernameoremail, params: { username: 'User1' }, test: async ({ fetch }) => { const [status, json] = await fetch({ method: 'GET' }).then( @@ -86,7 +93,7 @@ describe('api/v1/users', () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsername, + handler: api.v1.usersUsernameoremail, params: { username: 'User1' }, test: async ({ fetch }) => { const [status, json] = await fetch({ method: 'PATCH' }).then( @@ -106,7 +113,7 @@ describe('api/v1/users', () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsername, + handler: api.v1.usersUsernameoremail, params: { username: 'User1' }, test: async ({ fetch }) => { const [status, json] = await fetch({ method: 'DELETE' }).then( @@ -126,7 +133,7 @@ describe('api/v1/users', () => { expect.hasAssertions(); await testApiHandler({ - handler: api.v1.usersUsernameAuth, + handler: api.v1.usersUsernameoremailAuth, params: { username: 'User1' }, test: async ({ fetch }) => { const [status, json] = await fetch({ method: 'POST' }).then( @@ -143,7 +150,7 @@ describe('api/v1/users', () => { mockedAuthAppUser.mockReturnValue(Promise.resolve(true)); await testApiHandler({ - handler: api.v1.usersUsernameAuth, + handler: api.v1.usersUsernameoremailAuth, params: { username: 'User1' }, test: async ({ fetch }) => { const [status, json] = await fetch({ method: 'POST' }).then( @@ -157,66 +164,4 @@ describe('api/v1/users', () => { }); }); }); - - describe('/:username/questions [GET]', () => { - it('accepts GET requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.usersUsernameQuestions, - params: { username: 'User1' }, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'GET' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(json.questions).toBeArray(); - expect(Object.keys(json)).toHaveLength(2); - } - }); - }); - }); - - describe('/:username/answers [GET]', () => { - it('accepts GET requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.usersUsernameAnswers, - params: { username: 'User1' }, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'GET' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(json.answers).toBeArray(); - expect(Object.keys(json)).toHaveLength(2); - } - }); - }); - }); - - describe('/:username/points [PATCH]', () => { - it('accepts PATCH requests', async () => { - expect.hasAssertions(); - - await testApiHandler({ - handler: api.v1.usersUsernamePoints, - params: { username: 'User1' }, - test: async ({ fetch }) => { - const [status, json] = await fetch({ method: 'PATCH' }).then( - async (r) => [r.status, await r.json()] as [status: number, json: any] - ); - - expect(status).toBe(200); - expect(json.success).toBeTrue(); - expect(Object.keys(json)).toHaveLength(1); - } - }); - }); - }); });