Przeglądaj źródła

Add and implement test to not allow blank characters in username

sbkwgh 7 lat temu
rodzic
commit
6d92e495f3
2 zmienionych plików z 22 dodań i 0 usunięć
  1. 5 0
      models/user.js
  2. 17 0
      test/user.js

+ 5 - 0
models/user.js

@@ -18,6 +18,11 @@ module.exports = (sequelize, DataTypes) => {
 					if(typeof val !== 'string') {
 						throw new sequelize.ValidationError('username must be a string')
 					}
+				},
+				containsNoBlankCharacters (val) {
+					if(/\s/g.test(val)) {
+						throw new sequelize.ValidationError('username can\'t contain blank characters')
+					}
 				}
 			}
 		},

+ 17 - 0
test/user.js

@@ -278,6 +278,23 @@ describe('User', () => {
 					done()
 				})
 		})
+		it('should give an error if an username has blank character', (done) => {
+			chai.request(server)
+				.post('/api/v1/user')
+				.set('content-type', 'application/json')
+				.send({
+					username: 'username with space',
+					password: 'password'
+				})
+				.end((err, res) => {
+					res.should.have.status(400)
+					res.should.be.json
+					res.body.should.have.property('errors')
+					res.body.errors.should.include.something.that.has.property('message', 'username can\'t contain blank characters')
+
+					done()
+				})
+		})
 
 	})