Skip to content

Commit

Permalink
Merge pull request #104 from codex-team/auth-method-tests
Browse files Browse the repository at this point in the history
feat(tests) : auth method added
  • Loading branch information
e11sy authored Nov 6, 2023
2 parents 563227e + a149b00 commit 01cb4c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(userId)` for getting accessToken, which you can use in:
```
headers: {
authorization: `Bearer ${accessToken}`
}
```
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.
API test file shoul follow such structure:
Expand Down
12 changes: 12 additions & 0 deletions src/tests/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 userId - id of the user that will be considered the author of the request
* @returns accessToken for authorization
*/
function auth(userId: number) : string;
}

/**
Expand All @@ -50,6 +59,9 @@ beforeAll(async () => {
await insertData(orm);

global.api = api;
global.auth = (userId: number) => {
return domainServices.authService.signAccessToken({ id : userId });
};
}, TIMEOUT);

afterAll(async () => {
Expand Down

0 comments on commit 01cb4c9

Please sign in to comment.