Skip to content

Commit 45f0550

Browse files
committed
unit test
1 parent 432eff3 commit 45f0550

8 files changed

Lines changed: 317 additions & 21 deletions

File tree

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ test-unit:
88

99
test-integration:
1010
@echo "\nRunning integration tests..."
11-
@NODE_ENV=test CONFIG_FILE=${ROOT}/config/config.test.js mocha test/api/init test/api/auth --recursive
11+
@NODE_ENV=test CONFIG_FILE=${ROOT}/config/config.test.js mocha test/api/init test/api/users test/api/auth test/api/account --recursive
1212

1313
coverage:
1414
@echo "\n\nRunning coverage report..."
1515
rm -rf coverage
1616
@NODE_ENV=test CONFIG_FILE=${ROOT}/config/config.test.js ./node_modules/istanbul/lib/cli.js cover --report none --dir coverage/core ./node_modules/.bin/_mocha \
1717
test/unit -- --recursive
18-
./node_modules/istanbul/lib/cli.js cover --report none --dir coverage/api ./node_modules/.bin/_mocha \
19-
test/api/init test/api/auth -- --recursive
20-
./node_modules/istanbul/lib/cli.js report
18+
@NODE_ENV=test CONFIG_FILE=${ROOT}/config/config.test.js ./node_modules/istanbul/lib/cli.js cover --report none --dir coverage/api ./node_modules/.bin/_mocha \
19+
test/api/init test/api/users test/api/auth test/api/account -- --recursive
20+
@NODE_ENV=test CONFIG_FILE=${ROOT}/config/config.test.js ./node_modules/istanbul/lib/cli.js report
2121

2222
.PHONY: coverage

config/config.test.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,7 @@ config.test = {
3636
storageType: "local"
3737
},
3838
//邮件配置,注册模块验证邮箱需要使用 参考https://github.com/nodemailer/nodemailer
39-
smtpConfig:{
40-
host: "smtp.mxhichina.com",
41-
port: 465,
42-
secure: true,
43-
auth: {
44-
user: "",
45-
pass: ""
46-
}
47-
},
39+
smtpConfig: false,
4840
//配置redis, 注册时需要, 登录限制密码出错次数
4941
redis: {
5042
default: {

core/services/account-manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ proto.sendRegisterCode = function (email) {
162162
return models.Users.findOne({where: {email: email}})
163163
.then(function (u) {
164164
if (u) {
165-
throw new Error(`"${email}" 已经注册,请更换邮箱注册`);
165+
throw new Error(`"${email}" 已经注册过,请更换邮箱注册`);
166166
}
167167
})
168168
.then(function () {

core/services/email-manager.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ proto.sendMail = function (options) {
2222
return reject(new Error("to是必传参数"));
2323
}
2424
var smtpConfig = _.get(config, 'smtpConfig');
25+
if (!smtpConfig) {
26+
resolve({});
27+
}
2528
var transporter = nodemailer.createTransport(smtpConfig);
2629
var sendEmailAddress = _.get(smtpConfig, 'auth.user');
2730
var defaultMailOptions = {

routes/users.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ router.post('/', function (req, res, next) {
1818
return accountManager.checkRegisterCode(email, token)
1919
.then(function (u) {
2020
if (_.isString(password) && password.length < 6) {
21-
throw new Error('密码长度至少为6位');
21+
throw new Error('请您输入6~20位长度的密码');
2222
}
2323
return accountManager.register(email, password);
2424
})
@@ -32,9 +32,12 @@ router.post('/', function (req, res, next) {
3232
});
3333

3434
router.get('/exists', function (req, res, next) {
35-
var email = _.get(req, 'query.email');
35+
var email = _.trim(_.get(req, 'query.email'));
3636
models.Users.findOne({where: {email: email}})
3737
.then(function (u) {
38+
if (!email) {
39+
throw new Error(`请您输入邮箱地址`);
40+
}
3841
res.send({status: "OK", exists: u ? true : false});
3942
})
4043
.catch(function (e) {

test/api/account/account.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var app = require('../../../app');
2+
var request = require('supertest')(app);
3+
var should = require("should");
4+
var security = require('../../../core/utils/security');
5+
var factory = require('../../../core/utils/factory');
6+
var _ = require('lodash');
7+
8+
describe('api/account/account.test.js', function() {
9+
var account = '522539441@qq.com';
10+
var password = '123456';
11+
12+
describe('user modules', function(done) {
13+
var authToken;
14+
before(function(done){
15+
request.post('/auth/login')
16+
.send({
17+
account: account,
18+
password: password
19+
})
20+
.end(function(err, res) {
21+
should.not.exist(err);
22+
var rs = JSON.parse(res.text);
23+
rs.should.containEql({status:"OK"});
24+
authToken = (new Buffer(`auth:${_.get(rs, 'results.tokens')}`)).toString('base64');
25+
done();
26+
});
27+
});
28+
29+
it('should get account info successful', function(done) {
30+
request.get(`/account`)
31+
.set('Authorization', `Basic ${authToken}`)
32+
.send()
33+
.end(function(err, res) {
34+
should.not.exist(err);
35+
res.status.should.equal(200);
36+
var rs = JSON.parse(res.text);
37+
rs.should.have.properties('account');
38+
rs.account.should.have.properties(['email', 'id', 'linkedProviders', 'name']);
39+
done();
40+
});
41+
});
42+
43+
});
44+
45+
});

test/api/auth/auth.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ var request = require('supertest')(app);
33
var should = require("should");
44

55
describe('api/auth/test.js', function() {
6-
var account = 'lisong2010@gmail.com';
6+
var account = '522539441@qq.com';
77
var password = '123456';
8+
89
describe('sign in', function(done) {
910
it('should not sign in successful when account is empty', function(done) {
1011
request.post('/auth/login')
@@ -14,7 +15,7 @@ describe('api/auth/test.js', function() {
1415
})
1516
.end(function(err, res) {
1617
should.not.exist(err);
17-
res.text.should.equal(`{"status":"ERROR","errorMessage":"请您输入邮箱地址"}`);
18+
JSON.parse(res.text).should.containEql({status:"ERROR",errorMessage:"请您输入邮箱地址"});
1819
done();
1920
});
2021
});
@@ -26,7 +27,7 @@ describe('api/auth/test.js', function() {
2627
})
2728
.end(function(err, res) {
2829
should.not.exist(err);
29-
res.text.should.equal(`{"status":"ERROR","errorMessage":"您输入的邮箱或密码有误"}`);
30+
JSON.parse(res.text).should.containEql({status:"ERROR",errorMessage:"您输入的邮箱或密码有误"});
3031
done();
3132
});
3233
});
@@ -38,7 +39,7 @@ describe('api/auth/test.js', function() {
3839
})
3940
.end(function(err, res) {
4041
should.not.exist(err);
41-
res.text.should.equal(`{"status":"ERROR","errorMessage":"您输入的邮箱或密码有误"}`);
42+
JSON.parse(res.text).should.containEql({status:"ERROR",errorMessage:"您输入的邮箱或密码有误"});
4243
done();
4344
});
4445
});
@@ -50,7 +51,7 @@ describe('api/auth/test.js', function() {
5051
})
5152
.end(function(err, res) {
5253
should.not.exist(err);
53-
JSON.parse(res.text).status.should.equal('OK')
54+
JSON.parse(res.text).should.containEql({status:"OK"})
5455
done();
5556
});
5657
});

0 commit comments

Comments
 (0)