-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e59c117
commit 99a3b63
Showing
10 changed files
with
205 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ logs/ | |
npm-debug.log | ||
node_modules/ | ||
coverage/ | ||
run/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ install: | |
script: | ||
- npm run ci | ||
after_script: | ||
- npm i codecov && codecov | ||
- npminstall codecov && codecov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
|
||
module.exports = function(app) { | ||
app.validator.addRule('json', function (rule, value) { | ||
try { | ||
JSON.parse(value); | ||
} catch (err) { | ||
return this.t('must be json string'); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const createRule = { | ||
username: { | ||
type: 'email', | ||
}, | ||
password: { | ||
type: 'password', | ||
compare: 're-password' | ||
}, | ||
addition: { | ||
required: false, | ||
type: 'json' | ||
}, | ||
}; | ||
|
||
exports.create = function* () { | ||
this.validate(createRule); | ||
this.body = this.request.body; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
module.exports = function (app) { | ||
app.get('/users.json', app.controller.user.create); | ||
app.post('/users.json', app.controller.user.create); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
exports.security = { | ||
ctoken: false, | ||
csrf: false, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "validate_form" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
'use strict'; | ||
|
||
const request = require('supertest-as-promised'); | ||
const mm = require('egg-mock'); | ||
|
||
describe('test/validate.test.js', () => { | ||
let app; | ||
before(() => { | ||
app = mm.app({ | ||
baseDir: 'validate_form', | ||
plugin: 'validate', | ||
}); | ||
return app.ready(); | ||
}); | ||
|
||
describe('get', () => { | ||
it('should return invalid_param when body empty', () => { | ||
return request(app.listen()) | ||
.get('/users.json') | ||
.expect({ | ||
code: 'invalid_param', | ||
message: 'Validation Failed', | ||
errors: [ | ||
{ field: 'username', code: 'missing_field', message: 'required' }, | ||
{ field: 'password', code: 'missing_field', message: 'required' }, | ||
], | ||
}) | ||
.expect(422); | ||
}); | ||
|
||
it('should all pass', () => { | ||
return request(app.listen()) | ||
.get('/users.json') | ||
.send({ | ||
username: 'foo@gmail.com', | ||
password: '123456', | ||
're-password': '123456', | ||
}) | ||
.expect({ | ||
username: 'foo@gmail.com', | ||
password: '123456', | ||
're-password': '123456', | ||
}) | ||
.expect(200); | ||
}); | ||
|
||
}); | ||
|
||
describe('post', () => { | ||
it('should return invalid_param when body empty', () => { | ||
return request(app.listen()) | ||
.post('/users.json') | ||
.expect({ | ||
code: 'invalid_param', | ||
message: 'Validation Failed', | ||
errors: [ | ||
{ field: 'username', code: 'missing_field', message: 'required' }, | ||
{ field: 'password', code: 'missing_field', message: 'required' }, | ||
], | ||
}) | ||
.expect(422); | ||
}); | ||
|
||
it('should return invalid_param when length invaild', () => { | ||
return request(app.listen()) | ||
.post('/users.json') | ||
.send({ | ||
username: 'foo', | ||
password: '12345', | ||
}) | ||
.expect({ | ||
code: 'invalid_param', | ||
message: 'Validation Failed', | ||
errors: [ | ||
{ field: 'username', code: 'invalid', message: 'should be an email' }, | ||
{ field: 'password', code: 'invalid', message: 'length should bigger than 6' }, | ||
], | ||
}) | ||
.expect(422); | ||
}); | ||
|
||
it('should return invalid_param when password not equal to re-password', () => { | ||
return request(app.listen()) | ||
.post('/users.json') | ||
.send({ | ||
username: 'foo@gmail.com', | ||
password: '123456', | ||
're-password': '123123', | ||
}) | ||
.expect({ | ||
code: 'invalid_param', | ||
message: 'Validation Failed', | ||
errors: [ | ||
{ field: 'password', code: 'invalid', message: 'should equal to re-password' }, | ||
], | ||
}) | ||
.expect(422); | ||
}); | ||
|
||
it('should return invalid_param when username invaild', () => { | ||
return request(app.listen()) | ||
.post('/users.json') | ||
.send({ | ||
username: '.foo@gmail.com', | ||
password: '123456', | ||
're-password': '123456', | ||
}) | ||
.expect({ | ||
code: 'invalid_param', | ||
message: 'Validation Failed', | ||
errors: [{ field: 'username', code: 'invalid', message: 'should be an email' }], | ||
}) | ||
.expect(422); | ||
}); | ||
|
||
it('should all pass', () => { | ||
return request(app.listen()) | ||
.post('/users.json') | ||
.send({ | ||
username: 'foo@gmail.com', | ||
password: '123456', | ||
're-password': '123456', | ||
}) | ||
.expect({ | ||
username: 'foo@gmail.com', | ||
password: '123456', | ||
're-password': '123456', | ||
}) | ||
.expect(200); | ||
}); | ||
}); | ||
|
||
describe('addRule()', function() { | ||
it('should check custom rule ok', () => { | ||
return request(app.listen()) | ||
.post('/users.json') | ||
.send({ | ||
username: 'foo@gmail.com', | ||
password: '123456', | ||
're-password': '123456', | ||
addition: 'invalid json', | ||
}) | ||
.expect({ | ||
code: 'invalid_param', | ||
message: 'Validation Failed', | ||
errors: [{ field: 'addition', code: 'invalid', message: 'must be json string' }], | ||
}) | ||
.expect(422); | ||
}); | ||
}); | ||
}); |