Skip to content

Commit

Permalink
plugins gateway and terminal gateway e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oznu committed Aug 31, 2020
1 parent 6ff7a14 commit fc0137c
Show file tree
Hide file tree
Showing 31 changed files with 506 additions and 32 deletions.
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"ignore": [
"src/**/*.spec.ts"
],
"exec": "UIX_INSECURE_MODE=1 UIX_SERVICE_MODE=1 ts-node -r tsconfig-paths/register src/main.ts"
"exec": "UIX_INSECURE_MODE=1 UIX_SERVICE_MODE=1 HOMEBRIDGE_CONFIG_UI_TERMINAL=1 ts-node -r tsconfig-paths/register src/main.ts"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@
"smart home",
"hb-service"
]
}
}
8 changes: 8 additions & 0 deletions src/core/node-pty/node-pty.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { NodePtyService } from './node-pty.service';

@Module({
providers: [NodePtyService],
exports: [NodePtyService],
})
export class NodePtyModule { }
7 changes: 7 additions & 0 deletions src/core/node-pty/node-pty.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Injectable } from '@nestjs/common';
import * as pty from 'node-pty-prebuilt-multiarch';

@Injectable()
export class NodePtyService {
public spawn = pty.spawn;
}
1 change: 1 addition & 0 deletions src/modules/log/log.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EventEmitter } from 'events';
import { UseGuards } from '@nestjs/common';
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';

import { WsGuard } from '../../core/auth/guards/ws.guard';
import { LogService, LogTermSize } from './log.service';

Expand Down
3 changes: 3 additions & 0 deletions src/modules/log/log.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Module } from '@nestjs/common';
import { LogService } from './log.service';
import { LogGateway } from './log.gateway';

import { ConfigModule } from '../../core/config/config.module';
import { LoggerModule } from '../../core/logger/logger.module';
import { NodePtyModule } from '../../core/node-pty/node-pty.module';

