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
18 changes: 18 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.4.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
82 changes: 31 additions & 51 deletions tests/integration/api/v1/users/[username]/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,81 +9,61 @@ 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: "MesmoCase",
email: "mesmo.case@example.com",
password: "password123",
}),
test("With exact case match", async () => {
const createdUser = await orchestrator.createUser({
username: "MesmoCase",
});

expect(response1.status).toBe(201);

const response2 = await fetch(
const response = await fetch(
"http://localhost:3000/api/v1/users/MesmoCase",
);

expect(response2.status).toBe(200);
expect(response.status).toBe(200);

const response2Body = await response2.json();
const responseBody = await response.json();

expect(response2Body).toEqual({
id: response2Body.id,
expect(responseBody).toEqual({
id: responseBody.id,
username: "MesmoCase",
email: "mesmo.case@example.com",
password: response2Body.password,
created_at: response2Body.created_at,
updated_at: response2Body.updated_at,
email: createdUser.email,
password: responseBody.password,
created_at: responseBody.created_at,
updated_at: responseBody.updated_at,
});

expect(uuidVersion(response2Body.id)).toBe(4);
expect(Date.parse(response2Body.created_at)).not.toBeNaN();
expect(Date.parse(response2Body.updated_at)).not.toBeNaN();
expect(uuidVersion(responseBody.id)).toBe(4);
expect(Date.parse(responseBody.created_at)).not.toBeNaN();
expect(Date.parse(responseBody.updated_at)).not.toBeNaN();
});

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@example.com",
password: "password123",
}),
test("With case mismatch", async () => {
const createdUser = await orchestrator.createUser({
username: "CaseDiferente",
});

expect(response1.status).toBe(201);

const response2 = await fetch(
const response = await fetch(
"http://localhost:3000/api/v1/users/casediferente",
);

expect(response2.status).toBe(200);
expect(response.status).toBe(200);

const response2Body = await response2.json();
const responseBody = await response.json();

expect(response2Body).toEqual({
id: response2Body.id,
expect(responseBody).toEqual({
id: responseBody.id,
username: "CaseDiferente",
email: "case.diferente@example.com",
password: response2Body.password,
created_at: response2Body.created_at,
updated_at: response2Body.updated_at,
email: createdUser.email,
password: responseBody.password,
created_at: responseBody.created_at,
updated_at: responseBody.updated_at,
});

expect(uuidVersion(response2Body.id)).toBe(4);
expect(Date.parse(response2Body.created_at)).not.toBeNaN();
expect(Date.parse(response2Body.updated_at)).not.toBeNaN();
expect(uuidVersion(responseBody.id)).toBe(4);
expect(Date.parse(responseBody.created_at)).not.toBeNaN();
expect(Date.parse(responseBody.updated_at)).not.toBeNaN();
});

test("With nonexistent username'", async () => {
test("With nonexistent username", async () => {
const response = await fetch(
"http://localhost:3000/api/v1/users/UsuarioInexistente",
);
Expand Down
125 changes: 26 additions & 99 deletions tests/integration/api/v1/users/[username]/patch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ beforeAll(async () => {

describe("PATCH /api/v1/users/[username]", () => {
describe("Anonymous user", () => {
test("With nonexistent 'username''", async () => {
test("With nonexistent 'username'", async () => {
const response = await fetch(
"http://localhost:3000/api/v1/users/UsuarioInexistente",
{
Expand All @@ -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@example.com",
password: "password123",
}),
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@example.com",
password: "password123",
}),
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,43 +63,23 @@ 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@example.com",
password: "password123",
}),
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@example.com",
password: "password123",
}),
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: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: "email1@example.com",
email: "email1@curso.dev",
}),
},
);
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@example.com",
password: "password123",
}),
});

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@example.com",
email: createdUser.email,
password: responseBody.password,
created_at: responseBody.created_at,
updated_at: responseBody.updated_at,
Expand All @@ -185,29 +133,18 @@ 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@example.com",
password: "password123",
}),
});

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: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: "uniqueEmail2@example.com",
email: "uniqueEmail2@curso.dev",
}),
},
);
Expand All @@ -218,8 +155,8 @@ describe("PATCH /api/v1/users/[username]", () => {

expect(responseBody).toEqual({
id: responseBody.id,
username: "uniqueEmail1",
email: "uniqueEmail2@example.com",
username: createdUser.username,
email: "uniqueEmail2@curso.dev",
password: responseBody.password,
created_at: responseBody.created_at,
updated_at: responseBody.updated_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@example.com",
password: "password123",
}),
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@example.com",
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
Loading