Skip to content

Commit

Permalink
trade route deprecation and new available route (#2299)
Browse files Browse the repository at this point in the history
  • Loading branch information
vikasosmium authored Dec 19, 2024
1 parent ce6872b commit 7409ecd
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 3 deletions.
3 changes: 1 addition & 2 deletions controllers/trading.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const trade = async (req, res) => {
if (!canUserTrade) {
return res.boom.forbidden(errorMessage);
}

return res.json({ userBalance });
return res.json({ message: "Congrats, Stock Trade done successfully!! πŸŽ‰πŸŽ‰πŸŽ‰πŸŽŠπŸŽŠπŸŽŠ", userBalance });
} catch (err) {
logger.error(`Error during trading: ${err}`);
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
Expand Down
5 changes: 4 additions & 1 deletion routes/trading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const router = express.Router();
import authenticate from "../middlewares/authenticate";
import { newTrade } from "../middlewares/validators/trading";
import { trade } from "../controllers/trading";
import { devFlagMiddleware } from "../middlewares/devFlag";
import { userAuthorization } from "../middlewares/userAuthorization";

router.post("/stock/new/self", authenticate, newTrade, trade);
router.post("/stock/new/self", authenticate, newTrade, trade); // this route is being deprecated, please use new available route `/stock/new/:userId`
router.post("/stock/new/:userId", devFlagMiddleware, authenticate, userAuthorization, newTrade, trade);

module.exports = router;
120 changes: 120 additions & 0 deletions test/integration/trading.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import chai from "chai";
import chaiHttp from "chai-http";
import app from "../../server";
import authService from "../../services/authService";
import addUser from "../utils/addUser";
import cleanDb from "../utils/cleanDb";
import stocks from "../../models/stocks";
import wallet from "../../models/wallets";
import sinon from "sinon";
import config from "config";
import currencies from "../fixtures/currencies/currencies";
import tradeService from "../../services/tradingService";

const cookieName: string = config.get("userToken.cookieName");
chai.use(chaiHttp);
const { expect } = chai;

describe("POST /trade/stock/new/:userId", function () {
let jwt: string;
let userId: string;
let userStock;
const newStockData = { name: "DINERO", quantity: 20000, price: 2 };
let id;
let stockData;

beforeEach(async function () {
userId = await addUser();
jwt = authService.generateAuthToken({ userId });
({ id, stockData } = await stocks.addStock(newStockData));
await wallet.createWallet(userId, currencies.default);
userStock = {
stockId: id,
stockName: stockData.name,
tradeType: "BUY",
quantity: 1,
listedPrice: stockData.price,
totalPrice: 1 * stockData.price,
};
});

afterEach(async function () {
await cleanDb();
sinon.restore();
});

it("Should return success when trade is completed", function (done) {
chai
.request(app)
.post(`/trade/stock/new/${userId}?dev=true`)
.set("cookie", `${cookieName}=${jwt}`)
.send(userStock)
.end((err, res) => {
if (err) return done(err);

expect(res).to.have.status(200);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("Congrats, Stock Trade done successfully!! πŸŽ‰πŸŽ‰πŸŽ‰πŸŽŠπŸŽŠπŸŽŠ");
expect(res.body.userBalance).to.be.a("number");
return done();
});
});

it("Should return trade unsuccessful due to insufficient funds", function (done) {
userStock.tradeType = "BUY";
userStock.quantity = 2000;
userStock.totalPrice = 2000 * stockData.price;

chai
.request(app)
.post(`/trade/stock/new/${userId}?dev=true`)
.set("cookie", `${cookieName}=${jwt}`)
.send(userStock)
.end((err, res) => {
if (err) return done(err);

expect(res).to.have.status(403);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("Trade was not successful due to insufficient funds");
return done();
});
});

it("Should return trade unsuccessful due to insufficient stock quantity", function (done) {
userStock.tradeType = "SELL";
userStock.quantity = 20001;
userStock.totalPrice = 20001 * stockData.price;

chai
.request(app)
.post(`/trade/stock/new/${userId}?dev=true`)
.set("cookie", `${cookieName}=${jwt}`)
.send(userStock)
.end((err, res) => {
if (err) return done(err);

expect(res).to.have.status(403);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("Trade was not successful because you do not have enough quantity");
return done();
});
});

it("Should return 500 when an internal server error occurs", function (done) {
sinon.stub(tradeService, "trade").throws(new Error("Database error"));

chai
.request(app)
.post(`/trade/stock/new/${userId}?dev=true`)
.set("cookie", `${cookieName}=${jwt}`)
.send(userStock)
.end((err, res) => {
if (err) return done(err);

expect(res).to.have.status(500);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("An internal server error occurred");
return done();
});
});
});

0 comments on commit 7409ecd

Please sign in to comment.