@Module({
imports: [
ConfigModule,
LoggerModule,
NodePtyModule,
],
providers: [
LogService,
Expand Down
6 changes: 4 additions & 2 deletions src/modules/log/log.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as os from 'os';
import * as color from 'bash-color';
import * as semver from 'semver';
import * as pty from 'node-pty-prebuilt-multiarch';
import * as child_process from 'child_process';
import * as fs from 'fs-extra';
import { EventEmitter } from 'events';
import { Injectable } from '@nestjs/common';
import { Tail } from 'tail';

import { ConfigService } from '../../core/config/config.service';
import { NodePtyService } from '../../core/node-pty/node-pty.service';

export type LogTermSize = { cols: number, rows: number };

Expand All @@ -20,6 +21,7 @@ export class LogService {

constructor(
private configService: ConfigService,
private nodePtyService: NodePtyService,
) {
this.setLogMethod();
}
Expand Down Expand Up @@ -79,7 +81,7 @@ export class LogService {
const command = [...this.command];

// spawn the process that will output the logs
const term = pty.spawn(command.shift(), command, {
const term = this.nodePtyService.spawn(command.shift(), command, {
name: 'xterm-color',
cols: size.cols,
rows: size.rows,
Expand Down
5 changes: 3 additions & 2 deletions src/modules/platform-tools/terminal/terminal.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';
import { UseGuards } from '@nestjs/common';

import { WsAdminGuard } from '../../../core/auth/guards/ws-admin-guard';
import { TerminalService } from './terminal.service';
import { TerminalService, WsEventEmitter, TermSize } from './terminal.service';

@UseGuards(WsAdminGuard)
@WebSocketGateway({ namespace: 'platform-tools/terminal' })
Expand All @@ -11,7 +12,7 @@ export class TerminalGateway {
) { }

@SubscribeMessage('start-session')
startTerminalSession(client: any, payload: any) {
startTerminalSession(client: WsEventEmitter, payload: TermSize) {
return this.terminalService.startSession(client, payload);
}
}
3 changes: 3 additions & 0 deletions src/modules/platform-tools/terminal/terminal.module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';

import { TerminalService } from './terminal.service';
import { TerminalGateway } from './terminal.gateway';
import { ConfigModule } from '../../../core/config/config.module';
import { LoggerModule } from '../../../core/logger/logger.module';
import { NodePtyModule } from '../../../core/node-pty/node-pty.module';

@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
ConfigModule,
LoggerModule,
NodePtyModule,
],
providers: [
TerminalService,
Expand Down
18 changes: 13 additions & 5 deletions src/modules/platform-tools/terminal/terminal.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { EventEmitter } from 'events';
import * as fs from 'fs-extra';
import * as color from 'bash-color';
import * as pty from 'node-pty-prebuilt-multiarch';
import { Injectable } from '@nestjs/common';

import { ConfigService } from '../../../core/config/config.service';
import { Logger } from '../../../core/logger/logger.service';
import { NodePtyService } from '../../../core/node-pty/node-pty.service';

export type TermSize = { cols: number, rows: number };

export interface WsEventEmitter extends EventEmitter {
disconnect: () => void;
}

@Injectable()
export class TerminalService {
Expand All @@ -12,13 +19,14 @@ export class TerminalService {
constructor(
private configService: ConfigService,
private logger: Logger,
private nodePtyService: NodePtyService,
) { }

/**
* Create a new terminal session
* @param client
*/
async startSession(client, size) {
async startSession(client: WsEventEmitter, size: TermSize) {
this.ending = false;

// if terminal is not enabled, disconnect the client
Expand All @@ -34,7 +42,7 @@ export class TerminalService {
const shell = await fs.pathExists('/bin/bash') ? '/bin/bash' : '/bin/sh';

// spawn a new shell
const term = pty.spawn(shell, [], {
const term = this.nodePtyService.spawn(shell, [], {
name: 'xterm-color',
cols: size.cols,
rows: size.rows,
Expand Down Expand Up @@ -64,7 +72,7 @@ export class TerminalService {
});

// capture resize events
client.on('resize', (resize) => {
client.on('resize', (resize: TermSize) => {
try {
term.resize(resize.cols, resize.rows);
} catch (e) { }
Expand Down
9 changes: 5 additions & 4 deletions src/modules/plugins/plugins.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventEmitter } from 'events';
import { UseGuards } from '@nestjs/common';
import { SubscribeMessage, WebSocketGateway, WsException } from '@nestjs/websockets';
import * as color from 'bash-color';
Expand All @@ -15,7 +16,7 @@ export class PluginsGateway {
) { }

@SubscribeMessage('install')
async installPlugin(client, payload) {
async installPlugin(client: EventEmitter, payload: string) {
try {
return await this.pluginsService.installPlugin(payload, client);
} catch (e) {
Expand All @@ -26,7 +27,7 @@ export class PluginsGateway {
}

@SubscribeMessage('uninstall')
async uninstallPlugin(client, payload) {
async uninstallPlugin(client: EventEmitter, payload: string) {
try {
return await this.pluginsService.uninstallPlugin(payload, client);
} catch (e) {
Expand All @@ -37,7 +38,7 @@ export class PluginsGateway {
}

@SubscribeMessage('update')
async updatePlugin(client, payload) {
async updatePlugin(client: EventEmitter, payload: string) {
try {
return await this.pluginsService.updatePlugin(payload, client);
} catch (e) {
Expand All @@ -48,7 +49,7 @@ export class PluginsGateway {
}

@SubscribeMessage('homebridge-update')
async homebridgeUpdate(client, payload) {
async homebridgeUpdate(client: EventEmitter) {
try {
return await this.pluginsService.updateHomebridgePackage(client);
} catch (e) {
Expand Down
3 changes: 3 additions & 0 deletions src/modules/plugins/plugins.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as https from 'https';
import { Module, HttpModule } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';

import { PluginsService } from './plugins.service';
import { LoggerModule } from '../../core/logger/logger.module';
import { PluginsController } from './plugins.controller';
import { PluginsGateway } from './plugins.gateway';
import { ConfigModule } from '../../core/config/config.module';
import { NodePtyModule } from '../../core/node-pty/node-pty.module';

@Module({
imports: [
Expand All @@ -17,6 +19,7 @@ import { ConfigModule } from '../../core/config/config.module';
timeout: 5000,
httpsAgent: new https.Agent({ keepAlive: true }),
}),
NodePtyModule,
ConfigModule,
LoggerModule,
],
Expand Down
18 changes: 10 additions & 8 deletions src/modules/plugins/plugins.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventEmitter } from 'events';
import { Injectable, NotFoundException, InternalServerErrorException, HttpService } from '@nestjs/common';
import { HomebridgePlugin, IPackageJson, INpmSearchResults, INpmRegistryModule } from './types';
import axios from 'axios';
Expand All @@ -8,11 +9,11 @@ import * as fs from 'fs-extra';
import * as child_process from 'child_process';
import * as semver from 'semver';
import * as color from 'bash-color';
import * as pty from 'node-pty-prebuilt-multiarch';
import * as NodeCache from 'node-cache';

import { Logger } from '../../core/logger/logger.service';
import { ConfigService } from '../../core/config/config.service';
import { NodePtyService } from '../../core/node-pty/node-pty.service';

@Injectable()
export class PluginsService {
Expand Down Expand Up @@ -40,6 +41,7 @@ export class PluginsService {
constructor(
private httpService: HttpService,
private configService: ConfigService,
private nodePtyService: NodePtyService,
private logger: Logger,
) {

Expand Down Expand Up @@ -222,7 +224,7 @@ export class PluginsService {
* @param pluginName
* @param client
*/
async installPlugin(pluginName: string, client) {
async installPlugin(pluginName: string, client: EventEmitter) {
await this.getInstalledPlugins();

// install new plugins in the same location as this plugin
Expand All @@ -249,7 +251,7 @@ export class PluginsService {
* @param pluginName
* @param client
*/
async uninstallPlugin(pluginName: string, client) {
async uninstallPlugin(pluginName: string, client: EventEmitter) {
await this.getInstalledPlugins();
// find the plugin
const plugin = this.installedPlugins.find(x => x.name === pluginName);
Expand Down Expand Up @@ -281,7 +283,7 @@ export class PluginsService {
* @param pluginName
* @param client
*/
async updatePlugin(pluginName: string, client) {
async updatePlugin(pluginName: string, client: EventEmitter) {
if (pluginName === this.configService.name && this.configService.dockerOfflineUpdate) {
await this.updateSelfOffline(client);
return true;
Expand Down Expand Up @@ -361,7 +363,7 @@ export class PluginsService {
/**
* Updates the Homebridge package
*/
public async updateHomebridgePackage(client) {
public async updateHomebridgePackage(client: EventEmitter) {
const homebridge = await this.getHomebridgePackage();

// get the currently installed
Expand Down Expand Up @@ -412,7 +414,7 @@ export class PluginsService {
* Sets a flag telling the system to update the package next time the UI is restarted
* Dependend on OS support - currently only supported by the oznu/homebridge docker image
*/
public async updateSelfOffline(client) {
public async updateSelfOffline(client: EventEmitter) {
client.emit('stdout', color.yellow(`${this.configService.name} has been scheduled to update on the next container restart.\n\r\n\r`));
await new Promise(resolve => setTimeout(resolve, 800));

Expand Down Expand Up @@ -790,7 +792,7 @@ export class PluginsService {
* @param cwd
* @param client
*/
private async runNpmCommand(command: Array<string>, cwd: string, client) {
private async runNpmCommand(command: Array<string>, cwd: string, client: EventEmitter) {
let timeoutTimer;
command = command.filter(x => x.length);

Expand Down Expand Up @@ -839,7 +841,7 @@ export class PluginsService {
}

await new Promise((resolve, reject) => {
const term = pty.spawn(command.shift(), command, {
const term = this.nodePtyService.spawn(command.shift(), command, {
name: 'xterm-color',
cols: 80,
rows: 30,
Expand Down
1 change: 1 addition & 0 deletions test/e2e/accessories.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs-extra';
import { ValidationPipe } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { FastifyAdapter, NestFastifyApplication, } from '@nestjs/platform-fastify';

import { AuthModule } from '../../src/core/auth/auth.module';
import { AccessoriesModule } from '../../src/modules/accessories/accessories.module';
import { ConfigService } from '../../src/core/config/config.service';
Expand Down
1 change: 1 addition & 0 deletions test/e2e/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs-extra';
import { Test, TestingModule } from '@nestjs/testing';
import { ValidationPipe } from '@nestjs/common';
import { FastifyAdapter, NestFastifyApplication, } from '@nestjs/platform-fastify';

import { AppModule } from '../../src/app.module';

describe('AppController (e2e)', () => {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/auth.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs-extra';
import { ValidationPipe } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { FastifyAdapter, NestFastifyApplication, } from '@nestjs/platform-fastify';

import { AuthModule } from '../../src/core/auth/auth.module';
import { ConfigService } from '../../src/core/config/config.service';

Expand Down
1 change: 1 addition & 0 deletions test/e2e/backup.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs-extra';
import { ValidationPipe } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { FastifyAdapter, NestFastifyApplication, } from '@nestjs/platform-fastify';

import { AuthModule } from '../../src/core/auth/auth.module';
import { BackupModule } from '../../src/modules/backup/backup.module';
import { BackupService } from '../../src/modules/backup/backup.service';
Expand Down
1 change: 1 addition & 0 deletions test/e2e/config-editor.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs-extra';
import { ValidationPipe } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { FastifyAdapter, NestFastifyApplication, } from '@nestjs/platform-fastify';

import { AuthModule } from '../../src/core/auth/auth.module';
import { ConfigEditorModule } from '../../src/modules/config-editor/config-editor.module';
import { HomebridgeConfig } from '../../src/core/config/config.service';
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/custom-plugins.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as path from 'path';
import * as fs from 'fs-extra';
import axios from 'axios';
import { of, throwError } from 'rxjs';
import axios, { AxiosResponse, AxiosError } from 'axios';
import { ValidationPipe, HttpService } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { FastifyAdapter, NestFastifyApplication, } from '@nestjs/platform-fastify';
import { of, throwError } from 'rxjs';
import { AxiosResponse, AxiosError } from 'axios';

import { AuthModule } from '../../src/core/auth/auth.module';
import { CustomPluginsModule } from '../../src/modules/custom-plugins/custom-plugins.module';

Expand Down
1 change: 1 addition & 0 deletions test/e2e/fastify.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FastifyAdapter, NestFastifyApplication, } from '@nestjs/platform-fastif
import * as fastify from 'fastify';
import * as fastifyMultipart from 'fastify-multipart';
import * as helmet from 'helmet';

import { AppModule } from '../../src/app.module';

describe('FastifyOptions (e2e)', () => {
Expand Down
Loading

0 comments on commit fc0137c

Please sign in to comment.