diff --git a/controllers/arts.js b/controllers/arts.js index ac8b6ca3c..0b2fede85 100644 --- a/controllers/arts.js +++ b/controllers/arts.js @@ -68,6 +68,11 @@ const getUserArts = async (req, res) => { try { const userId = req.params.userId; const arts = await artsQuery.fetchUserArts(userId); + + if (!arts || arts.length === 0) { + return res.status(204).send(); + } + return res.json({ message: `User Arts of userId ${userId} returned successfully`, arts, diff --git a/test/integration/arts.test.js b/test/integration/arts.test.js index dd102b190..0721558f3 100644 --- a/test/integration/arts.test.js +++ b/test/integration/arts.test.js @@ -1,6 +1,8 @@ const chai = require("chai"); const { expect } = chai; const chaiHttp = require("chai-http"); +const sinon = require("sinon"); +const artsQuery = require("../../models/arts"); const app = require("../../server"); const authService = require("../../services/authService"); @@ -187,5 +189,50 @@ describe("Arts", function () { return done(); }); }); + + it("Should return 204 No Content if no arts are found", function (done) { + sinon.stub(artsQuery, "fetchUserArts").resolves([]); + + chai + .request(app) + .get(`/arts/${userId}?dev=true`) + .set("cookie", `${cookieName}=${jwt}`) + .end((err, res) => { + artsQuery.fetchUserArts.restore(); + + if (err) { + return done(err); + } + + expect(res).to.have.status(204); + expect(res.body).to.deep.equal({}); + return done(); + }); + }); + + it("Should return 500 Internal Server Error if there is an exception", function (done) { + sinon.stub(artsQuery, "fetchUserArts").throws(new Error("Database error")); + + chai + .request(app) + .get(`/arts/${userId}?dev=true`) + .set("cookie", `${cookieName}=${jwt}`) + .end((err, res) => { + artsQuery.fetchUserArts.restore(); + + if (err) { + return done(err); + } + + expect(res).to.have.status(500); + expect(res.body).to.deep.equal({ + statusCode: 500, + error: "Internal Server Error", + message: "An internal server error occurred", + }); + + return done(); + }); + }); }); });