|
| 1 | +import chai from "chai"; |
| 2 | +import chaiHttp from "chai-http"; |
| 3 | +import app from "../../server"; |
| 4 | +import authService from "../../services/authService"; |
| 5 | +import addUser from "../utils/addUser"; |
| 6 | +import cleanDb from "../utils/cleanDb"; |
| 7 | +import stocks from "../../models/stocks"; |
| 8 | +import sinon from "sinon"; |
| 9 | +import config from "config"; |
| 10 | + |
| 11 | +const cookieName: string = config.get("userToken.cookieName"); |
| 12 | +chai.use(chaiHttp); |
| 13 | +const { expect } = chai; |
| 14 | + |
| 15 | +describe("GET /stocks/:userId", function () { |
| 16 | + let jwt: string; |
| 17 | + let userId: string; |
| 18 | + let userStock; |
| 19 | + const stockData = { name: "EURO", quantity: 2, price: 10 }; |
| 20 | + |
| 21 | + beforeEach(async function () { |
| 22 | + userId = await addUser(); |
| 23 | + jwt = authService.generateAuthToken({ userId }); |
| 24 | + const { id } = await stocks.addStock(stockData); |
| 25 | + userStock = { stockId: id, stockName: "EURO", quantity: 1, orderValue: 10, initialStockValue: 2 }; |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(async function () { |
| 29 | + await cleanDb(); |
| 30 | + sinon.restore(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("Should return user stocks when stocks are available", async function () { |
| 34 | + await stocks.updateUserStocks(userId, userStock); |
| 35 | + |
| 36 | + const res = await chai.request(app).get(`/stocks/${userId}?dev=true`).set("cookie", `${cookieName}=${jwt}`); |
| 37 | + |
| 38 | + expect(res).to.have.status(200); |
| 39 | + expect(res.body).to.be.an("object"); |
| 40 | + expect(res.body.message).to.equal("User stocks returned successfully!"); |
| 41 | + expect(res.body.userStocks).to.be.an("array"); |
| 42 | + expect(res.body.userStocks.map(({ id, ...rest }) => rest)).to.deep.equal([{ ...userStock, userId }]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("Should return empty object when no stocks are found", function (done) { |
| 46 | + chai |
| 47 | + .request(app) |
| 48 | + .get(`/stocks/${userId}?dev=true`) |
| 49 | + .set("cookie", `${cookieName}=${jwt}`) |
| 50 | + .end((err, res) => { |
| 51 | + if (err) return done(err); |
| 52 | + |
| 53 | + expect(res).to.have.status(200); |
| 54 | + expect(res.body).to.be.an("object"); |
| 55 | + expect(res.body.message).to.equal("No stocks found"); |
| 56 | + expect(res.body.userStocks).to.be.an("array"); |
| 57 | + |
| 58 | + return done(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it("Should return 403 for unauthorized access", function (done) { |
| 63 | + const userId = "anotherUser123"; |
| 64 | + |
| 65 | + chai |
| 66 | + .request(app) |
| 67 | + .get(`/stocks/${userId}?dev=true`) |
| 68 | + .set("cookie", `${cookieName}=${jwt}`) |
| 69 | + .end((err, res) => { |
| 70 | + if (err) return done(err); |
| 71 | + |
| 72 | + expect(res).to.have.status(403); |
| 73 | + expect(res.body).to.be.an("object"); |
| 74 | + expect(res.body.message).to.equal("Unauthorized access"); |
| 75 | + |
| 76 | + return done(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it("Should return 500 when an internal server error occurs", function (done) { |
| 81 | + sinon.stub(stocks, "fetchUserStocks").throws(new Error("Database error")); |
| 82 | + |
| 83 | + chai |
| 84 | + .request(app) |
| 85 | + .get(`/stocks/${userId}?dev=true`) |
| 86 | + .set("cookie", `${cookieName}=${jwt}`) |
| 87 | + .end((err, res) => { |
| 88 | + if (err) return done(err); |
| 89 | + |
| 90 | + expect(res).to.have.status(500); |
| 91 | + expect(res.body).to.be.an("object"); |
| 92 | + expect(res.body.message).to.equal("An internal server error occurred"); |
| 93 | + |
| 94 | + return done(); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments