Skip to content

Commit

Permalink
setSession tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjadach-iv committed Dec 6, 2024
1 parent fbff4bc commit 09060a1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 58 deletions.
99 changes: 50 additions & 49 deletions src/api/session/setSession.spec.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
import nock from 'nock';
import { setSession } from './setSession';
import { sdkApiError } from '../../utils';
import { SetAliasPayloadType } from '../../types';
import { RemoveBasicAuthenticationPayloadType, SetSessionResponseType, SetSessionPayloadCallType } from '../../types';

const API_ENDPOINT = 'http://localhost:3001';
const API_TOKEN = 'S3CR3T-T0K3N';
const ALIAS = 'my-alias';
const API_TOKEN_INVALID = 'my-invalid-api-token';
const PEER_ID = 'peer123';
const PROTOCOL = 'udp';

describe('setAlias function', () => {
const body: RemoveBasicAuthenticationPayloadType<SetSessionPayloadCallType> = {
destination: PEER_ID,
capabilities: [
"Retransmission",
"Segmentation"
],
listenHost: "127.0.0.1:10000",
path: {
Hops: 1
},
target: {
Plain: "example.com:8080"
}
};


describe('setSession function', () => {
afterEach(() => {
nock.cleanAll();
});
/* Transition period between 2.1 and 2.2 */
test('should return 201 and undefined if successful using peerId', async function () {
nock(API_ENDPOINT)
.post('/api/v3/aliases', {
peerId: PEER_ID,
alias: ALIAS
})
.reply(201);

const result = await setAlias({
apiToken: API_TOKEN,
apiEndpoint: API_ENDPOINT,
peerId: PEER_ID,
alias: ALIAS
});
expect(result).toBe(true);
});
/* ------------------------------------ */
test('should return 201 and undefined if successful', async function () {
test('should return 200 if successful', async function () {
const resp = {
ip: "127.0.0.1",
port: 5542,
protocol: "tcp",
target: "example.com:80"
}
nock(API_ENDPOINT)
.post('/api/v3/aliases', {
destination: PEER_ID,
alias: ALIAS
})
.reply(201);
.post(`/api/v3/session/${PROTOCOL}`, body)
.reply(200, resp);

const result = await setAlias({
const result = await setSession({
apiToken: API_TOKEN,
apiEndpoint: API_ENDPOINT,
destination: PEER_ID,
alias: ALIAS
protocol: PROTOCOL,
...body
});
expect(result).toBe(true);
expect(result).toBe(resp);
});

test('should return 400 if invalid peerId was provided', async function () {
nock(API_ENDPOINT)
.post('/api/v3/aliases', { peerId: PEER_ID, alias: ALIAS })
.post(`/api/v3/session/${PROTOCOL}`, body)
.reply(400, { status: 'INVALID_PEERID' });

await expect(
setAlias({
setSession({
apiToken: API_TOKEN,
apiEndpoint: API_ENDPOINT,
peerId: PEER_ID,
alias: ALIAS
protocol: PROTOCOL,
...body
})
).rejects.toThrow(sdkApiError);
});
Expand All @@ -67,17 +69,16 @@ describe('setAlias function', () => {
status: 'UNAUTHORIZED',
error: 'authentication failed'
};
const invalidApiToken = 'my-invalid-api-token';
nock(API_ENDPOINT)
.post('/api/v3/aliases', { peerId: PEER_ID, alias: ALIAS })
.post(`/api/v3/session/${PROTOCOL}`, body)
.reply(401, expectedResponse);

await expect(
setAlias({
apiToken: invalidApiToken,
setSession({
apiToken: API_TOKEN_INVALID,
apiEndpoint: API_ENDPOINT,
peerId: PEER_ID,
alias: ALIAS
protocol: PROTOCOL,
...body
})
).rejects.toThrow(sdkApiError);
});
Expand All @@ -88,15 +89,15 @@ describe('setAlias function', () => {
error: 'You are not authorized to perform this action'
};
nock(API_ENDPOINT)
.post('/api/v3/aliases', { peerId: PEER_ID, alias: ALIAS })
.post(`/api/v3/session/${PROTOCOL}`, body)
.reply(403, expectedResponse);

await expect(
setAlias({
setSession({
apiToken: API_TOKEN,
apiEndpoint: API_ENDPOINT,
peerId: PEER_ID,
alias: ALIAS
protocol: PROTOCOL,
...body
})
).rejects.toThrow(sdkApiError);
});
Expand All @@ -107,15 +108,15 @@ describe('setAlias function', () => {
error: 'Full error message.'
};
nock(API_ENDPOINT)
.post('/api/v3/aliases', { peerId: PEER_ID, alias: ALIAS })
.post(`/api/v3/session/${PROTOCOL}`, body)
.reply(422, expectedResponse);

await expect(
setAlias({
setSession({
apiToken: API_TOKEN,
apiEndpoint: API_ENDPOINT,
peerId: PEER_ID,
alias: ALIAS
protocol: PROTOCOL,
...body
})
).rejects.toThrow(sdkApiError);
});
Expand Down
22 changes: 14 additions & 8 deletions src/api/session/setSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ZodError } from 'zod';
import {
ApiErrorResponse,
RemoveBasicAuthenticationPayloadType,
SetSessionResponse,
SetSessionResponseType,
SetSessionPayloadType,
SetSessionPayloadCallType
} from '../../types';
Expand All @@ -20,7 +22,7 @@ import { sdkApiError, fetchWithTimeout, getHeaders } from '../../utils';
*/
export const setSession = async (
payload: SetSessionPayloadType
): Promise<boolean> => {
): Promise<SetSessionResponseType> => {
const { protocol, apiToken, apiEndpoint, ...rest } = payload;
const body: RemoveBasicAuthenticationPayloadType<SetSessionPayloadCallType> = {
...rest
Expand All @@ -37,20 +39,24 @@ export const setSession = async (
payload.timeout
);

// received expected response
if (rawResponse.status === 200) {
return true;
}

// received unexpected error from server
if (rawResponse.status > 499) {
throw new Error(rawResponse.statusText);
}

// check if response has the structure of an expected api error
const jsonResponse = await rawResponse.json();
const isApiErrorResponse = ApiErrorResponse.safeParse(jsonResponse);

// parsedRes and error {} from HOPRd have the same type,
// we can only rely on rawResponse.ok to know if its a success
if (rawResponse.ok) {
const parsedRes = SetSessionResponse.safeParse(jsonResponse);
if (parsedRes.success) {
return parsedRes.data;
}
throw new ZodError(parsedRes.error.issues);
}

const isApiErrorResponse = ApiErrorResponse.safeParse(jsonResponse);
if (isApiErrorResponse.success) {
throw new sdkApiError({
status: rawResponse.status,
Expand Down
4 changes: 3 additions & 1 deletion src/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type GetSessionsResponseType = z.infer<typeof GetSessionsResponse>;
*/

export const SetSessionPayloadCall = BasePayload.extend({
capabilities: SessionCapabilities,
capabilities: SessionCapabilities.array(),
destination: z.string(),
listenHost: z.string(),
path: z.object({
Expand All @@ -53,6 +53,8 @@ export const SetSessionPayload = SetSessionPayloadCall.extend({

export type SetSessionPayloadCallType = z.infer<typeof SetSessionPayloadCall>;
export type SetSessionPayloadType = z.infer<typeof SetSessionPayload>;
export const SetSessionResponse = SessionPayload;
export type SetSessionResponseType = SessionPayloadType;

/**
* getAlias
Expand Down

0 comments on commit 09060a1

Please sign in to comment.