diff --git a/index.js b/index.js index 632d4ab..75701fe 100644 --- a/index.js +++ b/index.js @@ -102,7 +102,7 @@ function sendMsgFromWork(access_token) { } function sendMsgFromWechat() { - return getAccessToken + return getAccessToken() .then(sendMsgFromWork) .catch(err => { console.error(err); diff --git a/test.js b/test.js index 476f6bb..bf379cc 100644 --- a/test.js +++ b/test.js @@ -6,7 +6,7 @@ const SCKEY2 = 'testsckey2'; const TITLE = 'test for title'; const MESSAGE = 'test for message'; -test('send wechat', async t => { +test('send wechat with serverchan', async t => { t.plan(2); mock('process', { @@ -34,18 +34,98 @@ test('send wechat', async t => { mock.stopAll(); }); -test('send wechat with SERVER_CHAN_KEY', async t => { +test('get wechat corp access token', async t => { + t.plan(3); + mock('process', { env: { - SERVER_CHAN_KEY: SCKEY2, + PLUGIN_CORPID: 111, + PLUGIN_CORP_SECRET: 222, PLUGIN_TITLE: TITLE, PLUGIN_MESSAGE: MESSAGE } }); mock('request-promise-native', obj => { - t.is(obj.url, `https://sc.ftqq.com/${SCKEY2}.send`); - }); + if (obj.url.includes('gettoken')) { + t.deepEqual(obj, { + url: 'https://qyapi.weixin.qq.com/cgi-bin/gettoken', + qs: { + corpid: 111, + corpsecret: 222 + }, + json: true + }); + } - mock.reRequire('./index.js'); + return Promise.resolve({access_token: 1234}); + }); + const wechat = mock.reRequire('./index'); + t.is(1234, await wechat.getAccessToken()); + 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_MSG_URL: '', + PLUGIN_BTN_TEXT: 'more', + PLUGIN_AGENT_ID: 1122 + } + }); + mock('request-promise-native', 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', + tag: 'tag', + msgtype: 'textcard', + agentid: 1122, + safe: 0, + textcard: { + title: TITLE, + description: MESSAGE, + url: '', + btntext: 'more' + } + }, + json: true + }); + } + }); + 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(); });