Skip to content

Commit

Permalink
Merge e1f005e into 5daa7b3
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-casarrubias committed May 1, 2018
2 parents 5daa7b3 + e1f005e commit 52ce098
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onixjs/core",
"version": "1.0.0-alpha.22",
"version": "1.0.0-alpha.22.1",
"description": "An Enterprise Grade NodeJS Platform that implements Industry Standards and Patterns in order to provide Connectivity, Stability, High-Availability and High-Performance.",
"main": "dist/src/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/app.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class AppServer {
case OperationType.ONIX_REMOTE_CALL_PROCEDURE:
// Register Stream Request
if (operation.message.request.metadata.stream) {
this.streamer.register(operation, chunk => {
await this.streamer.register(operation, chunk => {
if (process.send)
process.send({
uuid: operation.uuid,
Expand Down
7 changes: 6 additions & 1 deletion src/core/call.responser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class CallResponser {
}
// Declare executable endpoint method and hooks references
let scope,
systemcall: boolean = false,
method: Function | null = null,
mainHook: Function = () => null,
slaveHook: Function | null = null,
Expand All @@ -54,6 +55,7 @@ export class CallResponser {
scope = this.factory.app;
method = this.factory.app[segments[1]];
mainHook = this.lifecycle.onAppMethodCall;
systemcall = true;
}
// Module level call (System only, not exposed)
if (segments.length > 2) {
Expand Down Expand Up @@ -90,7 +92,10 @@ export class CallResponser {
return;
}
// Verify the call request matches the ACL Rules
if (GroupMatch.verify(method.name, operation, config)) {
if (
(await GroupMatch.verify(method.name, operation, config)) ||
systemcall
) {
// Execute main hook, might be app/system or module level.
const result = await mainHook(
(name: string) => this.factory.scopes[segments[1]].get(name),
Expand Down
4 changes: 2 additions & 2 deletions src/core/call.streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class CallStreamer {
* @description This method will register an incoming call in order
* to send back an answer.
*/
register(operation: IAppOperation, handler) {
async register(operation: IAppOperation, handler) {
// Get segments from rpc endpoint
let scope,
method: Function | null = null,
Expand Down Expand Up @@ -73,7 +73,7 @@ export class CallStreamer {
}

// Verify the call request matches the ACL Rules
if (GroupMatch.verify(method.name, operation, config)) {
if (await GroupMatch.verify(method.name, operation, config)) {
// Default handler
const def = data => data;
// Execute main hook, might be app/system or module level.
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class OnixJS {
* @description Current Onix Version.
*/
get version(): string {
return '1.0.0-alpha.22';
return '1.0.0-alpha.22.1';
}
/**
* @property router
Expand Down
1 change: 1 addition & 0 deletions test/onixjs.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ test('Onix app greeter', async t => {
// a bidimentional array of booleans representing every app
// Answering between each others.
const results: boolean[][] = await onix.greet();
console.log(results);
// Both apps should communicate each other and say they are alive (true x 2)
t.deepEqual(results.reduce((a, b) => a.concat(b)), [true, true]);
});
Expand Down
98 changes: 93 additions & 5 deletions test/onixjs.core.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,45 @@ test('Core: CallResponser invalid call.', async t => {
'OnixJS Error: RPC Call is invalid "something.really.weird.to.call.which.is.invalid"',
);
});
// Test CallResponser not authorized call
test('Core: CallResponser not authorized call.', async t => {
@Component({})
class MyComponent {
@RPC()
testRPC() {
return 'ALO WORLD';
}
@Stream()
testSTREAM() {}
}
@Module({
models: [],
renderers: [],
services: [],
components: [MyComponent],
})
class MyModule {}
class MyApp extends Application {}
const factory: AppFactory = new AppFactory(MyApp);
factory.config = {network: {disabled: true}, modules: [MyModule]};
factory.notifier = new AppNotifier();
await factory.setup();
const responser: CallResponser = new CallResponser(factory);
const result = await responser.process({
uuid: Utils.uuid(),
type: OperationType.ONIX_REMOTE_CALL_PROCEDURE,
message: {
rpc: 'MyApp.MyModule.MyComponent.testRPC',
request: <IRequest>{},
},
});
t.is(result.code, 401);
});
// Test CallResponser valid call
test('Core: CallResponser valid call.', async t => {
@Component({})
@Component({
acl: [AllowEveryone],
})
class MyComponent {
@RPC()
testRPC() {
Expand Down Expand Up @@ -250,6 +286,7 @@ test('Core: CallResponser invalid call.', async t => {
// Test CallResponser Hooks
test('Core: CallResponser Hooks.', async t => {
@Component({
acl: [AllowEveryone],
lifecycle: async function(app, metadata, method) {
const methodResult = await method();
return methodResult;
Expand Down Expand Up @@ -290,9 +327,10 @@ test('Core: CallResponser Hooks.', async t => {
t.is(result.text, 'Hello Responser');
});

// Test CallResponser Hooks
test('Core: CallResponser Hooks.', async t => {
// Test CallStreamer Valid
test('Core: CallStreamer Valid.', async t => {
@Component({
acl: [AllowEveryone],
lifecycle: async function(app, metadata, method) {
const methodResult = await method();
return methodResult;
Expand All @@ -319,7 +357,7 @@ test('Core: CallResponser Hooks.', async t => {
factory.notifier = new AppNotifier();
await factory.setup();
const streamer: CallStreamer = new CallStreamer(factory);
streamer.register(
await streamer.register(
{
uuid: Utils.uuid(),
type: OperationType.ONIX_REMOTE_CALL_PROCEDURE,
Expand All @@ -339,6 +377,56 @@ test('Core: CallResponser Hooks.', async t => {
);
});

// Test CallStreamer Not Authorized
test('Core: CallStreamer Not Authorized.', async t => {
@Component({
lifecycle: async function(app, metadata, method) {
const methodResult = await method();
return methodResult;
},
})
class MyComponent {
@Stream()
test(stream) {
return stream({
text: 'Hello Streamer',
});
}
}
@Module({
models: [],
renderers: [],
services: [],
components: [MyComponent],
})
class MyModule {}
class MyApp extends Application {}
const factory: AppFactory = new AppFactory(MyApp);
factory.config = {network: {disabled: true}, modules: [MyModule]};
factory.notifier = new AppNotifier();
await factory.setup();
const streamer: CallStreamer = new CallStreamer(factory);
await streamer.register(
{
uuid: Utils.uuid(),
type: OperationType.ONIX_REMOTE_CALL_PROCEDURE,
message: {
rpc: 'MyApp.MyModule.MyComponent.test',
request: <IRequest>{
metadata: {stream: true},
payload: {},
},
},
},
result => {
console.log('SOME GOD DAMN RESULT: ', result);
if (result) {
t.is(result.code, 401);
}
},
);
});

// Test CallStreamer invalid call
test('Core: CallStreamer invalid call.', async t => {
class MyComponent {}
Expand All @@ -355,7 +443,7 @@ test('Core: CallStreamer invalid call.', async t => {
factory.notifier = new AppNotifier();
await factory.setup();
const streamer: CallStreamer = new CallStreamer(factory);
streamer.register(
await streamer.register(
{
uuid: Utils.uuid(),
type: OperationType.ONIX_REMOTE_CALL_PROCEDURE,
Expand Down

0 comments on commit 52ce098

Please sign in to comment.