diff --git a/app/controller/delegate.js b/app/controller/delegate.js index 7cead46..db212f6 100644 --- a/app/controller/delegate.js +++ b/app/controller/delegate.js @@ -5,7 +5,6 @@ const { Controller } = require('egg'); class DelegateController extends Controller { async message() { const ctx = this.ctx; - console.log(ctx.request.body); const { webhook, text, title } = ctx.request.body; const res = await ctx.helper.sendMarkdown({ diff --git a/test/app/controller/delegate.test.js b/test/app/controller/delegate.test.js index 61c5ff3..658bda0 100644 --- a/test/app/controller/delegate.test.js +++ b/test/app/controller/delegate.test.js @@ -3,20 +3,38 @@ 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: { - url: 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx', - }, - title: 'title', - text: [ 'text1', 'text2' ], + webhook, + title, + text, }); assert(body.success); - const data = body.data; - // data is the response from DingTalk, something like { errmsg: 'token is not exist', errcode: 300001 } - assert(data); + assert.deepStrictEqual(body.data, responseData); }); });