Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order route & factory #69

Merged
merged 3 commits into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions browser/js/common/factories/OrderFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
app.factory('OrderFactory', function ( $http ) {

var OrderFactory = {};

OrderFactory.fetchAll = function() {

return $http.get('/api/orders')
.then( function( res ) {
console.log( "OUTPUT!!!!!!!!!!!!!!!!!", res.data );
return res.data;
})
.then( null, function( err ) {

console.log( err );

});

}

return OrderFactory;

});
1 change: 1 addition & 0 deletions server/app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = router;

router.use('/members', require('./members'));
router.use('/cart', require('./cart'));
router.use('/orders', require('./order'));
router.use('/product', require('./products'));
router.use('/user', require('./user'));
router.use('/review', require('./review'));
Expand Down
61 changes: 61 additions & 0 deletions server/app/routes/order/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var router = require('express').Router();
module.exports = router;
var mongoose = require('mongoose');

var Order = mongoose.model( 'Order' );

var adminOnly = function( req, res, next ) {

if ( req.user && req.user.isAdmin ) {
next()
} else {
res.status( 401 ).end();
}

}

router.param('id', function ( req, res, next, id ) {

if ( !req.user ) return res.status( 401 ).end();

Order.findById( id )
.then( function( order ) {

if ( order === null ) return res.status( 404 ).end();

// you cannot look up another user's order via the order API
if ( order.user._id.toString() === req.user._id.toString() || req.user.isAdmin ) {
req.order = order;
next();
} else {
res.status( 401 ).end()
}

})
.then( null, next );

});

router.get('/', adminOnly, function ( req, res, next ) {

Order.find().populate('lineItems paymentInfo').exec()
.then( function( orders ){
res.status(200).json(orders);
})
.then( null, next );

});

router.get('/:id', function ( req, res ) {
res.status(200).json( req.order );
});

router.delete('/:id', adminOnly, function ( req, res, next ) {

req.order.remove()
.then( function() {
res.status(204).end();
})
.then( null, next );

});
68 changes: 68 additions & 0 deletions tests/browser/factories/order-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// this module will prevent ui-router from working
mod = angular.module('uiRouterNoop', [])
.service('$state', function() { return {} })
.service('$urlRouter', function() { return {} })

describe('OrderFactory', function() {

beforeEach(module('FullstackGeneratedApp'));
beforeEach(module('uiRouterNoop'));

var $httpBackend;
var $rootScope;
beforeEach('Inject tools', inject( function (_$httpBackend_, _$rootScope_) {
$httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
}));

var OrderFactory;
beforeEach('Inject factory', inject( function ( _OrderFactory_ ) {
OrderFactory = _OrderFactory_;
}));

it('should be an object', function () {
expect( OrderFactory ).to.be.an('object');
});

describe('API endpoints', function () {

afterEach( function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});

it('should GET /api/orders when calling fetchAll', function(done) {

$httpBackend.expectGET('/api/orders');
$httpBackend.whenGET('/api/orders').respond([{ works: true }]);

OrderFactory.fetchAll().then( function( orders ) {

expect( orders[0].works ).to.be.true;
done();

});

$httpBackend.flush();

});

it('should return undefined if it\'s unauthorized', function(done) {

$httpBackend.expectGET('/api/orders');
$httpBackend.whenGET('/api/orders').respond( 401, {} );

OrderFactory.fetchAll().then( function( orders ) {

expect( orders ).to.be.undefined;
done();

});

$httpBackend.flush();

});

});

});
48 changes: 48 additions & 0 deletions tests/server/routes/order-route-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Instantiate all models
var mongoose = require('mongoose');
require('../../../server/db/models');
var User = mongoose.model('User');
var Order = mongoose.model('Order');

var expect = require('chai').expect;

var dbURI = 'mongodb://localhost:27017/testingDB';
var clearDB = require('mocha-mongoose')(dbURI);

var supertest = require('supertest');
var app = require('../../../server/app');

describe('Order Route', function () {

before('Establish DB connection', function (done) {
if (mongoose.connection.db) return done();
mongoose.connect(dbURI, done);
});

after('Clear test database', function (done) {
clearDB(done);
});

describe( 'authorization', function () {

var agent;
beforeEach('create supertest agents', function() {
agent = supertest.agent(app);
});

it('protects the get all route from non-admins', function (done) {

agent.get('/api/orders')
.expect( 401 )
.end( function( err, res ) {
if ( err ) return done(err);

done();

});

});

});

});