-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Douglas Gubert <[email protected]>
- Loading branch information
1 parent
4a7a913
commit f09427e
Showing
4 changed files
with
203 additions
and
59 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
deno-runtime/handlers/tests/videoconference-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// 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 videoconfHandler from '../videoconference-handler.ts'; | ||
import { assertInstanceOf } from "https://deno.land/[email protected]/assert/assert_instance_of.ts"; | ||
import { JsonRpcError } from "jsonrpc-lite"; | ||
|
||
describe('handlers > videoconference', () => { | ||
// deno-lint-ignore no-unused-vars | ||
const mockMethodWithoutParam = (read: any, modify: any, http: any, persis: any): Promise<string> => Promise.resolve('ok none'); | ||
// deno-lint-ignore no-unused-vars | ||
const mockMethodWithOneParam = (call: any, read: any, modify: any, http: any, persis: any): Promise<string> => Promise.resolve('ok one'); | ||
// deno-lint-ignore no-unused-vars | ||
const mockMethodWithTwoParam = (call: any, user: any, read: any, modify: any, http: any, persis: any): Promise<string> => Promise.resolve('ok two'); | ||
// deno-lint-ignore no-unused-vars | ||
const mockMethodWithThreeParam = (call: any, user: any, options: any, read: any, modify: any, http: any, persis: any): Promise<string> => Promise.resolve('ok three'); | ||
const mockProvider = { | ||
empty: mockMethodWithoutParam, | ||
one: mockMethodWithOneParam, | ||
two: mockMethodWithTwoParam, | ||
three: mockMethodWithThreeParam, | ||
notAFunction: true, | ||
error: () => { throw new Error('Method execution error example') } | ||
} | ||
|
||
beforeEach(() => { | ||
AppObjectRegistry.clear(); | ||
AppObjectRegistry.set('videoConfProvider:test-provider', mockProvider); | ||
}); | ||
|
||
it('correctly handles execution of a videoconf method without additional params', async () => { | ||
const _spy = spy(mockProvider, 'empty'); | ||
|
||
const result = await videoconfHandler('videoconference:test-provider:empty', []); | ||
|
||
assertEquals(result, 'ok none') | ||
assertEquals(_spy.calls[0].args.length, 4); | ||
|
||
_spy.restore(); | ||
}); | ||
|
||
it('correctly handles execution of a videoconf method with one param', async () => { | ||
const _spy = spy(mockProvider, 'one'); | ||
|
||
const result = await videoconfHandler('videoconference:test-provider:one', ['call']); | ||
|
||
assertEquals(result, 'ok one') | ||
assertEquals(_spy.calls[0].args.length, 5); | ||
assertEquals(_spy.calls[0].args[0], 'call'); | ||
|
||
_spy.restore(); | ||
}); | ||
|
||
it('correctly handles execution of a videoconf method with two params', async () => { | ||
const _spy = spy(mockProvider, 'two'); | ||
|
||
const result = await videoconfHandler('videoconference:test-provider:two', ['call', 'user']); | ||
|
||
assertEquals(result, 'ok two') | ||
assertEquals(_spy.calls[0].args.length, 6); | ||
assertEquals(_spy.calls[0].args[0], 'call'); | ||
assertEquals(_spy.calls[0].args[1], 'user'); | ||
|
||
_spy.restore(); | ||
}); | ||
|
||
it('correctly handles execution of a videoconf method with three params', async () => { | ||
const _spy = spy(mockProvider, 'three'); | ||
|
||
const result = await videoconfHandler('videoconference:test-provider:three', ['call', 'user', 'options']); | ||
|
||
assertEquals(result, 'ok three') | ||
assertEquals(_spy.calls[0].args.length, 7); | ||
assertEquals(_spy.calls[0].args[0], 'call'); | ||
assertEquals(_spy.calls[0].args[1], 'user'); | ||
assertEquals(_spy.calls[0].args[2], 'options'); | ||
|
||
_spy.restore(); | ||
}); | ||
|
||
it('correctly handles an error on execution of a videoconf method', async () => { | ||
const result = await videoconfHandler('videoconference:test-provider:error', []); | ||
|
||
assertInstanceOf(result, JsonRpcError) | ||
assertObjectMatch(result, { | ||
message: 'Method execution error example', | ||
code: -32000 | ||
}) | ||
}) | ||
|
||
it('correctly handles an error when provider is not found', async () => { | ||
const providerName = 'error-provider' | ||
const result = await videoconfHandler(`videoconference:${providerName}:method`, []); | ||
|
||
assertInstanceOf(result, JsonRpcError) | ||
assertObjectMatch(result, { | ||
message: `Provider ${providerName} not found`, | ||
code: -32000 | ||
}) | ||
}) | ||
|
||
it('correctly handles an error if method is not a function of provider', async () => { | ||
const methodName = 'notAFunction' | ||
const providerName = 'test-provider' | ||
const result = await videoconfHandler(`videoconference:${providerName}:${methodName}`, []); | ||
|
||
assertInstanceOf(result, JsonRpcError) | ||
assertObjectMatch(result, { | ||
message: `Method ${methodName} not found on provider ${providerName}`, | ||
code: -32000 | ||
}) | ||
}) | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Defined, JsonRpcError } from "jsonrpc-lite"; | ||
import { AppObjectRegistry } from "../AppObjectRegistry.ts"; | ||
import { IVideoConfProvider } from "@rocket.chat/apps-engine/definition/videoConfProviders/IVideoConfProvider.ts"; | ||
import { AppAccessorsInstance } from "../lib/accessors/mod.ts"; | ||
import { Logger } from "../lib/logger.ts"; | ||
|
||
export default async function videoConferenceHandler(call: string, params: unknown): Promise<JsonRpcError | Defined> { | ||
const [, providerName, methodName] = call.split(':'); | ||
|
||
const provider = AppObjectRegistry.get<IVideoConfProvider>(`videoConfProvider:${providerName}`); | ||
const logger = AppObjectRegistry.get<Logger>('logger'); | ||
|
||
if (!provider) { | ||
return new JsonRpcError(`Provider ${providerName} not found`, -32000); | ||
} | ||
|
||
const method = provider[methodName as keyof IVideoConfProvider]; | ||
|
||
if (typeof method !== 'function') { | ||
return new JsonRpcError(`Method ${methodName} not found on provider ${providerName}`, -32000); | ||
} | ||
|
||
const [videoconf, user, options] = params as Array<unknown>; | ||
|
||
logger?.debug(`Executing ${methodName} on video conference provider...`); | ||
|
||
const args = [ | ||
...(videoconf ? [videoconf] : []), | ||
...(user ? [user] : []), | ||
...(options ? [options] : []), | ||
]; | ||
|
||
try { | ||
// deno-lint-ignore ban-types | ||
const result = await(method as Function).apply(provider, [ | ||
...args, | ||
AppAccessorsInstance.getReader(), | ||
AppAccessorsInstance.getModifier(), | ||
AppAccessorsInstance.getHttp(), | ||
AppAccessorsInstance.getPersistence() | ||
]); | ||
|
||
logger?.debug(`Video Conference Provider's ${methodName} was successfully executed.`); | ||
|
||
return result; | ||
} catch (e) { | ||
logger?.debug(`Video Conference Provider's ${methodName} was unsuccessful.`); | ||
return new JsonRpcError(e.message, -32000); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters