Skip to content

Commit

Permalink
test: add test cases (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and fengmk2 committed Jul 22, 2016
1 parent e59c117 commit 99a3b63
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ logs/
npm-debug.log
node_modules/
coverage/
run/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ install:
script:
- npm run ci
after_script:
- npm i codecov && codecov
- npminstall codecov && codecov
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ environment:

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && npminstall
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
},
"devDependencies": {
"autod": "^2.6.1",
"egg": "*",
"egg-bin": "^1.0.2",
"egg-ci": "^1.0.2",
"egg-mock": "*",
"eslint": "^3.1.0",
"eslint-config-egg": "^3.1.0",
"should": "^9.0.2",
"supertest": "^1.2.0"
"supertest": "^1.2.0",
"supertest-as-promised": "^3.2.0"
},
"engines": {
"node": ">=4.0.0"
Expand All @@ -45,4 +48,4 @@
"homepage": "https://github.com/eggjs/egg-validate#readme",
"author": "dead_horse",
"license": "MIT"
}
}
12 changes: 12 additions & 0 deletions test/fixtures/validate_form/app.js
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');
}
});
};
20 changes: 20 additions & 0 deletions test/fixtures/validate_form/app/controller/user.js
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;
};
5 changes: 5 additions & 0 deletions test/fixtures/validate_form/app/router.js
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);
};
6 changes: 6 additions & 0 deletions test/fixtures/validate_form/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

exports.security = {
ctoken: false,
csrf: false,
};
3 changes: 3 additions & 0 deletions test/fixtures/validate_form/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "validate_form"
}
151 changes: 151 additions & 0 deletions test/validate.test.js
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);
});
});
});

0 comments on commit 99a3b63

Please sign in to comment.