-
Notifications
You must be signed in to change notification settings - Fork 71
feat: implement-repository-for-user #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mlo-aa
wants to merge
1
commit into
VolunChain:main
Choose a base branch
from
mlo-aa:Feat/implement-repository-properly
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -3,9 +3,21 @@ import request from "supertest"; | |
| import express, { Express } from "express"; | ||
| import UserController from "../../presentation/controllers/UserController"; | ||
|
|
||
| // Mock the UserService | ||
| jest.mock("../../../../services/UserService"); | ||
| import { UserService } from "../../../../services/UserService"; | ||
| // Mock the repository | ||
| jest.mock("../../../../shared/infrastructure/container", () => ({ | ||
| container: { | ||
| [Symbol.for("USER_REPOSITORY")]: { | ||
| create: jest.fn(), | ||
| findById: jest.fn(), | ||
| findByEmail: jest.fn(), | ||
| update: jest.fn(), | ||
| delete: jest.fn(), | ||
| }, | ||
| }, | ||
| USER_REPOSITORY: Symbol.for("USER_REPOSITORY"), | ||
| })); | ||
|
|
||
| import { container, USER_REPOSITORY } from "../../../../shared/infrastructure/container"; | ||
|
|
||
| // Mock DTOs | ||
| jest.mock("../../dto/CreateUserDto", () => { | ||
|
|
@@ -34,14 +46,16 @@ function setupApp(): Express { | |
| app.get("/users/:id", controller.getUserById.bind(controller)); | ||
| app.get("/users", controller.getUserByEmail.bind(controller)); | ||
| app.put("/users/:id", controller.updateUser.bind(controller)); | ||
| app.delete("/users/:id", controller.deleteUser.bind(controller)); | ||
| return app; | ||
| } | ||
|
|
||
| const setupMockUserService = (methods: Partial<Record<string, unknown>>) => { | ||
| (UserService as jest.Mock).mockImplementation(() => methods); | ||
| const setupMockRepository = (methods: Partial<Record<string, unknown>>) => { | ||
| const mockRepo = container[USER_REPOSITORY] as any; | ||
| Object.assign(mockRepo, methods); | ||
| }; | ||
|
|
||
| describe("UserController", () => { | ||
| describe("UserController Integration Tests", () => { | ||
| let app: Express; | ||
|
|
||
| beforeEach(() => { | ||
|
|
@@ -51,78 +65,134 @@ describe("UserController", () => { | |
|
|
||
| describe("POST /users", () => { | ||
| it("should create a user and return 201", async () => { | ||
| const mockUser = { id: "1", email: "[email protected]" }; | ||
| setupMockUserService({ | ||
| createUser: jest.fn().mockResolvedValue(mockUser), | ||
| const mockUser = { | ||
| id: "1", | ||
| email: "[email protected]", | ||
| name: "Test User", | ||
| lastName: "Test", | ||
| wallet: "GABCDEF123456789", | ||
| isVerified: false, | ||
| createdAt: new Date(), | ||
| updatedAt: new Date() | ||
| }; | ||
|
|
||
| setupMockRepository({ | ||
| create: jest.fn().mockResolvedValue(mockUser), | ||
| }); | ||
|
|
||
| const res = await request(app) | ||
| .post("/users") | ||
| .send({ email: "[email protected]" }); | ||
| .send({ | ||
| email: "[email protected]", | ||
| name: "Test User", | ||
| lastName: "Test", | ||
| password: "password123", | ||
| wallet: "GABCDEF123456789" | ||
| }); | ||
|
|
||
| expect(res.status).toBe(201); | ||
| expect(res.body).toEqual(mockUser); | ||
| }); | ||
|
Comment on lines
93
to
95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dates in JSON responses are strings; assertions currently compare to Date objects Express serializes Date to ISO strings. Loosen assertions to shape + string dates. - expect(res.body).toEqual(mockUser);
+ expect(res.body).toEqual(
+ expect.objectContaining({
+ id: mockUser.id,
+ email: mockUser.email,
+ name: mockUser.name,
+ lastName: mockUser.lastName,
+ wallet: mockUser.wallet,
+ isVerified: mockUser.isVerified,
+ createdAt: expect.any(String),
+ updatedAt: expect.any(String),
+ })
+ );- expect(res.body).toEqual(mockUser);
+ expect(res.body).toEqual(
+ expect.objectContaining({
+ id: mockUser.id,
+ email: mockUser.email,
+ name: mockUser.name,
+ lastName: mockUser.lastName,
+ wallet: mockUser.wallet,
+ isVerified: mockUser.isVerified,
+ createdAt: expect.any(String),
+ updatedAt: expect.any(String),
+ })
+ );- expect(res.body).toEqual(mockUser);
+ expect(res.body).toEqual(
+ expect.objectContaining({
+ id: mockUser.id,
+ email: mockUser.email,
+ name: mockUser.name,
+ lastName: mockUser.lastName,
+ wallet: mockUser.wallet,
+ isVerified: mockUser.isVerified,
+ createdAt: expect.any(String),
+ updatedAt: expect.any(String),
+ })
+ );- expect(res.body.user).toEqual(mockUpdatedUser);
+ expect(res.body.user).toEqual(
+ expect.objectContaining({
+ id: mockUpdatedUser.id,
+ email: mockUpdatedUser.email,
+ name: mockUpdatedUser.name,
+ lastName: mockUpdatedUser.lastName,
+ wallet: mockUpdatedUser.wallet,
+ isVerified: mockUpdatedUser.isVerified,
+ createdAt: expect.any(String),
+ updatedAt: expect.any(String),
+ })
+ );Also applies to: 154-156, 197-199, 236-239 π€ Prompt for AI Agents |
||
|
|
||
| it("should handle errors and return 400", async () => { | ||
| (UserService as jest.Mock).mockImplementation(() => ({ | ||
| createUser: jest.fn().mockRejectedValue(new Error("fail")), | ||
| })); | ||
| it("should handle conflict errors and return 409", async () => { | ||
| setupMockRepository({ | ||
| create: jest.fn().mockRejectedValue(new Error("A user with this email or wallet already exists")), | ||
| }); | ||
|
|
||
| const res = await request(app) | ||
| .post("/users") | ||
| .send({ email: "[email protected]" }); | ||
| expect(res.status).toBe(400); | ||
| expect(res.body.error).toBe("fail"); | ||
| .send({ | ||
| email: "[email protected]", | ||
| name: "Test User", | ||
| lastName: "Test", | ||
| password: "password123", | ||
| wallet: "GABCDEF123456789" | ||
| }); | ||
|
|
||
| expect(res.status).toBe(409); | ||
| expect(res.body.error).toContain("already exists"); | ||
| }); | ||
|
|
||
| it("should handle validation errors with specific status codes", async () => { | ||
| (UserService as jest.Mock).mockImplementation(() => ({ | ||
| createUser: jest | ||
| .fn() | ||
| .mockRejectedValue(new Error("Invalid email format")), | ||
| })); | ||
| it("should handle validation errors and return 400", async () => { | ||
| setupMockRepository({ | ||
| create: jest.fn().mockRejectedValue(new Error("Validation error")), | ||
| }); | ||
|
|
||
| const res = await request(app) | ||
| .post("/users") | ||
| .send({ email: "invalid-email" }); | ||
| expect(res.status).toBe(422); | ||
| expect(res.body.error).toContain("Ivalid email format"); | ||
| .send({ | ||
| email: "[email protected]", | ||
| name: "Test User", | ||
| lastName: "Test", | ||
| password: "password123", | ||
| wallet: "GABCDEF123456789" | ||
| }); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| expect(res.body.error).toContain("validation"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("GET /users/:id", () => { | ||
| it("should return a user by id", async () => { | ||
| const mockUser = { id: "1", email: "[email protected]" }; | ||
| (UserService as jest.Mock).mockImplementation(() => ({ | ||
| getUserById: jest.fn().mockResolvedValue(mockUser), | ||
| })); | ||
| const mockUser = { | ||
| id: "1", | ||
| email: "[email protected]", | ||
| name: "Test User", | ||
| lastName: "Test", | ||
| wallet: "GABCDEF123456789", | ||
| isVerified: false, | ||
| createdAt: new Date(), | ||
| updatedAt: new Date() | ||
| }; | ||
|
|
||
| setupMockRepository({ | ||
| findById: jest.fn().mockResolvedValue(mockUser), | ||
| }); | ||
|
|
||
| const res = await request(app).get("/users/1"); | ||
| expect(res.status).toBe(200); | ||
| expect(res.body).toEqual(mockUser); | ||
| }); | ||
|
|
||
| it("should return 404 if user not found", async () => { | ||
| (UserService as jest.Mock).mockImplementation(() => ({ | ||
| getUserById: jest.fn().mockResolvedValue(null), | ||
| })); | ||
| setupMockRepository({ | ||
| findById: jest.fn().mockResolvedValue(null), | ||
| }); | ||
|
|
||
| const res = await request(app).get("/users/1"); | ||
| expect(res.status).toBe(404); | ||
| expect(res.body.error).toBe("User not found"); | ||
| }); | ||
|
|
||
| it("should handle errors and return 400", async () => { | ||
| (UserService as jest.Mock).mockImplementation(() => ({ | ||
| getUserById: jest.fn().mockRejectedValue(new Error("fail")), | ||
| })); | ||
| it("should handle errors and return 500", async () => { | ||
| setupMockRepository({ | ||
| findById: jest.fn().mockRejectedValue(new Error("Database error")), | ||
| }); | ||
|
|
||
| const res = await request(app).get("/users/1"); | ||
| expect(res.status).toBe(400); | ||
| expect(res.body.error).toBe("fail"); | ||
| expect(res.status).toBe(500); | ||
| expect(res.body.error).toBe("Internal server error"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("GET /users?email=", () => { | ||
| it("should return a user by email", async () => { | ||
| const mockUser = { id: "1", email: "[email protected]" }; | ||
| (UserService as jest.Mock).mockImplementation(() => ({ | ||
| getUserByEmail: jest.fn().mockResolvedValue(mockUser), | ||
| })); | ||
| const mockUser = { | ||
| id: "1", | ||
| email: "[email protected]", | ||
| name: "Test User", | ||
| lastName: "Test", | ||
| wallet: "GABCDEF123456789", | ||
| isVerified: false, | ||
| createdAt: new Date(), | ||
| updatedAt: new Date() | ||
| }; | ||
|
|
||
| setupMockRepository({ | ||
| findByEmail: jest.fn().mockResolvedValue(mockUser), | ||
| }); | ||
|
|
||
| const res = await request(app).get("/[email protected]"); | ||
| expect(res.status).toBe(200); | ||
| expect(res.body).toEqual(mockUser); | ||
|
|
@@ -135,41 +205,79 @@ describe("UserController", () => { | |
| }); | ||
|
|
||
| it("should return 404 if user not found", async () => { | ||
| (UserService as jest.Mock).mockImplementation(() => ({ | ||
| getUserByEmail: jest.fn().mockResolvedValue(null), | ||
| })); | ||
| setupMockRepository({ | ||
| findByEmail: jest.fn().mockResolvedValue(null), | ||
| }); | ||
|
|
||
| const res = await request(app).get("/[email protected]"); | ||
| expect(res.status).toBe(404); | ||
| expect(res.body.error).toBe("User not found"); | ||
| }); | ||
|
|
||
| it("should handle errors and return 400", async () => { | ||
| (UserService as jest.Mock).mockImplementation(() => ({ | ||
| getUserByEmail: jest.fn().mockRejectedValue(new Error("fail")), | ||
| })); | ||
| const res = await request(app).get("/[email protected]"); | ||
| expect(res.status).toBe(400); | ||
| expect(res.body.error).toBe("fail"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("PUT /users/:id", () => { | ||
| it("should update a user and return 200", async () => { | ||
| (UserService as jest.Mock).mockImplementation(() => ({ | ||
| updateUser: jest.fn().mockResolvedValue(undefined), | ||
| })); | ||
| const res = await request(app).put("/users/1").send({ name: "Updated" }); | ||
| const mockUpdatedUser = { | ||
| id: "1", | ||
| email: "[email protected]", | ||
| name: "Updated User", | ||
| lastName: "Updated", | ||
| wallet: "GABCDEF123456789", | ||
| isVerified: false, | ||
| createdAt: new Date(), | ||
| updatedAt: new Date() | ||
| }; | ||
|
|
||
| setupMockRepository({ | ||
| update: jest.fn().mockResolvedValue(mockUpdatedUser), | ||
| }); | ||
|
|
||
| const res = await request(app).put("/users/1").send({ name: "Updated User" }); | ||
| expect(res.status).toBe(200); | ||
| expect(res.body.message).toBe("User updated successfully"); | ||
| expect(res.body.user).toEqual(mockUpdatedUser); | ||
| }); | ||
|
|
||
| it("should handle errors and return 400", async () => { | ||
| (UserService as jest.Mock).mockImplementation(() => ({ | ||
| updateUser: jest.fn().mockRejectedValue(new Error("fail")), | ||
| })); | ||
| it("should handle not found errors and return 404", async () => { | ||
| setupMockRepository({ | ||
| update: jest.fn().mockRejectedValue(new Error("User not found")), | ||
| }); | ||
|
|
||
| const res = await request(app).put("/users/1").send({ name: "Updated" }); | ||
| expect(res.status).toBe(400); | ||
| expect(res.body.error).toBe("fail"); | ||
| expect(res.status).toBe(404); | ||
| expect(res.body.error).toBe("User not found"); | ||
| }); | ||
|
|
||
| it("should handle conflict errors and return 409", async () => { | ||
| setupMockRepository({ | ||
| update: jest.fn().mockRejectedValue(new Error("A user with this email already exists")), | ||
| }); | ||
|
|
||
| const res = await request(app).put("/users/1").send({ email: "[email protected]" }); | ||
| expect(res.status).toBe(409); | ||
| expect(res.body.error).toBe("A user with this email already exists"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("DELETE /users/:id", () => { | ||
| it("should delete a user and return 200", async () => { | ||
| setupMockRepository({ | ||
| delete: jest.fn().mockResolvedValue(undefined), | ||
| }); | ||
|
|
||
| const res = await request(app).delete("/users/1"); | ||
| expect(res.status).toBe(200); | ||
| expect(res.body.message).toBe("User deleted successfully"); | ||
| }); | ||
|
|
||
| it("should handle not found errors and return 404", async () => { | ||
| setupMockRepository({ | ||
| delete: jest.fn().mockRejectedValue(new Error("User not found")), | ||
| }); | ||
|
|
||
| const res = await request(app).delete("/users/1"); | ||
| expect(res.status).toBe(404); | ||
| expect(res.body.error).toBe("User not found"); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π οΈ Refactor suggestion
Harden JWT handling: no default secret, restrict algorithms, explicit expiry handling.
Using a default secret is unsafe; restrict accepted algs and handle
TokenExpiredErrordistinctly.Notes:
Also applies to: 26-31, 52-56