فهرست منبع

Add chai-thing package, update/simplify tests

sbkwgh 8 سال پیش
والد
کامیت
a5736ebb37
2فایلهای تغییر یافته به همراه29 افزوده شده و 93 حذف شده
  1. 1 0
      package.json
  2. 28 93
      test/user.js

+ 1 - 0
package.json

@@ -21,6 +21,7 @@
   "devDependencies": {
     "chai": "^3.5.0",
     "chai-http": "^3.0.0",
+    "chai-things": "^0.2.0",
     "mocha": "^3.2.0",
     "morgan": "^1.7.0"
   }

+ 28 - 93
test/user.js

@@ -1,14 +1,14 @@
 process.env.NODE_ENV = 'test'
 
 let chai = require('chai')
-let chaiHttp = require('chai-http')
 let server = require('../server')
 let should = chai.should()
 
 let User = require('../models').User
 const Errors = require('../lib/errors.js')
 
-chai.use(chaiHttp)
+chai.use(require('chai-http'))
+chai.use(require('chai-things'))
 
 describe('User', () => {
 	//Delete all rows in table after
@@ -51,151 +51,86 @@ describe('User', () => {
 					password: 'password'
 				})
 				.end((err, res) => {
-					res.should.have.status(500)
+					res.should.have.status(400)
 					res.should.be.json
-					res.body.should.have.property('error')
-					res.body.error.should.deep.equal(Errors.accountAlreadyCreated)
+					res.body.should.have.property('errors')
+					res.body.errors.should.include.something.that.deep.equals(Errors.accountAlreadyCreated)
 					
 					done()
 				})
 		})
 
 
-		it('should throw an error if no username', (done) => {
+		it('should throw an error if no username or password', (done) => {
 			chai.request(server)
 				.post('/api/v1/user')
 				.set('content-type', 'application/x-www-form-urlencoded')
-				.send({
-					password: 'password'
-				})
+				.send({})
 				.end((err, res) => {
-					res.should.have.status(500)
+					res.should.have.status(400)
 					res.should.be.json
-					res.body.should.have.property('error')
-					res.body.error.should.deep.equal(Errors.missingParameter('username'))
+					res.body.should.have.property('errors')
+					res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('username'))
+					res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('password'))
 					
 					done()
 				})
 		})
-		it('should throw an error if username is not a string', (done) => {
+		it('should throw an error if username or password are not a string', (done) => {
 			chai.request(server)
 				.post('/api/v1/user')
 				.set('content-type', 'application/x-www-form-urlencoded')
 				.send({
 					username: 123,
-					password: 'password'
-				})
-				.end((err, res) => {
-					res.should.have.status(500)
-					res.should.be.json
-					res.body.should.have.property('error')
-					res.body.error.should.deep.equal(Errors.invalidParameterType('username', 'string'))
-					
-					done()
-				})
-		})
-		it('should throw an error if username less than 6 characters', (done) => {
-			chai.request(server)
-				.post('/api/v1/user')
-				.set('content-type', 'application/x-www-form-urlencoded')
-				.send({
-					username: 'test',
-					password: 'password'
-				})
-				.end((err, res) => {
-					res.should.have.status(500)
-					res.should.be.json
-					res.body.should.have.property('error')
-					res.body.error.should.deep.equal(Errors.parameterLengthTooSmall('username', '6'))
-					
-					done()
-				})
-		})
-		it('should throw an error if username greater than 50 characters', (done) => {
-			chai.request(server)
-				.post('/api/v1/user')
-				.set('content-type', 'application/x-www-form-urlencoded')
-				.send({
-					username: '123456789012345678901234567890123456789012345678901',
-					password: 'password'
-				})
-				.end((err, res) => {
-					res.should.have.status(500)
-					res.should.be.json
-					res.body.should.have.property('error')
-					res.body.error.should.deep.equal(Errors.parameterLengthTooGreat('username', '50'))
-					
-					done()
-				})
-		})
-
-
-		it('should throw an error if no password', (done) => {
-			chai.request(server)
-				.post('/api/v1/user')
-				.set('content-type', 'application/x-www-form-urlencoded')
-				.send({
-					username: 'username1'
-				})
-				.end((err, res) => {
-					res.should.have.status(500)
-					res.should.be.json
-					res.body.should.have.property('error')
-					res.body.error.should.deep.equal(Errors.missingParameter('password'))
-					
-					done()
-				})
-		})
-		it('should throw an error if password is not a string', (done) => {
-			chai.request(server)
-				.post('/api/v1/user')
-				.set('content-type', 'application/x-www-form-urlencoded')
-				.send({
-					username: 'username1',
 					password: 123
 				})
 				.end((err, res) => {
-					res.should.have.status(500)
+					res.should.have.status(400)
 					res.should.be.json
-					res.body.should.have.property('error')
-					res.body.error.should.deep.equal(Errors.invalidParameterType('password', 'string'))
+					res.body.should.have.property('errors')
+					res.body.should.have.property('errors')
+					res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('username', 'string'))
+					res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('password', 'string'))
 					
 					done()
 				})
 		})
-		it('should throw an error if password less than 6 characters', (done) => {
+		it('should throw an error if username or password less than 6 characters', (done) => {
 			chai.request(server)
 				.post('/api/v1/user')
 				.set('content-type', 'application/x-www-form-urlencoded')
 				.send({
-					username: 'username1',
+					username: 'test',
 					password: 'pass'
 				})
 				.end((err, res) => {
 					res.should.have.status(500)
 					res.should.be.json
-					res.body.should.have.property('error')
-					res.body.error.should.deep.equal(Errors.parameterLengthTooSmall('password', '6'))
+					res.body.should.have.property('errors')
+					res.body.error.should.contain.something.that.deep.equals(Errors.parameterLengthTooSmall('username', '6'))
+					res.body.error.should.contain.something.that.deep.equals(Errors.parameterLengthTooSmall('password', '6'))
 					
 					done()
 				})
 		})
-		it('should throw an error if password greater than 100 characters', (done) => {
+		it('should throw an error if username greater than 50 characters or password is greater than 100 characters', (done) => {
 			chai.request(server)
 				.post('/api/v1/user')
 				.set('content-type', 'application/x-www-form-urlencoded')
 				.send({
-					username: 'username1',
+					username: '123456789012345678901234567890123456789012345678901',
 					password: '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901'
 				})
 				.end((err, res) => {
 					res.should.have.status(500)
 					res.should.be.json
-					res.body.should.have.property('error')
-					res.body.error.should.deep.equal(Errors.parameterLengthTooGreat('password', '100'))
+					res.body.should.have.property('errors')
+					res.body.error.should.contain.something.that.deep.equals(Errors.parameterLengthTooGreat('username', '50'))
+					res.body.error.should.contain.something.that.deep.equals(Errors.parameterLengthTooGreat('password', '100'))
 					
 					done()
 				})
 		})
+
 	})
 })