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
11 changes: 6 additions & 5 deletions infra/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ function onNoMatchHandler(request, response) {
}

function onErrorHandler(error, request, response) {
if (
error instanceof ValidationError ||
error instanceof NotFoundError ||
error instanceof UnauthorizedError
) {
if (error instanceof ValidationError || error instanceof NotFoundError) {
return response.status(error.statusCode).json(error);
}

if (error instanceof UnauthorizedError) {
clearSessionCookie(response);
return response.status(error.statusCode).json(error);
}

Expand Down
2 changes: 1 addition & 1 deletion models/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function create(userId) {
async function renew(sessionId) {
const expires_at = new Date(Date.now() + EXPIRATION_IN_MILLISECONDS);

const renewedSessionObject = runUpdateQuery(sessionId, expires_at);
const renewedSessionObject = await runUpdateQuery(sessionId, expires_at);
return renewedSessionObject;

async function runUpdateQuery(sessionId, expires_at) {
Expand Down
67 changes: 46 additions & 21 deletions tests/integration/api/v1/user/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,28 @@ describe("GET to /api/v1/user", () => {
action: "Verifique se este usuário está logado e tente novamente.",
statusCode: 401,
});

// Set - Cookie assertions
const parsedSetCookie = setCookieParser(response, {
map: true,
});

expect(parsedSetCookie.session_id).toEqual({
name: "session_id",
value: "invalid",
maxAge: -1,
path: "/",
httpOnly: true,
});
});

test("With session after 15 day", async () => {
test("With expired session", async () => {
jest.useFakeTimers({
now: new Date(Date.now() - (session.EXPIRATION_IN_MILLISECONDS + 1000)),
now: new Date(Date.now() - session.EXPIRATION_IN_MILLISECONDS),
});

const createUser = await orchestrator.createUser({
username: "UserWithSession15Day",
username: "UserWithExpiredSession",
});

const sessionObject = await orchestrator.createSession(createUser.id);
Expand All @@ -120,15 +133,27 @@ describe("GET to /api/v1/user", () => {
action: "Verifique se este usuário está logado e tente novamente.",
statusCode: 401,
});

const parsedSetCookie = setCookieParser(response, {
map: true,
});

expect(parsedSetCookie.session_id).toEqual({
name: "session_id",
value: "invalid",
maxAge: -1,
path: "/",
httpOnly: true,
});
});

test("With session 1 second before expiration", async () => {
test("With session after 15 day", async () => {
jest.useFakeTimers({
now: new Date(Date.now() - (session.EXPIRATION_IN_MILLISECONDS - 1000)),
now: new Date(Date.now() - (session.EXPIRATION_IN_MILLISECONDS + 1000)),
});

const createUser = await orchestrator.createUser({
username: "UserWithSession",
username: "UserWithSession15Day",
});

const sessionObject = await orchestrator.createSession(createUser.id);
Expand All @@ -141,27 +166,25 @@ describe("GET to /api/v1/user", () => {
},
});

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

const responseBody = await response.json();

expect(responseBody).toEqual({
id: createUser.id,
username: "UserWithSession",
email: createUser.email,
password: createUser.password,
created_at: createUser.created_at.toISOString(),
update_at: createUser.update_at.toISOString(),
name: "UnauthorizedError",
message: "Usuario não possui sessão ativa.",
action: "Verifique se este usuário está logado e tente novamente.",
statusCode: 401,
});
});

test("With expired session", async () => {
test("With session 1 second before expiration", async () => {
jest.useFakeTimers({
now: new Date(Date.now() - session.EXPIRATION_IN_MILLISECONDS),
now: new Date(Date.now() - (session.EXPIRATION_IN_MILLISECONDS - 1000)),
});

const createUser = await orchestrator.createUser({
username: "UserWithExpiredSession",
username: "UserWithSession",
});

const sessionObject = await orchestrator.createSession(createUser.id);
Expand All @@ -174,15 +197,17 @@ describe("GET to /api/v1/user", () => {
},
});

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

const responseBody = await response.json();

expect(responseBody).toEqual({
name: "UnauthorizedError",
message: "Usuario não possui sessão ativa.",
action: "Verifique se este usuário está logado e tente novamente.",
statusCode: 401,
id: createUser.id,
username: "UserWithSession",
email: createUser.email,
password: createUser.password,
created_at: createUser.created_at.toISOString(),
update_at: createUser.update_at.toISOString(),
});
});
});
Expand Down
Loading