From 2f5ea3021be10ab9990dd1657d16fc79cb12952b Mon Sep 17 00:00:00 2001 From: e11sy Date: Sun, 5 Nov 2023 22:26:58 +0300 Subject: [PATCH 1/3] feat(tests) : auth method added - new Globally exposed method for creating accessToken using id (global.auth) --- src/tests/utils/setup.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/tests/utils/setup.ts b/src/tests/utils/setup.ts index c445c631..00fb68af 100644 --- a/src/tests/utils/setup.ts +++ b/src/tests/utils/setup.ts @@ -25,6 +25,15 @@ declare global { */ /* eslint-disable-next-line no-var */ var api: Api | undefined; + + /** + * Globally exposed method for creating accessToken using id + * Is accessed as 'global.server' in tests + * + * @param id - id for making accessToken + * @returns accessToken for authorization + */ + function auth(id: number) : string; } /** @@ -50,6 +59,9 @@ beforeAll(async () => { await insertData(orm); global.api = api; + global.auth = (id: number) => { + return domainServices.authService.signAccessToken({ id : id }); + }; }, TIMEOUT); afterAll(async () => { From 0a998e8288faf54d40785048c6976380b5e3c617 Mon Sep 17 00:00:00 2001 From: e11sy Date: Sun, 5 Nov 2023 22:35:42 +0300 Subject: [PATCH 2/3] chore(tests): readme info added - added description of global.auth method into readme --- src/tests/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/tests/README.md b/src/tests/README.md index b0c5e08f..e0ce1379 100644 --- a/src/tests/README.md +++ b/src/tests/README.md @@ -8,6 +8,15 @@ File `src/tests/utils/setup.ts` does necessary preparations to make in possible ## Test data Test data lies in `src/tests/test-data/` directory. If you are missing some data for your tests, fill free to modify existing files or add new ones. Just remember to support relations between data that is usually provided by foreign key mechanism. +## Authorization in test +Use `global.auth(id)` for getting accessToken, which you can use in: +``` +headers: { + authorization: `Bearer ${accessToken}` +} +``` +in your fake rewuests + ## Writing tests Please, locate a test file near the file that it tests. Make sure test file name is the same as tested file name, but with `.test` suffix. API test file shoul follow such structure: From a149b00dc6bfd30ad9c2a0a817819fc3875c4ebc Mon Sep 17 00:00:00 2001 From: e11sy Date: Sun, 5 Nov 2023 22:45:39 +0300 Subject: [PATCH 3/3] chore(tests): rename variable - renamed variable from id to userId - typo fixed --- src/tests/README.md | 4 ++-- src/tests/utils/setup.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/tests/README.md b/src/tests/README.md index e0ce1379..1c048b72 100644 --- a/src/tests/README.md +++ b/src/tests/README.md @@ -9,13 +9,13 @@ File `src/tests/utils/setup.ts` does necessary preparations to make in possible Test data lies in `src/tests/test-data/` directory. If you are missing some data for your tests, fill free to modify existing files or add new ones. Just remember to support relations between data that is usually provided by foreign key mechanism. ## Authorization in test -Use `global.auth(id)` for getting accessToken, which you can use in: +Use `global.auth(userId)` for getting accessToken, which you can use in: ``` headers: { authorization: `Bearer ${accessToken}` } ``` -in your fake rewuests +in your fake requests ## Writing tests Please, locate a test file near the file that it tests. Make sure test file name is the same as tested file name, but with `.test` suffix. diff --git a/src/tests/utils/setup.ts b/src/tests/utils/setup.ts index 00fb68af..c9be8c95 100644 --- a/src/tests/utils/setup.ts +++ b/src/tests/utils/setup.ts @@ -30,10 +30,10 @@ declare global { * Globally exposed method for creating accessToken using id * Is accessed as 'global.server' in tests * - * @param id - id for making accessToken + * @param userId - id of the user that will be considered the author of the request * @returns accessToken for authorization */ - function auth(id: number) : string; + function auth(userId: number) : string; } /** @@ -59,8 +59,8 @@ beforeAll(async () => { await insertData(orm); global.api = api; - global.auth = (id: number) => { - return domainServices.authService.signAccessToken({ id : id }); + global.auth = (userId: number) => { + return domainServices.authService.signAccessToken({ id : userId }); }; }, TIMEOUT);