-
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.
Implemented tests for the following **AuthService** methods: - `signIn`: Verified successful sign-in and error handling. - `signUp`: Tested user registration process and error responses. - `signOut`: Checked the sign-out functionality. - `verifyEmail`: Confirmed email verification process. - `resendVerify`: Ensured resend verification logic works as expected. - `verifyToken`: Tested token verification and error handling. - `forgotPassword`: Validated forgot password request handling. - `resetPassword`: Checked password reset process. - `changePassword`: Tested password change functionality.
- Loading branch information
1 parent
e0b05f3
commit 02cd424
Showing
13 changed files
with
869 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
type ErrorResponseType = { | ||
status: number; | ||
message: string; | ||
response: Response; | ||
}; | ||
|
||
export class ErrorResponse extends Error { | ||
public status: number; | ||
public message: string; | ||
public response: Response; | ||
|
||
constructor({ status, message }: ErrorResponseType) { | ||
constructor({ status, message, response }: ErrorResponseType) { | ||
super(message); | ||
this.status = status; | ||
this.message = message; | ||
this.response = response; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,14 @@ | ||
type GenericResponseType = { | ||
message: string; | ||
response: Response; | ||
}; | ||
|
||
export class GenericResponse { | ||
public message: string; | ||
public response: Response; | ||
|
||
constructor({ message }: GenericResponseType) { | ||
constructor({ message, response }: GenericResponseType) { | ||
this.message = message; | ||
this.response = response; | ||
} | ||
} |
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
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,90 @@ | ||
import { describe, it, expect, vi, Mock } from "vitest"; | ||
|
||
import { ChangeDTO } from "@/lib/DTO/change-dto"; | ||
|
||
import { AuthService } from "./auth-service"; | ||
|
||
global.fetch = vi.fn(); | ||
|
||
const params: ChangeDTO = { | ||
email: "[email protected]", | ||
old_password: "Old_Password123", | ||
new_password: "New_Password123", | ||
}; | ||
|
||
describe("changePassword", () => { | ||
it("should return a GenericResponse on successful change-password", async () => { | ||
const message = "Change password success"; | ||
const mockResponse = { message }; | ||
|
||
(fetch as Mock).mockResolvedValueOnce({ | ||
ok: true, | ||
json: vi.fn().mockResolvedValueOnce(mockResponse), | ||
}); | ||
|
||
const res = await AuthService.changePassword(params); | ||
expect(res.message).toEqual(message); | ||
}); | ||
|
||
it("should throw an ErrorResponse on failed change-password", async () => { | ||
const error = "Change password failed"; | ||
const mockResponse = { error }; | ||
|
||
(fetch as Mock).mockResolvedValueOnce({ | ||
ok: false, | ||
status: 400, | ||
json: vi.fn().mockResolvedValueOnce(mockResponse), | ||
}); | ||
|
||
await expect(AuthService.changePassword(params)).rejects.toThrow( | ||
expect.objectContaining({ status: 400, message: error }) | ||
); | ||
}); | ||
|
||
it("should have content-type", async () => { | ||
(fetch as Mock).mockResolvedValueOnce({ | ||
ok: true, | ||
json: vi.fn().mockResolvedValueOnce({}), | ||
}); | ||
|
||
await AuthService.changePassword(params); | ||
expect(fetch).toHaveBeenCalledWith( | ||
expect.any(String), | ||
expect.objectContaining({ | ||
headers: { "Content-Type": "application/json" }, | ||
}) | ||
); | ||
}); | ||
|
||
it("should have JSON stringified in body", async () => { | ||
(fetch as Mock).mockResolvedValueOnce({ | ||
ok: true, | ||
json: vi.fn().mockResolvedValueOnce({}), | ||
}); | ||
|
||
await AuthService.changePassword(params); | ||
|
||
expect(fetch).toHaveBeenCalledWith( | ||
expect.any(String), | ||
expect.objectContaining({ | ||
body: JSON.stringify(params), | ||
}) | ||
); | ||
}); | ||
|
||
it("should have called with POST method", async () => { | ||
(fetch as Mock).mockResolvedValueOnce({ | ||
ok: true, | ||
json: vi.fn().mockResolvedValueOnce({}), | ||
}); | ||
|
||
await AuthService.changePassword(params); | ||
|
||
expect(fetch).toHaveBeenCalledWith( | ||
expect.any(String), | ||
expect.objectContaining({ | ||
method: "POST", | ||
}) | ||
); | ||
}); | ||
}); |
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,86 @@ | ||
import { describe, it, expect, vi, Mock } from "vitest"; | ||
|
||
import { AuthService } from "./auth-service"; | ||
|
||
global.fetch = vi.fn(); | ||
|
||
const email = "[email protected]"; | ||
|
||
describe("forgotPassword", () => { | ||
it("should return a GenericResponse on successful forgot-password", async () => { | ||
const message = "Forgot password success"; | ||
const mockResponse = { message }; | ||
|
||
(fetch as Mock).mockResolvedValueOnce({ | ||
ok: true, | ||
json: vi.fn().mockResolvedValueOnce(mockResponse), | ||
}); | ||
|
||
const res = await AuthService.forgotPassword(email); | ||
expect(res.message).toEqual(message); | ||
}); | ||
|
||
it("should throw an ErrorResponse on failed forgot-password", async () => { | ||
const error = "Forgot password failed"; | ||
const mockResponse = { error }; | ||
|
||
(fetch as Mock).mockResolvedValueOnce({ | ||
ok: false, | ||
status: 400, | ||
json: vi.fn().mockResolvedValueOnce(mockResponse), | ||
}); | ||
|
||
await expect(AuthService.forgotPassword(email)).rejects.toThrow( | ||
expect.objectContaining({ status: 400, message: error }) | ||
); | ||
}); | ||
|
||
it("should have content-type", async () => { | ||
(fetch as Mock).mockResolvedValueOnce({ | ||
ok: true, | ||
json: vi.fn().mockResolvedValueOnce({}), | ||
}); | ||
|
||
await AuthService.forgotPassword(email); | ||
expect(fetch).toHaveBeenCalledWith( | ||
expect.any(String), | ||
expect.objectContaining({ | ||
headers: { "Content-Type": "application/json" }, | ||
}) | ||
); | ||
}); | ||
|
||
it("should have JSON stringified in body", async () => { | ||
(fetch as Mock).mockResolvedValueOnce({ | ||
ok: true, | ||
json: vi.fn().mockResolvedValueOnce({}), | ||
}); | ||
|
||
await AuthService.forgotPassword(email); | ||
|
||
expect(fetch).toHaveBeenCalledWith( | ||
expect.any(String), | ||
expect.objectContaining({ | ||
body: JSON.stringify({ | ||
email: email, | ||
}), | ||
}) | ||
); | ||
}); | ||
|
||
it("should have called with POST method", async () => { | ||
(fetch as Mock).mockResolvedValueOnce({ | ||
ok: true, | ||
json: vi.fn().mockResolvedValueOnce({}), | ||
}); | ||
|
||
await AuthService.forgotPassword(email); | ||
|
||
expect(fetch).toHaveBeenCalledWith( | ||
expect.any(String), | ||
expect.objectContaining({ | ||
method: "POST", | ||
}) | ||
); | ||
}); | ||
}); |
Oops, something went wrong.