Skip to content

Commit 2f2232b

Browse files
committed
docs: updating docs
1 parent f3407df commit 2f2232b

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

README.md

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,58 @@
22

33
# Objectives
44

5-
The main objective of this project is to keep it as a knowledge base about backend testing, including tips and test examples, covering the majority of the most common use cases with unit, integration, and end-to-end tests.
5+
The main objective of this project is to be kept as a knowledge base about backend testing, including tips and test examples, covering the majority of the most common use cases with unit, integration, and end-to-end tests.
6+
7+
<br>
8+
9+
**NOTES** - This repository was refactored (10 Jan 2023) to add more complexity to the tests and to serve as backend to the Cypress tests repository. If you need unit tests, check an older version for it (before 1 Dez 2022. I will add new unit tests when i have time)
10+
11+
In the refactor i've added an AuthenticationGuard to the UsersController, forcing the consumer to be authenticated to retrieve the users data. The end-to-end tests in this api now have files called usecases (AuthenticationUsecases and UsersUsecases). These usecases are like lego pieces that can be assembled to create multiple test scenarios with ease.
12+
13+
<br>
14+
15+
```
16+
export const authenticatedUserData = makeSignupRequestData()
17+
export let loginResult: LoginResultDto = undefined
18+
19+
export const userSignup = async (requestData?: any) => {
20+
const data = requestData ?? authenticatedUserData
21+
return await request(server).post('/signup').send(data)
22+
}
23+
24+
export const userLogin = async (requestData?: any) => {
25+
const data = requestData ?? { email: authenticatedUserData.email, password: authenticatedUserData.password }
26+
return await request(server).post('/login').send(data)
27+
}
28+
29+
// Used for any test that requires a registered and logged in user to work
30+
export const setupAuthenticatedTestData = async () => {
31+
await initServer()
32+
await userSignup()
33+
const login = await userLogin()
34+
loginResult = login.body
35+
await app.close()
36+
}
37+
38+
```
39+
40+
41+
And this setup can be used as follows
42+
43+
44+
```
45+
describe('Authentication End-To-End Tests', () => {
46+
beforeEach(async () => {
47+
await setupAuthenticatedTestData()
48+
})
49+
50+
```
651

752
</br>
853

954
**Application**
1055

11-
The application is an authentication API with studies purpose only, where you call a third party authentication service (scr/infrastructure/AuthenticationClient). A fake service was made for the authentication client using an Array of Objects. This app has an authentication route (/register), where a new account can be created and a route to retrieve all accounts (/getAllAccounts).
56+
The application is an authentication API with studies purpose only, where you call a third party authentication service (scr/infrastructure/AuthenticationClient). A fake service was made for the authentication client using an Array of Objects. This app has an authentication route (/signup), where a new account can be created, a login route (/login) and two routes, one to retrieve all accounts (/users) and one to retrieve a single account (/users/:id).
1257

1358

1459
## Structure
@@ -41,29 +86,25 @@ Test files:
4186
### To reach the endpoints:
4287

4388
1. If youre using vsCode, install the extension REST Client : https://marketplace.visualstudio.com/items?itemName=humao.rest-client
44-
1. go to ``src/modules/authentication/authentication.http`` and send the requests
45-
89+
1. go to ``src/modules/authentication/presentation/authentication.http`` and send the requests
90+
1. You must create an account and log into the system to make requests in the users module.
91+
- ``src/modules/users/presentation/users.http``
4692

4793
```
48-
POST http://localhost:3003/register
94+
POST http://localhost:3003/signup
4995
content-type: application/json
5096
5197
{
5298
"name": "Test name",
5399
"email": "email@email.com",
100+
"password": "12345",
54101
"age": 9,
55102
"address": {
56103
"country": "BR",
57104
"zipCode": "99999991"
58105
}
59106
}
60107
61-
###
62-
63-
GET http://localhost:3003/getAllAccounts
64-
content-type: application/json
65-
66-
{}
67108
```
68109

69110
</br>

tests/e2e/authentication/AuthenticationUsecases.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const userLogin = async (requestData?: any) => {
1616
return await request(server).post('/login').send(data)
1717
}
1818

19-
// Used for every test who need a registered and loged in user to work
19+
// Used for any test that requires a registered and logged in user to work
2020
export const setupAuthenticatedTestData = async () => {
2121
await initServer()
2222
await userSignup()
@@ -25,18 +25,16 @@ export const setupAuthenticatedTestData = async () => {
2525
await app.close()
2626
}
2727

28-
const FORBIDDEN_RESOURCE = 'Forbidden resource'
2928

3029
/**
3130
* Check if the route is protected by @UseGuards(AuthenticationGuard)
32-
*
3331
*
3432
*/
3533
export function routeHasAuthenticationGuardTest(execSut: any): void {
3634
test('should return **Bad Request** if the loginToken is wrong.', async () => {
3735
const { status, body } = await execSut({ id: loginResult.id, loginToken: 'wrong_token' })
3836

39-
expect(body.message).toBe(FORBIDDEN_RESOURCE)
37+
expect(body.message).toBe('Forbidden resource')
4038
expect(status).toBe(403)
4139
})
4240
}

0 commit comments

Comments
 (0)