generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
trade route deprecation and new available route (#2299)
- Loading branch information
1 parent
ce6872b
commit 7409ecd
Showing
3 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |