Skip to content

Commit

Permalink
test: add api handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tapiarafael committed Jan 5, 2024
1 parent d8b094b commit 47fe9d0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
6 changes: 3 additions & 3 deletions deno-runtime/handlers/api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Defined, JsonRpcError } from "jsonrpc-lite";
import { AppAccessorsInstance } from "../lib/accessors/mod.ts";

export default async function apiHandler(call: string, params: unknown): Promise<JsonRpcError | Defined> {
const [, path, methodName] = call.split(':');
const [, path, httpMethod] = call.split(':');

const endpoint = AppObjectRegistry.get<IApiEndpoint>(`api:${path}`);
const logger = AppObjectRegistry.get<Logger>('logger');
Expand All @@ -14,10 +14,10 @@ export default async function apiHandler(call: string, params: unknown): Promise
return new JsonRpcError(`Endpoint ${path} not found`, -32000);
}

const method = endpoint[methodName as keyof IApiEndpoint];
const method = endpoint[httpMethod as keyof IApiEndpoint];

if (typeof method !== 'function') {
return new JsonRpcError(`Method ${methodName} not found`, -32000);
return new JsonRpcError(`${path}'s ${httpMethod} not exists`, -32000);
}

const [ request, endpointInfo ] = params as Array<unknown>;
Expand Down
79 changes: 79 additions & 0 deletions deno-runtime/handlers/tests/api-handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// deno-lint-ignore-file no-explicit-any
import { assertEquals, assertObjectMatch } from 'https://deno.land/[email protected]/assert/mod.ts';
import { beforeEach, describe, it } from 'https://deno.land/[email protected]/testing/bdd.ts';
import { spy } from "https://deno.land/[email protected]/testing/mock.ts";

import { AppObjectRegistry } from '../../AppObjectRegistry.ts';
import { assertInstanceOf } from "https://deno.land/[email protected]/assert/assert_instance_of.ts";
import { JsonRpcError } from "jsonrpc-lite";
import { IApiEndpoint } from "@rocket.chat/apps-engine/definition/api/IApiEndpoint.ts";
import apiHandler from "../api-handler.ts";

describe('handlers > api', () => {
const mockEndpoint: IApiEndpoint = {
path: '/test',
// deno-lint-ignore no-unused-vars
get: (request: any, endpoint: any, read: any, modify: any, http: any, persis: any) => Promise.resolve('ok'),
// deno-lint-ignore no-unused-vars
post: (request: any, endpoint: any, read: any, modify: any, http: any, persis: any) => Promise.resolve('ok'),
// deno-lint-ignore no-unused-vars
put: (request: any, endpoint: any, read: any, modify: any, http: any, persis: any) => { throw new Error('Method execution error example') },
}

beforeEach(() => {
AppObjectRegistry.clear();
AppObjectRegistry.set('api:/test', mockEndpoint);
});

it('correctly handles execution of an api endpoint method GET', async () => {
const _spy = spy(mockEndpoint, 'get');

const result = await apiHandler('api:/test:get', ['request', 'endpointInfo']);

assertEquals(result, 'ok');
assertEquals(_spy.calls[0].args.length, 6);
assertEquals(_spy.calls[0].args[0], 'request');
assertEquals(_spy.calls[0].args[1], 'endpointInfo');
});

it('correctly handles execution of an api endpoint method POST', async () => {
const _spy = spy(mockEndpoint, 'post');

const result = await apiHandler('api:/test:post', ['request', 'endpointInfo']);

assertEquals(result, 'ok');
assertEquals(_spy.calls[0].args.length, 6);
assertEquals(_spy.calls[0].args[0], 'request');
assertEquals(_spy.calls[0].args[1], 'endpointInfo');
});

it('correctly handles an error if the method not exists for the selected endpoint', async () => {
const result = await apiHandler(`api:/test:delete`, ['request', 'endpointInfo']);

assertInstanceOf(result, JsonRpcError)
assertObjectMatch(result, {
message: `/test's delete not exists`,
code: -32000
})
});

it('correctly handles an error if endpoint not exists', async () => {
const result = await apiHandler(`api:/error:get`, ['request', 'endpointInfo']);

assertInstanceOf(result, JsonRpcError)
assertObjectMatch(result, {
message: `Endpoint /error not found`,
code: -32000
})
});

it('correctly handles an error if the method execution fails', async () => {
const result = await apiHandler(`api:/test:put`, ['request', 'endpointInfo']);

assertInstanceOf(result, JsonRpcError)
assertObjectMatch(result, {
message: `Method execution error example`,
code: -32000
})
});
});

0 comments on commit 47fe9d0

Please sign in to comment.