Skip to content

Commit dcea09d

Browse files
committed
add test
1 parent d1d50b0 commit dcea09d

File tree

6 files changed

+180
-6
lines changed

6 files changed

+180
-6
lines changed

.editorconfig

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,5 @@ insert_final_newline = true
3636
[**.yml]
3737
indent_size = 2
3838

39-
[**.php]
40-
indent_size = 4
41-
indent_style = space
39+
[Makefile]
40+
indent_style = tab

Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
ROOT=$(shell pwd)
2+
3+
test: test-unit test-integration
4+
5+
test-unit:
6+
@echo "\nRunning unit tests..."
7+
@NODE_ENV=test CONFIG_FILE=${ROOT}/config/config.test.js mocha test/unit --recursive
8+
9+
test-integration:
10+
@echo "\nRunning integration tests..."
11+
@NODE_ENV=test CONFIG_FILE=${ROOT}/config/config.test.js mocha test/api/init test/api/auth --recursive
12+
13+
coverage:
14+
@echo "\n\nRunning coverage report..."
15+
rm -rf coverage
16+
@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 \
17+
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
21+
22+
.PHONY: coverage

config/config.test.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var config = {};
2+
config.test = {
3+
//数据库配置
4+
db: {
5+
username: "root",
6+
password: null,
7+
database: "codepush_test",
8+
host: "127.0.0.1",
9+
dialect: "mysql"
10+
},
11+
//七牛云存储配置 当storageType为qiniu时需要配置
12+
qiniu: {
13+
accessKey: "",
14+
secretKey: "",
15+
bucketName: "",
16+
downloadUrl: "" //文件下载域名地址
17+
},
18+
//文件存储在本地配置 当storageType为local时需要配置
19+
local: {
20+
storageDir: "/Users/tablee/workspaces/storage",
21+
//文件下载地址 CodePush Server 地址 + '/download' download对应app.js里面的地址
22+
downloadUrl: "http://localhost:3000/download"
23+
},
24+
common: {
25+
//登录jwt签名密钥,必须更改,否则有安全隐患,可以使用随机生成的字符串
26+
loginSecret: "CodePushServer",
27+
//当天登录密码错误尝试次数,超过次数帐户将会锁定。0:表示无限制,可能会出现暴力破解。 大于0:必须开启redis服务。
28+
tryLoginTimes: 0,
29+
//CodePush Web部署地址,也可以配置成CodePush Web地址
30+
//codePushWebUrl: "http://localhost:3001",
31+
//差异化更新版本生成数目 默认为3
32+
diffNums: 3,
33+
//数据目录,优化选项
34+
dataDir: "/Users/tablee/workspaces/data",
35+
//选择存储类型,目前支持local和qiniu配置
36+
storageType: "local"
37+
},
38+
//邮件配置,注册模块验证邮箱需要使用 参考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+
},
48+
//配置redis, 注册时需要, 登录限制密码出错次数
49+
redis: {
50+
default: {
51+
host: "127.0.0.1",
52+
port: 6379,
53+
retry_strategy: function (options) {
54+
if (options.error.code === 'ECONNREFUSED') {
55+
// End reconnecting on a specific error and flush all commands with a individual error
56+
return new Error('The server refused the connection');
57+
}
58+
if (options.total_retry_time > 1000 * 60 * 60) {
59+
// End reconnecting after a specific timeout and flush all commands with a individual error
60+
return new Error('Retry time exhausted');
61+
}
62+
if (options.times_connected > 10) {
63+
// End reconnecting with built in error
64+
return undefined;
65+
}
66+
// reconnect after
67+
return Math.max(options.attempt * 100, 3000);
68+
}
69+
}
70+
}
71+
}
72+
module.exports = config;

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"scripts": {
1010
"start": "node ./bin/www",
11-
"test": "istanbul cover _mocha"
11+
"test": "make test",
12+
"coverage": "make coverage"
1213
},
1314
"author": {
1415
"name": "Tab Lee",

test/routes.auth.test.js test/api/auth/auth.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var app = require('../app');
1+
var app = require('../../../app');
22
var request = require('supertest')(app);
33
var should = require("should");
44

5-
describe('routes/auth/test.js', function() {
5+
describe('api/auth/test.js', function() {
66
var account = '[email protected]';
77
var password = '123456';
88
describe('sign in', function(done) {

test/api/init/database.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var config = require('../../../core/config');
2+
var mysql = require('mysql');
3+
var redis = require('redis');
4+
var should = require('should');
5+
var fs = require('fs');
6+
var path = require('path');
7+
8+
describe('api/init/database.js', function() {
9+
10+
describe('create database', function(done) {
11+
it('should create database successful', function(done) {
12+
var connection = mysql.createConnection({
13+
host: config.db.host,
14+
user: config.db.username,
15+
password: config.db.password
16+
});
17+
connection.connect();
18+
connection.query(`CREATE DATABASE IF NOT EXISTS ${config.db.database}`, function(err, rows, fields) {
19+
should.not.exist(err);
20+
done();
21+
});
22+
connection.end();
23+
});
24+
});
25+
26+
describe('flushall redis', function(done) {
27+
it('should flushall redis successful', function(done) {
28+
var client = redis.createClient(config.redis.default);
29+
client.flushall(function (err, reply) {
30+
should.not.exist(err);
31+
reply.toLowerCase().should.equal('ok');
32+
done();
33+
});
34+
});
35+
});
36+
37+
describe('import data from sql files', function(done) {
38+
var connection;
39+
before(function() {
40+
connection = mysql.createConnection({
41+
host: config.db.host,
42+
user: config.db.username,
43+
password: config.db.password,
44+
database: config.db.database,
45+
multipleStatements: true
46+
});
47+
connection.connect();
48+
});
49+
50+
after(function() {
51+
connection.end();
52+
});
53+
54+
it('should import data codepush.sql successful', function(done) {
55+
var sql = fs.readFileSync(path.resolve(__dirname, '../../../sql/codepush.sql'), 'utf-8');
56+
connection.query(sql, function(err, results) {
57+
should.not.exist(err);
58+
done();
59+
});
60+
});
61+
62+
it('should import data codepush-v0.1.1.sql successful', function(done) {
63+
var sql = fs.readFileSync(path.resolve(__dirname, '../../../sql/codepush-v0.1.1.sql'), 'utf-8');
64+
connection.query(sql, function(err, results) {
65+
should.not.exist(err);
66+
done();
67+
});
68+
});
69+
70+
it('should import data codepush-v0.1.5.sql successful', function(done) {
71+
var sql = fs.readFileSync(path.resolve(__dirname, '../../../sql/codepush-v0.1.5.sql'), 'utf-8');
72+
connection.query(sql, function(err, results) {
73+
should.not.exist(err);
74+
done();
75+
});
76+
});
77+
78+
});
79+
80+
});

0 commit comments

Comments
 (0)