chore(ci): bump jest and add a few tests
This commit is contained in:
parent
c61d3882a2
commit
2c233ec3fb
3 changed files with 1676 additions and 988 deletions
2494
package-lock.json
generated
2494
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -27,8 +27,8 @@
|
|||
"eslint-plugin-import": "^2.27.5",
|
||||
"eslint-plugin-n": "^15.7.0",
|
||||
"eslint-plugin-promise": "^6.1.1",
|
||||
"jest": "^29.7.0",
|
||||
"supertest": "^6.3.3",
|
||||
"jest": "^30.4.2",
|
||||
"supertest": "^7.2.2",
|
||||
"swagger-autogen": "^2.23.7"
|
||||
},
|
||||
"keywords": [
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ describe('API health checks', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('API Authentication Tests', () => {
|
||||
describe('API session checks', () => {
|
||||
it('should return 403 Forbidden for invalid API key', async () => {
|
||||
const response = await request(app).get('/session/start/1')
|
||||
expect(response.status).toBe(403)
|
||||
|
|
@ -95,7 +95,7 @@ describe('API Authentication Tests', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('API Action Tests', () => {
|
||||
describe('API action checks', () => {
|
||||
it('should setup, create at least a QR, and terminate a client session', async () => {
|
||||
const response = await request(app).get('/session/start/4').set('x-api-key', 'test_api_key')
|
||||
expect(response.status).toBe(200)
|
||||
|
|
@ -123,6 +123,112 @@ describe('API Action Tests', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('Session endpoints - no active session', () => {
|
||||
beforeAll(() => {
|
||||
if (!fs.existsSync(process.env.SESSIONS_PATH)) {
|
||||
fs.mkdirSync(process.env.SESSIONS_PATH, { recursive: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('GET /session/getSessions returns empty array', async () => {
|
||||
const response = await request(app).get('/session/getSessions').set('x-api-key', 'test_api_key')
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body).toEqual({ success: true, result: [] })
|
||||
})
|
||||
|
||||
it('GET /session/status/:id returns session_not_found', async () => {
|
||||
const response = await request(app).get('/session/status/nonexistent').set('x-api-key', 'test_api_key')
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body).toEqual({ success: false, state: null, message: 'session_not_found' })
|
||||
})
|
||||
|
||||
it('GET /session/stop/:id succeeds silently for non-existent session', async () => {
|
||||
const response = await request(app).get('/session/stop/nonexistent').set('x-api-key', 'test_api_key')
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body).toEqual({ success: true, message: 'Session stopped successfully' })
|
||||
})
|
||||
|
||||
it('GET /session/qr/:id returns session_not_found', async () => {
|
||||
const response = await request(app).get('/session/qr/nonexistent').set('x-api-key', 'test_api_key')
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body).toEqual({ success: false, message: 'session_not_found' })
|
||||
})
|
||||
|
||||
it('GET /session/qr/:id/image returns session_not_found', async () => {
|
||||
const response = await request(app).get('/session/qr/nonexistent/image').set('x-api-key', 'test_api_key')
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body).toEqual({ success: false, message: 'session_not_found' })
|
||||
})
|
||||
|
||||
it('GET /session/restart/:id returns session_not_found', async () => {
|
||||
const response = await request(app).get('/session/restart/nonexistent').set('x-api-key', 'test_api_key')
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body).toEqual({ success: false, state: null, message: 'session_not_found' })
|
||||
})
|
||||
|
||||
it('GET /session/terminateAll succeeds with no active sessions', async () => {
|
||||
const response = await request(app).get('/session/terminateAll').set('x-api-key', 'test_api_key')
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body).toEqual({ success: true, message: 'Flush completed successfully' })
|
||||
})
|
||||
|
||||
it('POST /session/requestPairingCode/:id returns session_not_found', async () => {
|
||||
const response = await request(app).post('/session/requestPairingCode/nonexistent')
|
||||
.set('x-api-key', 'test_api_key')
|
||||
.send({ phoneNumber: '12025550108' })
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body).toEqual({ success: false, message: 'session_not_found' })
|
||||
})
|
||||
|
||||
it('GET /session/getPageScreenshot/:id returns session_not_found', async () => {
|
||||
const response = await request(app).get('/session/getPageScreenshot/nonexistent').set('x-api-key', 'test_api_key')
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body).toEqual({ success: false, message: 'session_not_found' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('Session validation - 404 for non-existent session', () => {
|
||||
it.each([
|
||||
['GET', '/client/getContacts/nonexistent'],
|
||||
['POST', '/client/sendMessage/nonexistent'],
|
||||
['POST', '/chat/getClassInfo/nonexistent'],
|
||||
['POST', '/chat/fetchMessages/nonexistent'],
|
||||
['POST', '/groupChat/getClassInfo/nonexistent'],
|
||||
['POST', '/groupChat/leave/nonexistent'],
|
||||
['POST', '/message/getClassInfo/nonexistent'],
|
||||
['POST', '/message/react/nonexistent'],
|
||||
['POST', '/contact/getClassInfo/nonexistent'],
|
||||
['POST', '/contact/getAbout/nonexistent'],
|
||||
['POST', '/channel/getClassInfo/nonexistent'],
|
||||
['POST', '/channel/sendMessage/nonexistent']
|
||||
])('%s %s returns 404 session_not_found', async (method, url) => {
|
||||
const response = await request(app)[method.toLowerCase()](url).set('x-api-key', 'test_api_key')
|
||||
expect(response.status).toBe(404)
|
||||
expect(response.body).toEqual({ success: false, error: 'session_not_found' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('Authentication - 403 without a valid API key', () => {
|
||||
it.each([
|
||||
['GET', '/client/getContacts/1'],
|
||||
['POST', '/chat/getClassInfo/1'],
|
||||
['POST', '/groupChat/getClassInfo/1'],
|
||||
['POST', '/message/getClassInfo/1'],
|
||||
['POST', '/contact/getClassInfo/1'],
|
||||
['POST', '/channel/getClassInfo/1']
|
||||
])('%s %s returns 403 when the API key header is missing', async (method, url) => {
|
||||
const response = await request(app)[method.toLowerCase()](url)
|
||||
expect(response.status).toBe(403)
|
||||
expect(response.body).toEqual({ success: false, error: 'Invalid API key' })
|
||||
})
|
||||
|
||||
it('returns 403 when the API key is wrong', async () => {
|
||||
const response = await request(app).get('/client/getContacts/1').set('x-api-key', 'wrong_api_key')
|
||||
expect(response.status).toBe(403)
|
||||
expect(response.body).toEqual({ success: false, error: 'Invalid API key' })
|
||||
})
|
||||
})
|
||||
|
||||
// Function to wait for a specific item to be equal a specific value
|
||||
const waitForFileNotToBeEmpty = (filePath, maxWaitTime = 10000, interval = 100) => {
|
||||
const start = Date.now()
|
||||
|
|
|
|||
Loading…
Reference in a new issue