From f6aa4dd015e8bd07c795f6c1e70adf99a6ffec94 Mon Sep 17 00:00:00 2001 From: Miguel Casqueira Date: Sun, 10 May 2020 21:03:16 -0400 Subject: [PATCH] Fixed stubs for test suite Closes: #1280 Change-type: patch Signed-off-by: Miguel Casqueira --- test/00-init.ts | 41 +++++++++++++++++++++-------------------- test/18-startup.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/test/00-init.ts b/test/00-init.ts index 9afc9482..0bccb9f8 100644 --- a/test/00-init.ts +++ b/test/00-init.ts @@ -6,30 +6,31 @@ process.env.DATABASE_PATH_2 = './test/data/database2.sqlite'; process.env.DATABASE_PATH_3 = './test/data/database3.sqlite'; process.env.LED_FILE = './test/data/led_file'; +import * as dbus from 'dbus'; +import { DBusError, DBusInterface } from 'dbus'; import { stub } from 'sinon'; -import * as dbus from 'dbus'; - -// Stub the dbus objects to dynamically generate the methods -// on request stub(dbus, 'getBus').returns({ getInterface: ( - _obj: unknown, - cb: (err: Error | undefined, iface: dbus.DBusInterface) => void, + _serviceName: string, + _objectPath: string, + _interfaceName: string, + interfaceCb: (err: null | DBusError, iface: DBusInterface) => void, ) => { - return cb( - undefined, - new Proxy( - {}, - { - get(_target, name) { - console.log(`Dbus method ${String(name)} requested`); - return () => { - /* noop */ - }; - }, - }, - ) as any, - ); + interfaceCb(null, { + Get: ( + _unitName: string, + _property: string, + getCb: (err: null | Error, value: unknown) => void, + ) => { + getCb(null, 'this is the value'); + }, + GetUnit: ( + _unitName: string, + getUnitCb: (err: null | Error, unitPath: string) => void, + ) => { + getUnitCb(null, 'this is the unit path'); + }, + } as any); }, } as any); diff --git a/test/18-startup.ts b/test/18-startup.ts index 7cec2ab1..1e623750 100644 --- a/test/18-startup.ts +++ b/test/18-startup.ts @@ -1,10 +1,52 @@ +import { SinonStub, stub } from 'sinon'; + +import APIBinder from '../src/api-binder'; +import { ApplicationManager } from '../src/application-manager'; +import DeviceState from '../src/device-state'; +import * as constants from '../src/lib/constants'; +import { DockerUtils as Docker } from '../src/lib/docker-utils'; import { Supervisor } from '../src/supervisor'; import { expect } from './lib/chai-config'; describe('Startup', () => { - it('should startup correctly', function() { + let initClientStub: SinonStub; + let startStub: SinonStub; + let vpnStatusPathStub: SinonStub; + let appManagerStub: SinonStub; + let deviceStateStub: SinonStub; + let dockerStub: SinonStub; + + before(() => { + initClientStub = stub(APIBinder.prototype as any, 'initClient').returns( + Promise.resolve(), + ); + startStub = stub(APIBinder.prototype as any, 'start').returns( + Promise.resolve(), + ); + appManagerStub = stub(ApplicationManager.prototype, 'init').returns( + Promise.resolve(), + ); + vpnStatusPathStub = stub(constants, 'vpnStatusPath').returns(''); + deviceStateStub = stub(DeviceState.prototype as any, 'applyTarget').returns( + Promise.resolve(), + ); + dockerStub = stub(Docker.prototype, 'listContainers').returns( + Promise.resolve([]), + ); + }); + + after(() => { + initClientStub.restore(); + startStub.restore(); + appManagerStub.restore(); + vpnStatusPathStub.restore(); + deviceStateStub.restore(); + dockerStub.restore(); + }); + + it('should startup correctly', async () => { const supervisor = new Supervisor(); - expect(supervisor.init()).to.not.throw; + expect(await supervisor.init()).to.not.throw; // Cast as any to access private properties const anySupervisor = supervisor as any; expect(anySupervisor.db).to.not.be.null;