mirror of
https://github.com/lizheming/drone-wechat.git
synced 2026-06-14 05:13:04 +08:00
Compare commits
48 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6288e4afac | |||
| 3e0dec29ee | |||
| 46b144516a | |||
| f6d38f75a8 | |||
| 58ca98d95f | |||
| b1482817f1 | |||
| 7c09b00e73 | |||
| 27431b7e9d | |||
| a3b45bf3d7 | |||
| 547d96caea | |||
| ebb4fa4536 | |||
| 5b410441e6 | |||
| a10b8c2982 | |||
| 31457c2db5 | |||
| 8f3fadac27 | |||
| 4f828a37a2 | |||
| 960b54b0da | |||
| 13e1a9672a | |||
| 7c508b94a1 | |||
| 4c689927d8 | |||
| 3810c42e90 | |||
| ae91f54be3 | |||
| a0ef9d4b26 | |||
| 1fc4bd1275 | |||
| e53de0b4e4 | |||
| f8afb41b72 | |||
| 76a3278435 | |||
| 40c3af1759 | |||
| 74ef2be5a1 | |||
| 759632c133 | |||
| 6c83fe7b28 | |||
| deba525faa | |||
| 749bc72903 | |||
| 9c55502ccb | |||
| 5f358b0cf9 | |||
| 93a5d6a792 | |||
| 4dd376b565 | |||
| ceeb1a1d14 | |||
| 05dde2a990 | |||
| 42f12803c1 | |||
| 387eff6ec0 | |||
| 85cb2c566d | |||
| 9db93837f7 | |||
| 651b21897a | |||
| be4e4a0a7a | |||
| da1cf0d3f3 | |||
| 1ee57a206a | |||
| 009655fcfe |
+81
@@ -0,0 +1,81 @@
|
|||||||
|
kind: pipeline
|
||||||
|
name: default
|
||||||
|
|
||||||
|
steps:
|
||||||
|
# - name: restore-cache
|
||||||
|
# image: drillster/drone-volume-cache
|
||||||
|
# volumes:
|
||||||
|
# - name: cache
|
||||||
|
# path: /cache
|
||||||
|
# settings:
|
||||||
|
# restore: true
|
||||||
|
# mount:
|
||||||
|
# - ./node_modules
|
||||||
|
|
||||||
|
- name: build
|
||||||
|
image: node:lts-alpine
|
||||||
|
commands:
|
||||||
|
- npm set config package-lock false
|
||||||
|
- npm install
|
||||||
|
- npm test
|
||||||
|
|
||||||
|
- name: coveralls
|
||||||
|
pull: true
|
||||||
|
image: lizheming/drone-coveralls
|
||||||
|
environment:
|
||||||
|
COVERALLS_REPO_TOKEN:
|
||||||
|
from_secret: coveralls_token
|
||||||
|
settings:
|
||||||
|
debug: true
|
||||||
|
files:
|
||||||
|
- coverage/lcov.info
|
||||||
|
|
||||||
|
# - name: code analysis
|
||||||
|
# image: jgentry/drone-sonar
|
||||||
|
# environment:
|
||||||
|
# sonar_host: "https://sonarcloud.io"
|
||||||
|
# sonar_login:
|
||||||
|
# from_secret: sonar_token
|
||||||
|
|
||||||
|
# - name: rebuild-cache
|
||||||
|
# image: drillster/drone-volume-cache
|
||||||
|
# volumes:
|
||||||
|
# - name: cache
|
||||||
|
# path: /cache
|
||||||
|
# settings:
|
||||||
|
# rebuild: true
|
||||||
|
# mount:
|
||||||
|
# - ./node_modules
|
||||||
|
|
||||||
|
# - name: wechat
|
||||||
|
# image: lizheming/drone-wechat
|
||||||
|
# pull: always
|
||||||
|
# settings:
|
||||||
|
# title: >
|
||||||
|
# {% if success %}
|
||||||
|
# ✅ {{repo.owner}}/{{repo.name}} 第 {{build.number}} 次构建成功!
|
||||||
|
# {% else %}
|
||||||
|
# ❌ {{repo.owner}}/{{repo.name}} 第 {{build.number}} 次构建失败了,快来修理下吧。
|
||||||
|
# {% endif %}
|
||||||
|
# corpid:
|
||||||
|
# from_secret: corpid
|
||||||
|
# corp_secret:
|
||||||
|
# from_secret: corp_secret
|
||||||
|
# agent_id:
|
||||||
|
# from_secret: agent_id
|
||||||
|
# to_user: "@all"
|
||||||
|
# message: >
|
||||||
|
# {% if success %}
|
||||||
|
# 😊主人,{{repo.owner}}/{{repo.name}}第{{build.number}}次构建成功!
|
||||||
|
# {% else %}
|
||||||
|
# 😭主人,{{repo.owner}}/{{repo.name}}第{{build.number}}次构建失败了,快来修理下吧。
|
||||||
|
# {% endif %}
|
||||||
|
# when:
|
||||||
|
# status:
|
||||||
|
# - success
|
||||||
|
# - failure
|
||||||
|
|
||||||
|
# volumes:
|
||||||
|
# - name: cache
|
||||||
|
# host:
|
||||||
|
# path: /tmp/cache
|
||||||
@@ -57,3 +57,4 @@ typings/
|
|||||||
# dotenv environment variables file
|
# dotenv environment variables file
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
lcov.info
|
||||||
+8
-8
@@ -1,8 +1,8 @@
|
|||||||
language: node_js
|
# language: node_js
|
||||||
node_js:
|
# node_js:
|
||||||
- '8'
|
# - '8'
|
||||||
sudo: false
|
# sudo: false
|
||||||
script:
|
# script:
|
||||||
- "npm test"
|
# - "npm test"
|
||||||
after_success:
|
# after_success:
|
||||||
- 'npm install coveralls && ./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
|
# - 'npm install coveralls && ./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
|
||||||
|
|||||||
@@ -10,22 +10,6 @@ image: lizheming/drone-wechat
|
|||||||
|
|
||||||
The Wechat plugin posts build status messages to your account. The below pipeline configuration demonstrates simple usage:
|
The Wechat plugin posts build status messages to your account. The below pipeline configuration demonstrates simple usage:
|
||||||
|
|
||||||
```diff
|
|
||||||
pipline:
|
|
||||||
wechat:
|
|
||||||
image: lizheming/drone-wechat
|
|
||||||
+ sckey: xxxxxx
|
|
||||||
title: ${DRONE_REPO_NAME}
|
|
||||||
message: >
|
|
||||||
{%if success %}
|
|
||||||
build {{build.number}} succeeded. Good job.
|
|
||||||
{% else %}
|
|
||||||
build {{build.number}} failed. Fix me please.
|
|
||||||
{% endif %}
|
|
||||||
```
|
|
||||||
|
|
||||||
If you want push notification with your own wechat corp id, you can config like this:
|
|
||||||
|
|
||||||
```diff
|
```diff
|
||||||
pipeline:
|
pipeline:
|
||||||
wechat:
|
wechat:
|
||||||
@@ -77,9 +61,6 @@ msg_url
|
|||||||
btn_text
|
btn_text
|
||||||
: The text for the button on the card
|
: The text for the button on the card
|
||||||
|
|
||||||
sckey
|
|
||||||
: SCKEY get from [ServerChan](http://sc.ftqq.com)
|
|
||||||
|
|
||||||
title
|
title
|
||||||
: Notification title
|
: Notification title
|
||||||
|
|
||||||
@@ -106,9 +87,6 @@ wechat\_to\_user
|
|||||||
wechat\_to\_tag
|
wechat\_to\_tag
|
||||||
: The tag ids to send the message to
|
: The tag ids to send the message to
|
||||||
|
|
||||||
wechat_sckey
|
|
||||||
: SCKEY get from [ServerChan](http://sc.ftqq.com)
|
|
||||||
|
|
||||||
# Template Reference
|
# Template Reference
|
||||||
|
|
||||||
repo.owner
|
repo.owner
|
||||||
|
|||||||
+2
-1
@@ -3,11 +3,12 @@ FROM mhart/alpine-node:8.9.3
|
|||||||
LABEL maintainer="lizheming <i@imnerd.org>" \
|
LABEL maintainer="lizheming <i@imnerd.org>" \
|
||||||
org.label-schema.name="Drone Wechat Notification" \
|
org.label-schema.name="Drone Wechat Notification" \
|
||||||
org.label-schema.vendor="lizheming" \
|
org.label-schema.vendor="lizheming" \
|
||||||
org.label-schema.schema-version="1.1.0"
|
org.label-schema.schema-version="1.2.0"
|
||||||
|
|
||||||
WORKDIR /wechat
|
WORKDIR /wechat
|
||||||
COPY package.json /wechat/package.json
|
COPY package.json /wechat/package.json
|
||||||
RUN npm install --production --registry=https://registry.npm.taobao.org
|
RUN npm install --production --registry=https://registry.npm.taobao.org
|
||||||
|
|
||||||
COPY index.js /wechat/index.js
|
COPY index.js /wechat/index.js
|
||||||
|
COPY plugin.js /wechat/plugin.js
|
||||||
ENTRYPOINT [ "node", "/wechat/index.js" ]
|
ENTRYPOINT [ "node", "/wechat/index.js" ]
|
||||||
@@ -1,97 +1,57 @@
|
|||||||
const process = require('process');
|
const { configParser, exec } = require('./plugin');
|
||||||
const render = require('drone-render');
|
configParser({
|
||||||
const request = require('request-promise-native');
|
corpid: {
|
||||||
|
usage: 'The corpid for authorization',
|
||||||
const {
|
env: 'PLUGIN_CORPID,WECHAT_CORPID'
|
||||||
PLUGIN_CORPID,
|
|
||||||
WECHAT_CORPID,
|
|
||||||
PLUGIN_CORP_SECRET,
|
|
||||||
WECHAT_CORP_SECRET,
|
|
||||||
PLUGIN_AGENT_ID,
|
|
||||||
WECHAT_AGENT_ID,
|
|
||||||
PLUGIN_TO_PARTY,
|
|
||||||
WECHAT_TO_PARTY,
|
|
||||||
PLUGIN_TO_USER,
|
|
||||||
WECHAT_TO_USER,
|
|
||||||
PLUGIN_TO_TAG,
|
|
||||||
WECHAT_TO_TAG,
|
|
||||||
|
|
||||||
PLUGIN_MSG_TYPE,
|
|
||||||
PLUGIN_SAFE,
|
|
||||||
PLUGIN_MSG_URL,
|
|
||||||
PLUGIN_BTN_TEXT,
|
|
||||||
|
|
||||||
PLUGIN_TITLE,
|
|
||||||
PLUGIN_MESSAGE
|
|
||||||
} = process.env;
|
|
||||||
|
|
||||||
function getAccessToken() {
|
|
||||||
const CORPID = PLUGIN_CORPID || WECHAT_CORPID;
|
|
||||||
const CORP_SECRET = PLUGIN_CORP_SECRET || WECHAT_CORP_SECRET;
|
|
||||||
|
|
||||||
return request({
|
|
||||||
url: 'https://qyapi.weixin.qq.com/cgi-bin/gettoken',
|
|
||||||
qs: {
|
|
||||||
corpid: CORPID,
|
|
||||||
corpsecret: CORP_SECRET
|
|
||||||
},
|
},
|
||||||
json: true
|
corp_secret: {
|
||||||
}).then(resp => {
|
usage: 'The corp secret for authorization',
|
||||||
if (!resp.access_token) {
|
env: 'PLUGIN_CORP_SECRET,WECHAT_CORP_SECRET'
|
||||||
throw new Error(resp);
|
|
||||||
}
|
|
||||||
return resp.access_token;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function sendMsgFromWork(access_token) {
|
|
||||||
const TO_USER = PLUGIN_TO_USER || WECHAT_TO_USER || '@all';
|
|
||||||
const TO_PARTY = PLUGIN_TO_PARTY || WECHAT_TO_PARTY;
|
|
||||||
const TO_TAG = PLUGIN_TO_TAG || WECHAT_TO_TAG;
|
|
||||||
const AGENT_ID = PLUGIN_AGENT_ID || WECHAT_AGENT_ID;
|
|
||||||
|
|
||||||
const MSG_TYPE = PLUGIN_MSG_TYPE || 'textcard';
|
|
||||||
const SAFE = PLUGIN_SAFE || 0;
|
|
||||||
const TITLE = PLUGIN_TITLE;
|
|
||||||
const DESCRIPTION = render(PLUGIN_MESSAGE);
|
|
||||||
const MSG_URL = PLUGIN_MSG_URL;
|
|
||||||
const BTN_TEXT = PLUGIN_BTN_TEXT;
|
|
||||||
|
|
||||||
return request({
|
|
||||||
method: 'POST',
|
|
||||||
url: 'https://qyapi.weixin.qq.com/cgi-bin/message/send',
|
|
||||||
qs: {
|
|
||||||
access_token
|
|
||||||
},
|
},
|
||||||
body: {
|
agent_id: {
|
||||||
touser: TO_USER,
|
usage: 'The agent id to send the message',
|
||||||
toparty: TO_PARTY,
|
env: 'PLUGIN_AGENT_ID,WECHAT_AGENT_ID'
|
||||||
tag: TO_TAG,
|
|
||||||
msgtype: MSG_TYPE,
|
|
||||||
agentid: AGENT_ID,
|
|
||||||
safe: SAFE,
|
|
||||||
textcard: {
|
|
||||||
title: TITLE,
|
|
||||||
description: DESCRIPTION,
|
|
||||||
url: MSG_URL,
|
|
||||||
btntext: BTN_TEXT
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
json: true
|
to_party: {
|
||||||
});
|
usage: 'The party ids to send message',
|
||||||
|
env: 'PLUGIN_TO_PARTY,WECHAT_TO_PARTY'
|
||||||
|
},
|
||||||
|
to_user: {
|
||||||
|
usage: 'The user ids to send the message to',
|
||||||
|
env: 'PLUGIN_TO_USER,WECHAT_TO_USER'
|
||||||
|
},
|
||||||
|
to_tag: {
|
||||||
|
usage: 'The tag ids to send the message to',
|
||||||
|
env: 'PLUGIN_TO_TAG,WECHAT_TO_TAG'
|
||||||
|
},
|
||||||
|
msg_type: {
|
||||||
|
usage: 'The message send type',
|
||||||
|
def: 'textcard',
|
||||||
|
env: 'PLUGIN_MSG_TYPE'
|
||||||
|
},
|
||||||
|
safe: {
|
||||||
|
usage: 'encrypt message, default is false',
|
||||||
|
def: 0,
|
||||||
|
env: 'PLUGIN_SAFE'
|
||||||
|
},
|
||||||
|
msg_url: {
|
||||||
|
usage: 'The link for the text card click',
|
||||||
|
env: 'PLUGIN_MSG_URL,DRONE_BUILD_LINK'
|
||||||
|
},
|
||||||
|
btn_text: {
|
||||||
|
usage: 'The text for the button on the card',
|
||||||
|
env: 'PLUGIN_BTN_TEXT'
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
usage: 'Notification title',
|
||||||
|
env: 'PLUGIN_TITLE'
|
||||||
|
},
|
||||||
|
message: {
|
||||||
|
usage: 'Notification body message, support markdown',
|
||||||
|
env: 'PLUGIN_MESSAGE'
|
||||||
|
},
|
||||||
|
debug: {
|
||||||
|
usage: 'debug mode',
|
||||||
|
env: 'PLUGIN_DEBUG'
|
||||||
}
|
}
|
||||||
|
})(exec);
|
||||||
function sendMsgFromWechat() {
|
|
||||||
return getAccessToken()
|
|
||||||
.then(sendMsgFromWork)
|
|
||||||
.catch(err => {
|
|
||||||
console.error(err);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
sendMsgFromWechat();
|
|
||||||
module.exports = {
|
|
||||||
getAccessToken,
|
|
||||||
sendMsgFromWork,
|
|
||||||
sendMsgFromWechat
|
|
||||||
};
|
|
||||||
|
|||||||
+4
-3
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "drone-wechat",
|
"name": "drone-wechat",
|
||||||
"version": "2.0.0",
|
"version": "1.1.7",
|
||||||
"description": "drone wechat notification plugin",
|
"description": "drone wechat notification plugin",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "npm run eslint && npm run test-cov",
|
"test": "npm run eslint && npm run test-cov",
|
||||||
"test-cov": "nyc ava test.js && nyc report --reporter=html",
|
"test-cov": "nyc ava test.js && nyc report --reporter=lcov --reporter=text-lcov",
|
||||||
"eslint": "eslint index.js"
|
"eslint": "eslint index.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -25,7 +25,8 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/lizheming/drone-wechat#readme",
|
"homepage": "https://github.com/lizheming/drone-wechat#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"drone-render": "^1.0.0",
|
"coveralls": "^3.0.2",
|
||||||
|
"drone-render": "^1.0.4",
|
||||||
"request": "^2.83.0",
|
"request": "^2.83.0",
|
||||||
"request-promise-native": "^1.0.5"
|
"request-promise-native": "^1.0.5"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
const process = require('process');
|
||||||
|
const render = require('drone-render');
|
||||||
|
const request = require('request-promise-native');
|
||||||
|
|
||||||
|
function log(...args) {
|
||||||
|
const debug = process.env.PLUGIN_DEBUG;
|
||||||
|
if (!debug) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.log(...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
function configParser(configs) {
|
||||||
|
const ret = {};
|
||||||
|
for (const configName in configs) {
|
||||||
|
const { env, def } = configs[configName];
|
||||||
|
if (def !== undefined) {
|
||||||
|
ret[configName] = typeof def === 'function' ? def() : def;
|
||||||
|
}
|
||||||
|
env.split(/\s*,\s*/).some(envar => {
|
||||||
|
if (process.env.hasOwnProperty(envar)) {
|
||||||
|
ret[configName] = process.env[envar];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return fn => fn(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAccessToken(corpid, corpsecret) {
|
||||||
|
return request({
|
||||||
|
url: 'https://qyapi.weixin.qq.com/cgi-bin/gettoken',
|
||||||
|
qs: { corpid, corpsecret },
|
||||||
|
json: true
|
||||||
|
}).then(resp => {
|
||||||
|
if (!resp.access_token) {
|
||||||
|
throw new Error(resp);
|
||||||
|
}
|
||||||
|
return resp.access_token;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendMsgFromWork({
|
||||||
|
access_token,
|
||||||
|
to_user: touser,
|
||||||
|
to_party: toparty,
|
||||||
|
to_tag: totag,
|
||||||
|
msg_type: msgtype,
|
||||||
|
agent_id: agentid,
|
||||||
|
msg_url: url,
|
||||||
|
btn_text: btntext,
|
||||||
|
message,
|
||||||
|
safe,
|
||||||
|
title
|
||||||
|
}) {
|
||||||
|
title = title && render(title);
|
||||||
|
totag = totag && render(totag);
|
||||||
|
btntext = btntext && render(btntext);
|
||||||
|
const description = message && render(message);
|
||||||
|
|
||||||
|
const textcard = { title, url, btntext, description };
|
||||||
|
|
||||||
|
return request({
|
||||||
|
method: 'POST',
|
||||||
|
url: 'https://qyapi.weixin.qq.com/cgi-bin/message/send',
|
||||||
|
qs: { access_token },
|
||||||
|
body: { touser, toparty, totag, msgtype, agentid, safe, textcard },
|
||||||
|
json: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function exec({ corpid, corp_secret, ...config }) {
|
||||||
|
try {
|
||||||
|
log('config parse end, config except corpid and corp_secret:', config);
|
||||||
|
const access_token = await getAccessToken(corpid, corp_secret);
|
||||||
|
log('access_token request success!');
|
||||||
|
log('http request data:', config);
|
||||||
|
const resp = await sendMsgFromWork({ ...config, access_token });
|
||||||
|
log('send msg success, and http response content is:');
|
||||||
|
log(resp);
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.log('==Send notification to Wechat success!==');
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
configParser,
|
||||||
|
getAccessToken,
|
||||||
|
sendMsgFromWork,
|
||||||
|
exec
|
||||||
|
};
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
sonar.projectKey=drone-wechat
|
||||||
|
sonar.organization=lizheming-github
|
||||||
|
sonar.language=js
|
||||||
|
sonar.sources=index.js,plugin.js
|
||||||
|
sonar.tests=test.js
|
||||||
|
sonar.javascript.lcov.reportPaths=coverage/lcov.info
|
||||||
|
sonar.host.url=https://sonarcloud.io
|
||||||
|
sonar.exclusions=test.js
|
||||||
@@ -1,68 +1,57 @@
|
|||||||
const { test } = require('ava');
|
const { test } = require('ava');
|
||||||
const mock = require('mock-require');
|
const mock = require('mock-require');
|
||||||
|
|
||||||
const SCKEY = 'testsckey';
|
// const SCKEY = 'testsckey';
|
||||||
const SCKEY2 = 'testsckey2';
|
// const SCKEY2 = 'testsckey2';
|
||||||
const TITLE = 'test for title';
|
const TITLE = 'test for title';
|
||||||
const MESSAGE = 'test for message';
|
const MESSAGE = 'test for message';
|
||||||
|
|
||||||
test('send wechat with serverchan', async t => {
|
// test('send wechat with serverchan', async t => {
|
||||||
t.plan(2);
|
// t.plan(2);
|
||||||
|
|
||||||
mock('process', {
|
// mock('process', {
|
||||||
env: {
|
// env: {
|
||||||
PLUGIN_SCKEY: SCKEY,
|
// PLUGIN_SCKEY: SCKEY,
|
||||||
PLUGIN_TITLE: TITLE,
|
// PLUGIN_TITLE: TITLE,
|
||||||
PLUGIN_MESSAGE: MESSAGE
|
// PLUGIN_MESSAGE: MESSAGE
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
mock('drone-render', text => {
|
// mock('drone-render', text => {
|
||||||
t.is(text, MESSAGE);
|
// t.is(text, MESSAGE);
|
||||||
return text + '1';
|
// return text + '1';
|
||||||
});
|
// });
|
||||||
mock('request-promise-native', obj => {
|
// mock('request-promise-native', obj => {
|
||||||
t.deepEqual(obj, {
|
// t.deepEqual(obj, {
|
||||||
url: `https://sc.ftqq.com/${SCKEY}.send`,
|
// url: `https://sc.ftqq.com/${SCKEY}.send`,
|
||||||
qs: {
|
// qs: {
|
||||||
text: TITLE,
|
// text: TITLE,
|
||||||
desp: MESSAGE + '1'
|
// desp: MESSAGE + '1'
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
});
|
// });
|
||||||
|
|
||||||
mock.reRequire('./index.js');
|
// mock.reRequire('./index.js');
|
||||||
mock.stopAll();
|
// mock.stopAll();
|
||||||
});
|
// });
|
||||||
|
|
||||||
test('get wechat corp access token', async t => {
|
// test('get wechat corp access token', async t => {
|
||||||
t.plan(3);
|
// 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
|
||||||
|
// });
|
||||||
|
|
||||||
mock('process', {
|
// return Promise.resolve({ access_token: 1234 });
|
||||||
env: {
|
// });
|
||||||
PLUGIN_CORPID: 111,
|
// const plugin = mock.reRequire('./plugin');
|
||||||
PLUGIN_CORP_SECRET: 222,
|
// t.is(1234, await plugin.getAccessToken(111, 222));
|
||||||
PLUGIN_TITLE: TITLE,
|
// mock.stopAll();
|
||||||
PLUGIN_MESSAGE: MESSAGE
|
// });
|
||||||
}
|
|
||||||
});
|
|
||||||
mock('request-promise-native', obj => {
|
|
||||||
if (obj.url.includes('gettoken')) {
|
|
||||||
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 wechat = mock.reRequire('./index');
|
|
||||||
t.is(1234, await wechat.getAccessToken());
|
|
||||||
mock.stopAll();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('send wechat with corp id', async t => {
|
test('send wechat with corp id', async t => {
|
||||||
mock('process', {
|
mock('process', {
|
||||||
@@ -71,12 +60,13 @@ test('send wechat with corp id', async t => {
|
|||||||
PLUGIN_MESSAGE: MESSAGE,
|
PLUGIN_MESSAGE: MESSAGE,
|
||||||
PLUGIN_TO_PARTY: 'party',
|
PLUGIN_TO_PARTY: 'party',
|
||||||
PLUGIN_TO_TAG: 'tag',
|
PLUGIN_TO_TAG: 'tag',
|
||||||
|
PLUGIN_TO_USER: '@all',
|
||||||
PLUGIN_MSG_URL: '',
|
PLUGIN_MSG_URL: '',
|
||||||
PLUGIN_BTN_TEXT: 'more',
|
PLUGIN_BTN_TEXT: 'more',
|
||||||
PLUGIN_AGENT_ID: 1122
|
PLUGIN_AGENT_ID: 1122
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
mock('request-promise-native', obj => {
|
mock('request-promise-native', function(obj) {
|
||||||
if (obj.url.includes('gettoken')) {
|
if (obj.url.includes('gettoken')) {
|
||||||
return Promise.resolve({ access_token: 1234 });
|
return Promise.resolve({ access_token: 1234 });
|
||||||
}
|
}
|
||||||
@@ -90,7 +80,7 @@ test('send wechat with corp id', async t => {
|
|||||||
body: {
|
body: {
|
||||||
touser: '@all',
|
touser: '@all',
|
||||||
toparty: 'party',
|
toparty: 'party',
|
||||||
tag: 'tag',
|
totag: 'tag',
|
||||||
msgtype: 'textcard',
|
msgtype: 'textcard',
|
||||||
agentid: 1122,
|
agentid: 1122,
|
||||||
safe: 0,
|
safe: 0,
|
||||||
@@ -105,7 +95,7 @@ test('send wechat with corp id', async t => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
mock.reRequire('./index');
|
await mock.reRequire('./index');
|
||||||
mock.stopAll();
|
mock.stopAll();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user