-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iam): forgot-password http controller + specs
- Loading branch information
Showing
8 changed files
with
105 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/identity-and-access/use-cases/forgot-password/http.controller.e2e.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Server } from "http"; | ||
import supertest from "supertest"; | ||
import { afterAll, beforeAll, describe, expect, it } from "vitest"; | ||
import { bootstrap } from "../../../main.js"; | ||
|
||
describe("ForgotPasswordHttpController", () => { | ||
let server: Server; | ||
|
||
beforeAll(async () => { | ||
server = await bootstrap(); | ||
}); | ||
|
||
afterAll(async () => { | ||
server.close(); | ||
}); | ||
|
||
describe("POST /auth/forgot-password", () => { | ||
it("should return 204 when the password reset process has been initiated", async () => { | ||
const response = await supertest(server) | ||
.post("/auth/forgot-password") | ||
.send({ email: "[email protected]" }); | ||
|
||
expect(response.status).toEqual(204); | ||
}); | ||
|
||
it("should return 404 when attempting to reset password for an unregistered email address", async () => { | ||
const response = await supertest(server) | ||
.post("/auth/forgot-password") | ||
.send({ email: "[email protected]" }); | ||
|
||
expect(response.status).toEqual(404); | ||
expect(response.body).toEqual({ | ||
code: "resource-not-found", | ||
detail: "The Account you are trying to access does not exist.", | ||
pointer: "/data/attributes/email", | ||
resource: "Account", | ||
searchedByFieldName: "email", | ||
searchedByValue: "[email protected]", | ||
status: 404, | ||
timestamp: expect.any(String), | ||
title: "Resource Not Found", | ||
}); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
src/identity-and-access/use-cases/forgot-password/http.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Body, Controller, HttpCode, HttpStatus, Post } from "@nestjs/common"; | ||
import { ForgotPasswordHttpRequestBody } from "./http.request.js"; | ||
import { ForgotPasswordUseCase } from "./use-case.js"; | ||
|
||
@Controller() | ||
export class ForgotPasswordHttpController { | ||
constructor(private readonly useCase: ForgotPasswordUseCase) {} | ||
|
||
@HttpCode(HttpStatus.NO_CONTENT) | ||
@Post("/auth/forgot-password") | ||
async handle(@Body() body: ForgotPasswordHttpRequestBody) { | ||
return await this.useCase.execute(body); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/identity-and-access/use-cases/forgot-password/http.request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { IsEmail } from "class-validator"; | ||
|
||
export class ForgotPasswordHttpRequestBody { | ||
@IsEmail() | ||
email: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,12 @@ describe("SignInWithCredentialsHttpController", () => { | |
server.close(); | ||
}); | ||
|
||
describe("POST /authentication/sign-in", () => { | ||
describe("POST /auth/sign-in", () => { | ||
it("should return 200 and set the access token in a secure, HTTP-only, SameSite=Strict cookie when the credentials are valid", async () => { | ||
const response = await supertest(server) | ||
.post("/authentication/sign-in") | ||
.send({ | ||
email: "[email protected]", | ||
password: "password", | ||
}); | ||
const response = await supertest(server).post("/auth/sign-in").send({ | ||
email: "[email protected]", | ||
password: "password", | ||
}); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(response.headers["set-cookie"][0]).toMatch( | ||
|
@@ -32,12 +30,10 @@ describe("SignInWithCredentialsHttpController", () => { | |
}); | ||
|
||
it("should return 404 when the account does not exist", async () => { | ||
const response = await supertest(server) | ||
.post("/authentication/sign-in") | ||
.send({ | ||
email: "[email protected]", | ||
password: "password", | ||
}); | ||
const response = await supertest(server).post("/auth/sign-in").send({ | ||
email: "[email protected]", | ||
password: "password", | ||
}); | ||
|
||
expect(response.status).toEqual(404); | ||
expect(response.body).toEqual({ | ||
|
@@ -54,12 +50,10 @@ describe("SignInWithCredentialsHttpController", () => { | |
}); | ||
|
||
it("should return 401 when the password is invalid", async () => { | ||
const response = await supertest(server) | ||
.post("/authentication/sign-in") | ||
.send({ | ||
email: "[email protected]", | ||
password: "wrong-password", | ||
}); | ||
const response = await supertest(server).post("/auth/sign-in").send({ | ||
email: "[email protected]", | ||
password: "wrong-password", | ||
}); | ||
|
||
expect(response.status).toEqual(401); | ||
expect(response.body).toEqual({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,12 @@ describe("SignInWithCredentialsHttpController", () => { | |
server.close(); | ||
}); | ||
|
||
describe("POST /authentication/sign-up", () => { | ||
describe("POST /auth/sign-up", () => { | ||
it("should return 201 when the account is created", async () => { | ||
const response = await supertest(server) | ||
.post("/authentication/sign-up") | ||
.send({ | ||
email: "[email protected]", | ||
password: "password", | ||
}); | ||
const response = await supertest(server).post("/auth/sign-up").send({ | ||
email: "[email protected]", | ||
password: "password", | ||
}); | ||
|
||
expect(response.status).toEqual(201); | ||
expect(response.body).toEqual({ | ||
|
@@ -32,12 +30,10 @@ describe("SignInWithCredentialsHttpController", () => { | |
}); | ||
|
||
it("should return 409 when the email is already taken", async () => { | ||
const response = await supertest(server) | ||
.post("/authentication/sign-up") | ||
.send({ | ||
email: "[email protected]", | ||
password: "password", | ||
}); | ||
const response = await supertest(server).post("/auth/sign-up").send({ | ||
email: "[email protected]", | ||
password: "password", | ||
}); | ||
|
||
expect(response.status).toEqual(409); | ||
expect(response.body).toEqual({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters