Sfoglia il codice sorgente

Implement category test

sbkwgh 8 anni fa
parent
commit
5634d6aff5
1 ha cambiato i file con 54 aggiunte e 40 eliminazioni
  1. 54 40
      test/category.js

+ 54 - 40
test/category.js

@@ -4,14 +4,14 @@ let chai = require('chai')
 let server = require('../server')
 let should = chai.should()
 
-let { User, Category } = require('../models')
+let { sequelize } = require('../models')
 
 const Errors = require('../lib/errors.js')
 
 chai.use(require('chai-http'))
 chai.use(require('chai-things'))
 
-describe('Thread and post', () => {
+describe('Category', () => {
 	//Wait for app to start before commencing
 	before((done) => {
 		server.on('appStarted', () => {
@@ -22,10 +22,7 @@ describe('Thread and post', () => {
 	//Delete all rows in table after
 	//tests completed
 	after((done) => {
-		Promise.all([
-			User.sync({ force: true }),
-			Category.sync({ force: true })
-		])
+		sequelize.sync({ force: true })
 			.then(() => {
 				done(null);
 			})
@@ -57,14 +54,19 @@ describe('Thread and post', () => {
 			res.body.should.have.property('name', 'category')
 		})
 		it('should return an error if category already exists', async () => {
-			let res = await agent
-				.post('/api/v1/category')
-				.set('content-type', 'application/json')
-				.send({ name: 'category' })
-
-			res.should.be.json
-			res.should.have.status(400)
-			res.body.errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyCreated)
+			try {
+				let res = await agent
+					.post('/api/v1/category')
+					.set('content-type', 'application/json')
+					.send({ name: 'category' })
+
+				res.should.be.json
+				res.should.have.status(400)
+				res.body.errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyExists)
+			} catch (res) {
+				res.should.have.status(400)
+				JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyExists)
+			}
 		})
 		it('should return an error if not an admin account', async () => {
 			let agent = chai.request.agent(server)
@@ -77,39 +79,51 @@ describe('Thread and post', () => {
 					password: 'password',
 				})
 
-			let res = await agent
-				.post('/api/v1/category')
-				.set('content-type', 'application/json')
-				.send({ name: 'category1' })
-
-			res.should.be.json
-			res.should.have.status(401)
-			res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
+			try {
+				let res = await agent
+					.post('/api/v1/category')
+					.set('content-type', 'application/json')
+					.send({ name: 'category1' })
+
+				res.should.be.json
+				res.should.have.status(401)
+				res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
+			} catch (res) {
+				res.should.have.status(401)
+				JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
+			}
 		})
 		it('should return an error if not logged', async () => {
-			await chai.request(server)
-				.post('/api/v1/category')
-				.set('content-type', 'application/json')
-				.send({ name: 'category1' })
-
-			res.should.be.json
-			res.should.have.status(401)
-			res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
+			try {
+				await chai.request(server)
+					.post('/api/v1/category')
+					.set('content-type', 'application/json')
+					.send({ name: 'category1' })
+
+				res.should.be.json
+				res.should.have.status(401)
+				res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
+			} catch (res) {
+				res.should.have.status(401)
+				JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
+			}
 		})
 	})
 
-	describe('GET /category', async () => {
-		let agent = chai.request.agent(server)
+	describe('GET /category', () => {
+		before(async () => {
+			let agent = chai.request.agent(server)
 
-		await agent
-			.post('/api/v1/user/adminaccount')
-			.set('content-type', 'application/json')
-			.send({ password: 'password' })
+			await agent
+				.post('/api/v1/user/adminaccount/login')
+				.set('content-type', 'application/json')
+				.send({ password: 'password' })
 
-		await agent
-			.post('/api/v1/category')
-			.set('content-type', 'application/json')
-			.send({ name: 'another_category' })
+			await agent
+				.post('/api/v1/category')
+				.set('content-type', 'application/json')
+				.send({ name: 'another_category' })
+		})
 
 		it('should return all categories', async () => {
 			let res = await chai.request(server)