Skip to content

Commit

Permalink
feat: implement update user route
Browse files Browse the repository at this point in the history
  • Loading branch information
Fullstack Mechanic authored and Fullstack Mechanic committed Aug 4, 2024
1 parent db59807 commit 1e4729e
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ GOOGLE_AUTH_CALLBACK_URL=
FLW_PUBLIC_KEY=
FLW_SECRET_KEY=
FLW_ENCRYPTION_KEY=
BASE_URL=
BASE_URL=
10 changes: 5 additions & 5 deletions src/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class UserController {
profile_id: user?.profile?.id,
username: user?.profile?.username,
bio: user?.profile?.bio,
job_title: user?.profile?.jobTitle,
jobTitle: user?.profile?.jobTitle,
language: user?.profile?.language,
pronouns: user?.profile?.pronouns,
department: user?.profile?.department,
Expand Down Expand Up @@ -129,7 +129,7 @@ class UserController {
* bio:
* type: string
* example: Samurai from Africa
* job_title:
* jobTitle:
* type: string
* example: Warrior
* language:
Expand Down Expand Up @@ -193,7 +193,7 @@ class UserController {
last_name,
username,
bio,
job_title,
jobTitle,
language,
pronouns,
department,
Expand Down Expand Up @@ -223,7 +223,7 @@ class UserController {
last_name,
username,
bio,
job_title,
jobTitle,
language,
pronouns,
department,
Expand All @@ -238,7 +238,7 @@ class UserController {
profile_id: updatedUser?.profile?.id,
username: updatedUser?.profile?.username,
bio: updatedUser?.profile?.bio,
job_title: updatedUser?.profile?.jobTitle,
jobTitle: updatedUser?.profile?.jobTitle,
language: updatedUser?.profile?.language,
pronouns: updatedUser?.profile?.pronouns,
department: updatedUser?.profile?.department,
Expand Down
7 changes: 4 additions & 3 deletions src/services/userservice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { User, Profile } from "../models/user";
import { User } from "../models/user";
import { Profile } from "../models/profile";
import AppDataSource from "../data-source";
import { ResourceNotFound } from "../middleware";

Expand All @@ -20,8 +21,8 @@ export class UserService {

static async updateUserById(
id: string,
updateData: Partial<Profile>,
): Promise<Profile> {
updateData: Partial<User & Profile>,
): Promise<User> {
const userRepository = AppDataSource.getRepository(User);
let user = await userRepository.findOne({
where: { id },
Expand Down
114 changes: 114 additions & 0 deletions src/test/UserController.test.ts
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");
});
});

0 comments on commit 1e4729e

Please sign in to comment.