-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
121 lines (112 loc) · 2.82 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const { test } = require('ava');
const mock = require('mock-require');
// const SCKEY = 'testsckey';
// const SCKEY2 = 'testsckey2';
const TITLE = 'test for title';
const MESSAGE = 'test for message';
// test('send wechat with serverchan', async t => {
// t.plan(2);
// mock('process', {
// env: {
// PLUGIN_SCKEY: SCKEY,
// PLUGIN_TITLE: TITLE,
// PLUGIN_MESSAGE: MESSAGE
// }
// });
// mock('drone-render', text => {
// t.is(text, MESSAGE);
// return text + '1';
// });
// mock('request-promise-native', obj => {
// t.deepEqual(obj, {
// url: `https://sc.ftqq.com/${SCKEY}.send`,
// qs: {
// text: TITLE,
// desp: MESSAGE + '1'
// }
// });
// });
// mock.reRequire('./index.js');
// mock.stopAll();
// });
// test('get wechat corp access token', async t => {
// t.plan(2);
// mock('request-promise-native', obj => {
// t.deepEqual(obj, {
// url: 'https://qyapi.weixin.qq.com/cgi-bin/gettoken',
// qs: {
// corpid: 111,
// corpsecret: 222
// },
// json: true
// });
// return Promise.resolve({ access_token: 1234 });
// });
// const plugin = mock.reRequire('./plugin');
// t.is(1234, await plugin.getAccessToken(111, 222));
// mock.stopAll();
// });
test('send wechat with corp id', async t => {
mock('process', {
env: {
PLUGIN_TITLE: TITLE,
PLUGIN_MESSAGE: MESSAGE,
PLUGIN_TO_PARTY: 'party',
PLUGIN_TO_TAG: 'tag',
PLUGIN_TO_USER: '@all',
PLUGIN_MSG_URL: '',
PLUGIN_BTN_TEXT: 'more',
PLUGIN_AGENT_ID: 1122
}
});
mock('request-promise-native', function(obj) {
if (obj.url.includes('gettoken')) {
return Promise.resolve({ access_token: 1234 });
}
if (obj.url.includes('send')) {
t.deepEqual(obj, {
method: 'POST',
url: 'https://qyapi.weixin.qq.com/cgi-bin/message/send',
qs: {
access_token: 1234
},
body: {
touser: '@all',
toparty: 'party',
totag: 'tag',
msgtype: 'textcard',
agentid: 1122,
safe: 0,
textcard: {
title: TITLE,
description: MESSAGE,
url: '',
btntext: 'more'
}
},
json: true
});
}
});
await mock.reRequire('./index');
mock.stopAll();
});
test('send wechat', async t => {
mock('process', {
env: {
PLUGIN_TITLE: TITLE,
PLUGIN_MESSAGE: MESSAGE
}
});
mock('request-promise-native', obj => {
if (obj.url.includes('gettoken')) {
return Promise.resolve({ errcode: 1, errmsg: 123 });
}
if (obj.url.includes('send')) {
t.fail();
}
});
mock.reRequire('./index');
await new Promise(resolve => setTimeout(resolve, 2000)).then(() => t.pass());
mock.stopAll();
});