Преглед изворни кода

Add tests and corresponding errors for username and password fields

sbkwgh пре 8 година
родитељ
комит
1464287215
2 измењених фајлова са 143 додато и 9 уклоњено
  1. 10 0
      lib/errors.js
  2. 133 9
      test/user.js

+ 10 - 0
lib/errors.js

@@ -1,7 +1,17 @@
 const Errors = {
 	unknown: 'An unknown error occured on our end. Please try again later',
+	accountAlreadyCreated: 'This account has already been created',
 	missingParameter (param) {
 		return `The request is missing the parameter "${param}"`
+	},
+	invalidParameterType (param, type) {
+		return `Parameter "${param}" must be of type ${type}`
+	},
+	parameterLengthTooSmall (param, length) {
+		return `Parameter "${param}" must be greater than ${length} characters in length`
+	},
+	parameterLengthTooLarge (param, length) {
+		return `Parameter "${param}" must be less than ${length} characters in length`
 	}
 }
 

+ 133 - 9
test/user.js

@@ -6,7 +6,7 @@ let server = require('../server')
 let should = chai.should()
 
 let User = require('../models').User
-const Errors = require('../lib/errors.js');
+const Errors = require('../lib/errors.js')
 
 chai.use(chaiHttp)
 
@@ -29,8 +29,8 @@ describe('User', () => {
 				.post('/api/v1/user')
 				.set('content-type', 'application/x-www-form-urlencoded')
 				.send({
-					username: 'test',
-					password: 'pass'
+					username: 'username',
+					password: 'password'
 				})
 				.end((err, res) => {
 					res.should.have.status(200)
@@ -41,34 +41,158 @@ describe('User', () => {
 					done()
 				})
 		})
+
+		it('should throw an error if account already created', (done) => {
+			chai.request(server)
+				.post('/api/v1/user')
+				.set('content-type', 'application/x-www-form-urlencoded')
+				.send({
+					username: 'username',
+					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.accountAlreadyCreated)
+					
+					done()
+				})
+		})
+
+
 		it('should throw an error if no username', (done) => {
 			chai.request(server)
 				.post('/api/v1/user')
 				.set('content-type', 'application/x-www-form-urlencoded')
 				.send({
+					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.missingParameter('username'))
+					
+					done()
+				})
+		})
+		it('should throw an error if username is 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.be.json
+					res.body.should.have.property('error')
+					res.body.error.should.deep.equal(Errors.invalidParameterType('password', 'string'))
+					
+					done()
+				})
+		})
+		it('should throw an error if password less than 6 characters', (done) => {
+			chai.request(server)
+				.post('/api/v1/user')
+				.set('content-type', 'application/x-www-form-urlencoded')
+				.send({
+					username: 'username1',
 					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()
+					res.body.error.should.deep.equal(Errors.parameterLengthTooSmall('password', '6'))
 					
 					done()
 				})
 		})
-		it('should throw an error if no pass', (done) => {
+		it('should throw an error if password greater than 100 characters', (done) => {
 			chai.request(server)
 				.post('/api/v1/user')
 				.set('content-type', 'application/x-www-form-urlencoded')
 				.send({
-					username: 'test1'
+					username: 'username1',
+					password: '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901'
 				})
 				.end((err, res) => {
-					res.should.have.status(200)
+					res.should.have.status(500)
 					res.should.be.json
-					res.body.should.have.property('username', 'test')
-					res.body.should.have.property('hash')
+					res.body.should.have.property('error')
+					res.body.error.should.deep.equal(Errors.parameterLengthTooGreat('password', '100'))
 					
 					done()
 				})