diff --git a/src/api/session/closeSession.spec.ts b/src/api/session/closeSession.spec.ts index 279a34a..8a7c624 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 8e84fa7..2e321b9 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 420f023..79a30ad 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 50883c0..a991f04 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 d1909fb..d1c1109 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,