Bläddra i källkod

Fixes for tests and their implementations

sbkwgh 8 år sedan
förälder
incheckning
357432c8b2
3 ändrade filer med 87 tillägg och 10 borttagningar
  1. 1 1
      models/log.js
  2. 57 4
      routes/log.js
  3. 29 5
      test/log.js

+ 1 - 1
models/log.js

@@ -16,7 +16,7 @@ module.exports = (sequelize, DataTypes) => {
 			validate: {
 				isIn: {
 					args: [routes],
-					msg: "Route does not exist"
+					msg: "route does not exist"
 				}
 			}
 		}

+ 57 - 4
routes/log.js

@@ -2,15 +2,68 @@ let express = require('express')
 let router = express.Router()
 
 const Errors = require('../lib/errors')
+let { Sequelize, Log, Thread, User } = require('../models')
 
 router.post('/', async (req, res) => {
 	try {
+		let thread, user
+		if(req.body.route === 'thread') {
+			thread = await Thread.findById(req.body.resourceId)
 
-	} catch (e) {
-		res.status(500)
-		res.json({
-			errors: Errors.unknown
+			if(!thread) throw Errors.sequelizeValidation(Sequelize, {
+				error: 'thread does not exist',
+				value: req.body.resourceId
+			})
+		} else if(
+			req.body.route === 'userPosts' ||
+			req.body.route === 'userThreads'
+		) {
+			user = await User.findById(req.body.resourceId)
+
+			if(!user) throw Errors.sequelizeValidation(Sequelize, {
+				error: 'user does not exist',
+				value: req.body.resourceId
+			})
+		} else if(
+			(req.body.route === 'settingsGeneral' ||
+			req.body.route === 'settingsAccount') &&
+			!req.session.loggedIn
+		) {
+			throw Errors.requestNotAuthorized
+		}
+
+		let log = await Log.create({
+			route: req.body.route
 		})
+
+		if(thread) await log.setThread(thread)
+		if(user) await log.setUser(user)
+		if(req.session.username) {
+			let sessionUser = await User.findOne({
+				where: { username: req.session.username }
+			})
+			await log.setSessionUser(sessionUser)
+		}
+
+		res.json(log.toJSON())
+
+	} catch (e) {
+		if(e.name in Errors) {
+			res.status(401)
+			res.json({
+				errors: [e]
+			})
+		} else if(e instanceof Sequelize.ValidationError) {
+			res.status(400)
+			res.json(e)
+		} else {
+			console.log(e)
+
+			res.status(500)
+			res.json({
+				errors: Errors.unknown
+			})
+		}
 	}
 })
 

+ 29 - 5
test/log.js

@@ -36,7 +36,7 @@ describe('Log', () => {
 						admin: true
 					})
 
-				await admin
+				await user
 					.post('/api/v1/user')
 					.set('content-type', 'application/json')
 					.send({
@@ -75,8 +75,8 @@ describe('Log', () => {
 			res.should.be.json
 			res.body.should.have.property('id', 1)
 			res.body.should.have.property('route', 'index')
-			res.body.should.have.property('UserId', null)
-			res.body.should.have.property('ThreadId', null)
+			res.body.should.not.have.property('UserId')
+			res.body.should.not.have.property('ThreadId')
 
 			let log = await Log.findById(res.body.id)
 			log.should.not.be.null
@@ -112,13 +112,13 @@ describe('Log', () => {
 			let res = await chai.request(server)
 				.post('/api/v1/log')
 				.set('content-type', 'application/json')
-				.send({ route: 'thread', resourceId: 1 })
+				.send({ route: 'userPosts', resourceId: 1 })
 
 			res.should.have.status(200)
 			res.should.be.json
 
 			let log = await Log.findById(res.body.id)
-			log.should.have.property('route', 'thread')
+			log.should.have.property('route', 'userPosts')
 			log.should.have.property('UserId', 1)
 		})
 
@@ -170,6 +170,30 @@ describe('Log', () => {
 					done()
 				})
 		})
+		it('should return an error if not logged in for settingsAccount route', done => {
+			chai.request(server)
+				.post('/api/v1/log')
+				.set('content-type', 'application/json')
+				.send({ route: 'settingsAccount' })
+				.end((err, res) => {
+					res.should.have.status(401)
+					res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
+
+					done()
+				})
+		})
+		it('should return an error if not logged in for settingsGeneral route', done => {
+			chai.request(server)
+				.post('/api/v1/log')
+				.set('content-type', 'application/json')
+				.send({ route: 'settingsGeneral' })
+				.end((err, res) => {
+					res.should.have.status(401)
+					res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
+
+					done()
+				})
+		})
 
 	})