Skip to content

tests: missing tests on websocket #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 103 additions & 1 deletion packages/wallet-service/tests/ws.utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
import { mockedAddAlert } from '@tests/utils/alerting.utils.mock';
import { connectionInfoFromEvent } from '@src/ws/utils';
import { connectionInfoFromEvent, sendMessageToClient } from '@src/ws/utils';
import { Severity } from '@src/types';

import { logger } from '@tests/winston.mock';
import { RedisClient } from 'redis';
import {
GoneException,
} from '@aws-sdk/client-apigatewaymanagementapi';
import { RedisConfig } from '@src/types';

const mockedSend = jest.fn();

jest.mock('@src/redis', () => {
const originalModule = jest.requireActual('@src/redis');
return {
...originalModule,
endWsConnection: jest.fn(),
};
});

jest.mock('@aws-sdk/client-apigatewaymanagementapi', () => {
const originalModule = jest.requireActual('@aws-sdk/client-apigatewaymanagementapi');
return {
...originalModule,
ApiGatewayManagementApiClient: jest.fn().mockImplementation(() => ({
send: mockedSend,
})),
};
});

jest.mock('redis', () => ({
RedisClient: jest.fn().mockImplementation(() => ({
endWsConnection: jest.fn(),
on: jest.fn(),
set: jest.fn(),
get: jest.fn(),
del: jest.fn(),
quit: jest.fn(),
})),
}));

import { endWsConnection } from '@src/redis';

test('connectionInfoFromEvent', async () => {
expect.hasAssertions();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down Expand Up @@ -42,3 +82,65 @@ test('missing WS_DOMAIN should throw', () => {
Severity.MINOR,
);
});

describe('sendMessageToClient', () => {
let client: any;
const redisConfig: RedisConfig = {
url: 'http://doesntmatter.com',
password: 'password',
};
const connInfo = { url: 'http://example.com', id: '1234' };
const message = 'hello';

beforeEach(() => {
jest.clearAllMocks();
client = new RedisClient(redisConfig);
});

it('should send a message successfully', async () => {
mockedSend.mockResolvedValue({
$metadata: { httpStatusCode: 200 },
});

await sendMessageToClient(client, connInfo, message);

expect(mockedSend).toHaveBeenCalledWith(expect.objectContaining({
input: expect.objectContaining({
ConnectionId: connInfo.id,
Data: JSON.stringify(message),
})
}));

expect(logger.error).not.toHaveBeenCalled();
});

it('should log and throw an error if API Gateway returns non-200 status', async () => {
mockedSend.mockResolvedValue({
$metadata: { httpStatusCode: 400 },
});

await sendMessageToClient(client, connInfo, message);

expect(mockedAddAlert).toHaveBeenCalledWith(
'Unhandled error while sending websocket message to client',
'The wallet-service was unable to handle an error while attempting to send a message to a websocket client. Please check the logs.',
Severity.MINOR,
{
ConnectionId: connInfo.id,
Message: JSON.stringify(message),
}
);
});

it('should handle GoneException by closing the connection', async () => {
mockedSend.mockRejectedValue(new GoneException({
message: 'Connection is gone.',
$metadata: {
httpStatusCode: 410,
}
}));

await sendMessageToClient(client, connInfo, message);
expect(endWsConnection).toHaveBeenCalledWith(client, connInfo.id);
});
});
Loading