Skip to content

Commit

Permalink
test(delegate): test dingtalk integration, pass response to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
paradite committed Feb 17, 2019
1 parent 2d63693 commit d45d23c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"trailingComma": "es5",
"singleQuote": true
}
21 changes: 9 additions & 12 deletions app/controller/delegate.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
'use strict';

const {
Controller,
} = require('egg');
const { Controller } = require('egg');

class DelegateController extends Controller {

async message() {
const ctx = this.ctx;
const {
webhook,
text,
title,
} = ctx.request.body;
console.log(ctx.request.body);
const { webhook, text, title } = ctx.request.body;

await ctx.helper.sendMarkdown({
const res = await ctx.helper.sendMarkdown({
webhook,
title: title || 'title',
text,
});

ctx.success({});
if (res.data) {
ctx.success(res.data);
} else {
ctx.success({});
}
}

}

module.exports = DelegateController;
10 changes: 7 additions & 3 deletions app/extend/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
const debug = require('debug')('reliable:reliable-dingtalk');
const ChatBot = require('dingtalk-robot-sender');

// response format is axios
// https://github.com/axios/axios#response-schema
// https://github.com/x-cold/dingtalk-robot/blob/master/lib/bot.js
const sendMarkdown = async options => {
debug(options);
const robot = new ChatBot({
webhook: options.webhook.url,
});
let res;
if (options.isRawMarkdown) {
await robot.markdown(options.title, options.text);
return;
res = await robot.markdown(options.title, options.text);
}
await robot.markdown(options.title, options.text.join('\n\n'));
res = await robot.markdown(options.title, options.text.join('\n\n'));
return res;
};

module.exports = {
Expand Down
5 changes: 2 additions & 3 deletions config/config.local.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

module.exports = () => {
const config = exports = {};
const config = (exports = {});

config.reliableView = {
// assetsUrl: '//127.0.0.1:8080',
assetsUrl: '//127.0.0.1:8080',
};
config.authorize = {
enable: false,
Expand All @@ -22,4 +22,3 @@ module.exports = () => {
};
return config;
};

22 changes: 22 additions & 0 deletions test/app/controller/delegate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const { app, assert } = require('egg-mock/bootstrap');

describe('test/app/controller/delegate.test.js', () => {
it('POST /api/delegate/message delegate message', async () => {
const { body } = await app
.httpRequest()
.post('/api/delegate/message')
.send({
webhook: {
url: 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx',
},
title: 'title',
text: [ 'text1', 'text2' ],
});
assert(body.success);
const data = body.data;
// data is the response from DingTalk, something like { errmsg: 'token is not exist', errcode: 300001 }
assert(data);
});
});

0 comments on commit d45d23c

Please sign in to comment.