From 6c5f82d4cb5db9db0d196ea22934a7ef5e9c6bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Jadach?= Date: Fri, 6 Dec 2024 14:38:31 +0100 Subject: [PATCH] closeSession.spec test --- src/api/session/closeSession.spec.ts | 56 +++++++++++++++++----------- src/api/session/closeSession.ts | 2 +- src/api/session/setSession.spec.ts | 2 +- src/api/session/setSession.ts | 1 + src/types/session.ts | 2 +- 5 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/api/session/closeSession.spec.ts b/src/api/session/closeSession.spec.ts index 279a34aa..8a7c624b 100644 --- a/src/api/session/closeSession.spec.ts +++ b/src/api/session/closeSession.spec.ts @@ -1,45 +1,57 @@ import { sdkApiError } from '../../utils'; -import { removeAlias } from './removeAlias'; +import { closeSession } from './closeSession'; +import { + RemoveBasicAuthenticationPayloadType, + CloseSessionPayloadCallType +} from '../../types'; import nock from 'nock'; const API_ENDPOINT = 'http://localhost:3001'; const API_TOKEN = 'S3CR3T-T0K3N'; -const ALIAS = 'my-alias'; +const API_TOKEN_INVALID = 'my-invalid-api-token'; +const PROTOCOL = 'udp'; -describe('removeAlias', () => { +const body: RemoveBasicAuthenticationPayloadType = { + listeningIp: "127.0.0.1", + port: 9999 +}; + + +describe('closeSession', () => { afterEach(() => { nock.cleanAll(); }); - test('should return 204 if alias is removed successfully', async function () { - const API_TOKEN = 'my-api-key'; - - nock(API_ENDPOINT).delete(`/api/v3/aliases/${ALIAS}`).reply(204); + test('should return 204 if session is removed successfully', async function () { + nock(API_ENDPOINT) + .delete(`/api/v3/session/${PROTOCOL}`, body) + .reply(204); - const response = await removeAlias({ + const response = await closeSession({ apiEndpoint: API_ENDPOINT, apiToken: API_TOKEN, - alias: ALIAS + protocol: PROTOCOL, + ...body }); expect(response).toBe(true); }); test('should return 401 if authentication failed', async function () { - const invalidApiToken = 'my-invalid-api-token'; const expectedResponse = { status: 'UNAUTHORIZED', error: 'authentication failed' }; nock(API_ENDPOINT) - .delete(`/api/v3/aliases/${ALIAS}`) + .delete(`/api/v3/session/${PROTOCOL}`, body) .reply(401, expectedResponse); await expect( - removeAlias({ - alias: ALIAS, - apiToken: invalidApiToken, - apiEndpoint: API_ENDPOINT + closeSession({ + apiEndpoint: API_ENDPOINT, + apiToken: API_TOKEN_INVALID, + protocol: PROTOCOL, + ...body }) ).rejects.toThrow(sdkApiError); }); @@ -51,13 +63,14 @@ describe('removeAlias', () => { }; nock(API_ENDPOINT) - .delete(`/api/v3/aliases/${ALIAS}`) + .delete(`/api/v3/session/${PROTOCOL}`, body) .reply(403, expectedResponse); await expect( - removeAlias({ + closeSession({ apiEndpoint: API_ENDPOINT, apiToken: API_TOKEN, - alias: ALIAS + protocol: PROTOCOL, + ...body }) ).rejects.toThrow(sdkApiError); }); @@ -69,14 +82,15 @@ describe('removeAlias', () => { }; nock(API_ENDPOINT) - .delete(`/api/v3/aliases/${ALIAS}`) + .delete(`/api/v3/session/${PROTOCOL}`, body) .reply(422, expectedResponse); await expect( - removeAlias({ + closeSession({ apiEndpoint: API_ENDPOINT, apiToken: API_TOKEN, - alias: ALIAS + protocol: PROTOCOL, + ...body }) ).rejects.toThrow(sdkApiError); }); diff --git a/src/api/session/closeSession.ts b/src/api/session/closeSession.ts index 8e84fa79..2e321b94 100644 --- a/src/api/session/closeSession.ts +++ b/src/api/session/closeSession.ts @@ -20,7 +20,7 @@ import { * @returns A Promise that resolves to true if the alias was successfully removed. * @throws An error that occurred while processing the request. */ -export const removeAlias = async ( +export const closeSession = async ( payload: CloseSessionPayloadType ): Promise => { const { protocol, apiToken, apiEndpoint, ...rest } = payload; diff --git a/src/api/session/setSession.spec.ts b/src/api/session/setSession.spec.ts index 420f023c..79a30ad0 100644 --- a/src/api/session/setSession.spec.ts +++ b/src/api/session/setSession.spec.ts @@ -46,7 +46,7 @@ describe('setSession function', () => { protocol: PROTOCOL, ...body }); - expect(result).toBe(resp); + expect(result).toEqual(resp); }); test('should return 400 if invalid peerId was provided', async function () { diff --git a/src/api/session/setSession.ts b/src/api/session/setSession.ts index 50883c05..a991f043 100644 --- a/src/api/session/setSession.ts +++ b/src/api/session/setSession.ts @@ -51,6 +51,7 @@ export const setSession = async ( if (rawResponse.ok) { const parsedRes = SetSessionResponse.safeParse(jsonResponse); if (parsedRes.success) { + console.log('xx', parsedRes.data) return parsedRes.data; } throw new ZodError(parsedRes.error.issues); diff --git a/src/types/session.ts b/src/types/session.ts index d1909fb4..d1c11090 100644 --- a/src/types/session.ts +++ b/src/types/session.ts @@ -8,7 +8,7 @@ import { BasePayload } from './general'; const SessionProtocols = z.enum([ "udp", "tcp"]); const SessionCapabilities = z.enum([ "Retransmission", "Segmentation"]); -export const SessionPayload = BasePayload.extend({ +export const SessionPayload = z.object({ ip: z.string(), port: z.number(), protocol: SessionProtocols,