Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
117 changes: 22 additions & 95 deletions tests/integration/api/v1/users/[username]/patch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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,
Expand All @@ -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: {
Expand All @@ -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,
Expand All @@ -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: {
Expand All @@ -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,
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/orchestrator.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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;