diff --git a/package-lock.json b/package-lock.json index 9b48b9b..ca5c1d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "devDependencies": { "@commitlint/cli": "19.8.1", "@commitlint/config-conventional": "19.8.1", + "@faker-js/faker": "9.7.0", "commitizen": "4.3.1", "concurrently": "9.2.0", "cz-conventional-changelog": "3.3.0", @@ -1145,6 +1146,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.1", "resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.1.tgz", diff --git a/package.json b/package.json index 9f0ba6f..99f50aa 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "devDependencies": { "@commitlint/cli": "19.8.1", "@commitlint/config-conventional": "19.8.1", + "@faker-js/faker": "9.7.0", "commitizen": "4.3.1", "concurrently": "9.2.0", "cz-conventional-changelog": "3.3.0", diff --git a/tests/integration/api/v1/users/[username]/get.test.js b/tests/integration/api/v1/users/[username]/get.test.js index 07bd692..686b328 100644 --- a/tests/integration/api/v1/users/[username]/get.test.js +++ b/tests/integration/api/v1/users/[username]/get.test.js @@ -10,20 +10,12 @@ beforeAll(async () => { describe("GET /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: "CamelCaseUser", - email: "snake_case@curso.dev", - password: "senha123", - }), + await orchestrator.createUser({ + username: "CamelCaseUser", + email: "snake_case@curso.dev", + password: "senha123", }); - expect(response1.status).toBe(201); - const response2 = await fetch( "http://localhost:3000/api/v1/users/CamelCaseUser", ); @@ -47,20 +39,12 @@ describe("GET /api/v1/users/[username]", () => { }); test("With case missmatch", async () => { - const response1 = await fetch("http://localhost:3000/api/v1/users", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username: "DifferentCaseUser", - email: "different_case@curso.dev", - password: "senha123", - }), + await orchestrator.createUser({ + username: "DifferentCaseUser", + email: "different_case@curso.dev", + password: "senha123", }); - expect(response1.status).toBe(201); - const response2 = await fetch( "http://localhost:3000/api/v1/users/differentcaseuser", ); diff --git a/tests/integration/api/v1/users/[username]/patch.test.js b/tests/integration/api/v1/users/[username]/patch.test.js index 2ae9749..c5a9000 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 email1Response = 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(email1Response.status).toBe(201); - - const email2Response = 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(email2Response.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: { @@ -163,7 +111,6 @@ describe("PATCH /api/v1/users/[username]", () => { }), }, ); - expect(response.status).toBe(200); const responseBody = await response.json(); @@ -171,7 +118,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 +132,10 @@ 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 +153,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 +168,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 +191,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 +204,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 correctPassowordMatch = await password.compare( "newPassword2", userInDatabase.password, diff --git a/tests/orchestrator.js b/tests/orchestrator.js index 1af34b1..4118a31 100644 --- a/tests/orchestrator.js +++ b/tests/orchestrator.js @@ -1,6 +1,8 @@ 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(); @@ -28,9 +30,19 @@ 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;