Skip to content

Commit

Permalink
Release 1.0.0-alpha.28 🔎
Browse files Browse the repository at this point in the history
- Increased test coverage
  • Loading branch information
Jonathan committed May 7, 2018
1 parent debcfef commit 8de08a5
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 45 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.27",
"version": "1.0.0-alpha.28",
"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
62 changes: 22 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class OnixJS {
* @description Current Onix Version.
*/
get version(): string {
return '1.0.0-alpha.27';
return '1.0.0-alpha.28';
}
/**
* @property router
Expand Down Expand Up @@ -78,10 +78,6 @@ export class OnixJS {
process.on('exit', async () => {
// Signal child processes to stop their internal processes
await this.stop();
// Kill'em all right now.
Object.keys(this._apps).forEach(reference =>
this._apps[reference].process.kill('SIGHUP'),
);
});
// Log Onix Version
console.info('Loading Onix Server Version: ', this.version);
Expand Down Expand Up @@ -347,7 +343,13 @@ export class OnixJS {
).then(
(res: OperationType.APP_STOP_RESPONSE[]) =>
new Promise<OperationType.APP_STOP_RESPONSE[]>((resolve, reject) => {
this.server.close(() => resolve(res));
this.server.close(() => {
// Kill'em all right now.
Object.keys(this._apps).forEach(reference =>
this._apps[reference].process.kill('SIGHUP'),
);
resolve(res);
});
}),
);
}
Expand Down Expand Up @@ -410,44 +412,24 @@ export class OnixJS {
// Promisify exists and readfile
const AsyncExists = promisify(fs.exists);
const AsyncReadFile = promisify(fs.readFile);
try {
// Verify the pathname exists
const exist: boolean = await AsyncExists(pathname);
// If not, return 404 code
if (!exist) {
// if the file is not found, return 404
res.statusCode = 404;
return res.end(
JSON.stringify({
code: res.statusCode,
message: `Activation file not found.`,
}),
);
}
try {
// read file from file system
const data = await AsyncReadFile(pathname);
// Set response headers
res.setHeader('Content-type', 'text/plain');
res.end(data.toString());
} catch (e) {
res.setHeader('Content-type', 'application/json');
res.end(
JSON.stringify({
error: 404,
message: 'Oops!!! something went wrong',
}),
);
}
} catch (e) {
res.setHeader('Content-type', 'application/json');
res.end(
// Verify the pathname exists
const exist: boolean = await AsyncExists(pathname);
// If not, return 404 code
if (!exist) {
// if the file is not found, return 404
res.statusCode = 404;
return res.end(
JSON.stringify({
error: 404,
message: 'Oops!!! something went wrong',
code: res.statusCode,
message: `Activation file not found.`,
}),
);
}
// read file from file system
const data = await AsyncReadFile(pathname);
// Set response headers
res.setHeader('Content-type', 'text/plain');
res.end(data.toString());
},
);
}
Expand Down
184 changes: 180 additions & 4 deletions test/onixjs.core.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ import {
Constructor,
Router,
IComponentConfig,
IACLRule,
AccessType,
GroupList,
ModelProvider,
OnixMethod,
} from '../src';
import {Groups} from '../src/core/acl.groups';
import {LifeCycle} from '../src/core/lifecycle';
import {Injector} from '../src/core/injector';
import {HostBoot} from '../src/core/host.boot';
Expand All @@ -43,6 +49,7 @@ import {Mongoose, Schema} from 'mongoose';
import {GroupMatch} from '../src/core/acl.group.match';
import {AllowEveryone} from '../src/core/acl.everyone';
import {WSAdapter} from '../src/adapters/ws.adapter';
import {OnixMessage} from '@onixjs/sdk';
const cwd = path.join(process.cwd(), 'dist', 'test');
// Test AppFactory

Expand Down Expand Up @@ -292,7 +299,11 @@ test('Core: CallResponser invalid call.', async t => {
test('Core: CallResponser Hooks.', async t => {
@Component({
acl: [AllowEveryone],
lifecycle: async function(app, metadata, method) {
lifecycle: async function(
models: ModelProvider,
message: OnixMessage,
method: OnixMethod,
) {
const methodResult = await method();
return methodResult;
},
Expand Down Expand Up @@ -336,8 +347,16 @@ test('Core: CallResponser Hooks.', async t => {
test('Core: CallStreamer Valid.', async t => {
@Component({
acl: [AllowEveryone],
lifecycle: async function(app, metadata, method) {
const methodResult = await method();
lifecycle: async function(
models: ModelProvider,
message: OnixMessage,
method: OnixMethod,
) {
const noRealModel = models('NotReal');
let methodResult;
if (!noRealModel) {
methodResult = await method();
}
return methodResult;
},
})
Expand All @@ -349,6 +368,64 @@ test('Core: CallStreamer Valid.', async t => {
});
}
}
@Module({
models: [],
renderers: [],
services: [],
components: [MyComponent],
lifecycle: async function(
models: ModelProvider,
message: OnixMessage,
method: OnixMethod,
) {
const noRealModel = models('NotReal');
let methodResult;
if (!noRealModel) {
methodResult = await method();
}
return methodResult;
},
})
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 => {
if (result) {
t.is(result.text, 'Hello Streamer');
}
},
);
});

// Test CallStreamer Valid No Hooks
test('Core: CallStreamer Valid No Hooks.', async t => {
@Component({
acl: [AllowEveryone],
})
class MyComponent {
@Stream()
test(stream) {
return stream({
text: 'Hello Streamer',
});
}
}
@Module({
models: [],
renderers: [],
Expand Down Expand Up @@ -385,7 +462,11 @@ test('Core: CallStreamer Valid.', async t => {
// Test CallStreamer Not Authorized
test('Core: CallStreamer Not Authorized.', async t => {
@Component({
lifecycle: async function(app, metadata, method) {
lifecycle: async function(
models: ModelProvider,
message: OnixMessage,
method: OnixMethod,
) {
const methodResult = await method();
return methodResult;
},
Expand All @@ -398,6 +479,69 @@ test('Core: CallStreamer Not Authorized.', async t => {
});
}
}
@Module({
models: [],
renderers: [],
services: [],
components: [MyComponent],
lifecycle: async function(
models: ModelProvider,
message: OnixMessage,
method: OnixMethod,
) {
const methodResult = await method();
return methodResult;
},
})
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 => {
if (result) {
t.is(result.code, 401);
}
},
);
});

// Test CallStreamer Not Method Authorized
test('Core: CallStreamer Not Method Authorized.', async t => {
@Component({
acl: [
class AllowSome implements IACLRule {
// Define methods to be granted
methods: string[] = ['nomethod'];
// Define Access Grant
access: AccessType = AccessType.ALLOW;
// Define groups with the defined grant
groups: GroupList = [Groups.Everyone];
},
],
})
class MyComponent {
@Stream()
test(stream) {
return stream({
text: 'Hello Streamer',
});
}
}
@Module({
models: [],
renderers: [],
Expand Down Expand Up @@ -583,6 +727,38 @@ test('Core: host boot ssl activation file.', async t => {
t.is(result, 'activation-hello-world');
await instance.host.stop();
});
// Test host boot ssl activation file missing directory
test('Core: host boot ssl activation file missing directory.', async t => {
const instance: HostBoot = new HostBoot(
{
apps: ['TodoApp@todo.app:8079'],
},
{
cwd,
port: 5001,
adapters: {websocket: WSAdapter},
network: {
ssl: {
activation: {
endpoint: '/.well-known/activation.txt',
path: '../../test/activation.notexistent.txt',
},
},
},
},
);
await instance.run();
// Create HTTP Client
const client: NodeJS.HTTP = new NodeJS.HTTP();
// Call the decorated JSON
const result: any = <any>await client.get(
'http://127.0.0.1:5001/.well-known/activation.txt',
);
// Test Service
t.is(result.code, 404);
t.is(result.message, 'Activation file not found.');
await instance.host.stop();
});
// Test host boot throws
test('Core: host boot throws.', async t => {
const error = await t.throws(
Expand Down

0 comments on commit 8de08a5

Please sign in to comment.