Skip to content

Commit

Permalink
done rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
SaddockAime committed May 28, 2024
1 parent 4d48fbf commit b25b46a
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 217 deletions.
18 changes: 18 additions & 0 deletions src/modules/user/controller/userControllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// user Controllers
import { Request, Response } from "express";
import httpStatus from "http-status";
import userRepositories from "../repository/userRepositories";

const updateUserStatus = async (req: Request, res: Response): Promise<void> => {
try {
const userId: number = Number(req.params.id);
const data = await userRepositories.updateUserStatus(userId, req.body.status);

res
.status(httpStatus.OK)
.json({ message: "Status updated successfully.", data });
} catch (error) {
// res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ status: httpStatus.INTERNAL_SERVER_ERROR,message: error.message });
}
};
export default { updateUserStatus };
15 changes: 15 additions & 0 deletions src/modules/user/repository/userRepositories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// user repositories
import Users from "../../../databases/models/users"

const getUserById = async (id: number) => {
return await Users.findOne({where: { id: id } });
};

const updateUserStatus = async (userId: number, status: string) => {
await Users.update({ status },{ where: { id: userId } });

return await getUserById(userId);

};

export default { getUserById, updateUserStatus }
361 changes: 155 additions & 206 deletions src/modules/user/test/user.spec.ts
Original file line number Diff line number Diff line change
@@ -1,206 +1,155 @@
// // user Tests
// import chai, { expect } from "chai";
// import chaiHttp from "chai-http";
// import sinon from "sinon";
// // import httpStatus from "http-status";
// import app from "../../../index";
// import userRepo from "../repository/userRepositories";
// import Users from "../../../databases/models/users"

// chai.use(chaiHttp);

// const router = () => chai.request(app);

// const testUserId = 1;

// describe("User Account Status Management", () => {
// let getUserStub: sinon.SinonStub;
// let updateUserStub: sinon.SinonStub;
// beforeEach(() => {
// getUserStub = sinon.stub(userRepo, "getSingleUserById");
// updateUserStub = sinon.stub(userRepo, "updateUserStatus");
// });

// afterEach(() => {
// sinon.restore();
// });

// describe("Disable User Account", () => {
// it("Should return 404 if user doesn't exist", (done) => {
// getUserStub.resolves(null);
// router()
// .put("/api/admin-change-status/disable/100000")
// .end((err, res) => {
// expect(res).to.have.status(404);
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("success", false);
// expect(res.body).to.have.property("message", "User doesn't exist.");
// done(err);
// });
// });

// it("Should disable the user account successfully", (done) => {
// getUserStub.resolves({ id: testUserId, status: true });
// updateUserStub.resolves();
// router()
// .put(`/api/admin-change-status/disable/${testUserId}`)
// .end((err, res) => {
// expect(res).to.have.status(200);
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("success", true);
// expect(res.body).to.have.property("message", "User account disabled successfully");
// done(err);
// });
// });

// it("Should handle invalid user ID", (done) => {
// router()
// .put("/api/admin-change-status/disable/invalid_id")
// .end((err, res) => {
// expect(res).to.have.status(400);
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("success", false);
// expect(res.body).to.have.property("message", "Invalid user ID");
// done(err);
// });
// });

// it("Should handle server errors", (done) => {
// getUserStub.rejects(new Error("Database error"));
// router()
// .put(`/api/admin-change-status/disable/${testUserId}`)
// .end((err, res) => {
// expect(res).to.have.status(500);
// expect(res.body).to.be.an("object");
// // expect(res.body).to.have.property("status", httpStatus.INTERNAL_SERVER_ERROR);
// expect(res.body).to.have.property("message");
// done(err);
// });
// });
// });

// describe("Enable User Account", () => {
// it("Should return 404 if user doesn't exist", (done) => {
// getUserStub.resolves(null);
// router()
// .put("/api/admin-change-status/enable/100000")
// .end((err, res) => {
// expect(res).to.have.status(404);
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("success", false);
// expect(res.body).to.have.property("message", "User doesn't exist.");
// done(err);
// });
// });

// it("Should enable the user account successfully", (done) => {
// getUserStub.resolves({ id: testUserId, status: false });
// updateUserStub.resolves();
// router()
// .put(`/api/admin-change-status/enable/${testUserId}`)
// .end((err, res) => {
// expect(res).to.have.status(200);
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("success", true);
// expect(res.body).to.have.property("message", "User account enabled successfully");
// done(err);
// });
// });

// it("Should handle invalid user ID", (done) => {
// router()
// .put("/api/admin-change-status/enable/invalid_id")
// .end((err, res) => {
// expect(res).to.have.status(400);
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("success", false);
// expect(res.body).to.have.property("message", "Invalid user ID");
// done(err);
// });
// });

// it("Should handle server errors", (done) => {
// getUserStub.rejects(new Error("Database error"));
// router()
// .put(`/api/admin-change-status/enable/${testUserId}`)
// .end((err, res) => {
// expect(res).to.have.status(500);
// expect(res.body).to.be.an("object");
// // expect(res.body).to.have.property("status", httpStatus.INTERNAL_SERVER_ERROR);
// expect(res.body).to.have.property("message");
// done(err);
// });
// });
// });
// });

// // userRepositories.test.ts
// describe("User Repository Functions", () => {
// let findOneStub: sinon.SinonStub;
// let updateStub: sinon.SinonStub;

// beforeEach(() => {
// findOneStub = sinon.stub(Users, "findOne");
// updateStub = sinon.stub(Users, "update");
// });

// afterEach(() => {
// sinon.restore();
// });

// describe("getSingleUserById", () => {
// it("should return a user if found", async () => {
// const user = { id: 1, status: true };
// findOneStub.resolves(user);

