forked from HermanKoii/Koii-Task-Funder-Express
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
130 lines (106 loc) · 4.02 KB
/
index.test.js
File metadata and controls
130 lines (106 loc) · 4.02 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const express = require('express');
const request = require('supertest');
const crypto = require('crypto');
// Mock the external dependencies
jest.mock('@_koii/create-task-cli', () => {
return {
FundTask: jest.fn().mockResolvedValue(true),
KPLEstablishConnection: jest.fn().mockResolvedValue(true),
KPLFundTask: jest.fn().mockResolvedValue(true),
getTaskStateInfo: jest.fn().mockResolvedValue({
stake_pot_account: 'mockStakePotAccount',
token_type: null
}),
establishConnection: jest.fn().mockResolvedValue(true),
checkProgram: jest.fn().mockResolvedValue(true),
KPLCheckProgram: jest.fn().mockResolvedValue(true)
};
});
jest.mock('@_koii/web3.js', () => {
return {
PublicKey: jest.fn().mockImplementation((key) => ({
toString: () => key
})),
Connection: jest.fn().mockImplementation(() => ({
// Mock connection methods if needed
})),
Keypair: {
fromSecretKey: jest.fn().mockReturnValue({
publicKey: 'mockPublicKey',
secretKey: new Uint8Array([1,2,3,4])
})
}
};
});
jest.mock('axios', () => {
return {
post: jest.fn().mockResolvedValue({})
};
});
// Import the app after mocking dependencies
const app = require('./index');
describe('Task Funding Service', () => {
let server;
beforeAll(() => {
// Set up environment variables for testing
process.env.SIGNING_SECRET = 'test_secret';
process.env.funder_keypair = JSON.stringify([1,2,3,4]); // Mock keypair
});
beforeEach(() => {
server = app.listen(0); // Use a random available port
});
afterEach(() => {
server.close();
jest.clearAllMocks();
});
function createSlackSignature(body, secret, timestamp) {
const sigBasestring = `v0:${timestamp}:${body}`;
const hmac = crypto.createHmac('sha256', secret);
return 'v0=' + hmac.update(sigBasestring).digest('hex');
}
it('should reject requests without valid Slack signature', async () => {
const body = 'text=fund+task123+100&user_id=U06NM9A2VC1&response_url=http://example.com';
const timestamp = Math.floor(Date.now() / 1000);
const response = await request(server)
.post('/fundtask')
.set('x-slack-signature', 'invalid_signature')
.set('x-slack-request-timestamp', timestamp)
.send(body);
expect(response.statusCode).toBe(400);
expect(response.text).toBe('Invalid request signature');
}, 10000);
it('should reject requests from unauthorized users', async () => {
const body = 'text=fund+task123+100&user_id=UNAUTHORIZED_USER&response_url=http://example.com';
const timestamp = Math.floor(Date.now() / 1000);
const signature = createSlackSignature(body, process.env.SIGNING_SECRET, timestamp);
const response = await request(server)
.post('/fundtask')
.set('x-slack-signature', signature)
.set('x-slack-request-timestamp', timestamp)
.send(body);
expect(response.statusCode).toBe(403);
}, 10000);
it('should successfully fund a task for authorized user', async () => {
const body = 'text=task123+100&user_id=U06NM9A2VC1&response_url=http://example.com';
const timestamp = Math.floor(Date.now() / 1000);
const signature = createSlackSignature(body, process.env.SIGNING_SECRET, timestamp);
const response = await request(server)
.post('/fundtask')
.set('x-slack-signature', signature)
.set('x-slack-request-timestamp', timestamp)
.send(body);
expect(response.statusCode).toBe(200);
expect(response.text).toBe('Task funded successfully');
}, 10000);
it('should handle invalid request body gracefully', async () => {
const body = 'invalid_body';
const timestamp = Math.floor(Date.now() / 1000);
const signature = createSlackSignature(body, process.env.SIGNING_SECRET, timestamp);
const response = await request(server)
.post('/fundtask')
.set('x-slack-signature', signature)
.set('x-slack-request-timestamp', timestamp)
.send(body);
expect(response.statusCode).toBe(500);
}, 10000);
});