From 6dec22a67a6dd492481d05c8a882d82fa54026cc Mon Sep 17 00:00:00 2001 From: Alisson Pereira Date: Sat, 23 Aug 2025 19:36:07 -0300 Subject: [PATCH 1/2] build: install @faker-js/faker --- package-lock.json | 17 +++++++++++++++++ package.json | 1 + 2 files changed, 18 insertions(+) diff --git a/package-lock.json b/package-lock.json index 8195d88..515cf2f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,7 @@ "devDependencies": { "@commitlint/cli": "19.3.0", "@commitlint/config-conventional": "19.2.2", + "@faker-js/faker": "9.7.0", "commitizen": "4.3.0", "concurrently": "8.2.2", "cz-conventional-changelog": "3.3.0", @@ -1003,6 +1004,22 @@ "node": "^12.22.0 || ^14.17.0 || >=16.0.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" + } + ], + "engines": { + "node": ">=18.0.0", + "npm": ">=9.0.0" + } + }, "node_modules/@humanwhocodes/config-array": { "version": "0.11.14", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.14.tgz", diff --git a/package.json b/package.json index fb86f8a..97e75c6 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "devDependencies": { "@commitlint/cli": "19.3.0", "@commitlint/config-conventional": "19.2.2", + "@faker-js/faker": "9.7.0", "commitizen": "4.3.0", "concurrently": "8.2.2", "cz-conventional-changelog": "3.3.0", From 16ca759eaa989b154d17706f51f368d4dbfd701e Mon Sep 17 00:00:00 2001 From: Alisson Pereira Date: Sat, 23 Aug 2025 19:38:58 -0300 Subject: [PATCH 2/2] feat: create and use orchestrator.createUser() --- .../api/v1/users/[username]/patch.test.js | 117 ++++-------------- tests/orchestrator.js | 13 ++ 2 files changed, 35 insertions(+), 95 deletions(-) diff --git a/tests/integration/api/v1/users/[username]/patch.test.js b/tests/integration/api/v1/users/[username]/patch.test.js index 3a252ac..80fd4bc 100644 --- a/tests/integration/api/v1/users/[username]/patch.test.js +++ b/tests/integration/api/v1/users/[username]/patch.test.js @@ -32,34 +32,14 @@ describe("PATCH /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@curso.dev", - 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@curso.dev", - 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: { @@ -83,36 +63,16 @@ describe("PATCH /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@curso.dev", - password: "senha123", - }), + await orchestrator.createUser({ + email: "email1@curso.dev", }); - 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@curso.dev", - password: "senha123", - }), + const createdUser2 = await orchestrator.createUser({ + email: "email2@curso.dev", }); - expect(user2Response.status).toBe(201); - const response = await fetch( - "http://localhost:3000/api/v1/users/email2", + `http://localhost:3000/api/v1/users/${createdUser2.username}`, { method: "PATCH", headers: { @@ -137,22 +97,10 @@ describe("PATCH /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@curso.dev", - password: "senha123", - }), - }); - - expect(user1Response.status).toBe(201); + const createdUser = await orchestrator.createUser(); const response = await fetch( - "http://localhost:3000/api/v1/users/uniqueUser1", + `http://localhost:3000/api/v1/users/${createdUser.username}`, { method: "PATCH", headers: { @@ -171,7 +119,7 @@ describe("PATCH /api/v1/users/[username]", () => { expect(responseBody).toEqual({ id: responseBody.id, username: "uniqueUser2", - email: "uniqueUser1@curso.dev", + email: createdUser.email, password: responseBody.password, created_at: responseBody.created_at, updated_at: responseBody.updated_at, @@ -185,22 +133,11 @@ describe("PATCH /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@curso.dev", - password: "senha123", - }), - }); - - expect(user1Response.status).toBe(201); + const createdUser = await orchestrator.createUser(); const response = await fetch( - "http://localhost:3000/api/v1/users/uniqueEmail1", + `http://localhost:3000/api/v1/users/${createdUser.username}`, + { method: "PATCH", headers: { @@ -218,7 +155,7 @@ describe("PATCH /api/v1/users/[username]", () => { expect(responseBody).toEqual({ id: responseBody.id, - username: "uniqueEmail1", + username: createdUser.username, email: "uniqueEmail2@curso.dev", password: responseBody.password, created_at: responseBody.created_at, @@ -233,22 +170,12 @@ describe("PATCH /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@curso.dev", - password: "newPassword1", - }), + const createdUser = 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/${createdUser.username}`, { method: "PATCH", headers: { @@ -266,8 +193,8 @@ describe("PATCH /api/v1/users/[username]", () => { expect(responseBody).toEqual({ id: responseBody.id, - username: "newPassword1", - email: "newPassword1@curso.dev", + username: createdUser.username, + email: createdUser.email, password: responseBody.password, created_at: responseBody.created_at, updated_at: responseBody.updated_at, @@ -279,7 +206,7 @@ describe("PATCH /api/v1/users/[username]", () => { expect(responseBody.updated_at > responseBody.created_at).toBe(true); - const userInDatabase = await user.findOneByUsername("newPassword1"); + const userInDatabase = await user.findOneByUsername(createdUser.username); const correctPasswordMatch = await password.compare( "newPassword2", userInDatabase.password diff --git a/tests/orchestrator.js b/tests/orchestrator.js index d9806f7..6737a56 100644 --- a/tests/orchestrator.js +++ b/tests/orchestrator.js @@ -1,6 +1,9 @@ import retry from "async-retry"; +import { faker } from "@faker-js/faker"; + import database from "infra/database.js"; import migrator from "models/migrator.js"; +import user from "models/user.js"; async function waitForAllServices() { await waitForWebServer(); @@ -29,10 +32,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;