diff --git a/package-lock.json b/package-lock.json index 40ef707..aa52d91 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ "@commitlint/cli": "21.0.1", "@commitlint/config-conventional": "21.0.1", "@eslint/js": "^9.39.4", + "@faker-js/faker": "9.7.0", "@next/eslint-plugin-next": "^16.2.6", "commitizen": "4.3.1", "concurrently": "9.2.1", @@ -1047,6 +1048,23 @@ "node": "^18.18.0 || ^20.9.0 || >=21.1.0" } }, + "node_modules/@faker-js/faker": { + "version": "9.7.0", + "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-9.7.0.tgz", + "integrity": "sha512-aozo5vqjCmDoXLNUJarFZx2IN/GgGaogY4TMJ6so/WLZOWpSV7fvj2dmrV6sEAnUm1O7aCrhTibjpzeDFgNqbg==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/fakerjs" + } + ], + "license": "MIT", + "engines": { + "node": ">=18.0.0", + "npm": ">=9.0.0" + } + }, "node_modules/@humanfs/core": { "version": "0.19.2", "resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.2.tgz", diff --git a/package.json b/package.json index 956f7d6..add4544 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "@commitlint/cli": "21.0.1", "@commitlint/config-conventional": "21.0.1", "@eslint/js": "^9.39.4", + "@faker-js/faker": "9.7.0", "@next/eslint-plugin-next": "^16.2.6", "commitizen": "4.3.1", "concurrently": "9.2.1", diff --git a/teste/integration/api/v1/orchestrator.js b/teste/integration/api/v1/orchestrator.js index dc29ac1..3d31804 100644 --- a/teste/integration/api/v1/orchestrator.js +++ b/teste/integration/api/v1/orchestrator.js @@ -1,6 +1,9 @@ import retry from "async-retry"; +import { faker } from "@faker-js/faker"; + import database from "infra/database"; import migrator from "models/migrator"; +import user from "models/user"; async function waitForAllServices() { await waitForWebServer(); @@ -30,10 +33,20 @@ async function runPendingMigrations() { await migrator.runPendingMigrations(); } +async function createUser(userObject) { + return await user.create({ + username: + userObject.username || faker.internet.username().replace(/[_.-]/g, ""), + email: userObject.email || faker.internet.email(), + password: userObject.password || "validpassword", + }); +} + const orchestrator = { waitForAllServices, clearDatabase, runPendingMigrations, + createUser, }; export default orchestrator; diff --git a/teste/integration/api/v1/users/[username]/get.test.js b/teste/integration/api/v1/users/[username]/get.test.js index 8522e25..abe72c7 100644 --- a/teste/integration/api/v1/users/[username]/get.test.js +++ b/teste/integration/api/v1/users/[username]/get.test.js @@ -10,20 +10,12 @@ beforeAll(async () => { describe("GET to /api/v1/users/[username]", () => { describe("Anonymous user", () => { test("With exact case match", async () => { - const response1 = await fetch("http://localhost:3000/api/v1/users", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username: "MesmoCase", - email: "mesmo.case@gmail.com", - password: "senha123", - }), + await orchestrator.createUser({ + username: "MesmoCase", + email: "mesmo.case@gmail.com", + password: "senha123", }); - expect(response1.status).toBe(201); - const response2 = await fetch( "http://localhost:3000/api/v1/users/MesmoCase", ); @@ -46,20 +38,12 @@ describe("GET to /api/v1/users/[username]", () => { }); test("With case mismatch", async () => { - const response1 = await fetch("http://localhost:3000/api/v1/users", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username: "CaseDiferente", - email: "case.diferente@gmail.com", - password: "senha123", - }), + await orchestrator.createUser({ + username: "CaseDiferente", + email: "case.diferente@gmail.com", + password: "senha123", }); - expect(response1.status).toBe(201); - const response2 = await fetch( "http://localhost:3000/api/v1/users/casediferente", ); diff --git a/teste/integration/api/v1/users/[username]/patch.test.js b/teste/integration/api/v1/users/[username]/patch.test.js index 87b13b3..a5bc93b 100644 --- a/teste/integration/api/v1/users/[username]/patch.test.js +++ b/teste/integration/api/v1/users/[username]/patch.test.js @@ -31,34 +31,14 @@ describe("PATCH to /api/v1/users/[username]", () => { }); test("With duplicated 'username'", async () => { - const user1Response = await fetch("http://localhost:3000/api/v1/users", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username: "user1", - email: "user1@gmail.com", - password: "senha123", - }), + await orchestrator.createUser({ + username: "user1", }); - expect(user1Response.status).toBe(201); - - const user2Response = await fetch("http://localhost:3000/api/v1/users", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username: "user2", - email: "user2@gmail.com", - password: "senha123", - }), + await orchestrator.createUser({ + username: "user2", }); - expect(user2Response.status).toBe(201); - const response = await fetch("http://localhost:3000/api/v1/users/user2", { method: "PATCH", headers: { @@ -81,36 +61,16 @@ describe("PATCH to /api/v1/users/[username]", () => { }); test("With duplicated 'email'", async () => { - const user1Response = await fetch("http://localhost:3000/api/v1/users", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username: "email1", - email: "email1@gmail.com", - password: "senha123", - }), + await orchestrator.createUser({ + email: "email1@gmail.com", }); - expect(user1Response.status).toBe(201); - - const user2Response = await fetch("http://localhost:3000/api/v1/users", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username: "email2", - email: "email2@gmail.com", - password: "senha123", - }), + const createUser2 = await orchestrator.createUser({ + email: "email2@gmail.com", }); - expect(user2Response.status).toBe(201); - const response = await fetch( - "http://localhost:3000/api/v1/users/email2", + `http://localhost:3000/api/v1/users/${createUser2.username}`, { method: "PATCH", headers: { @@ -134,20 +94,10 @@ describe("PATCH to /api/v1/users/[username]", () => { }); test("With unique 'username'", async () => { - const user1Response = await fetch("http://localhost:3000/api/v1/users", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username: "uniqueUser1", - email: "uniqueUser1@gmail.com", - password: "senha123", - }), + const createUser = await orchestrator.createUser({ + username: "uniqueUser1", }); - expect(user1Response.status).toBe(201); - const response = await fetch( "http://localhost:3000/api/v1/users/uniqueUser1", { @@ -168,7 +118,7 @@ describe("PATCH to /api/v1/users/[username]", () => { expect(responseBody).toEqual({ id: responseBody.id, username: "uniqueUser2", - email: "uniqueUser1@gmail.com", + email: createUser.email, password: responseBody.password, created_at: responseBody.created_at, update_at: responseBody.update_at, @@ -182,22 +132,12 @@ describe("PATCH to /api/v1/users/[username]", () => { }); test("With unique 'email'", async () => { - const user1Response = await fetch("http://localhost:3000/api/v1/users", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username: "uniqueEmail1", - email: "uniqueEmail1@gmail.com", - password: "senha123", - }), + const createUser = await orchestrator.createUser({ + email: "uniqueEmail1@gmail.com", }); - expect(user1Response.status).toBe(201); - const response = await fetch( - "http://localhost:3000/api/v1/users/uniqueEmail1", + `http://localhost:3000/api/v1/users/${createUser.username}`, { method: "PATCH", headers: { @@ -215,7 +155,7 @@ describe("PATCH to /api/v1/users/[username]", () => { expect(responseBody).toEqual({ id: responseBody.id, - username: "uniqueEmail1", + username: createUser.username, email: "uniqueEmail2@gmail.com", password: responseBody.password, created_at: responseBody.created_at, @@ -230,22 +170,12 @@ describe("PATCH to /api/v1/users/[username]", () => { }); test("With new 'password'", async () => { - const user1Response = await fetch("http://localhost:3000/api/v1/users", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username: "newPassword1", - email: "newPassword1@gmail.com", - password: "newPassword1", - }), + const createUser = await orchestrator.createUser({ + password: "newPassword1", }); - expect(user1Response.status).toBe(201); - const response = await fetch( - "http://localhost:3000/api/v1/users/newPassword1", + `http://localhost:3000/api/v1/users/${createUser.username}`, { method: "PATCH", headers: { @@ -263,8 +193,8 @@ describe("PATCH to /api/v1/users/[username]", () => { expect(responseBody).toEqual({ id: responseBody.id, - username: "newPassword1", - email: "newPassword1@gmail.com", + username: createUser.username, + email: createUser.email, password: responseBody.password, created_at: responseBody.created_at, update_at: responseBody.update_at, @@ -276,14 +206,14 @@ describe("PATCH to /api/v1/users/[username]", () => { expect(responseBody.update_at > responseBody.created_at).toBe(true); - const userInDatabase = await user.findOneByUsername("newPassword1"); + const userInDatabase = await user.findOneByUsername(createUser.username); const correctPasswordMatch = await password.compare( "newPassword2", userInDatabase.password, ); const incorrectPasswordMatch = await password.compare( - "newPassword1", + createUser.username, userInDatabase.password, );