Skip to content

Commit

Permalink
Group tests to separate unit e2e tests. Add e2e test against my own b…
Browse files Browse the repository at this point in the history
…shc.
  • Loading branch information
holomekc committed Dec 4, 2023
1 parent 6718ec8 commit f307857
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 11 deletions.
20 changes: 20 additions & 0 deletions .run/Tests E2E Template.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Tests E2E Template" type="mocha-javascript-test-runner">
<node-interpreter>project</node-interpreter>
<node-options />
<mocha-package>$PROJECT_DIR$/node_modules/mocha</mocha-package>
<working-directory>$PROJECT_DIR$</working-directory>
<pass-parent-env>true</pass-parent-env>
<envs>
<env name="BSHC_CERT" value="" />
<env name="BSHC_HOST" value="" />
<env name="BSHC_PRIV" value="" />
</envs>
<ui>bdd</ui>
<extra-mocha-options>--grep e2e --exit</extra-mocha-options>
<test-kind>DIRECTORY</test-kind>
<test-directory>$PROJECT_DIR$/test</test-directory>
<recursive>true</recursive>
<method v="2" />
</configuration>
</component>
2 changes: 1 addition & 1 deletion .run/Tests.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<working-directory>$PROJECT_DIR$</working-directory>
<pass-parent-env>true</pass-parent-env>
<ui>bdd</ui>
<extra-mocha-options />
<extra-mocha-options>--grep should --exit</extra-mocha-options>
<test-kind>DIRECTORY</test-kind>
<test-directory>$PROJECT_DIR$/test</test-directory>
<recursive>true</recursive>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint --ext .ts src",
"lint:fix": "eslint --ext .ts src --fix",
"prettier": "prettier --write .",
"test": "cross-env TS_NODE_PROJECT='./tsconfig.spec.json' nyc mocha --exit"
"test": "cross-env TS_NODE_PROJECT='./tsconfig.spec.json' nyc mocha --grep should --exit"
},
"keywords": [
"Bosch Smart Home",
Expand Down
41 changes: 41 additions & 0 deletions test/bosch-smart-home-bridge.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { BoschSmartHomeBridge, BoschSmartHomeBridgeBuilder, BshbUtils } from "../src";
import { expect } from "chai";
import { DefaultTestLogger } from "./bshc-mock";

const host: string = process.env.BSHC_HOST!;
const identifier: string = process.env.BSHC_IDENTIFIER!;
const password: string = process.env.BSHC_PWD!;
const clientCert: string = "-----BEGIN CERTIFICATE-----\n" + process.env.BSHC_CERT! + "\n-----END CERTIFICATE-----";
const clientPrivateKey: string =
"-----BEGIN RSA PRIVATE KEY-----\n" + process.env.BSHC_PRIV! + "\n-----END RSA PRIVATE KEY-----";

describe("BshbUtils", () => {
let bshb: BoschSmartHomeBridge;
before(() => {
const certResult = BshbUtils.generateClientCertificate();
bshb = BoschSmartHomeBridgeBuilder.builder()
.withHost(host)
.withClientCert(clientCert)
.withClientPrivateKey(clientPrivateKey)
.withIgnoreCertificateCheck(true)
.withLogger(new DefaultTestLogger())
.build();
});

it("e2e get rooms", (done) => {
let response: any;
bshb
.getBshcClient()
.getRooms()
.subscribe({
next: (value) => (response = value),
error: (error) => {
expect.fail(error, "Expected that rooms returns a result");
},
complete: () => {
expect(response).to.be.not.null;
done();
},
});
});
});
40 changes: 34 additions & 6 deletions test/bosch-smart-home-bridge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("BoschSmartHomeBridge", () => {
bshcAdmin = resetBshcAdminRouter();
});

it("test not paired", (done) => {
it("should not be paired yet, and button is not pressed", (done) => {
bshc.get("/smarthome/rooms", (req, res) => {
res.statusCode = 401;
res.json({});
Expand All @@ -35,8 +35,7 @@ describe("BoschSmartHomeBridge", () => {

const identifier = BshbUtils.generateIdentifier();

let response: any;
bshb.pairIfNeeded("test", identifier, "test", 1000, 1).subscribe({
bshb.pairIfNeeded("test", identifier, "test", 1000, 0).subscribe({
next: (value) => expect.fail("Expected not connected"),
error: (error) => {
expect(error).not.to.be.null;
Expand All @@ -48,7 +47,36 @@ describe("BoschSmartHomeBridge", () => {
});
});

it("test already pairing", (done) => {
it("should not be paired yet, and button is pressed", (done) => {
bshc.get("/smarthome/rooms", (req, res) => {
res.statusCode = 401;
res.json({});
});
bshcAdmin.post("/smarthome/clients", (req, res) => {
res.json({
url: "http://localhost:8884",
token: "someToken",
});
});

const identifier = BshbUtils.generateIdentifier();

let response: any;
bshb.pairIfNeeded("test", identifier, "test", 1000, 0).subscribe({
next: (value) => (response = value),
error: (error) => {
expect.fail(error, "Expected that pairing is successful");
},
complete: () => {
expect(response).to.be.not.null;
expect(response.url).to.be.not.null;
expect(response.token).to.be.not.null;
done();
},
});
});

it("should already be paired", (done) => {
bshc.get("/smarthome/rooms", (req, res) => {
res.json([
{
Expand All @@ -63,10 +91,10 @@ describe("BoschSmartHomeBridge", () => {
const identifier = BshbUtils.generateIdentifier();

let response: any;
bshb.pairIfNeeded("test", identifier, "test", 1000, 1).subscribe({
bshb.pairIfNeeded("test", identifier, "test", 1000, 0).subscribe({
next: (value) => (response = value),
error: (error) => {
expect.fail("Expected that rooms returns a result");
expect.fail(error, "Expected that rooms returns a result");
},
complete: () => {
expect(response).to.be.not.null;
Expand Down
6 changes: 3 additions & 3 deletions test/bshb-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { BshbUtils } from "../src/bshb-utils";
import { BshbUtils } from "../src";
import { expect } from "chai";

describe("BshbUtils", () => {
it("generate identifier", () => {
it("should generate identifier", () => {
const identifier = BshbUtils.generateIdentifier();

expect(identifier).to.not.be.null;
expect(identifier.length).to.be.greaterThan(0);
});

it("generate client certificate", () => {
it("should generate client certificate", () => {
const result = BshbUtils.generateClientCertificate();

expect(result).to.not.be.null;
Expand Down

0 comments on commit f307857

Please sign in to comment.