forked from baaxl9vh/nest-wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.service.spec.ts
65 lines (50 loc) · 2.36 KB
/
component.service.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { XMLParser } from 'fast-xml-parser';
import { ComponentService } from './component.service';
describe('component service test', () => {
let component: ComponentService;
beforeAll(() => {
component = new ComponentService({
componentAppId: 'wxb11529c136998cb6',
componentSecret: '',
componentToken: 'pamtest',
componentEncodingAESKey: 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG',
});
});
it('Test encrypt & decrypt message', () => {
const timestamp = '1409304348';
const nonce = 'xxxxxx';
const text = '<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>';
const encryptMessage = component.encryptMessage(text, timestamp, nonce);
const parser = new XMLParser();
const xml = parser.parse(encryptMessage).xml;
const encryptXml = `<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[${xml.Encrypt}]]></Encrypt></xml>`;
const signature = xml.MsgSignature;
const decrypt = component.decryptMessage(signature, timestamp, nonce, encryptXml);
expect(decrypt).toStrictEqual(text);
});
it('Test start push ticket', async () => {
const service = new ComponentService({
componentAppId: 'wxb11529c136998cb6',
componentSecret: '',
componentToken: 'pamtest',
componentEncodingAESKey: 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG',
});
const ret = await service.startPushTicket();
expect(ret.data).toBeDefined();
// 41004 no secret
expect(ret.data.errcode).toEqual(41004);
});
it('Test request access token', async () => {
const service = new ComponentService({
componentAppId: 'wxb11529c136998cb6',
componentSecret: 'xxxxxx',
componentToken: 'pamtest',
componentEncodingAESKey: 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG',
});
service.setTicket('ticket');
const ret = await service.requestComponentToken();
expect(ret.data).toBeDefined();
// 40125 invalid appsecret rid
expect(ret.data.errcode).toEqual(40125);
});
});