-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧪 chore: add SMS Service API test
- Loading branch information
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
packages/matrix-identity-server/src/utils/sms-service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { TwakeLogger } from '@twake/logger' | ||
import type { Config } from '../types' | ||
import { SmsService } from './sms-service' | ||
|
||
const loggerMock = { | ||
info: jest.fn(), | ||
error: jest.fn() | ||
} | ||
|
||
const mockConfig = { | ||
sms_api_key: 'test_key', | ||
sms_api_login: 'test_secret', | ||
sms_api_url: 'http://localhost/sms/api' | ||
} | ||
|
||
describe('the SMS service', () => { | ||
const smsService = new SmsService( | ||
mockConfig as Config, | ||
loggerMock as unknown as TwakeLogger | ||
) | ||
|
||
describe('the send method', () => { | ||
it('should send a SMS', async () => { | ||
global.fetch = jest.fn().mockImplementation(() => { | ||
return Promise.resolve({ | ||
json: () => Promise.resolve({ success: true }), | ||
status: 200 | ||
}) | ||
}) | ||
|
||
await smsService.send('test', 'test') | ||
expect(loggerMock.error).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should log an error when failed to send SMS', async () => { | ||
global.fetch = jest.fn().mockImplementation(() => { | ||
return Promise.resolve({ | ||
json: () => Promise.resolve({}), | ||
status: 401 | ||
}) | ||
}) | ||
|
||
await smsService.send('test', 'test') | ||
|
||
expect(loggerMock.error).toHaveBeenCalledWith( | ||
'Failed to send SMS', | ||
expect.anything() | ||
) | ||
}) | ||
|
||
it('should throw an error when instantiated with missing config', async () => { | ||
expect( | ||
() => new SmsService({} as Config, loggerMock as unknown as TwakeLogger) | ||
).toThrow('Missing SMS API configuration') | ||
}) | ||
|
||
it('should call the API endpoint with correct parameters', async () => { | ||
global.fetch = jest.fn().mockImplementation(() => { | ||
return Promise.resolve({ | ||
json: () => Promise.resolve({ success: true }), | ||
status: 200 | ||
}) | ||
}) | ||
|
||
await smsService.send('+330744556688', 'test sms') | ||
|
||
expect(fetch).toHaveBeenCalledWith( | ||
'http://localhost/sms/api/sms-campaign/send', | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'api-login': 'test_secret', | ||
'api-key': 'test_key' | ||
}, | ||
body: JSON.stringify({ | ||
to: '+330744556688', | ||
body: 'test sms', | ||
from: 'Twake Chat' | ||
}) | ||
} | ||
) | ||
}) | ||
}) | ||
}) |