Skip to content
This repository was archived by the owner on Jul 9, 2019. It is now read-only.

Commit d270cb8

Browse files
committed
More fixed tests
1 parent a33452f commit d270cb8

25 files changed

+103
-219
lines changed

__tests__/config.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as all from "../app/config";
2+
import "jest";
23

34
describe("foo", () => {
45
it("bars", () => {
5-
expect(true).toBe((true);
6+
expect(true).toBe((true));
67
});
7-
});
8+
});

__tests__/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import * as all from "../app/index";
22

33
describe("foo", () => {
44
it("bars", () => {
5-
expect(true).toBe((true);
5+
expect(true).toBe((true));
66
});
7-
});
7+
});

__tests__/logger.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import * as all from "../app/logger";
22

33
describe("foo", () => {
44
it("bars", () => {
5-
expect(true).toBe((true);
5+
expect(true).toBe((true));
66
});
7-
});
7+
});

__tests__/on_ready.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import * as all from "../app/on_ready";
22

33
describe("foo", () => {
44
it("bars", () => {
5-
expect(true).toBe((true);
5+
expect(true).toBe((true));
66
});
7-
});
7+
});

__tests__/security/authenticate.ts

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var auth = require("../../app/security/authenticate");
2-
var axios = require("axios");
1+
import "jest";
2+
import { authenticate as auth } from "../../app/security/authenticate";
3+
import axios from "axios";
34
var EMAIL = "[email protected]";
45
var PASSWORD = "password123";
56
var validJWT;
@@ -22,9 +23,10 @@ describe("authentication", function() {
2223
console.log("STAND UP AN API SERVER LOCALLY!");
2324
})
2425
})
25-
it("logs in and attaches JSON web token to user", function(done) {
26+
27+
xit("logs in and attaches JSON web token to user", function(done) {
2628
var finished = false;
27-
var client = {};
29+
var client: any = {};
2830
var callback = function(_, isAuthorized) {
2931
expect(isAuthorized).toBeTruthy();
3032
expect(client.permissions).toBeDefined();
@@ -36,26 +38,13 @@ describe("authentication", function() {
3638

3739
it("logs in with a JWT as a password", function(done) {
3840
var finished = false;
39-
var client = {};
41+
var client: any = {};
4042
var callback = function(_, isAuthorized) {
4143
expect(isAuthorized).toBeTruthy();
4244
expect(client.permissions).toBeDefined();
4345
expect(client.permissions.sub).toBe('[email protected]');
4446
done();
4547
};
4648
auth(client, EMAIL, validJWT, callback);
47-
})
48-
49-
it("logs in with a username/password", function(done) {
50-
var finished = false;
51-
var client = {};
52-
var callback = function(_, isAuthorized) {
53-
expect(isAuthorized).toBeTruthy();
54-
expect(client.permissions).toBeDefined();
55-
expect(client.permissions.sub).toBe('[email protected]');
56-
done();
57-
};
58-
auth(client, EMAIL, PASSWORD, callback);
5949
});
60-
6150
});

__tests__/security/authorize_publish.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import * as all from "../../app/security/authorize_publish";
22

33
describe("foo", () => {
44
it("bars", () => {
5-
expect(true).toBe((true);
5+
expect(true).toBe((true));
66
});
7-
});
7+
});

__tests__/security/authorize_subscribe.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import * as all from "../../app/security/authorize_subscribe";
22

33
describe("foo", () => {
44
it("bars", () => {
5-
expect(true).toBe((true);
5+
expect(true).toBe((true));
66
});
77
});

__tests__/security/can_use_topic.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import * as all from "../../app/security/can_use_topic";
22

33
describe("foo", () => {
44
it("bars", () => {
5-
expect(true).toBe((true);
5+
expect(true).toBe((true));
66
});
77
});

__tests__/security/fetch_token.ts

-35
This file was deleted.

__tests__/security/maybe_enable_ssl.ts

-7
This file was deleted.

__tests__/security/verify_token.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
var auth = require("../../app/security/authenticate");
2-
var verify = require('../../app/security/verify_token');
3-
var fetchRealJWT = require("../support/fetch_real_jwt");
1+
import { authenticate as auth } from "../../app/security/authenticate";
2+
import { verifyToken as verify } from '../../app/security/verify_token';
3+
import { fetchRealJWT } from "../../support/fetch_real_token";
4+
45
var EMAIL = "[email protected]";
56
var PASSWORD = "password123";
67
var validJWT;
78
var invalidJWT = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbkBhZG1pbi5jb20iLCJpYXQiOjE0NTg4MTk1MzYsImp0aSI6ImE0MWQxMGU1LTk3NDUtNDEzOS1hYmJlLWQ5MjgzY2M2MGRjMiIsImlzcyI6ImZhcm1ib3Qtd2ViLWFwcCIsImV4cCI6MTQ1OTE2NTEzNiwiYWxnIjoiUlMyNTYifQ.reuRxMr_WMgu9prisSjGBuIuKRQw9Tmc5U_kWJyzFm0';
89

910
describe("token verification", function () {
10-
beforeAll(function(done) {
11+
beforeAll(function (done) {
1112
fetchRealJWT()
12-
.then(function(token) {
13+
.then(function (token) {
1314
validJWT = token;
1415
done();
1516
})
1617
})
17-
it("knows when you're lying", function () {
18+
it("knows when you're lying", function (done) {
1819
function assertions(error) {
1920
expect(error.message).toBeDefined();
2021
done();

app/config.ts

+32-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
import { log } from "./logger";
2+
import { ServerOpts } from "mosca";
23

3-
// TODO: Clean up ENV management into something less crappy.
4-
// or use a 3rd party node ENV manager module.
5-
export let DEFAULT_URL = "http://localhost:3000";
6-
export let webAppUrl;
7-
export let httpPort = 3002;
8-
export let mqttPort = 1883;
4+
export let webAppUrl = process.env.WEB_API_URL || "http://localhost:3000";
5+
log(`Using ${webAppUrl} as API URL`);
96

10-
if (process.env.WEB_API_URL) {
11-
webAppUrl = process.env.WEB_API_URL;
12-
} else {
13-
webAppUrl = DEFAULT_URL;
14-
log("You did not set WEB_API_URL. Defaulting to " + DEFAULT_URL);
15-
}
7+
export function generateConfig(sslDomain = "") {
8+
const SSL_DIR = `/etc/letsencrypt/live/${sslDomain}/`;
9+
10+
let config: ServerOpts = {
11+
allowNonSecure: true,
12+
port: 1883,
13+
http: { // for teh websockets
14+
port: 3002,
15+
bundle: true,
16+
static: "./public"
17+
},
18+
https: {
19+
port: 443
20+
},
21+
secure: {
22+
port: 8883,
23+
keyPath: SSL_DIR + "privkey.pem",
24+
certPath: SSL_DIR + "cert.pem"
25+
}
26+
};
1627

28+
// Remove SSL features if SSL_DOMAIN
29+
// was not set.
30+
if (!sslDomain) {
31+
delete config.https;
32+
delete config.secure;
33+
}
34+
35+
return config;
36+
}

app/index.ts

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
1-
import { Server, ServerOpts } from "mosca";
2-
import * as conf from "./config";
1+
import { Server } from "mosca";
2+
import { generateConfig } from "./config";
33
import { onReady } from "./on_ready";
4-
import { maybeEnableSSL } from "./security/maybe_enable_ssl";
5-
import { authorizePublish } from "./security/authorize_publish";
6-
import { authorizeSubscribe } from "./security/authorize_subscribe";
74

8-
let input: ServerOpts = {
9-
allowNonSecure: true,
10-
port: conf.mqttPort,
11-
http: { // for teh websockets
12-
port: conf.httpPort,
13-
bundle: true,
14-
static: "./public"
15-
}
16-
};
17-
18-
maybeEnableSSL(input);
19-
let server = new Server(input);
5+
let server = new Server(generateConfig(process.env.SSL_DOMAIN));
206

217
server.on("ready", onReady(server));
22-
server.on("ready", function () {
23-
console.dir(input);
24-
});
25-
server.authorizePublish = authorizePublish;
26-
server.authorizeSubscribe = authorizeSubscribe;

app/logger.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export function log(args: any) {
22
if (!process.env.DISABLE_LOGS) {
33
console.log.apply(this, arguments);
44
}
5-
}
5+
}

app/on_ready.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import { authenticate } from "./security/authenticate";
22
import { authorizePublish } from "./security/authorize_publish";
33
import { authorizeSubscribe } from "./security/authorize_subscribe";
44
import { log } from "./logger";
5-
import { startLogging } from "./start_logging";
65

76
export let onReady = (server) => () => {
87
log("Server online");
9-
startLogging(server);
10-
startSecurity(server);
11-
};
12-
13-
function startSecurity(server) {
8+
server.on("clientConnected", () => log("clientConnected"));
9+
server.on("clientDisconnecting", () => log("clientDisconnecting"));
10+
server.on("clientDisconnected", () => log("clientDisconnected"));
11+
server.on("published", () => log("published"));
12+
server.on("subscribed", () => log("subscribed"));
13+
server.on("unsubscribed", () => log("unsubscribed"));
14+
server.on("error", () => log("error"));
1415
server.authenticate = authenticate;
1516
server.authorizePublish = authorizePublish;
1617
server.authorizeSubscribe = authorizeSubscribe;
17-
}
18+
};

app/security/authenticate.ts

+4-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
11
// test@test.com password123
2-
import { fetchToken } from "./fetch_token";
32
import { verifyToken } from "./verify_token";
43
import { log } from "../logger";
5-
import Axios, { Promise as AxiosPromise } from "axios";
6-
7-
type tokenHandler = (u: string, pass: string) => AxiosPromise<any>;
8-
function determineAuthStrategy(username: string, password: string): tokenHandler {
9-
// Really long password? Probably a JWT.
10-
if (password.length > 100) {
11-
return verifyToken;
12-
} else {
13-
return fetchToken;
14-
}
15-
};
164

175
export function authenticate(client, username: string, password: string, callback) {
18-
password = (password || "").toString();
19-
username = username || "";
20-
let auth = determineAuthStrategy(username, password);
216
if (client && client.connection && client.connection.stream) {
227
log(client.connection.stream.remoteAddress);
238
}
24-
log("AUTH START")
25-
auth(password, username)
26-
.then(function (permissions) {
9+
log("AUTH START");
10+
verifyToken(password.toString())
11+
.then(function(permissions) {
2712
log("AUTH OK " + username);
2813
client.permissions = permissions;
2914
callback(null, true);
30-
}, function (error) {
15+
}, function(error) {
3116
log("AUTH FAIL " + username);
3217
log(error.message);
3318
log(error);

app/security/authorize_publish.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { canUseTopic } from "./can_use_topic";
22

33
export function authorizePublish(client, topic, payload, callback) {
44
callback(null, canUseTopic(client, topic));
5-
}
5+
}

app/security/can_use_topic.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { log } from "../logger";
44
// following pattern bot/XYZ/#
55

66
export function canUseTopic(client, topic) {
7-
let hasBot = topic && client && client.permissions && client.permissions.bot;
7+
let hasBot = topic &&
8+
client &&
9+
client.permissions &&
10+
client.permissions.bot;
811
if (!hasBot) {
912
log("Tried to access topic " + (topic || "???") + " but no bot/topic provided.");
1013
return false;

app/security/fetch_token.ts

-19
This file was deleted.

0 commit comments

Comments
 (0)