@telstra/messaging
Version:
Telstra SDK Messaging
517 lines (470 loc) • 21.1 kB
text/typescript
import { AssertionError } from '@telstra/core';
import { Messages } from '../../../src/classes/index.js';
import { TMessageSend, TMessageUpdate, TMessageUpdateTags, TMultimediaContentType } from '../../../src/types/index.js';
import { Constants } from '../../shared/Constants.js';
import AUTH_CONFIG from '../../shared/credentials.json' with { type: 'json' };
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest';
import { server } from '../../shared/mswServer.js';
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
const messages = new Messages(AUTH_CONFIG);
const validMessageData: TMessageUpdate = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
to: '+61234567890',
from: 'private',
messageContent: 'Hello from Messaging SDK!',
tags: ['Msg', 'V3'],
};
const expectAssertionError = async (data: Partial<TMessageUpdate>) => {
const callback = async () => messages.update(data as TMessageUpdate);
await expect(callback).rejects.toThrow(AssertionError);
};
describe('Message', () => {
describe('getAllMessages', () => {
describe('when the client sends a valid request', () => {
it('should pass', async () => {
await expect(messages.getAll()).resolves.toEqual(Constants.GET_ALL_MESSAGES_RESPONSE);
});
});
});
describe('getMessage', () => {
describe('when the client sends a valid request', () => {
it('should pass', async () => {
await expect(messages.get('8369468e-20c9-11ee-be56-0242ac120002')).resolves.toEqual(
Constants.GET_MESSAGE_RESPONSE,
);
});
});
});
describe('send', () => {
const validData: TMessageSend[] = [
{ to: '+61234567890', from: 'private', messageContent: 'Hello from Messaging SDK!' },
{ to: ['+61234567890'], from: 'private', messageContent: 'Hello from Messaging SDK!' },
{ to: ['+61234567890'], from: 'private', messageContent: 'Hello from Messaging SDK!' },
{ to: ['+61234567890'], from: '0423456789', messageContent: 'Hello from Messaging SDK!' },
{
to: ['+61234567890'],
from: 'private',
multimedia: [
{
type: TMultimediaContentType.IMAGE_JPEG,
fileName: 'hello.jpeg',
payload:
'R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj==',
},
],
},
{
to: [
'+61234567891',
'+61234567892',
'+61234567893',
'+61234567894',
'+61234567895',
'+61234567896',
'+61234567897',
'+61234567898',
'+61234567899',
'+62234567890',
],
from: 'private',
multimedia: [
{
type: TMultimediaContentType.IMAGE_JPEG,
fileName: 'hello.jpeg',
payload:
'R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj==',
},
],
},
];
describe('when the client sends valid required attributes', () => {
validData.forEach((data, index) => {
it(`should pass with valid data set ${index + 1}`, async () => {
await expect(messages.send(data)).resolves.toEqual(Constants.SEND_MESSAGE_RESPONSE);
});
});
});
const invalidData: { data: any; error: any }[] = [
{ data: undefined, error: AssertionError },
{ data: {}, error: AssertionError },
{ data: { from: 'private', messageContent: '123456' }, error: AssertionError },
{ data: { to: '+61234567890', messageContent: '123456' }, error: AssertionError },
{ data: { to: '+61234567890', from: 'private' }, error: AssertionError },
{ data: { to: 123456, from: 'private', messageContent: '123456' }, error: AssertionError },
{ data: { to: [], from: 'private', messageContent: '123456' }, error: AssertionError },
{ data: { to: [123456], from: 'private', messageContent: '123456' }, error: AssertionError },
{ data: { to: ['1234'], from: 'private', messageContent: '123456' }, error: AssertionError },
{ data: { to: ['+6123456789012345'], from: 'private', messageContent: '123456' }, error: AssertionError },
{ data: { to: 123456, from: 7891011, messageContent: '123456' }, error: AssertionError },
{ data: { to: '+61234567890', messageContent: 'Hello from Messaging SDK', from: 123456 }, error: AssertionError },
{ data: { to: '+61234567890', from: 'private', messageContent: 123456 }, error: AssertionError },
{ data: { to: ['+61234567890'], from: 'private', multimedia: 'string value' }, error: AssertionError },
{
data: {
to: ['+61234567890'],
from: 'private',
multimedia: [{ fileName: 'demo.jpeg', payload: 'payload' }],
},
error: AssertionError,
},
{
data: {
to: ['+61234567890'],
from: 'private',
multimedia: [{ type: 123, fileName: 'demo.jpeg', payload: 'payload' }],
},
error: AssertionError,
},
{
data: {
to: ['+61234567890'],
from: 'private',
multimedia: [{ type: 'invalid string', fileName: 'demo.jpeg', payload: 'payload' }],
},
error: AssertionError,
},
{
data: {
to: ['+61234567890'],
from: 'private',
multimedia: [{ type: 'image/gif', fileName: 'demo.jpeg' }],
},
error: AssertionError,
},
{
data: {
to: ['+61234567890'],
from: 'private',
multimedia: [{ type: TMultimediaContentType.IMAGE_JPEG, fileName: 'demo.jpeg', payload: 123 }],
},
error: AssertionError,
},
];
describe('when the client sends invalid required attributes', () => {
invalidData.forEach(({ data, error }, index) => {
it(`should fail with invalid data set ${index + 1}`, async () => {
const callback = async () => messages.send(data);
await expect(callback).rejects.toThrow(error);
});
});
});
const validOptionalData: TMessageSend = {
to: ['+61234567890'],
from: 'private',
multimedia: [
{
type: TMultimediaContentType.IMAGE_JPEG,
fileName: 'hello.jpeg',
payload:
'R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj==',
},
],
retryTimeout: 10,
scheduleSend: '2023-07-13T05:25:14.591Z',
deliveryNotification: true,
statusCallbackUrl: 'https://example.com',
tags: ['Msg-Api', 'V3'],
};
describe('when the client sends invalid optional attributes', () => {
it('should pass with valid attributes', async () => {
await expect(messages.send(validOptionalData)).resolves.toEqual(Constants.SEND_MESSAGE_RESPONSE);
});
const invalidOptionalData: { data: Partial<TMessageSend>; error: any }[] = [
{
data: { ...validOptionalData, retryTimeout: '10' as any },
error: AssertionError,
},
{
data: { ...validOptionalData, scheduleSend: '10:15:39' },
error: AssertionError,
},
{
data: { ...validOptionalData, scheduleSend: 'some invalid string' },
error: AssertionError,
},
{
data: { ...validOptionalData, deliveryNotification: 'false' as any },
error: AssertionError,
},
{
data: { ...validOptionalData, statusCallbackUrl: 'not a url' },
error: AssertionError,
},
{
data: { ...validOptionalData, queuePriority: '2' },
error: AssertionError,
},
];
invalidOptionalData.forEach(({ data, error }, index) => {
it(`should fail with invalid optional data set ${index + 1}`, async () => {
const callback = async () => messages.send(data as TMessageSend);
await expect(callback).rejects.toThrow(error);
});
});
});
});
describe('update', () => {
describe('when the client sends valid required attributes', () => {
it('should pass when [messageId] is a UUID v1 string', async () => {
await expect(messages.update(validMessageData)).resolves.toEqual(Constants.UPDATE_MESSAGE_RESPONSE);
});
});
describe('when the client sends invalid required attributes', () => {
it('should fail and throw an assertion error when [to] is missing', async () => {
const data = { ...validMessageData, to: undefined };
await expectAssertionError(data);
});
it('should fail and throw an assertion error when [from] is missing', async () => {
const data = { ...validMessageData, from: undefined };
await expectAssertionError(data);
});
it('should fail and throw an assertion error when [messageContent] is missing', async () => {
const data = { ...validMessageData, messageContent: undefined };
await expectAssertionError(data);
});
it('should fail and throw an assertion error when [to] is a number', async () => {
const data = { ...validMessageData, to: 123456 as any };
await expectAssertionError(data);
});
it('should fail and throw an assertion error when [to] is an empty array', async () => {
const data = { ...validMessageData, to: [] };
await expectAssertionError(data);
});
it('should fail and throw an assertion error when [to] is an array of numbers', async () => {
const data = { ...validMessageData, to: [123456 as any] };
await expectAssertionError(data);
});
it('should fail and throw an assertion error when [to] is an array of strings of length less than 5', async () => {
const data = { ...validMessageData, to: ['1234'] };
await expectAssertionError(data);
});
it('should fail and throw an assertion error when [to] is an array of strings of length greater than 16', async () => {
const data = { ...validMessageData, to: ['+6123456789012345'] };
await expectAssertionError(data);
});
it('should fail and throw an assertion error when [to, from] are numbers', async () => {
const data = { ...validMessageData, to: 123456 as any, from: 7891011 as any };
await expectAssertionError(data);
});
it('should fail and throw an assertion error when [from] is a number', async () => {
const data = { ...validMessageData, from: 123456 as any };
await expectAssertionError(data);
});
it('should pass when [from] is a string', async () => {
const data = { ...validMessageData, from: '123456' };
await expect(messages.update(data)).resolves.toEqual(Constants.UPDATE_MESSAGE_RESPONSE);
});
it('should fail and throw an assertion error when [messageContent] is a number', async () => {
const data = { ...validMessageData, messageContent: 123456 as any };
await expectAssertionError(data);
});
it('should fail and throw an assertion error when [multimedia] is not an array', async () => {
const data = { ...validMessageData, multimedia: 'string value' as any };
await expectAssertionError(data);
});
it('should fail and throw an assertion error as the [type] property of [multimedia] object is required field', async () => {
const data = {
...validMessageData,
multimedia: [{ fileName: 'demo.jpeg', payload: 'payload' }] as any,
};
await expectAssertionError(data);
});
it('should fail and throw an assertion error when the [type] property of [multimedia] object is not a string', async () => {
const data = {
...validMessageData,
multimedia: [{ type: 123, fileName: 'demo.jpeg', payload: 'payload' }] as any,
};
await expectAssertionError(data);
});
it('should fail and throw an assertion error as the [payload] property of [multimedia] object is required field', async () => {
const data = {
...validMessageData,
multimedia: [{ type: 'image/gif', fileName: 'demo.jpeg' }] as any,
};
await expectAssertionError(data);
});
it('should fail and throw an assertion error when the [payload] property of [multimedia] object is not a string', async () => {
const data = {
...validMessageData,
multimedia: [{ type: TMultimediaContentType.IMAGE_JPEG, fileName: 'demo.jpeg', payload: 123 }] as any,
};
await expectAssertionError(data);
});
});
describe('when the client sends invalid optional attributes', () => {
it('should pass with valid attributes', async () => {
const data: TMessageUpdate = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
to: '+61234567890',
from: 'private',
multimedia: [
{
type: TMultimediaContentType.IMAGE_JPEG,
fileName: 'hello.jpeg',
payload:
'R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj==',
},
],
retryTimeout: 10,
scheduleSend: '2023-07-13T05:25:14.591Z',
deliveryNotification: true,
statusCallbackUrl: 'https://example.com',
queuePriority: 2,
tags: ['Msg-Api', 'V3'],
};
await expect(messages.update(data)).resolves.toEqual(Constants.UPDATE_MESSAGE_RESPONSE);
});
it('should fail and throw an assertion error when [retryTimeout] is a string', async () => {
const data: TMessageUpdate = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
to: '+61234567890',
from: 'private',
multimedia: {
type: TMultimediaContentType.IMAGE_JPEG,
fileName: 'hello.jpeg',
payload:
'R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj==',
},
retryTimeout: '10' as any,
};
const callback = async () => messages.update(data);
await expect(callback).rejects.toThrow(AssertionError);
});
it('should fail and throw an assertion error when [scheduleSend] is a string length < 10', async () => {
const data: TMessageUpdate = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
to: '+61234567890',
from: 'private',
multimedia: {
type: TMultimediaContentType.IMAGE_JPEG,
fileName: 'hello.jpeg',
payload:
'R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj==',
},
scheduleSend: '10:15:39',
};
const callback = async () => messages.update(data);
await expect(callback).rejects.toThrow(AssertionError);
});
it('should fail and throw an assertion error when [scheduleSend] is not a valid date-time', async () => {
const data: TMessageUpdate = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
to: '+61234567890',
from: 'private',
multimedia: {
type: TMultimediaContentType.IMAGE_JPEG,
fileName: 'hello.jpeg',
payload:
'R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj==',
},
scheduleSend: 'some invalid string',
};
const callback = async () => messages.update(data);
await expect(callback).rejects.toThrow(AssertionError);
});
it('should fail and throw an assertion error when [deliveryNotification] is a string', async () => {
const data: TMessageUpdate = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
to: '+61234567890',
from: 'private',
multimedia: {
type: TMultimediaContentType.IMAGE_JPEG,
fileName: 'hello.jpeg',
payload:
'R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj==',
},
deliveryNotification: 'false' as any,
};
const callback = async () => messages.update(data);
await expect(callback).rejects.toThrow(AssertionError);
});
it('should fail and throw an assertion error when [statusCallbackUrl] is not a url', async () => {
const data: TMessageUpdate = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
to: '+61234567890',
from: 'private',
multimedia: {
type: TMultimediaContentType.IMAGE_JPEG,
fileName: 'hello.jpeg',
payload:
'R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj==',
},
statusCallbackUrl: 'not a url',
};
const callback = async () => messages.update(data);
await expect(callback).rejects.toThrow(AssertionError);
});
it('should fail and throw an assertion error when [queuePriority] is a string', async () => {
const data: TMessageUpdate = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
to: '+61234567890',
from: 'private',
multimedia: {
type: TMultimediaContentType.IMAGE_JPEG,
fileName: 'hello.jpeg',
payload:
'R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj==',
},
queuePriority: '2',
};
const callback = async () => messages.update(data);
await expect(callback).rejects.toThrow(AssertionError);
});
});
describe('updateTags', () => {
describe('when the client sends valid required attributes', () => {
it('should pass when [messageId] is a UUID v1 string and [tags] is array of strings', async () => {
const data: TMessageUpdateTags = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
tags: ['Msg', 'V3'],
};
await expect(messages.updateTags(data)).resolves.toEqual('');
});
});
describe('when the client sends invalid required attributes', () => {
it('should fail and throw an assertion error when [messageId] is not passed', async () => {
// @ts-expect-error
const data: TMessageUpdateTags = {
tags: ['Msg', 'V3'],
};
const callback = async () => messages.updateTags(data);
await expect(callback).rejects.toThrow(AssertionError);
});
it('should fail and throw an assertion error when [messageId] is invalid UUID v1', async () => {
const data: TMessageUpdateTags = {
messageId: '8540d774-4555-4d2b-b788-4ecb19412e85',
tags: ['Msg', 'V3'],
};
const callback = async () => messages.updateTags(data);
await expect(callback).rejects.toThrow(AssertionError);
});
it('should fail and throw an assertion error when [tags] is not passed', async () => {
// @ts-expect-error
const data: TMessageUpdateTags = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
};
const callback = async () => messages.updateTags(data);
await expect(callback).rejects.toThrow(AssertionError);
});
it('should fail and throw an assertion error when [tags] is a string', async () => {
const data: TMessageUpdateTags = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
tags: 'Msg' as any,
};
const callback = async () => messages.updateTags(data);
await expect(callback).rejects.toThrow(AssertionError);
});
it('should fail and throw an assertion error when [tags] is an array of numbers', async () => {
const data: TMessageUpdateTags = {
messageId: '8369468e-20c9-11ee-be56-0242ac120002',
tags: [123, 786, 'Msg'] as any,
};
const callback = async () => messages.updateTags(data);
await expect(callback).rejects.toThrow(AssertionError);
});
});
});
});
});