-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
289 additions
and
17 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,20 @@ | ||
INSERT INTO menu(food_name, food_image, price) | ||
VALUES( | ||
'Tasty Prawns', | ||
'https://i.imgur.com/mTHYwlc.jpg', | ||
1250 | ||
); | ||
|
||
INSERT INTO menu(food_name, food_image, price) | ||
VALUES( | ||
'Turkey Wings', | ||
'https://i.imgur.com/Bfn1CxC.jpg', | ||
950 | ||
); | ||
|
||
INSERT INTO menu(food_name, food_image, price) | ||
VALUES( | ||
'Chicken Wings', | ||
'https://i.imgur.com/z490cis.jpg', | ||
850 | ||
); |
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,9 @@ | ||
import { Router } from 'express'; | ||
import AuthHandler from '../middleware/authHandler'; | ||
import OrderController from '../controllers/orderController'; | ||
|
||
const router = new Router(); | ||
|
||
router.get('/users/:id/orders', AuthHandler.authorize, OrderController.getAllUserOrders); | ||
|
||
export default router; |
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,100 @@ | ||
import chai from 'chai'; | ||
import 'chai/register-should'; | ||
import chaiHttp from 'chai-http'; | ||
import dirtyChai from 'dirty-chai'; | ||
|
||
import app from '../../server/index'; | ||
import pool from '../../server/db/config'; | ||
import { | ||
seedData, | ||
emptyTablesPromise, | ||
populateUsersTablePromise, | ||
populateMenuTablePromise, | ||
populateOrdersTablePromise, | ||
generateValidToken, | ||
} from '../seed/seed'; | ||
|
||
chai.use(chaiHttp); | ||
chai.use(dirtyChai); | ||
|
||
describe('GET /users/<userId>/orders', () => { | ||
beforeEach(async () => { | ||
await emptyTablesPromise; | ||
await populateMenuTablePromise; | ||
await populateUsersTablePromise; | ||
await populateOrdersTablePromise; | ||
}); | ||
const { validUser, validUserTwo } = seedData.users; | ||
|
||
it('should successfully get all orders for specified user', (done) => { | ||
chai.request(app) | ||
.get(`/api/v1/users/${validUser.id}/orders`) | ||
.set('x-auth', generateValidToken(validUser)) | ||
.end((err, res) => { | ||
if (err) done(err); | ||
|
||
res.status.should.eql(200); | ||
res.body.should.have.all.keys(['status', 'message', 'orders']); | ||
res.body.orders.should.be.an('array'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should return a 401 if user isn\'t authenticated', (done) => { | ||
chai.request(app) | ||
.get(`/api/v1/users/${validUser.id}/orders`) | ||
.set('x-auth', '') | ||
.end((err, res) => { | ||
if (err) done(err); | ||
|
||
res.status.should.eql(401); | ||
res.body.should.have.all.keys(['status', 'message']); | ||
res.body.status.should.eql('error'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should only return orders placed by specified user', (done) => { | ||
chai.request(app) | ||
.get(`/api/v1/users/${validUser.id}/orders`) | ||
.set('x-auth', generateValidToken(validUser)) | ||
.end(async (err, res) => { | ||
if (err) done(err); | ||
|
||
try { | ||
const orderCount = (await pool.query('SELECT * FROM orders WHERE author=$1', [validUser.id])).rowCount; | ||
res.body.orders.length.should.eql(orderCount); | ||
done(); | ||
} catch (error) { | ||
done(error); | ||
} | ||
}); | ||
}); | ||
|
||
it('should return a 403 if user tries to get orders not placed by them', (done) => { | ||
chai.request(app) | ||
.get(`/api/v1/users/${validUserTwo.id}/orders`) | ||
.set('x-auth', generateValidToken(validUser)) | ||
.end((err, res) => { | ||
if (err) done(err); | ||
|
||
res.status.should.eql(403); | ||
res.body.status.should.eql('error'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should return a 400 if specified user id is not a number', (done) => { | ||
chai.request(app) | ||
.get('/api/v1/users/dontdothis/orders') | ||
.set('x-auth', generateValidToken(validUser)) | ||
.end((err, res) => { | ||
if (err) done(err); | ||
|
||
res.status.should.eql(400); | ||
res.body.status.should.eql('error'); | ||
res.body.message.should.eql('invalid user id'); | ||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.