// const result = await userRepo.getSingleUserById(1);

// expect(findOneStub.calledOnce).to.be.true;
// expect(findOneStub.calledWith({ where: { id: 1 } })).to.be.true;
// expect(result).to.equal(user);
// });

// it("should return null if user not found", async () => {
// findOneStub.resolves(null);

// const result = await userRepo.getSingleUserById(1000);

// expect(findOneStub.calledOnce).to.be.true;
// expect(result).to.be.null;
// });

// it("should throw an error if there is a database error", async () => {
// findOneStub.rejects(new Error("Database error"));

// try {
// await userRepo.getSingleUserById(1);
// } catch (error) {
// expect(findOneStub.calledOnce).to.be.true;
// expect(error.message).to.equal("Database error");
// }
// });
// });

// describe("updateUserStatus", () => {
// it("should update the user status successfully", async () => {
// updateStub.resolves([1]);

// const result = await userRepo.updateUserStatus(1, false);

// expect(updateStub.calledOnce).to.be.true;
// expect(updateStub.calledWith({ status: false }, { where: { id: 1 } })).to.be.true;
// expect(result).to.eql([1]);
// });

// it("should throw an error if there is a database error", async () => {
// updateStub.rejects(new Error("Database error"));

// try {
// await userRepo.updateUserStatus(1, false);
// } catch (error) {
// expect(updateStub.calledOnce).to.be.true;
// expect(error.message).to.equal("Database error");
// }
// });
// });
// });
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
// user Tests
import chai, { expect } from "chai";
import chaiHttp from "chai-http";
import sinon, { SinonStub } from "sinon";
import httpStatus from "http-status";
import app from "../../../index";
import userRepositories from "../../../modules/user/repository/userRepositories";
import Users from "../../../databases/models/users";


chai.use(chaiHttp);
const router = () => chai.request(app);
const testUserId = 1;

describe("User Controller test cases", () => {
let getUserStub: sinon.SinonStub;
let updateUserStub: sinon.SinonStub;


beforeEach(() => {
getUserStub = sinon.stub(userRepositories, "getUserById");
updateUserStub = sinon.stub(userRepositories, "updateUserStatus");
});

afterEach(async () => {
sinon.restore();
await Users.destroy({ where: {} });
});
describe("updateUserStatus", () => {
it("should update the user status successfully", (done) => {
// const updatedUser = { id: testUserId, status: "disabled" };
const updatedUser = { id: testUserId, status: "disabled", email: "[email protected]" };
getUserStub.resolves(updatedUser);
updateUserStub.resolves(updatedUser);

router()
.put(`/api/users/update-user-status/${testUserId}`)
.send({ status: "disabled" })
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("message", "Status updated successfully.");
expect(res.body).to.have.property("data").that.is.an("object").with.property("status", "disabled");
done(err);
});
});

it("should handle invalid user ID", (done) => {
router()
.put("/api/users/update-user-status/invalid_id")
.send({ status: "disabled" })
.end((err, res) => {
expect(res).to.have.status(400);
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("success", false);
expect(res.body).to.have.property("message", "Invalid user ID");
done(err);
});
});

it("should return 404 if user doesn't exist", (done) => {
getUserStub.resolves(null);

router()
.put(`/api/users/update-user-status/${testUserId}`)
.send({ status: "disabled" })
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("success", false);
expect(res.body).to.have.property("message", "User doesn't exist.");
done(err);
});
});

it("should handle server errors", (done) => {
updateUserStub.rejects(new Error("Database error"));

router()
.put(`/api/users/update-user-status/${testUserId}`)
.send({ status: "disabled" })
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("message", "User doesn't exist.");
done(err);
});
});
});
});

describe("User Repository Functions", () => {
let findOneStub: sinon.SinonStub;
let updateStub: sinon.SinonStub;

beforeEach(() => {
findOneStub = sinon.stub(Users, "findOne");
updateStub = sinon.stub(Users, "update");
});

afterEach(() => {
sinon.restore();
});

describe("getSingleUserById", () => {
it("should return a user if found", async () => {
const user = { id: 1, status: true, email: "[email protected]" };
findOneStub.resolves(user);
const result = await userRepositories.getUserById(1);
expect(findOneStub.calledOnce).to.be.true;
expect(findOneStub.calledWith({ where: { id: 1 } })).to.be.true;
expect(result).to.equal(user);
});

it("should return null if user not found", async () => {
findOneStub.resolves(null);
const result = await userRepositories.getUserById(1000);
expect(findOneStub.calledOnce).to.be.true;
expect(result).to.be.null;
});

it("should throw an error if there is a database error", async () => {
findOneStub.rejects(new Error("Database error"));
try {
await userRepositories.getUserById(1);
} catch (error) {
expect(findOneStub.calledOnce).to.be.true;
expect(error.message).to.equal("Database error");
}
});
});

describe("updateUserStatus", () => {
it("should update the user status successfully", async () => {
updateStub.resolves([1]);
const user = { id: 1, status: true, email: "[email protected]" };
const result = await userRepositories.updateUserStatus(1, "enabled");
expect(updateStub.calledOnce).to.be.true;
expect(updateStub.calledWith({ status: true }, { where: { id: 1 } })).to.be.false;
});

it("should throw an error if there is a database error", async () => {
updateStub.rejects(new Error("Database error"));
try {
await userRepositories.updateUserStatus(1, "enabled");
} catch (error) {
expect(updateStub.calledOnce).to.be.true;
expect(error.message).to.equal("Database error");
}
});
});
});

Loading

0 comments on commit b25b46a

Please sign in to comment.