|
| 1 | +const chai = require('chai') |
| 2 | +const { expect } = chai |
| 3 | +const chaiHttp = require('chai-http') |
| 4 | + |
| 5 | +const app = require('../../server') |
| 6 | +const authService = require('../../services/authService') |
| 7 | +const addUser = require('../utils/addUser') |
| 8 | +const cleanDb = require('../utils/cleanDb') |
| 9 | + |
| 10 | +const { createNewAuction } = require('../../models/auctions') |
| 11 | +const { createWallet } = require('../../models/wallets') |
| 12 | + |
| 13 | +// Import fixtures |
| 14 | +const userData = require('../fixtures/user/user')() |
| 15 | +const { auctionData, auctionKeys, auctionWithIdKeys } = require('../fixtures/auctions/auctions') |
| 16 | +const { initial_price: initialPrice, item_type: itemType, end_time: endTime, quantity } = auctionData |
| 17 | +const currenciesData = require('../fixtures/currencies/currencies') |
| 18 | + |
| 19 | +const config = require('config') |
| 20 | +const cookieName = config.get('userToken.cookieName') |
| 21 | + |
| 22 | +chai.use(chaiHttp) |
| 23 | + |
| 24 | +describe('Auctions', function () { |
| 25 | + let jwt |
| 26 | + let auctionId |
| 27 | + |
| 28 | + beforeEach(async function () { |
| 29 | + const userId = await addUser() |
| 30 | + jwt = authService.generateAuthToken({ userId }) |
| 31 | + await createWallet(userId, currenciesData) |
| 32 | + auctionId = await createNewAuction({ seller: userId, initialPrice, endTime, itemType, quantity }) |
| 33 | + }) |
| 34 | + |
| 35 | + afterEach(async function () { |
| 36 | + await cleanDb() |
| 37 | + }) |
| 38 | + |
| 39 | + describe('GET /auctions', function () { |
| 40 | + it('Should return the ongoing auctions', function (done) { |
| 41 | + chai |
| 42 | + .request(app) |
| 43 | + .get('/auctions') |
| 44 | + .end((err, res) => { |
| 45 | + if (err) { |
| 46 | + return done(err) |
| 47 | + } |
| 48 | + |
| 49 | + expect(res).to.have.status(200) |
| 50 | + expect(res.body).to.be.a('object') |
| 51 | + expect(res.body).to.have.all.keys(...auctionKeys) |
| 52 | + expect(res.body.message).to.be.equal('Auctions returned successfully!') |
| 53 | + expect(res.body.auctions).to.be.a('array') |
| 54 | + |
| 55 | + return done() |
| 56 | + }) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + describe('GET /auctions/:id', function () { |
| 61 | + it('Should return the ongoing auctions for given Id', function (done) { |
| 62 | + chai |
| 63 | + .request(app) |
| 64 | + .get(`/auctions/${auctionId}`) |
| 65 | + .end((err, res) => { |
| 66 | + if (err) { |
| 67 | + return done(err) |
| 68 | + } |
| 69 | + |
| 70 | + expect(res).to.have.status(200) |
| 71 | + expect(res.body).to.be.a('object') |
| 72 | + expect(res.body).to.have.all.keys(...auctionWithIdKeys) |
| 73 | + expect(res.body.item).to.be.a('string') |
| 74 | + expect(res.body.quantity).to.be.a('number') |
| 75 | + expect(res.body.end_time).to.be.a('number') |
| 76 | + expect(res.body.highest_bid).to.be.a('number') |
| 77 | + expect(res.body.start_time).to.be.a('number') |
| 78 | + expect(res.body.bidders_and_bids).to.be.a('array') |
| 79 | + expect(res.body.seller).to.be.equal(userData[0].username) |
| 80 | + expect(res.body.item).to.be.equal(auctionData.item_type) |
| 81 | + expect(res.body.quantity).to.be.equal(auctionData.quantity) |
| 82 | + expect(res.body.end_time).to.be.equal(auctionData.end_time) |
| 83 | + expect(res.body.highest_bid).to.be.equal(auctionData.initial_price) |
| 84 | + |
| 85 | + return done() |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + it('Should return 404, for Auction not found', function (done) { |
| 90 | + chai |
| 91 | + .request(app) |
| 92 | + .get('/auctions/invalidId') |
| 93 | + .end((err, res) => { |
| 94 | + if (err) { |
| 95 | + return done(err) |
| 96 | + } |
| 97 | + |
| 98 | + expect(res).to.have.status(404) |
| 99 | + expect(res.body).to.be.a('object') |
| 100 | + expect(res.body.message).to.equal('Auction doesn\'t exist') |
| 101 | + |
| 102 | + return done() |
| 103 | + }) |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + describe('POST /auctions', function () { |
| 108 | + it('Should create a new auction', function (done) { |
| 109 | + chai |
| 110 | + .request(app) |
| 111 | + .post('/auctions') |
| 112 | + .set('cookie', `${cookieName}=${jwt}`) |
| 113 | + .send(auctionData) |
| 114 | + .end((err, res) => { |
| 115 | + if (err) { |
| 116 | + return done(err) |
| 117 | + } |
| 118 | + |
| 119 | + expect(res).to.have.status(201) |
| 120 | + expect(res.body).to.be.a('object') |
| 121 | + expect(res.body.message).to.be.equal('Auction created successfully!') |
| 122 | + |
| 123 | + return done() |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + it('User should have enough items in wallet to sell', function (done) { |
| 128 | + chai |
| 129 | + .request(app) |
| 130 | + .post('/auctions') |
| 131 | + .set('cookie', `${cookieName}=${jwt}`) |
| 132 | + .send({ ...auctionData, quantity: 5 }) |
| 133 | + .end((err, res) => { |
| 134 | + if (err) { |
| 135 | + return done(err) |
| 136 | + } |
| 137 | + |
| 138 | + expect(res).to.have.status(403) |
| 139 | + expect(res.body).to.be.a('object') |
| 140 | + expect(res.body.message).to.be.equal(`You do not have enough of ${itemType}s!`) |
| 141 | + |
| 142 | + return done() |
| 143 | + }) |
| 144 | + }) |
| 145 | + |
| 146 | + it('Should return 401, for Unauthenticated User', function (done) { |
| 147 | + chai |
| 148 | + .request(app) |
| 149 | + .post('/auctions') |
| 150 | + .end((err, res) => { |
| 151 | + if (err) { |
| 152 | + return done(err) |
| 153 | + } |
| 154 | + |
| 155 | + expect(res).to.have.status(401) |
| 156 | + expect(res.body).to.be.a('object') |
| 157 | + expect(res.body).to.deep.equal({ |
| 158 | + statusCode: 401, |
| 159 | + error: 'Unauthorized', |
| 160 | + message: 'Unauthenticated User' |
| 161 | + }) |
| 162 | + |
| 163 | + return done() |
| 164 | + }) |
| 165 | + }) |
| 166 | + }) |
| 167 | + |
| 168 | + describe('POST /auctions/bid/:id', function () { |
| 169 | + it('Should make a new bid with given auctionId', function (done) { |
| 170 | + chai |
| 171 | + .request(app) |
| 172 | + .post(`/auctions/bid/${auctionId}`) |
| 173 | + .set('cookie', `${cookieName}=${jwt}`) |
| 174 | + .send({ bid: 500 }) |
| 175 | + .end((err, res) => { |
| 176 | + if (err) { |
| 177 | + return done(err) |
| 178 | + } |
| 179 | + |
| 180 | + expect(res).to.have.status(201) |
| 181 | + expect(res.body).to.be.a('object') |
| 182 | + expect(res.body.message).to.be.equal('Successfully placed bid!') |
| 183 | + |
| 184 | + return done() |
| 185 | + }) |
| 186 | + }) |
| 187 | + |
| 188 | + it('User should have sufficient balance for bidding', function (done) { |
| 189 | + chai |
| 190 | + .request(app) |
| 191 | + .post(`/auctions/bid/${auctionId}`) |
| 192 | + .set('cookie', `${cookieName}=${jwt}`) |
| 193 | + .send({ bid: 1001 }) |
| 194 | + |
| 195 | + .end((err, res) => { |
| 196 | + if (err) { |
| 197 | + return done(err) |
| 198 | + } |
| 199 | + |
| 200 | + expect(res).to.have.status(403) |
| 201 | + expect(res.body).to.be.a('object') |
| 202 | + expect(res.body.message).to.be.equal('You do not have sufficient money') |
| 203 | + |
| 204 | + return done() |
| 205 | + }) |
| 206 | + }) |
| 207 | + |
| 208 | + it('Bid Should be higher than the previous bid', function (done) { |
| 209 | + chai |
| 210 | + .request(app) |
| 211 | + .post(`/auctions/bid/${auctionId}`) |
| 212 | + .set('cookie', `${cookieName}=${jwt}`) |
| 213 | + .send({ bid: 50 }) |
| 214 | + .end((err, res) => { |
| 215 | + if (err) { |
| 216 | + return done(err) |
| 217 | + } |
| 218 | + |
| 219 | + expect(res).to.have.status(403) |
| 220 | + expect(res.body).to.be.a('object') |
| 221 | + expect(res.body.message).to.be.equal('Your bid was not higher than current one!') |
| 222 | + |
| 223 | + return done() |
| 224 | + }) |
| 225 | + }) |
| 226 | + |
| 227 | + it('Should return 401, for Unauthenticated User', function (done) { |
| 228 | + chai |
| 229 | + .request(app) |
| 230 | + .post('/auctions/bid/invalidId') |
| 231 | + .end((err, res) => { |
| 232 | + if (err) { |
| 233 | + return done(err) |
| 234 | + } |
| 235 | + |
| 236 | + expect(res).to.have.status(401) |
| 237 | + expect(res.body).to.be.a('object') |
| 238 | + expect(res.body).to.deep.equal({ |
| 239 | + statusCode: 401, |
| 240 | + error: 'Unauthorized', |
| 241 | + message: 'Unauthenticated User' |
| 242 | + }) |
| 243 | + |
| 244 | + return done() |
| 245 | + }) |
| 246 | + }) |
| 247 | + }) |
| 248 | +}) |
0 commit comments