|
| 1 | +const chai = require("chai"); |
| 2 | +const sinon = require("sinon"); |
| 3 | +const { expect } = chai; |
| 4 | +const authenticateProfile = require("../../../middlewares/authenticateProfile.js"); |
| 5 | + |
| 6 | +describe("authenticateProfile Middleware", function () { |
| 7 | + let req, res, next, authenticateStub, auth; |
| 8 | + |
| 9 | + beforeEach(function () { |
| 10 | + authenticateStub = sinon.spy(); |
| 11 | + auth = authenticateProfile(authenticateStub); |
| 12 | + |
| 13 | + req = { |
| 14 | + query: {}, |
| 15 | + }; |
| 16 | + res = { |
| 17 | + boom: { |
| 18 | + unauthorized: sinon.spy(), |
| 19 | + forbidden: sinon.spy(), |
| 20 | + }, |
| 21 | + }; |
| 22 | + next = sinon.spy(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should call authenticate when profile query is true", async function () { |
| 26 | + req.query.profile = "true"; |
| 27 | + await auth(req, res, next); |
| 28 | + |
| 29 | + expect(authenticateStub.withArgs(req, res, next).calledOnce).to.equal(true); |
| 30 | + expect(next.calledOnce).to.equal(false); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should call next when profile query is not true", async function () { |
| 34 | + req.query.profile = "false"; |
| 35 | + |
| 36 | + await auth(req, res, next); |
| 37 | + |
| 38 | + expect(authenticateStub.calledOnce).to.equal(false); |
| 39 | + expect(next.calledOnce).to.equal(true); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should call next when profile query is missing", async function () { |
| 43 | + await auth(req, res, next); |
| 44 | + |
| 45 | + expect(authenticateStub.calledOnce).to.equal(false); |
| 46 | + expect(next.calledOnce).to.equal(true); |
| 47 | + }); |
| 48 | +}); |
0 commit comments