Skip to content

Commit

Permalink
Add tests for authService methods
Browse files Browse the repository at this point in the history
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
Fingertips18 committed Sep 28, 2024
1 parent e0b05f3 commit 02cd424
Show file tree
Hide file tree
Showing 13 changed files with 869 additions and 4 deletions.
5 changes: 4 additions & 1 deletion client/src/lib/classes/error-response-class.ts
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;
}
}
5 changes: 4 additions & 1 deletion client/src/lib/classes/generic-response-class.ts
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;
}
}
5 changes: 4 additions & 1 deletion client/src/lib/classes/user-response-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { UserDTO } from "@/lib/DTO/user-dto";
type UserResponseType = {
message: string;
user: UserDTO;
response: Response;
};

export class UserResponse {
public message: string;
public user: UserDTO;
public response: Response;

constructor({ message, user }: UserResponseType) {
constructor({ message, user, response }: UserResponseType) {
this.message = message;
this.user = user;
this.response = response;
}
}
19 changes: 18 additions & 1 deletion client/src/lib/services/auth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export const AuthService = {
throw new ErrorResponse({
status: res.status,
message: data.error,
response: res,
});
}

return new GenericResponse({
message: data.message,
response: res,
});
},
signIn: async (signIn: SignInDTO) => {
Expand All @@ -49,12 +51,14 @@ export const AuthService = {
throw new ErrorResponse({
status: res.status,
message: data.error,
response: res,
});
}

return new UserResponse({
message: data.message,
user: data.user,
response: res,
});
},
signOut: async () => {
Expand All @@ -71,6 +75,7 @@ export const AuthService = {

return new GenericResponse({
message: data.message,
response: res,
});
},
verifyEmail: async (token: string) => {
Expand All @@ -90,11 +95,13 @@ export const AuthService = {
throw new ErrorResponse({
status: res.status,
message: data.error,
response: res,
});
}

return new GenericResponse({
message: data.message,
response: res,
});
},
resendVerify: async (email: string) => {
Expand All @@ -110,14 +117,16 @@ export const AuthService = {

const data = await res.json();
if (!res.ok) {
return new ErrorResponse({
throw new ErrorResponse({
status: res.status,
message: data.error,
response: res,
});
}

return new GenericResponse({
message: data.message,
response: res,
});
},
verifyToken: async () => {
Expand All @@ -132,12 +141,14 @@ export const AuthService = {
throw new ErrorResponse({
status: res.status,
message: data.error,
response: res,
});
}

return new UserResponse({
message: data.message,
user: data.user,
response: res,
});
},
forgotPassword: async (email: string) => {
Expand All @@ -157,11 +168,13 @@ export const AuthService = {
throw new ErrorResponse({
status: res.status,
message: data.error,
response: res,
});
}

return new GenericResponse({
message: data.message,
response: res,
});
},
resetPassword: async (reset: ResetDTO) => {
Expand All @@ -185,11 +198,13 @@ export const AuthService = {
throw new ErrorResponse({
status: res.status,
message: data.error,
response: res,
});
}

return new GenericResponse({
message: data.message,
response: res,
});
},
changePassword: async (change: ChangeDTO) => {
Expand All @@ -207,11 +222,13 @@ export const AuthService = {
throw new ErrorResponse({
status: res.status,
message: data.error,
response: res,
});
}

return new GenericResponse({
message: data.message,
response: res,
});
},
};
90 changes: 90 additions & 0 deletions client/src/lib/services/change-password.test.ts
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",
})
);
});
});
86 changes: 86 additions & 0 deletions client/src/lib/services/forgot-password.test.ts
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",
})
);
});
});
Loading

0 comments on commit 02cd424

Please sign in to comment.