Skip to content

Commit a8290ea

Browse files
authored
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.
1 parent 75f22dd commit a8290ea

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

controllers/arts.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ const getSelfArts = async (req, res) => {
5050
try {
5151
const { id } = req.userData;
5252
const arts = await artsQuery.fetchUserArts(id);
53+
res.set(
54+
"X-Deprecation-Warning",
55+
"WARNING: This endpoint is deprecated and will be removed in the future. Please use /arts/:userId to get the art details."
56+
);
5357
return res.json({
5458
message: "User arts returned successfully!",
5559
arts,
@@ -64,6 +68,11 @@ const getUserArts = async (req, res) => {
6468
try {
6569
const userId = req.params.userId;
6670
const arts = await artsQuery.fetchUserArts(userId);
71+
72+
if (!arts || arts.length === 0) {
73+
return res.status(204).send();
74+
}
75+
6776
return res.json({
6877
message: `User Arts of userId ${userId} returned successfully`,
6978
arts,

routes/arts.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ const router = express.Router();
33
import authenticate from "../middlewares/authenticate";
44
import arts from "../controllers/arts";
55
import artValidator from "../middlewares/validators/arts";
6+
import { devFlagMiddleware } from "../middlewares/devFlag";
67

78
router.get("/", arts.fetchArts);
8-
router.get("/user/self", authenticate, arts.getSelfArts);
9-
router.get("/user/:userId", authenticate, arts.getUserArts);
9+
router.get("/user/self", authenticate, arts.getSelfArts); // this route is soon going to be deprecated soon, please use /arts/:userId endpoint.
10+
router.get("/user/:userId", authenticate, arts.getUserArts); // this route is soon going to be deprecated soon, please use /arts/:userId endpoint.
11+
router.get("/:userId", devFlagMiddleware, authenticate, arts.getUserArts);
1012
router.post("/user/add", authenticate, artValidator.createArt, arts.addArt);
1113

1214
module.exports = router;

test/integration/arts.test.js

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const chai = require("chai");
22
const { expect } = chai;
33
const chaiHttp = require("chai-http");
4+
const sinon = require("sinon");
5+
const artsQuery = require("../../models/arts");
46

57
const app = require("../../server");
68
const authService = require("../../services/authService");
@@ -13,14 +15,17 @@ const artData = require("../fixtures/arts/arts")();
1315

1416
const config = require("config");
1517
const cookieName = config.get("userToken.cookieName");
18+
const { addJoinData } = require("../../models/users");
19+
const joinData = require("../fixtures/user/join");
1620

1721
chai.use(chaiHttp);
1822

1923
describe("Arts", function () {
2024
let jwt;
25+
let userId = "";
2126

2227
beforeEach(async function () {
23-
const userId = await addUser();
28+
userId = await addUser();
2429
jwt = authService.generateAuthToken({ userId });
2530
await arts.addArt(artData[0], userId);
2631
});
@@ -108,6 +113,10 @@ describe("Arts", function () {
108113
expect(res.body.arts).to.be.a("array");
109114
expect(res.body.arts[0]).to.be.a("object");
110115
expect(res.body.arts[0].title).to.equal(artData[0].title);
116+
expect(res).to.have.header(
117+
"X-Deprecation-Warning",
118+
"WARNING: This endpoint is deprecated and will be removed in the future. Please use /arts/:userId to get the art details."
119+
);
111120

112121
return done();
113122
});
@@ -134,4 +143,96 @@ describe("Arts", function () {
134143
});
135144
});
136145
});
146+
147+
describe("GET /arts/:userId", function () {
148+
beforeEach(async function () {
149+
await addJoinData(joinData(userId)[0]);
150+
});
151+
152+
it("Should get all the arts of the user", function (done) {
153+
chai
154+
.request(app)
155+
.get(`/arts/${userId}?dev=true`)
156+
.set("cookie", `${cookieName}=${jwt}`)
157+
.end((err, res) => {
158+
if (err) {
159+
return done(err);
160+
}
161+
expect(res).to.have.status(200);
162+
expect(res.body).to.be.a("object");
163+
expect(res.body.message).to.equal(`User Arts of userId ${userId} returned successfully`);
164+
expect(res.body.arts).to.be.a("array");
165+
expect(res.body.arts[0]).to.be.a("object");
166+
expect(res.body.arts[0].title).to.equal(artData[0].title);
167+
168+
return done();
169+
});
170+
});
171+
172+
it("Should return 401, for Unauthenticated User", function (done) {
173+
chai
174+
.request(app)
175+
.get(`/arts/${userId}?dev=true`)
176+
.end((err, res) => {
177+
if (err) {
178+
return done(err);
179+
}
180+
181+
expect(res).to.have.status(401);
182+
expect(res.body).to.be.a("object");
183+
expect(res.body).to.deep.equal({
184+
statusCode: 401,
185+
error: "Unauthorized",
186+
message: "Unauthenticated User",
187+
});
188+
189+
return done();
190+
});
191+
});
192+
193+
it("Should return 204 No Content if no arts are found", function (done) {
194+
sinon.stub(artsQuery, "fetchUserArts").resolves([]);
195+
196+
chai
197+
.request(app)
198+
.get(`/arts/${userId}?dev=true`)
199+
.set("cookie", `${cookieName}=${jwt}`)
200+
.end((err, res) => {
201+
artsQuery.fetchUserArts.restore();
202+
203+
if (err) {
204+
return done(err);
205+
}
206+
207+
expect(res).to.have.status(204);
208+
expect(res.body).to.deep.equal({});
209+
return done();
210+
});
211+
});
212+
213+
it("Should return 500 Internal Server Error if there is an exception", function (done) {
214+
sinon.stub(artsQuery, "fetchUserArts").throws(new Error("Database error"));
215+
216+
chai
217+
.request(app)
218+
.get(`/arts/${userId}?dev=true`)
219+
.set("cookie", `${cookieName}=${jwt}`)
220+
.end((err, res) => {
221+
artsQuery.fetchUserArts.restore();
222+
223+
if (err) {
224+
return done(err);
225+
}
226+
227+
expect(res).to.have.status(500);
228+
expect(res.body).to.deep.equal({
229+
statusCode: 500,
230+
error: "Internal Server Error",
231+
message: "An internal server error occurred",
232+
});
233+
234+
return done();
235+
});
236+
});
237+
});
137238
});

0 commit comments

Comments
 (0)