From d01029ccbf19592769f71dfa5997212dd207d2fe Mon Sep 17 00:00:00 2001 From: GoldenJaden <yaroslav-gusev-00@mail.ru> Date: Wed, 6 Mar 2024 19:55:24 +0300 Subject: [PATCH 1/3] Move team rights test data to a separate file --- src/presentation/http/router/note.test.ts | 100 +--------- .../http/router/noteSettings.test.ts | 187 +----------------- src/tests/utils/team-rights.ts | 34 ++++ 3 files changed, 45 insertions(+), 276 deletions(-) create mode 100644 src/tests/utils/team-rights.ts diff --git a/src/presentation/http/router/note.test.ts b/src/presentation/http/router/note.test.ts index 9ab72fd5..b75826a8 100644 --- a/src/presentation/http/router/note.test.ts +++ b/src/presentation/http/router/note.test.ts @@ -1,6 +1,7 @@ import { MemberRole } from '@domain/entities/team.js'; import { describe, test, expect, beforeEach } from 'vitest'; import type User from '@domain/entities/user.js'; +import { memberRightsTestData } from '@tests/utils/team-rights'; describe('Note API', () => { beforeEach(async () => { @@ -243,38 +244,7 @@ describe('Note API', () => { }); describe('PATCH note/:notePublicId ', () => { - test.each([ - /** Returns 200 if user is team member with a Write role */ - { - role: MemberRole.Write, - isAuthorized: true, - expectedStatusCode: 200, - }, - - /** Returns 403 if user is team member with a Read role */ - { - role: MemberRole.Read, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', - }, - - /** Returns 403 if user is not in the team */ - { - role: null, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', - }, - - /** Returns 401 if user is not authorized */ - { - role: null, - isAuthorized: false, - expectedStatusCode: 401, - expectedMessage: 'You must be authenticated to access this resource', - }, - ]) + test.each(memberRightsTestData) ('Patch note by public id', async ({ role, isAuthorized, expectedStatusCode, expectedMessage }) => { /** Only if user has a Write role, he can edit the note */ const canEdit = role === MemberRole.Write; @@ -459,38 +429,7 @@ describe('Note API', () => { }); describe('DELETE /note/:notePublicId', () => { - test.each([ - /** Returns 200 if user is team member with a Write role */ - { - role: MemberRole.Write, - isAuthorized: true, - expectedStatusCode: 200, - }, - - /** Returns 403 if user is team member with a Read role */ - { - role: MemberRole.Read, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', - }, - - /** Returns 403 if user is not in the team */ - { - role: null, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', - }, - - /** Returns 401 if user is not authorized */ - { - role: null, - isAuthorized: false, - expectedStatusCode: 401, - expectedMessage: 'You must be authenticated to access this resource', - }, - ]) + test.each(memberRightsTestData) ('Delete note by public id', async ({ role, isAuthorized, expectedStatusCode, expectedMessage }) => { /** Create test user - creator of note */ const creator = await global.db.insertUser(); @@ -674,38 +613,7 @@ describe('Note API', () => { accessToken = global.auth(user.id); }); - test.each([ - /** Returns 200 if user is team member with a Write role */ - { - role: MemberRole.Write, - isAuthorized: true, - expectedStatusCode: 200, - }, - - /** Returns 403 if user is team member with a Read role */ - { - role: MemberRole.Read, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', - }, - - /** Returns 403 if user is not in the team */ - { - role: null, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', - }, - - /** Returns 401 if user is not authorized */ - { - role: null, - isAuthorized: false, - expectedStatusCode: 401, - expectedMessage: 'You must be authenticated to access this resource', - }, - ]) + test.each(memberRightsTestData) ('Unlink any parent from note by it\'s public id', async ({ role, isAuthorized, expectedStatusCode, expectedMessage }) => { /* Create second user, who will be the creator of the note */ const creator = await global.db.insertUser(); diff --git a/src/presentation/http/router/noteSettings.test.ts b/src/presentation/http/router/noteSettings.test.ts index 8df0d878..9047f63e 100644 --- a/src/presentation/http/router/noteSettings.test.ts +++ b/src/presentation/http/router/noteSettings.test.ts @@ -1,5 +1,6 @@ import { describe, test, expect, beforeEach } from 'vitest'; import { MemberRole } from '@domain/entities/team.js'; +import { memberRightsTestData } from '@tests/utils/team-rights'; describe('NoteSettings API', () => { beforeEach(async () => { @@ -12,38 +13,7 @@ describe('NoteSettings API', () => { await global.db.truncateTables(); }); describe('GET /note-settings/:notePublicId ', () => { - test.each([ - /** Returns 401 when the user is not authorized */ - { - role: null, - isAuthorized: false, - expectedStatusCode: 401, - expectedMessage: 'You must be authenticated to access this resource', - }, - - /** Returns 200 if user is a team member with a Write role */ - { - role: MemberRole.Write, - isAuthorized: true, - expectedStatusCode: 200, - }, - - /** Returns 403 when user is a team member with a Read role */ - { - role: MemberRole.Read, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', - }, - - /** Returns 403 when user is not in the team */ - { - role: null, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', - }, - ]) + test.each(memberRightsTestData) ('Get note settings and team by public id', async ({ role, isAuthorized, expectedStatusCode, expectedMessage }) => { /** Create test user - creator of a note */ const creator = await global.db.insertUser(); @@ -156,38 +126,7 @@ describe('NoteSettings API', () => { }); describe('GET /note-settings/:notePublicId/team ', () => { - test.each([ - /** Returns 200 if user is a team member with a Write role */ - { - role: MemberRole.Write, - isAuthorized: true, - expectedStatusCode: 200, - }, - - /** Returns 403 if user is a team member with a Read role */ - { - role: MemberRole.Read, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', - }, - - /** Returns 401 when the user is not authorized */ - { - role: null, - isAuthorized: false, - expectedStatusCode: 401, - expectedMessage: 'You must be authenticated to access this resource', - }, - - /** Returns 403 when the the user is not in the team */ - { - role: null, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', - }, - ]) + test.each(memberRightsTestData) ('Get note team by public id', async ({ role, isAuthorized, expectedStatusCode, expectedMessage }) => { /** Create test user - creator of a note */ const creator = await global.db.insertUser(); @@ -282,35 +221,7 @@ describe('NoteSettings API', () => { }); describe('PATCH /note-settings/:notePublicId ', () => { - test.each([ - /** Returns 200 if user is a team member with a Write role */ - { - role: MemberRole.Write, - isAuthorized: true, - expectedStatusCode: 200, - }, - - /** Returns 403 if user is a team member with a Read role */ - { - role: MemberRole.Read, - isAuthorized: true, - expectedStatusCode: 403, - }, - - /** Returns 403 if user is not in the team */ - { - role: null, - isAuthorized: true, - expectedStatusCode: 403, - }, - - /** Returns 401 if user is not authorized */ - { - role: null, - isAuthorized: false, - expectedStatusCode: 401, - }, - ]) + test.each(memberRightsTestData) ('Update note settings by public id', async ({ role, isAuthorized, expectedStatusCode }) => { /** Create test user - creator of a note */ const creator = await global.db.insertUser(); @@ -399,35 +310,7 @@ describe('NoteSettings API', () => { }); describe('PATCH /note-settings/:notePublicId/invitation-hash ', () => { - test.each([ - /** Returns 200 if user is a team member with a Write role */ - { - role: MemberRole.Write, - isAuthorized: true, - expectedStatusCode: 200, - }, - - /** Returns 403 if user is a team member with a Read role */ - { - role: MemberRole.Read, - isAuthorized: true, - expectedStatusCode: 403, - }, - - /** Returns 403 if user is not in the team */ - { - role: null, - isAuthorized: true, - expectedStatusCode: 403, - }, - - /** Returns 401 if user is not authorized */ - { - role: null, - isAuthorized: false, - expectedStatusCode: 401, - }, - ]) + test.each(memberRightsTestData) ('Generate invitation hash', async ({ role, isAuthorized, expectedStatusCode }) => { /** Create test user - creator of a note */ const creator = await global.db.insertUser(); @@ -521,35 +404,7 @@ describe('NoteSettings API', () => { }); describe('PATCH /note-settings/:notePublicId/team', () => { - test.each([ - /** Returns 200 if user is a team member with a Write role */ - { - role: MemberRole.Write, - isAuthorized: true, - expectedStatusCode: 200, - }, - - /** Returns 403 if user is a team member with a Read role */ - { - role: MemberRole.Read, - isAuthorized: true, - expectedStatusCode: 403, - }, - - /** Returns 403 if user is not in the team */ - { - role: null, - isAuthorized: true, - expectedStatusCode: 403, - }, - - /** Returns 401 if user is not authorized */ - { - role: null, - isAuthorized: false, - expectedStatusCode: 401, - }, - ]) + test.each(memberRightsTestData) ('Update team member role by user id and note id', async ({ role, isAuthorized, expectedStatusCode }) => { /** Create test user - creator of a note */ const creator = await global.db.insertUser(); @@ -659,35 +514,7 @@ describe('NoteSettings API', () => { }); describe('DELETE /:notePublicId/team', () => { - test.each([ - /** Returns 200 if user is a team member with a Write role */ - { - role: MemberRole.Write, - isAuthorized: true, - expectedStatusCode: 200, - }, - - /** Returns 403 if user is a team member with a Read role */ - { - role: MemberRole.Read, - isAuthorized: true, - expectedStatusCode: 403, - }, - - /** Returns 403 if user is not in the team */ - { - role: null, - isAuthorized: true, - expectedStatusCode: 403, - }, - - /** Returns 401 if user is not authorized */ - { - role: null, - isAuthorized: false, - expectedStatusCode: 401, - }, - ]) + test.each(memberRightsTestData) ('Delete user from the team', async ( { role, isAuthorized, expectedStatusCode } ) => { const creator = await global.db.insertUser(); diff --git a/src/tests/utils/team-rights.ts b/src/tests/utils/team-rights.ts new file mode 100644 index 00000000..c4a16883 --- /dev/null +++ b/src/tests/utils/team-rights.ts @@ -0,0 +1,34 @@ +import { MemberRole } from '@domain/entities/team.js'; + +export let memberRightsTestData: any[] = [ + /** Returns 200 if user is team member with a Write role */ + { + role: MemberRole.Write, + isAuthorized: true, + expectedStatusCode: 200, + }, + + /** Returns 403 if user is team member with a Read role */ + { + role: MemberRole.Read, + isAuthorized: true, + expectedStatusCode: 403, + expectedMessage: 'Permission denied', + }, + + /** Returns 403 if user is not in the team */ + { + role: null, + isAuthorized: true, + expectedStatusCode: 403, + expectedMessage: 'Permission denied', + }, + + /** Returns 401 if user is not authorized */ + { + role: null, + isAuthorized: false, + expectedStatusCode: 401, + expectedMessage: 'You must be authenticated to access this resource', + }, +]; From cd76fc41c7a993e228a83fb2e1a6149ed8c38ff3 Mon Sep 17 00:00:00 2001 From: GoldenJaden <yaroslav-gusev-00@mail.ru> Date: Sat, 9 Mar 2024 20:59:45 +0300 Subject: [PATCH 2/3] Wrap test context --- src/presentation/http/router/note.test.ts | 15 +++++-- .../http/router/noteSettings.test.ts | 30 +++++++++++--- src/tests/utils/team-rights.ts | 40 +++++++++++-------- 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/presentation/http/router/note.test.ts b/src/presentation/http/router/note.test.ts index b75826a8..51ef3230 100644 --- a/src/presentation/http/router/note.test.ts +++ b/src/presentation/http/router/note.test.ts @@ -245,7 +245,10 @@ describe('Note API', () => { describe('PATCH note/:notePublicId ', () => { test.each(memberRightsTestData) - ('Patch note by public id', async ({ role, isAuthorized, expectedStatusCode, expectedMessage }) => { + ('Patch note by public id', async ({ testContext }) => { + /** Get data from context */ + const { role, isAuthorized, expectedStatusCode, expectedMessage } = testContext; + /** Only if user has a Write role, he can edit the note */ const canEdit = role === MemberRole.Write; @@ -430,7 +433,10 @@ describe('Note API', () => { describe('DELETE /note/:notePublicId', () => { test.each(memberRightsTestData) - ('Delete note by public id', async ({ role, isAuthorized, expectedStatusCode, expectedMessage }) => { + ('Delete note by public id', async ({ testContext }) => { + /** Get data from context */ + const { role, isAuthorized, expectedStatusCode, expectedMessage } = testContext; + /** Create test user - creator of note */ const creator = await global.db.insertUser(); @@ -614,7 +620,10 @@ describe('Note API', () => { accessToken = global.auth(user.id); }); test.each(memberRightsTestData) - ('Unlink any parent from note by it\'s public id', async ({ role, isAuthorized, expectedStatusCode, expectedMessage }) => { + ('Unlink any parent from note by it\'s public id', async ({ testContext }) => { + /** Get data from context */ + const { role, isAuthorized, expectedStatusCode, expectedMessage } = testContext; + /* Create second user, who will be the creator of the note */ const creator = await global.db.insertUser(); diff --git a/src/presentation/http/router/noteSettings.test.ts b/src/presentation/http/router/noteSettings.test.ts index 9047f63e..fc84662b 100644 --- a/src/presentation/http/router/noteSettings.test.ts +++ b/src/presentation/http/router/noteSettings.test.ts @@ -14,7 +14,10 @@ describe('NoteSettings API', () => { }); describe('GET /note-settings/:notePublicId ', () => { test.each(memberRightsTestData) - ('Get note settings and team by public id', async ({ role, isAuthorized, expectedStatusCode, expectedMessage }) => { + ('Get note settings and team by public id', async ({ testContext }) => { + /** Get data from context */ + const { role, isAuthorized, expectedStatusCode, expectedMessage } = testContext; + /** Create test user - creator of a note */ const creator = await global.db.insertUser(); @@ -127,7 +130,10 @@ describe('NoteSettings API', () => { describe('GET /note-settings/:notePublicId/team ', () => { test.each(memberRightsTestData) - ('Get note team by public id', async ({ role, isAuthorized, expectedStatusCode, expectedMessage }) => { + ('Get note team by public id', async ({ testContext }) => { + /** Get data from context */ + const { role, isAuthorized, expectedStatusCode, expectedMessage } = testContext; + /** Create test user - creator of a note */ const creator = await global.db.insertUser(); @@ -222,7 +228,10 @@ describe('NoteSettings API', () => { describe('PATCH /note-settings/:notePublicId ', () => { test.each(memberRightsTestData) - ('Update note settings by public id', async ({ role, isAuthorized, expectedStatusCode }) => { + ('Update note settings by public id', async ({ testContext }) => { + /** Get data from context */ + const { role, isAuthorized, expectedStatusCode } = testContext; + /** Create test user - creator of a note */ const creator = await global.db.insertUser(); @@ -311,7 +320,10 @@ describe('NoteSettings API', () => { describe('PATCH /note-settings/:notePublicId/invitation-hash ', () => { test.each(memberRightsTestData) - ('Generate invitation hash', async ({ role, isAuthorized, expectedStatusCode }) => { + ('Generate invitation hash', async ({ testContext }) => { + /** Get data from context */ + const { role, isAuthorized, expectedStatusCode } = testContext; + /** Create test user - creator of a note */ const creator = await global.db.insertUser(); @@ -405,7 +417,10 @@ describe('NoteSettings API', () => { describe('PATCH /note-settings/:notePublicId/team', () => { test.each(memberRightsTestData) - ('Update team member role by user id and note id', async ({ role, isAuthorized, expectedStatusCode }) => { + ('Update team member role by user id and note id', async ({ testContext }) => { + /** Get data from context */ + const { role, isAuthorized, expectedStatusCode } = testContext; + /** Create test user - creator of a note */ const creator = await global.db.insertUser(); @@ -515,7 +530,10 @@ describe('NoteSettings API', () => { describe('DELETE /:notePublicId/team', () => { test.each(memberRightsTestData) - ('Delete user from the team', async ( { role, isAuthorized, expectedStatusCode } ) => { + ('Delete user from the team', async ({ testContext }) => { + /** Get data from context */ + const { role, isAuthorized, expectedStatusCode } = testContext; + const creator = await global.db.insertUser(); const user = await global.db.insertUser(); diff --git a/src/tests/utils/team-rights.ts b/src/tests/utils/team-rights.ts index c4a16883..d5379243 100644 --- a/src/tests/utils/team-rights.ts +++ b/src/tests/utils/team-rights.ts @@ -1,34 +1,42 @@ import { MemberRole } from '@domain/entities/team.js'; -export let memberRightsTestData: any[] = [ +export const memberRightsTestData = [ /** Returns 200 if user is team member with a Write role */ { - role: MemberRole.Write, - isAuthorized: true, - expectedStatusCode: 200, + testContext: { + role: MemberRole.Write, + isAuthorized: true, + expectedStatusCode: 200, + }, }, /** Returns 403 if user is team member with a Read role */ { - role: MemberRole.Read, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', + testContext: { + role: MemberRole.Read, + isAuthorized: true, + expectedStatusCode: 403, + expectedMessage: 'Permission denied', + }, }, /** Returns 403 if user is not in the team */ { - role: null, - isAuthorized: true, - expectedStatusCode: 403, - expectedMessage: 'Permission denied', + testContext:{ + role: null, + isAuthorized: true, + expectedStatusCode: 403, + expectedMessage: 'Permission denied', + }, }, /** Returns 401 if user is not authorized */ { - role: null, - isAuthorized: false, - expectedStatusCode: 401, - expectedMessage: 'You must be authenticated to access this resource', + testContext: { + role: null, + isAuthorized: false, + expectedStatusCode: 401, + expectedMessage: 'You must be authenticated to access this resource', + }, }, ]; From e8dbc8f00acda106af01a3133a6d178783fcb02f Mon Sep 17 00:00:00 2001 From: GoldenJaden <yaroslav-gusev-00@mail.ru> Date: Sat, 9 Mar 2024 21:09:11 +0300 Subject: [PATCH 3/3] Rename memberRightsTestData to memberRight --- src/presentation/http/router/note.test.ts | 8 ++++---- src/presentation/http/router/noteSettings.test.ts | 14 +++++++------- src/tests/utils/team-rights.ts | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/presentation/http/router/note.test.ts b/src/presentation/http/router/note.test.ts index 51ef3230..7e7231c8 100644 --- a/src/presentation/http/router/note.test.ts +++ b/src/presentation/http/router/note.test.ts @@ -1,7 +1,7 @@ import { MemberRole } from '@domain/entities/team.js'; import { describe, test, expect, beforeEach } from 'vitest'; import type User from '@domain/entities/user.js'; -import { memberRightsTestData } from '@tests/utils/team-rights'; +import { memberRight } from '@tests/utils/team-rights'; describe('Note API', () => { beforeEach(async () => { @@ -244,7 +244,7 @@ describe('Note API', () => { }); describe('PATCH note/:notePublicId ', () => { - test.each(memberRightsTestData) + test.each(memberRight) ('Patch note by public id', async ({ testContext }) => { /** Get data from context */ const { role, isAuthorized, expectedStatusCode, expectedMessage } = testContext; @@ -432,7 +432,7 @@ describe('Note API', () => { }); describe('DELETE /note/:notePublicId', () => { - test.each(memberRightsTestData) + test.each(memberRight) ('Delete note by public id', async ({ testContext }) => { /** Get data from context */ const { role, isAuthorized, expectedStatusCode, expectedMessage } = testContext; @@ -619,7 +619,7 @@ describe('Note API', () => { accessToken = global.auth(user.id); }); - test.each(memberRightsTestData) + test.each(memberRight) ('Unlink any parent from note by it\'s public id', async ({ testContext }) => { /** Get data from context */ const { role, isAuthorized, expectedStatusCode, expectedMessage } = testContext; diff --git a/src/presentation/http/router/noteSettings.test.ts b/src/presentation/http/router/noteSettings.test.ts index fc84662b..5470f5cc 100644 --- a/src/presentation/http/router/noteSettings.test.ts +++ b/src/presentation/http/router/noteSettings.test.ts @@ -1,6 +1,6 @@ import { describe, test, expect, beforeEach } from 'vitest'; import { MemberRole } from '@domain/entities/team.js'; -import { memberRightsTestData } from '@tests/utils/team-rights'; +import { memberRight } from '@tests/utils/team-rights'; describe('NoteSettings API', () => { beforeEach(async () => { @@ -13,7 +13,7 @@ describe('NoteSettings API', () => { await global.db.truncateTables(); }); describe('GET /note-settings/:notePublicId ', () => { - test.each(memberRightsTestData) + test.each(memberRight) ('Get note settings and team by public id', async ({ testContext }) => { /** Get data from context */ const { role, isAuthorized, expectedStatusCode, expectedMessage } = testContext; @@ -129,7 +129,7 @@ describe('NoteSettings API', () => { }); describe('GET /note-settings/:notePublicId/team ', () => { - test.each(memberRightsTestData) + test.each(memberRight) ('Get note team by public id', async ({ testContext }) => { /** Get data from context */ const { role, isAuthorized, expectedStatusCode, expectedMessage } = testContext; @@ -227,7 +227,7 @@ describe('NoteSettings API', () => { }); describe('PATCH /note-settings/:notePublicId ', () => { - test.each(memberRightsTestData) + test.each(memberRight) ('Update note settings by public id', async ({ testContext }) => { /** Get data from context */ const { role, isAuthorized, expectedStatusCode } = testContext; @@ -319,7 +319,7 @@ describe('NoteSettings API', () => { }); describe('PATCH /note-settings/:notePublicId/invitation-hash ', () => { - test.each(memberRightsTestData) + test.each(memberRight) ('Generate invitation hash', async ({ testContext }) => { /** Get data from context */ const { role, isAuthorized, expectedStatusCode } = testContext; @@ -416,7 +416,7 @@ describe('NoteSettings API', () => { }); describe('PATCH /note-settings/:notePublicId/team', () => { - test.each(memberRightsTestData) + test.each(memberRight) ('Update team member role by user id and note id', async ({ testContext }) => { /** Get data from context */ const { role, isAuthorized, expectedStatusCode } = testContext; @@ -529,7 +529,7 @@ describe('NoteSettings API', () => { }); describe('DELETE /:notePublicId/team', () => { - test.each(memberRightsTestData) + test.each(memberRight) ('Delete user from the team', async ({ testContext }) => { /** Get data from context */ const { role, isAuthorized, expectedStatusCode } = testContext; diff --git a/src/tests/utils/team-rights.ts b/src/tests/utils/team-rights.ts index d5379243..c9340503 100644 --- a/src/tests/utils/team-rights.ts +++ b/src/tests/utils/team-rights.ts @@ -1,6 +1,6 @@ import { MemberRole } from '@domain/entities/team.js'; -export const memberRightsTestData = [ +export const memberRight = [ /** Returns 200 if user is team member with a Write role */ { testContext: {