diff --git a/.editorconfig b/.editorconfig index 37443fc..32eaf39 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,7 +3,7 @@ # top-most EditorConfig file root = true -[*.{js, ts}] +[*.{js, ts, json}] indent_style = space indent_size = 2 end_of_line = lf diff --git a/.eslintrc.json b/.eslintrc.json index ba3b7a6..1623b22 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,28 +1,28 @@ { - "env": { - "es2021": true, - "node": true - }, - "extends": [ - "eslint:recommended", - "plugin:@typescript-eslint/recommended" - ], - "parser": "@typescript-eslint/parser", - "parserOptions": { - "ecmaVersion": "latest", - "sourceType": "module" - }, - "plugins": [ - "@typescript-eslint" - ], - "settings": { - "import/resolver": { - "typescript": { - "project": "./tsconfig.json" - } - } - }, - "rules": { - "@typescript-eslint/no-explicit-any": "off" + "env": { + "es2021": true, + "node": true + }, + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended" + ], + "parser": "@typescript-eslint/parser", + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module" + }, + "plugins": [ + "@typescript-eslint" + ], + "settings": { + "import/resolver": { + "typescript": { + "project": "./tsconfig.json" + } } + }, + "rules": { + "@typescript-eslint/no-explicit-any": "off" + } } \ No newline at end of file diff --git a/src/api/routers/users.e2e.test.ts b/src/api/routers/users.e2e.test.ts index fa4cf89..86a832b 100644 --- a/src/api/routers/users.e2e.test.ts +++ b/src/api/routers/users.e2e.test.ts @@ -1,7 +1,7 @@ import { faker } from "@faker-js/faker/locale/pt_BR"; describe('Users endpoints', () => { - afterAll(async () => { + afterEach(async () => { await globalThis.db_client.query('DELETE FROM users'); }); @@ -86,4 +86,30 @@ describe('Users endpoints', () => { expect(response.body.message).toEqual('User not found'); }); }); + + describe('GET: /users', () => { + beforeAll(async () => { + await globalThis.db_client.query( + 'INSERT INTO users (id, email, name, password, phone_number) VALUES ($1, $2, $3, $4, $5)', + [faker.string.uuid(), faker.internet.email(), faker.person.fullName(), faker.string.alphanumeric(10), faker.string.numeric(11)] + ); + + await globalThis.db_client.query( + 'INSERT INTO users (id, email, name, password, phone_number) VALUES ($1, $2, $3, $4, $5)', + [faker.string.uuid(), faker.internet.email(), faker.person.fullName(), faker.string.alphanumeric(10), faker.string.numeric(11)] + ); + }); + + it('returns an array of users', async () => { + expect.assertions(2); + + const response = await globalThis.request + .get('/api/users') + .set('Content-Type', 'application/json') + .send(); + + expect(response.status).toEqual(200); + expect(response.body).toHaveLength(2); + }); + }); }); \ No newline at end of file diff --git a/src/api/routers/users.ts b/src/api/routers/users.ts index b6729b5..96ac690 100644 --- a/src/api/routers/users.ts +++ b/src/api/routers/users.ts @@ -2,13 +2,13 @@ import validator from '@api/middlewares/validator'; import BcryptEncryptor from '@b3_stock_alerts/BcryptEncryptor'; import PgUserRepository from '@b3_stock_alerts/PgUserRepository'; import UserService from '@b3_stock_alerts/UserService'; +import NotFoundError from '@shared/NotFoundError'; import { NextFunction, Request, Response, Router } from 'express'; import { checkSchema } from 'express-validator'; import { create_user } from './validations'; -import NotFoundError from '@shared/NotFoundError'; const router = Router(); -const userService = new UserService(new PgUserRepository(), new BcryptEncryptor()); +const user_service = new UserService(new PgUserRepository(), new BcryptEncryptor()); router.post('/users', checkSchema(create_user), @@ -22,7 +22,7 @@ router.post('/users', password } = request.body; - const result = await userService.createUser({ name, email, phone_number, password }); + const result = await user_service.createUser({ name, email, phone_number, password }); if (result.data) { return response.status(201).json(result.data); @@ -35,7 +35,7 @@ router.post('/users', router.get('/users/:id', async (request: Request, response: Response, next: NextFunction) => { try { - const result = await userService.getUser(request.params.id); + const result = await user_service.getUser(request.params.id); if (result.error instanceof NotFoundError) { return response.status(404).json({ message: result.error.message }); } @@ -46,9 +46,11 @@ router.get('/users/:id', async (request: Request, response: Response, next: Next } }); -router.get('/users', (_request: Request, response: Response, next: NextFunction) => { +router.get('/users', async (_: Request, response: Response, next: NextFunction) => { try { - return response.status(204).json(); + const result = await user_service.listUsers(); + + return response.status(200).json(result.data); } catch (e) { return next(e); }