|
2 | 2 |
|
3 | 3 | # Objectives
|
4 | 4 |
|
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 | +``` |
6 | 51 |
|
7 | 52 | </br>
|
8 | 53 |
|
9 | 54 | **Application**
|
10 | 55 |
|
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). |
12 | 57 |
|
13 | 58 |
|
14 | 59 | ## Structure
|
@@ -41,29 +86,25 @@ Test files:
|
41 | 86 | ### To reach the endpoints:
|
42 | 87 |
|
43 | 88 | 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`` |
46 | 92 |
|
47 | 93 | ```
|
48 |
| -POST http://localhost:3003/register |
| 94 | +POST http://localhost:3003/signup |
49 | 95 | content-type: application/json
|
50 | 96 |
|
51 | 97 | {
|
52 | 98 | "name": "Test name",
|
53 | 99 | "email": "email@email.com",
|
| 100 | + "password": "12345", |
54 | 101 | "age": 9,
|
55 | 102 | "address": {
|
56 | 103 | "country": "BR",
|
57 | 104 | "zipCode": "99999991"
|
58 | 105 | }
|
59 | 106 | }
|
60 | 107 |
|
61 |
| -### |
62 |
| -
|
63 |
| -GET http://localhost:3003/getAllAccounts |
64 |
| -content-type: application/json |
65 |
| -
|
66 |
| -{} |
67 | 108 | ```
|
68 | 109 |
|
69 | 110 | </br>
|
|
0 commit comments