-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fullstack Mechanic
authored and
Fullstack Mechanic
committed
Aug 4, 2024
1 parent
db59807
commit 1e4729e
Showing
4 changed files
with
124 additions
and
9 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 |
---|---|---|
|
@@ -29,4 +29,4 @@ GOOGLE_AUTH_CALLBACK_URL= | |
FLW_PUBLIC_KEY= | ||
FLW_SECRET_KEY= | ||
FLW_ENCRYPTION_KEY= | ||
BASE_URL= | ||
BASE_URL= |
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,114 @@ | ||
import request from "supertest"; | ||
import app from "../app"; | ||
import { UserService } from "../services"; | ||
import { User } from "../models"; | ||
import AppDataSource from "../data-source"; | ||
import { BadRequest } from "../middleware"; | ||
|
||
jest.mock("../services/UserService"); | ||
|
||
describe("UserController.updateProfile", () => { | ||
let userId: string; | ||
let mockUser: User; | ||
|
||
beforeAll(async () => { | ||
if (!AppDataSource.isInitialized) { | ||
await AppDataSource.initialize(); | ||
} | ||
|
||
userId = "valid-user-id"; | ||
mockUser = { | ||
id: userId, | ||
first_name: "John", | ||
last_name: "Doe", | ||
profile: { | ||
id: "profile-id", | ||
username: "johndoe", | ||
bio: "A short bio", | ||
jobTitle: "Developer", | ||
language: "English", | ||
pronouns: "he/him", | ||
department: "Engineering", | ||
social_links: ["https://twitter.com/johndoe"], | ||
timezones: "UTC", | ||
}, | ||
} as User; | ||
}); | ||
|
||
afterAll(async () => { | ||
if (AppDataSource.isInitialized) { | ||
await AppDataSource.destroy(); | ||
} | ||
}); | ||
|
||
it("should update the user profile successfully", async () => { | ||
(UserService.getUserById as jest.Mock).mockResolvedValue(mockUser); | ||
(UserService.updateUserById as jest.Mock).mockResolvedValue({ | ||
...mockUser, | ||
first_name: "Jane", | ||
}); | ||
|
||
const response = await request(app) | ||
.put("/api/v1/users/me") | ||
.set("Authorization", `Bearer valid-token`) | ||
.send({ | ||
first_name: "Jane", | ||
last_name: "Doe", | ||
username: "janedoe", | ||
bio: "A new bio", | ||
jobTitle: "Senior Developer", | ||
language: "French", | ||
pronouns: "she/her", | ||
department: "Engineering", | ||
social_links: ["https://twitter.com/janedoe"], | ||
timezones: "CET", | ||
}); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body.data).toMatchObject({ | ||
id: userId, | ||
first_name: "Jane", | ||
last_name: "Doe", | ||
profile_id: "profile-id", | ||
username: "janedoe", | ||
bio: "A new bio", | ||
jobTitle: "Senior Developer", | ||
language: "French", | ||
pronouns: "she/her", | ||
department: "Engineering", | ||
social_links: ["https://twitter.com/janedoe"], | ||
timezones: "CET", | ||
}); | ||
}); | ||
|
||
it("should return 404 if user is not found", async () => { | ||
(UserService.getUserById as jest.Mock).mockResolvedValue(null); | ||
|
||
const response = await request(app) | ||
.put("/api/v1/users/me") | ||
.set("Authorization", `Bearer valid-token`) | ||
.send({ | ||
first_name: "Jane", | ||
last_name: "Doe", | ||
}); | ||
|
||
expect(response.status).toBe(404); | ||
expect(response.body.message).toBe("User Not Found!"); | ||
}); | ||
|
||
it("should return 400 for invalid user ID format", async () => { | ||
(UserService.getUserById as jest.Mock).mockImplementation(() => { | ||
throw new BadRequest("Unauthorized! Invalid User Id Format"); | ||
}); | ||
|
||
const response = await request(app) | ||
.put("/api/v1/users/me") | ||
.set("Authorization", `Bearer valid-token`) | ||
.send({ | ||
first_name: "Jane", | ||
}); | ||
|
||
expect(response.status).toBe(400); | ||
expect(response.body.message).toBe("Unauthorized! Invalid User Id Format"); | ||
}); | ||
}); |