Browse Source

Add route 'guards' for admin/user account, fix test callbacks

sbkwgh 8 years ago
parent
commit
61ba7f0f50
2 changed files with 25 additions and 6 deletions
  1. 21 2
      routes/report.js
  2. 4 4
      test/report.js

+ 21 - 2
routes/report.js

@@ -2,11 +2,30 @@ let express = require('express')
 let router = express.Router()
 let router = express.Router()
 
 
 let { User, Post, Report } = require('../models')
 let { User, Post, Report } = require('../models')
+const Errors = require('../lib/errors')
 
 
-router.all('*', (req, res) => {})
+router.all('*', (req, res, next) => {
+	if(req.session.loggedIn) {
+		next()
+	} else {
+		res.status(401)
+		res.json({
+			errors: [Errors.requestNotAuthorized]
+		})
+	}
+})
 router.post('/', async (req, res) => {})
 router.post('/', async (req, res) => {})
 
 
-router.all('*', (req, res) => {})
+router.all('*', (req, res, next) => {
+	if(req.session.admin) {
+		next()
+	} else {
+		res.status(401)
+		res.json({
+			errors: [Errors.requestNotAuthorized]
+		})
+	}
+})
 router.get('/', async (req, res) => {})
 router.get('/', async (req, res) => {})
 
 
 module.exports = router
 module.exports = router

+ 4 - 4
test/report.js

@@ -112,7 +112,7 @@ describe('Report', () => {
 					postId: 1,
 					postId: 1,
 					reason: 'spam'
 					reason: 'spam'
 				})
 				})
-				.end((res, err) => {
+				.end((err, res) => {
 					res.should.have.status(401)
 					res.should.have.status(401)
 					res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
 					res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
 
 
@@ -127,7 +127,7 @@ describe('Report', () => {
 					postId: 'fake',
 					postId: 'fake',
 					reason: 'spam'
 					reason: 'spam'
 				})
 				})
-				.end((res, err) => {
+				.end((err, res) => {
 					res.should.have.status(400)
 					res.should.have.status(400)
 					res.body.errors.should.contain.something.that.has.property('message', 'Post id is not valid')
 					res.body.errors.should.contain.something.that.has.property('message', 'Post id is not valid')
 
 
@@ -142,7 +142,7 @@ describe('Report', () => {
 					postId: 1,
 					postId: 1,
 					reason: 'not a reason'
 					reason: 'not a reason'
 				})
 				})
-				.end((res, err) => {
+				.end((err, res) => {
 					res.should.have.status(400)
 					res.should.have.status(400)
 					res.body.errors.should.contain.something.that.has.property('message', 'Report reason can only be one of the pre-defined options')
 					res.body.errors.should.contain.something.that.has.property('message', 'Report reason can only be one of the pre-defined options')
 				
 				
@@ -190,7 +190,7 @@ describe('Report', () => {
 		it('should return an error if not admin account', done => {
 		it('should return an error if not admin account', done => {
 			userAccount
 			userAccount
 				.get('/api/v1/report')
 				.get('/api/v1/report')
-				.end((res, err) => {
+				.end((err, res) => {
 					res.should.have.status(401)
 					res.should.have.status(401)
 					res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
 					res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)