From b24208f73effbed9845e9844e6f337f011954526 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Thu, 10 Oct 2024 18:56:32 +0800 Subject: [PATCH] Add checkSuperadminExists test --- .../tests/controllers/authController.test.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Server/tests/controllers/authController.test.js b/Server/tests/controllers/authController.test.js index 5c7ec1d94..28cbc7437 100644 --- a/Server/tests/controllers/authController.test.js +++ b/Server/tests/controllers/authController.test.js @@ -2,6 +2,7 @@ const { registerUser, loginUser, editUser, + checkSuperadminExists, } = require("../../controllers/authController"); const jwt = require("jsonwebtoken"); @@ -215,3 +216,44 @@ describe("Auth Controller - editUser", async () => { expect(next.firstCall.args[0].status).to.equal(422); }); }); + +describe("Auth Controller - checkSuperadminExists", async () => { + beforeEach(() => { + req = { + db: { + checkSuperadmin: sinon.stub(), + }, + }; + res = { + status: sinon.stub().returnsThis(), + json: sinon.stub(), + }; + next = sinon.stub(); + }); + + it("should return true if a superadmin exists", async () => { + req.db.checkSuperadmin.resolves(true); + await checkSuperadminExists(req, res, next); + expect(res.status.calledWith(200)).to.be.true; + expect( + res.json.calledWith({ + success: true, + msg: successMessages.AUTH_SUPERADMIN_EXISTS, + data: true, + }) + ).to.be.true; + }); + + it("should return false if a superadmin does not exist", async () => { + req.db.checkSuperadmin.resolves(false); + await checkSuperadminExists(req, res, next); + expect(res.status.calledWith(200)).to.be.true; + expect( + res.json.calledWith({ + success: true, + msg: successMessages.AUTH_SUPERADMIN_EXISTS, + data: false, + }) + ).to.be.true; + }); +});