Skip to content

Commit

Permalink
Added new route to fetch user Art (#2255)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
vikasosmium authored Nov 18, 2024
1 parent 75f22dd commit a8290ea
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
9 changes: 9 additions & 0 deletions controllers/arts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions routes/arts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
103 changes: 102 additions & 1 deletion test/integration/arts.test.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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);
});
Expand Down Expand Up @@ -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();
});
Expand All @@ -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();
});
});
});
});

0 comments on commit a8290ea

Please sign in to comment.