Skip to content

Commit

Permalink
closeSession.spec test
Browse files Browse the repository at this point in the history
  • Loading branch information
mjadach-iv committed Dec 6, 2024
1 parent 09060a1 commit 6c5f82d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
56 changes: 35 additions & 21 deletions src/api/session/closeSession.spec.ts
Original file line number Diff line number Diff line change
@@ -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<CloseSessionPayloadCallType> = {
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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion src/api/session/closeSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> => {
const { protocol, apiToken, apiEndpoint, ...rest } = payload;
Expand Down
2 changes: 1 addition & 1 deletion src/api/session/setSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
1 change: 1 addition & 0 deletions src/api/session/setSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6c5f82d

Please sign in to comment.