thaibulksms-api
Version:
วิธีส่ง SMS ง่ายๆด้วย Thaibulksms API ทั้งแบบข้อความและ OTP
181 lines (150 loc) • 4.57 kB
JavaScript
'use strict';
const axios = require('axios');
const expect = require('expect')
const otp = require('../otp')
jest.mock('axios');
describe('Request OTP', () => {
test('Check typeof otp equalt function', () => {
expect(typeof otp).toBe('function')
})
test('Load package otp', () => {
expect(otp()).toBeDefined();
})
test('Check apikey and apiSecret [V1]', () => {
let options = {
apiKey: 'keyotptest',
apiSecret: 'secretTest',
version: 'v1'
}
const _otp = otp(options)
expect(_otp.configs.apiKey).toBe(options.apiKey)
expect(_otp.configs.apiSecret).toBe(options.apiSecret)
})
test('Check request fail [V1]', async () => {
const respFail = {
response: {
status: 400,
statusText: 'Bad Request',
data: {
error: {
code: 400,
errors: [
{
detail: {
statusText: 'invalidateion slsls'
}
}
],
}
}
}
};
axios.post.mockReturnValue(Promise.reject(respFail))
let options = {
apiKey: 'keyotptest',
apiSecret: 'secretTest',
version: 'v1'
}
try {
const _otp = otp(options)
await _otp.request()
} catch (error) {
expect(error).toMatchObject({
code: respFail.response.status,
message: respFail.response.data.error.errors[0],
statusText: respFail.response.statusText,
})
}
})
test('Check request success [V1]', async () => {
const resp = {
"data": {
"data": {
"status": "string",
"token": "string"
}
}
};
axios.post.mockResolvedValue(resp);
let options = {
apiKey: 'keyotptest',
apiSecret: 'secretTest',
version: 'v1'
}
const _otp = otp(options)
try {
const res = await _otp.request()
expect(res).toEqual(resp.data)
} catch (error) {
expect(error).toBe(1);
}
})
test('Check apikey and apiSecret [V2]', () => {
let options = {
apiKey: 'keyotptest',
apiSecret: 'secretTest',
version: 'v2'
}
const _otp = otp(options)
expect(_otp.configs.apiKey).toBe(options.apiKey)
expect(_otp.configs.apiSecret).toBe(options.apiSecret)
})
test('Check request fail [V2]', async () => {
const respFail = {
response: {
status: 400,
statusText: 'Bad Request',
data: {
code: 400,
errors: [
{
detail: {
statusText: 'invalidateion slsls'
}
}
],
}
}
};
axios.post.mockReturnValue(Promise.reject(respFail))
let options = {
apiKey: 'keyotptest',
apiSecret: 'secretTest',
version: 'v2'
}
try {
const _otp = otp(options)
await _otp.request()
} catch (error) {
expect(error).toMatchObject({
code: respFail.response.status,
message: respFail.response.data.errors[0],
statusText: respFail.response.statusText,
})
}
})
test('Check request success [V2]', async () => {
const resp = {
"data": {
"data": {
"status": "success",
"token": "Qp3L2v1A",
"refno": "T3A57"
}
}
};
axios.post.mockResolvedValue(resp);
let options = {
apiKey: 'keyotptest',
apiSecret: 'secretTest',
version: 'v2'
}
const _otp = otp(options)
try {
const res = await _otp.request()
expect(res).toEqual(resp.data)
} catch (error) {
expect(error).toBe(1);
}
})
})