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
7 changes: 7 additions & 0 deletions models/password.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import bcryptjs from "bcryptjs";
import { ValidationError } from "infra/errors.js";

async function hash(password) {
if (!password) {
throw new ValidationError({
message: "A senha de usuário é obritória.",
action: "Informe a senha de usuário para realizar esta operação.",
});
}
const rounds = getNumberOfRounds();
const pepper = getPepper();
return await bcryptjs.hash(password + pepper, rounds);
Expand Down
29 changes: 18 additions & 11 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
66 changes: 20 additions & 46 deletions tests/integration/api/v1/users/[username]/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,49 @@ 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: "CaseMatch",
email: "case.match@felipebatista.dev",
password: "senha123",
}),
await orchestrator.createUser({
username: "CaseMatch",
});
expect(response1.status).toBe(201);

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

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({
...responseBody,
username: "CaseMatch",
email: "case.match@felipebatista.dev",
password: response2Body.password,
created_at: response2Body.created_at,
updated_at: response2Body.updated_at,
});

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

test("With mismatch case", async () => {
const response1 = await fetch("http://localhost:3000/api/v1/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: "MismatchCase",
email: "mismatch.case@felipebatista.dev",
password: "senha123",
}),
await orchestrator.createUser({
username: "MismatchCase",
});
expect(response1.status).toBe(201);

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

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({
...responseBody,
username: "MismatchCase",
email: "mismatch.case@felipebatista.dev",
password: response2Body.password,
created_at: response2Body.created_at,
updated_at: response2Body.updated_at,
});

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

test("With nonexistent username", async () => {
Expand Down
Loading