Skip to content

Commit

Permalink
Merge pull request #64 from macacajs/test-delegate
Browse files Browse the repository at this point in the history
test(delegate): test dingtalk integration, pass response to controller
  • Loading branch information
paradite authored Feb 24, 2019
2 parents 2d63693 + 7c7788e commit 3144888
Show file tree
Hide file tree
Showing 6 changed files with 62 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
}
20 changes: 8 additions & 12 deletions app/controller/delegate.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
'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;
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;
};

1 change: 1 addition & 0 deletions docker/reliable-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ start server:

```bash
npm run dev
# open app at localhost:7001
```

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

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

describe('test/app/controller/delegate.test.js', () => {
const webhook = {
url: 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx',
};
const title = 'test title';
const text = [ 'text1', 'text2' ];
const responseData = { test: 1 };

before(function() {
app.mockContext({
helper: {
sendMarkdown: async options => {
assert.deepStrictEqual(options.webhook, webhook);
assert.deepStrictEqual(options.title, title);
assert.deepStrictEqual(options.text, text);
return {
data: responseData,
};
},
},
});
});

it('POST /api/delegate/message delegate message', async () => {
const { body } = await app
.httpRequest()
.post('/api/delegate/message')
.send({
webhook,
title,
text,
});
assert(body.success);
assert.deepStrictEqual(body.data, responseData);
});
});

0 comments on commit 3144888

Please sign in to comment.