Skip to content

Commit 08d64e1

Browse files
committed
Update test cases
1 parent bf0cad7 commit 08d64e1

File tree

8 files changed

+258
-38
lines changed

8 files changed

+258
-38
lines changed

lib/api_common.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,10 @@ API.prototype.getLatestToken = function (callback) {
222222
*/
223223
API.mixin = function (obj) {
224224
for (var key in obj) {
225-
if (obj.hasOwnProperty(key)) {
226-
if (API.prototype.hasOwnProperty(key)) {
227-
throw new Error('Don\'t allow override existed prototype method. method: '+ key);
228-
}
229-
API.prototype[key] = obj[key];
225+
if (API.prototype.hasOwnProperty(key)) {
226+
throw new Error('Don\'t allow override existed prototype method. method: '+ key);
230227
}
228+
API.prototype[key] = obj[key];
231229
}
232230
};
233231

test/api_common.test.js

Lines changed: 170 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,141 @@ describe('common.js', function () {
8383
});
8484
});
8585
});
86+
87+
describe('mock saveToken err', function () {
88+
var api = new API(config.appid, config.appsecret);
89+
before(function () {
90+
muk(api, 'saveToken', function (token, callback) {
91+
process.nextTick(function () {
92+
callback(new Error('mock saveToken err'));
93+
});
94+
});
95+
});
96+
after(function () {
97+
muk.restore();
98+
});
99+
100+
it('should ok', function (done) {
101+
api.getAccessToken(function (err, token) {
102+
expect(err).to.be.ok();
103+
expect(err).to.have.property('message', 'mock saveToken err');
104+
done();
105+
});
106+
});
107+
});
86108
});
87109

88110
describe('preRequest', function () {
89111
it('should ok', function (done) {
90112
var api = new API(config.appid, config.appsecret);
91113
api.preRequest(function (callback) {
92114
callback();
93-
}, [function () {
115+
}, [function (err) {
116+
expect(err).not.to.be.ok();
94117
done();
95118
}]);
96119
});
120+
121+
describe('mock getToken err', function () {
122+
var api = new API(config.appid, config.appsecret);
123+
before(function () {
124+
muk(api, 'getToken', function (callback) {
125+
process.nextTick(function () {
126+
callback(new Error('mock getToken error'));
127+
});
128+
});
129+
});
130+
after(function () {
131+
muk.restore();
132+
});
133+
134+
it('should not ok', function (done) {
135+
api.preRequest(function (callback) {
136+
callback();
137+
}, [function (err) {
138+
expect(err).to.be.ok();
139+
expect(err).have.property('message', 'mock getToken error');
140+
done();
141+
}]);
142+
});
143+
});
144+
145+
describe('mock getAccessToken err', function () {
146+
var api = new API(config.appid, config.appsecret);
147+
before(function () {
148+
muk(api, 'getAccessToken', function (callback) {
149+
process.nextTick(function () {
150+
callback(new Error('mock getAccessToken error'));
151+
});
152+
});
153+
});
154+
after(function () {
155+
muk.restore();
156+
});
157+
158+
it('should not ok', function (done) {
159+
api.preRequest(function (callback) {
160+
callback();
161+
}, [function (err) {
162+
expect(err).to.be.ok();
163+
expect(err).have.property('message', 'mock getAccessToken error');
164+
done();
165+
}]);
166+
});
167+
});
168+
169+
describe('mock getToken ok', function () {
170+
var api = new API(config.appid, config.appsecret);
171+
before(function () {
172+
muk(api, 'getToken', function (callback) {
173+
process.nextTick(function () {
174+
callback(null, {accessToken: 'token', expireTime: (new Date().getTime() + 10000)});
175+
});
176+
});
177+
});
178+
after(function () {
179+
muk.restore();
180+
});
181+
182+
it('should not ok', function (done) {
183+
api.preRequest(function (callback) {
184+
callback();
185+
}, [function (err) {
186+
expect(err).not.to.be.ok();
187+
done();
188+
}]);
189+
});
190+
});
191+
192+
describe('mock getToken ok', function () {
193+
var api = new API(config.appid, config.appsecret);
194+
before(function () {
195+
muk(api, 'getToken', function (callback) {
196+
process.nextTick(function () {
197+
callback(null, {accessToken: 'token', expireTime: (new Date().getTime() + 10000)});
198+
});
199+
});
200+
});
201+
202+
after(function () {
203+
muk.restore();
204+
});
205+
206+
it('should not ok', function (done) {
207+
var i = 0;
208+
api.preRequest(function (callback) {
209+
i++;
210+
if (i === 1) {
211+
callback(null, {errcode: 40001});
212+
} else {
213+
callback(null, {errcode: 0});
214+
}
215+
}, [function (err) {
216+
expect(err).not.to.be.ok();
217+
done();
218+
}]);
219+
});
220+
});
97221
});
98222

99223
describe('getLatestToken', function () {
@@ -105,5 +229,50 @@ describe('common.js', function () {
105229
done();
106230
});
107231
});
232+
233+
describe('mock getToken err', function () {
234+
var api = new API(config.appid, config.appsecret);
235+
before(function () {
236+
muk(api, 'getToken', function (callback) {
237+
process.nextTick(function () {
238+
callback(new Error('mock getToken error'));
239+
});
240+
});
241+
});
242+
after(function () {
243+
muk.restore();
244+
});
245+
246+
it('should not ok', function (done) {
247+
api.getLatestToken(function (err) {
248+
expect(err).to.be.ok();
249+
expect(err).have.property('message', 'mock getToken error');
250+
done();
251+
});
252+
});
253+
});
254+
255+
describe('mock getToken ok', function () {
256+
var api = new API(config.appid, config.appsecret);
257+
before(function () {
258+
muk(api, 'getToken', function (callback) {
259+
process.nextTick(function () {
260+
callback(null, {accessToken: 'token', expireTime: (new Date().getTime() + 10000)});
261+
});
262+
});
263+
});
264+
after(function () {
265+
muk.restore();
266+
});
267+
268+
it('should not ok', function (done) {
269+
api.getLatestToken(function (err, token) {
270+
expect(err).not.to.be.ok();
271+
expect(token).have.property('accessToken');
272+
expect(token).have.property('expireTime');
273+
done();
274+
});
275+
});
276+
});
108277
});
109278
});

