Skip to content

Commit bf0cad7

Browse files
committed
Refine test cases
1 parent f174874 commit bf0cad7

File tree

7 files changed

+253
-139
lines changed

7 files changed

+253
-139
lines changed

test/api_common.test.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
var API = require('../').API;
2+
var urllib = require('urllib');
3+
var muk = require('muk');
4+
var expect = require('expect.js');
5+
var config = require('./config');
6+
7+
describe('common.js', function () {
8+
describe('mixin', function () {
9+
it('should ok', function () {
10+
API.mixin({sayHi: function () {}});
11+
expect(API.prototype).to.have.property('sayHi');
12+
});
13+
14+
it('should not ok when override method', function () {
15+
var obj = {sayHi: function () {}};
16+
expect(API.mixin).withArgs(obj).to.throwException(/Don't allow override existed prototype method\./);
17+
});
18+
});
19+
20+
describe('getAccessToken', function () {
21+
it('should ok', function (done) {
22+
var api = new API(config.appid, config.appsecret);
23+
api.getAccessToken(function (err, token) {
24+
expect(err).not.to.be.ok();
25+
expect(token).to.only.have.keys('accessToken', 'expireTime');
26+
done();
27+
});
28+
});
29+
30+
it('should not ok', function (done) {
31+
var api = new API('appid', 'secret');
32+
api.getAccessToken(function (err, token) {
33+
expect(err).to.be.ok();
34+
expect(err).to.have.property('name', 'WeChatAPIError');
35+
expect(err).to.have.property('message', 'invalid credential');
36+
done();
37+
});
38+
});
39+
40+
describe('mock urllib err', function () {
41+
before(function () {
42+
muk(urllib, 'request', function (url, args, callback) {
43+
var err = new Error('Urllib Error');
44+
err.name = 'UrllibError';
45+
callback(err);
46+
});
47+
});
48+
49+
after(function () {
50+
muk.restore();
51+
});
52+
53+
it('should get mock error', function (done) {
54+
var api = new API('appid', 'secret');
55+
api.getAccessToken(function (err, token) {
56+
expect(err).to.be.ok();
57+
expect(err).to.have.property('name', 'WeChatAPIUrllibError');
58+
expect(err).to.have.property('message', 'Urllib Error');
59+
done();
60+
});
61+
});
62+
});
63+
64+
describe('mock token', function () {
65+
before(function () {
66+
muk(urllib, 'request', function (url, args, callback) {
67+
process.nextTick(function () {
68+
callback(null, {"access_token": "ACCESS_TOKEN","expires_in": 7200});
69+
});
70+
});
71+
});
72+
after(function () {
73+
muk.restore();
74+
});
75+
76+
it('should ok', function (done) {
77+
var api = new API('appid', 'secret');
78+
api.getAccessToken(function (err, token) {
79+
expect(err).not.to.be.ok();
80+
expect(token).to.have.property('accessToken', 'ACCESS_TOKEN');
81+
// token.should.have.property('expireTime', 7200);
82+
done();
83+
});
84+
});
85+
});
86+
});
87+
88+
describe('preRequest', function () {
89+
it('should ok', function (done) {
90+
var api = new API(config.appid, config.appsecret);
91+
api.preRequest(function (callback) {
92+
callback();
93+
}, [function () {
94+
done();
95+
}]);
96+
});
97+
});
98+
99+
describe('getLatestToken', function () {
100+
it('should ok', function (done) {
101+
var api = new API(config.appid, config.appsecret);
102+
api.getLatestToken(function (err, token) {
103+
expect(err).not.to.be.ok();
104+
expect(token).to.only.have.keys('accessToken', 'expireTime');
105+
done();
106+
});
107+
});
108+
});
109+
});

test/api_custom_service.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var config = require('./config');
2+
var API = require('../').API;
3+
var muk = require('muk');
4+
var urllib = require('urllib');
5+
var expect = require('expect.js');
6+
7+
describe('api_custom_service', function () {
8+
var api = new API(config.appid, config.appsecret);
9+
10+
describe('getRecords', function () {
11+
it('should unauthorized with empty list', function (done) {
12+
api.updateFeedback('openid', 'feedback_id', function (err, data, res) {
13+
expect(err).to.be.ok();
14+
expect(data).to.have.property('errcode', 48001);
15+
expect(data).to.have.property('errmsg', 'api unauthorized');
16+
done();
17+
});
18+
});
19+
20+
describe('getRecords mock', function () {
21+
before(function () {
22+
muk(urllib, 'request', function (url, args, callback) {
23+
var data = {"recordlist": []};
24+
var res = {
25+
headers: {
26+
'content-type': 'application/json'
27+
}
28+
};
29+
process.nextTick(function () {
30+
callback(null, data, res);
31+
});
32+
});
33+
});
34+
35+
after(function () {
36+
muk.restore();
37+
});
38+
39+
it('getRecords should ok', function (done) {
40+
var condition = {
41+
"starttime" : 123456789,
42+
"endtime" : 987654321,
43+
// "openid" : "OPENID",
44+
"pagesize" : 10,
45+
"pageindex" : 1,
46+
};
47+
48+
api.getRecords(condition, function (err, data, res) {
49+
expect(err).not.to.be.ok();
50+
expect(data).to.have.property('recordlist');
51+
done();
52+
});
53+
});
54+
});
55+
});
56+
57+
describe('getCustomServiceList', function () {
58+
it('should unauthorized', function (done) {
59+
api.getCustomServiceList(function (err, data, res) {
60+
expect(err).to.be.ok();
61+
expect(data).to.have.property('errcode', 48001);
62+
expect(data).to.have.property('errmsg', 'api unauthorized');
63+
done();
64+
});
65+
});
66+
});
67+
68+
describe('getOnlineCustomServiceList', function () {
69+
it('should unauthorized', function (done) {
70+
api.getOnlineCustomServiceList(function (err, data, res) {
71+
expect(err).to.be.ok();
72+
expect(data).to.have.property('errcode', 48001);
73+
expect(data).to.have.property('errmsg', 'api unauthorized');
74+
done();
75+
});
76+
});
77+
});
78+
});

test/api_feedback.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var config = require('./config');
2+
var API = require('../').API;
3+
var expect = require('expect.js');
4+
5+
describe('api_feedback', function () {
6+
var api = new API(config.appid, config.appsecret);
7+
8+
describe('updateFeedback', function () {
9+
it('should unauthorized with empty list', function (done) {
10+
api.updateFeedback('openid', 'feedback_id', function (err, data, res) {
11+
expect(err).to.be.ok();
12+
expect(data).to.have.property('errcode', 48001);
13+
expect(data).to.have.property('errmsg', 'api unauthorized');
14+
done();
15+
});
16+
});
17+
});
18+
});

test/api_payment.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var config = require('./config');
2+
var API = require('../').API;
3+
var expect = require('expect.js');
4+
5+
describe('api_payment', function () {
6+
var api = new API(config.appid, config.appsecret);
7+
8+
describe('deliverNotify', function () {
9+
it('should unauthorized', function (done) {
10+
api.deliverNotify('{}', function (err, data, res) {
11+
expect(err).to.be.ok();
12+
expect(data).to.have.property('errcode', 48001);
13+
expect(data).to.have.property('errmsg', 'api unauthorized');
14+
done();
15+
});
16+
});
17+
});
18+
19+
describe('orderQuery', function () {
20+
it('orderQuery should ok', function (done) {
21+
api.orderQuery('{}', function (err, data, res) {
22+
expect(err).to.be.ok();
23+
expect(data).to.have.property('errcode', 48001);
24+
expect(data).to.have.property('errmsg', 'api unauthorized');
25+
done();
26+
});
27+
});
28+
});
29+
});

test/api_shop_common.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var path = require('path');
2+
var config = require('./config');
3+
var API = require('../').API;
4+
var expect = require('expect.js');
5+
6+
describe('api_shop_common', function () {
7+
var api = new API(config.appid, config.appsecret);
8+
9+
describe('uploadPicture', function () {
10+
it('should unauthorized', function (done) {
11+
api.uploadPicture(path.join(__dirname, 'fixture/image.jpg'), function (err, data, res) {
12+
expect(err).to.be.ok();
13+
expect(data).to.have.property('errcode', 48001);
14+
expect(data).to.have.property('errmsg', 'api unauthorized');
15+
done();
16+
});
17+
});
18+
});
19+
});

test/common.test.js

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -13,86 +13,6 @@ var thumbId = 'BHxGDVy7WY6BCOcv3AwbywUE630Vw0tAV_V8bzBaCZid4Km5fwXrVOso3X0zas4n'
1313
var movieId = 'b4F8SfaZZQwalDxwPjd923ACV5IUeYvZ9-dYKf5ytXrS-IImXEkl2U8Fl5EH-jCF';
1414

1515
describe('common.js', function () {
16-
describe('mixin', function () {
17-
it('should ok', function () {
18-
API.mixin({sayHi: function () {}});
19-
expect(API.prototype).to.have.property('sayHi');
20-
});
21-
22-
it('should not ok when override method', function () {
23-
var obj = {sayHi: function () {}};
24-
expect(API.mixin).withArgs(obj).to.throwException(/Don't allow override existed prototype method\./);
25-
});
26-
});
27-
28-
describe('getAccessToken', function () {
29-
it('should ok', function (done) {
30-
var api = new API(config.appid, config.appsecret);
31-
api.getAccessToken(function (err, token) {
32-
should.not.exist(err);
33-
expect(token).to.only.have.keys('accessToken', 'expireTime');
34-
done();
35-
});
36-
});
37-
38-
it('should not ok', function (done) {
39-
var api = new API('appid', 'secret');
40-
api.getAccessToken(function (err, token) {
41-
should.exist(err);
42-
err.name.should.be.equal('WeChatAPIError');
43-
err.message.should.be.equal('invalid credential');
44-
done();
45-
});
46-
});
47-
48-
describe('mock urllib err', function () {
49-
before(function () {
50-
muk(urllib, 'request', function (url, args, callback) {
51-
var err = new Error('Urllib Error');
52-
err.name = 'UrllibError';
53-
callback(err);
54-
});
55-
});
56-
57-
after(function () {
58-
muk.restore();
59-
});
60-
61-
it('should get mock error', function (done) {
62-
var api = new API('appid', 'secret');
63-
api.getAccessToken(function (err, token) {
64-
should.exist(err);
65-
err.name.should.be.equal('WeChatAPIUrllibError');
66-
err.message.should.be.equal('Urllib Error');
67-
done();
68-
});
69-
});
70-
});
71-
72-
describe('mock token', function () {
73-
before(function () {
74-
muk(urllib, 'request', function (url, args, callback) {
75-
process.nextTick(function () {
76-
callback(null, {"access_token": "ACCESS_TOKEN","expires_in": 7200});
77-
});
78-
});
79-
});
80-
after(function () {
81-
muk.restore();
82-
});
83-
84-
it('should ok', function (done) {
85-
var api = new API('appid', 'secret');
86-
api.getAccessToken(function (err, token) {
87-
should.not.exist(err);
88-
token.should.have.property('accessToken', 'ACCESS_TOKEN');
89-
// token.should.have.property('expireTime', 7200);
90-
done();
91-
});
92-
});
93-
});
94-
});
95-
9616
describe('isAccessTokenValid', function () {
9717
it('should invalid', function () {
9818
var token = new API.AccessToken('token', new Date().getTime() - 7200 * 1000);
@@ -630,41 +550,5 @@ describe('common.js', function () {
630550
});
631551
});
632552
});
633-
634-
describe('getRecords mock', function () {
635-
before(function () {
636-
muk(urllib, 'request', function (url, args, callback) {
637-
var data = {"recordlist": []};
638-
var res = {
639-
headers: {
640-
'content-type': 'application/json'
641-
}
642-
};
643-
process.nextTick(function () {
644-
callback(null, data, res);
645-
});
646-
});
647-
});
648-
649-
after(function () {
650-
muk.restore();
651-
});
652-
653-
it('getRecords should ok', function (done) {
654-
var condition = {
655-
"starttime" : 123456789,
656-
"endtime" : 987654321,
657-
// "openid" : "OPENID",
658-
"pagesize" : 10,
659-
"pageindex" : 1,
660-
};
661-
662-
api.getRecords(condition, function (err, data, res) {
663-
expect(err).not.to.be.ok();
664-
expect(data).to.have.property('recordlist');
665-
done();
666-
});
667-
});
668-
});
669553
});
670554
});

0 commit comments

Comments
 (0)