From a8290ea4ec75de66a50659b41c4d648ec6cca862 Mon Sep 17 00:00:00 2001 From: Vikas Singh <59792866+vikasosmium@users.noreply.github.com> Date: Tue, 19 Nov 2024 01:13:15 +0530 Subject: [PATCH] Added new route to fetch user Art (#2255) * added new route to fetch user art * added new route under featureflag * added test cases for new route * wrote 2 new test cases and and one no content cond. --- controllers/arts.js | 9 +++ routes/arts.ts | 6 +- test/integration/arts.test.js | 103 +++++++++++++++++++++++++++++++++- 3 files changed, 115 insertions(+), 3 deletions(-) diff --git a/controllers/arts.js b/controllers/arts.js index b259d21dc..0b2fede85 100644 --- a/controllers/arts.js +++ b/controllers/arts.js @@ -50,6 +50,10 @@ const getSelfArts = async (req, res) => { try { const { id } = req.userData; const arts = await artsQuery.fetchUserArts(id); + res.set( + "X-Deprecation-Warning", + "WARNING: This endpoint is deprecated and will be removed in the future. Please use /arts/:userId to get the art details." + ); return res.json({ message: "User arts returned successfully!", arts, @@ -64,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/routes/arts.ts b/routes/arts.ts index 3d0b08eb9..07d71e098 100644 --- a/routes/arts.ts +++ b/routes/arts.ts @@ -3,10 +3,12 @@ const router = express.Router(); import authenticate from "../middlewares/authenticate"; import arts from "../controllers/arts"; import artValidator from "../middlewares/validators/arts"; +import { devFlagMiddleware } from "../middlewares/devFlag"; router.get("/", arts.fetchArts); -router.get("/user/self", authenticate, arts.getSelfArts); -router.get("/user/:userId", authenticate, arts.getUserArts); +router.get("/user/self", authenticate, arts.getSelfArts); // this route is soon going to be deprecated soon, please use /arts/:userId endpoint. +router.get("/user/:userId", authenticate, arts.getUserArts); // this route is soon going to be deprecated soon, please use /arts/:userId endpoint. +router.get("/:userId", devFlagMiddleware, authenticate, arts.getUserArts); router.post("/user/add", authenticate, artValidator.createArt, arts.addArt); module.exports = router; diff --git a/test/integration/arts.test.js b/test/integration/arts.test.js index e8bb4f780..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"); @@ -13,14 +15,17 @@ const artData = require("../fixtures/arts/arts")(); const config = require("config"); const cookieName = config.get("userToken.cookieName"); +const { addJoinData } = require("../../models/users"); +const joinData = require("../fixtures/user/join"); chai.use(chaiHttp); describe("Arts", function () { let jwt; + let userId = ""; beforeEach(async function () { - const userId = await addUser(); + userId = await addUser(); jwt = authService.generateAuthToken({ userId }); await arts.addArt(artData[0], userId); }); @@ -108,6 +113,10 @@ describe("Arts", function () { expect(res.body.arts).to.be.a("array"); expect(res.body.arts[0]).to.be.a("object"); expect(res.body.arts[0].title).to.equal(artData[0].title); + expect(res).to.have.header( + "X-Deprecation-Warning", + "WARNING: This endpoint is deprecated and will be removed in the future. Please use /arts/:userId to get the art details." + ); return done(); }); @@ -134,4 +143,96 @@ describe("Arts", function () { }); }); }); + + describe("GET /arts/:userId", function () { + beforeEach(async function () { + await addJoinData(joinData(userId)[0]); + }); + + it("Should get all the arts of the user", function (done) { + chai + .request(app) + .get(`/arts/${userId}?dev=true`) + .set("cookie", `${cookieName}=${jwt}`) + .end((err, res) => { + if (err) { + return done(err); + } + expect(res).to.have.status(200); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.equal(`User Arts of userId ${userId} returned successfully`); + expect(res.body.arts).to.be.a("array"); + expect(res.body.arts[0]).to.be.a("object"); + expect(res.body.arts[0].title).to.equal(artData[0].title); + + return done(); + }); + }); + + it("Should return 401, for Unauthenticated User", function (done) { + chai + .request(app) + .get(`/arts/${userId}?dev=true`) + .end((err, res) => { + if (err) { + return done(err); + } + + expect(res).to.have.status(401); + expect(res.body).to.be.a("object"); + expect(res.body).to.deep.equal({ + statusCode: 401, + error: "Unauthorized", + message: "Unauthenticated User", + }); + + 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(); + }); + }); + }); });