test/api_custom_service.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ describe('api_custom_service', function () {
5858
it('should unauthorized', function (done) {
5959
api.getCustomServiceList(function (err, data, res) {
6060
expect(err).to.be.ok();
61-
expect(data).to.have.property('errcode', 48001);
62-
expect(data).to.have.property('errmsg', 'api unauthorized');
61+
expect(data).to.have.property('errcode');
62+
expect(data).to.have.property('errmsg');
6363
done();
6464
});
6565
});
@@ -69,8 +69,8 @@ describe('api_custom_service', function () {
6969
it('should unauthorized', function (done) {
7070
api.getOnlineCustomServiceList(function (err, data, res) {
7171
expect(err).to.be.ok();
72-
expect(data).to.have.property('errcode', 48001);
73-
expect(data).to.have.property('errmsg', 'api unauthorized');
72+
expect(data).to.have.property('errcode');
73+
expect(data).to.have.property('errmsg');
7474
done();
7575
});
7676
});

test/api_mass_send.test.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,56 +149,58 @@ describe('api_mass_send.js', function () {
149149
});
150150

151151
describe('massSendText', function () {
152-
xit('should ok', function (done) {
152+
it('should ok', function (done) {
153153
api.massSendText('群发消息', [puling], function (err, data) {
154-
expect(err).to.be.ok();
155-
expect(err).to.have.property('message', 'api unauthorized');
154+
expect(err).not.to.be.ok();
155+
expect(data).to.have.property('errcode', 0);
156+
expect(data).to.have.property('errmsg', 'send job submission success');
156157
done();
157158
});
158159
});
159160
});
160161

161162
describe('massSendImage', function () {
162-
xit('should ok', function (done) {
163+
it('should ok', function (done) {
163164
api.massSendImage(imageId, [puling], function (err, data) {
164165
expect(err).to.be.ok();
165-
expect(err).to.have.property('message', 'api unauthorized');
166+
expect(err).to.have.property('code');
167+
expect(err).to.have.property('message');
166168
done();
167169
});
168170
});
169171
});
170172

171173
describe('deleteMass', function () {
172-
xit('should ok', function (done) {
174+
it('should ok', function (done) {
173175
api.deleteMass('messageId', function (err, data) {
174176
expect(err).to.be.ok();
175177
expect(err).to.have.property('code', -1);
176-
expect(err).to.have.property('message', 'api unauthorized');
178+
expect(err).to.have.property('message', 'system error');
177179
done();
178180
});
179181
});
180182
});
181183

182184
describe('massSendVoice', function () {
183-
xit('should ok', function (done) {
185+
it('should ok', function (done) {
184186
api.massSendVoice('media_id', [puling], function (err, data) {
185187
expect(err).to.be.ok();
186-
expect(err).to.have.property('message', 'api unauthorized');
188+
expect(err).to.have.property('message', 'invalid media_id');
187189
done();
188190
});
189191
});
190192
});
191193

192194
describe('massSendVideo', function () {
193-
xit('should ok', function (done) {
195+
it('should ok', function (done) {
194196
var opts = {
195197
media_id: 'media_id',
196198
title: 'title',
197199
description: 'description'
198200
};
199201
api.massSendVideo(opts, [puling], function (err, data) {
200202
expect(err).to.be.ok();
201-
expect(err).to.have.property('message', 'api unauthorized');
203+
expect(err).to.have.property('message', 'invalid media_id');
202204
done();
203205
});
204206
});
@@ -248,35 +250,35 @@ describe('api_mass_send.js', function () {
248250
after(function () {
249251
muk.restore();
250252
});
251-
xit('send to openids should ok', function (done) {
253+
it('send to openids should ok', function (done) {
252254
var opts = {
253255
"media_id": "rF4UdIMfYK3efUfyoddYRMU50zMiRmmt_l0kszupYh_SzrcW5Gaheq05p_lHuOTQ",
254256
"title": "TITLE",
255257
"description": "Description"
256258
};
257259
api.massSendMPVideo(opts, [puling], function (err, data) {
258260
expect(err).to.be.ok();
259-
expect(err).to.have.property('message', 'api unauthorized');
261+
expect(err).to.have.property('message', 'invalid media_id');
260262
done();
261263
});
262264
});
263265
});
264266
});
265267

266268
describe('massSendNews', function () {
267-
xit('should ok', function (done) {
269+
it('should ok', function (done) {
268270
api.massSendNews('media id', [puling], function (err, data) {
269271
expect(err).to.be.ok();
270-
expect(err).to.have.property('message', 'api unauthorized');
272+
expect(err).to.have.property('message', 'invalid media_id');
271273
done();
272274
});
273275
});
274276

275-
xit('should ok with groupid', function (done) {
277+
it('should ok with groupid', function (done) {
276278
api.massSendNews('media id', 'invalid groupid', function (err, data) {
277279
expect(err).to.be.ok();
278280
expect(err).to.have.property('code', 40050);
279-
expect(err).to.have.property('message', 'api unauthorized');
281+
expect(err).to.have.property('message', 'invalid timeline type');
280282
done();
281283
});
282284
});

test/api_shop_order.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ describe('api_shop_order', function () {
4444
});
4545
});
4646

47+
it('should exception: (string, callback)', function () {
48+
expect(function () {
49+
api.getOrdersByStatus('some string', function () {});
50+
}).to.throwException(/first parameter must be Number or Date/);
51+
});
52+
4753
it('should unauthorized with beginTime&endTime(status, endTime, callback)', function (done) {
4854
api.getOrdersByStatus(2, new Date(), function (err, data, res) {
4955
expect(err).to.be.ok();
@@ -53,6 +59,12 @@ describe('api_shop_order', function () {
5359
});
5460
});
5561

62+
it('should exception: (string, string, callback)', function () {
63+
expect(function () {
64+
api.getOrdersByStatus('some string', 'string', function () {});
65+
}).to.throwException(/first parameter must be Number and second parameter must be Date/);
66+
});
67+
5668
it('should unauthorized with beginTime&endTime(status, beginTime, endTime, callback)', function (done) {
5769
api.getOrdersByStatus(2, new Date(), new Date(), function (err, data, res) {
5870
expect(err).to.be.ok();

0 commit comments

Comments
 (0)