mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-21 17:33:29 +01:00
Tests for /register
This commit is contained in:
@@ -25,7 +25,7 @@ const WSDL_FILE = path.resolve(
|
|||||||
function server(
|
function server(
|
||||||
sonos: Sonos,
|
sonos: Sonos,
|
||||||
bonobService: Service,
|
bonobService: Service,
|
||||||
webAddress: string | "http://localhost:1234",
|
webAddress: string | "http://localhost:4534",
|
||||||
musicService: MusicService,
|
musicService: MusicService,
|
||||||
linkCodes: LinkCodes = new InMemoryLinkCodes()
|
linkCodes: LinkCodes = new InMemoryLinkCodes()
|
||||||
): Express {
|
): Express {
|
||||||
@@ -58,8 +58,15 @@ function server(
|
|||||||
|
|
||||||
app.post("/register", (_, res) => {
|
app.post("/register", (_, res) => {
|
||||||
sonos.register(bonobService).then((success) => {
|
sonos.register(bonobService).then((success) => {
|
||||||
if (success) res.send("Yay");
|
if (success) {
|
||||||
else res.send("boo hoo");
|
res.render("success", {
|
||||||
|
message: `Successfully registered`,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(500).render("failure", {
|
||||||
|
message: `Registration failed!`,
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -79,7 +86,9 @@ function server(
|
|||||||
if (isSuccess(authResult)) {
|
if (isSuccess(authResult)) {
|
||||||
if (linkCodes.has(linkCode)) {
|
if (linkCodes.has(linkCode)) {
|
||||||
linkCodes.associate(linkCode, authResult);
|
linkCodes.associate(linkCode, authResult);
|
||||||
res.render("loginOK");
|
res.render("success", {
|
||||||
|
message: `Login successful`,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
res.status(400).render("failure", {
|
res.status(400).render("failure", {
|
||||||
message: `Invalid linkCode!`,
|
message: `Invalid linkCode!`,
|
||||||
@@ -138,7 +147,7 @@ function server(
|
|||||||
.update(association.userId)
|
.update(association.userId)
|
||||||
.digest("hex"),
|
.digest("hex"),
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
throw {
|
throw {
|
||||||
@@ -147,10 +156,10 @@ function server(
|
|||||||
faultstring: "Link Code not found retry...",
|
faultstring: "Link Code not found retry...",
|
||||||
detail: {
|
detail: {
|
||||||
ExceptionInfo: "NOT_LINKED_RETRY",
|
ExceptionInfo: "NOT_LINKED_RETRY",
|
||||||
SonosError: "5"
|
SonosError: "5",
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getSessionId: ({
|
getSessionId: ({
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ const randomInt = (max: number) => Math.floor(Math.random() * max);
|
|||||||
const randomIpAddress = () => `127.0.${randomInt(255)}.${randomInt(255)}`;
|
const randomIpAddress = () => `127.0.${randomInt(255)}.${randomInt(255)}`;
|
||||||
|
|
||||||
export const aService = (fields: Partial<Service> = {}): Service => ({
|
export const aService = (fields: Partial<Service> = {}): Service => ({
|
||||||
name: `Test Music Service ${uuid()}`,
|
|
||||||
sid: randomInt(500),
|
sid: randomInt(500),
|
||||||
|
name: `Test Music Service ${uuid()}`,
|
||||||
uri: "https://sonos-test.example.com/",
|
uri: "https://sonos-test.example.com/",
|
||||||
secureUri: "https://sonos-test.example.com/",
|
secureUri: "https://sonos-test.example.com/",
|
||||||
strings: {
|
strings: {
|
||||||
|
|||||||
@@ -1,122 +0,0 @@
|
|||||||
import request from "supertest";
|
|
||||||
import makeServer from "../src/server";
|
|
||||||
import { SONOS_DISABLED, Sonos, Device } from "../src/sonos";
|
|
||||||
|
|
||||||
import { aDevice, aService } from './builders';
|
|
||||||
import { InMemoryMusicService } from "./in_memory_music_service";
|
|
||||||
|
|
||||||
describe("index", () => {
|
|
||||||
describe("when sonos integration is disabled", () => {
|
|
||||||
const server = makeServer(SONOS_DISABLED, aService(), 'http://localhost:1234', new InMemoryMusicService());
|
|
||||||
|
|
||||||
describe("devices list", () => {
|
|
||||||
it("should be empty", async () => {
|
|
||||||
const res = await request(server).get("/").send();
|
|
||||||
|
|
||||||
expect(res.status).toEqual(200);
|
|
||||||
expect(res.text).not.toMatch(/class=device/);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("when there are 2 devices and bonob is not registered", () => {
|
|
||||||
const service1 = aService({
|
|
||||||
name: "s1",
|
|
||||||
sid: 1,
|
|
||||||
});
|
|
||||||
const service2 = aService({
|
|
||||||
name: "s2",
|
|
||||||
sid: 2,
|
|
||||||
});
|
|
||||||
const service3 = aService({
|
|
||||||
name: "s3",
|
|
||||||
sid: 3,
|
|
||||||
});
|
|
||||||
const service4 = aService({
|
|
||||||
name: "s4",
|
|
||||||
sid: 4,
|
|
||||||
});
|
|
||||||
const missingBonobService = aService({
|
|
||||||
name: "bonobMissing",
|
|
||||||
sid: 88
|
|
||||||
})
|
|
||||||
|
|
||||||
const device1: Device = aDevice({
|
|
||||||
name: "device1",
|
|
||||||
ip: "172.0.0.1",
|
|
||||||
port: 4301,
|
|
||||||
});
|
|
||||||
|
|
||||||
const device2: Device = aDevice({
|
|
||||||
name: "device2",
|
|
||||||
ip: "172.0.0.2",
|
|
||||||
port: 4302,
|
|
||||||
});
|
|
||||||
|
|
||||||
const fakeSonos: Sonos = {
|
|
||||||
devices: () => Promise.resolve([device1, device2]),
|
|
||||||
services: () => Promise.resolve([service1, service2, service3, service4]),
|
|
||||||
register: () => Promise.resolve(false),
|
|
||||||
};
|
|
||||||
|
|
||||||
const server = makeServer(fakeSonos, missingBonobService, 'http://localhost:1234', new InMemoryMusicService());
|
|
||||||
|
|
||||||
describe("devices list", () => {
|
|
||||||
it("should contain the devices returned from sonos", async () => {
|
|
||||||
const res = await request(server).get("/").send();
|
|
||||||
|
|
||||||
expect(res.status).toEqual(200);
|
|
||||||
expect(res.text).toMatch(/device1\s+\(172.0.0.1:4301\)/);
|
|
||||||
expect(res.text).toMatch(/device2\s+\(172.0.0.2:4302\)/);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("services", () => {
|
|
||||||
it("should contain a list of services returned from sonos", async () => {
|
|
||||||
const res = await request(server).get("/").send();
|
|
||||||
|
|
||||||
expect(res.status).toEqual(200);
|
|
||||||
expect(res.text).toMatch(/Services\s+4/);
|
|
||||||
expect(res.text).toMatch(/s1\s+\(1\)/);
|
|
||||||
expect(res.text).toMatch(/s2\s+\(2\)/);
|
|
||||||
expect(res.text).toMatch(/s3\s+\(3\)/);
|
|
||||||
expect(res.text).toMatch(/s4\s+\(4\)/);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("registration status", () => {
|
|
||||||
it("should be not-registered", async () => {
|
|
||||||
const res = await request(server).get("/").send();
|
|
||||||
expect(res.status).toEqual(200);
|
|
||||||
expect(res.text).toMatch(/No existing service registration/);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("when there are 2 devices and bonob is registered", () => {
|
|
||||||
const service1 = aService();
|
|
||||||
|
|
||||||
const service2 = aService();
|
|
||||||
|
|
||||||
const bonobService = aService({
|
|
||||||
name: "bonobNotMissing",
|
|
||||||
sid: 99
|
|
||||||
})
|
|
||||||
|
|
||||||
const fakeSonos: Sonos = {
|
|
||||||
devices: () => Promise.resolve([]),
|
|
||||||
services: () => Promise.resolve([service1, service2, bonobService]),
|
|
||||||
register: () => Promise.resolve(false),
|
|
||||||
};
|
|
||||||
|
|
||||||
const server = makeServer(fakeSonos, bonobService, 'http://localhost:1234', new InMemoryMusicService());
|
|
||||||
|
|
||||||
describe("registration status", () => {
|
|
||||||
it("should be registered", async () => {
|
|
||||||
const res = await request(server).get("/").send();
|
|
||||||
expect(res.status).toEqual(200);
|
|
||||||
expect(res.text).toMatch(/Existing service config/);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -57,7 +57,7 @@ class SonosDriver {
|
|||||||
.then(response => ({
|
.then(response => ({
|
||||||
expectSuccess: () => {
|
expectSuccess: () => {
|
||||||
expect(response.status).toEqual(200);
|
expect(response.status).toEqual(200);
|
||||||
expect(response.text).toContain("Login Successful")
|
expect(response.text).toContain("Login successful")
|
||||||
},
|
},
|
||||||
expectFailure: () => {
|
expectFailure: () => {
|
||||||
expect(response.status).toEqual(403);
|
expect(response.status).toEqual(403);
|
||||||
@@ -95,7 +95,7 @@ describe("scenarios", () => {
|
|||||||
const username = "validuser";
|
const username = "validuser";
|
||||||
const password = "validpassword";
|
const password = "validpassword";
|
||||||
|
|
||||||
it("should successfully sign up", async () => {
|
it("should successfuly sign up", async () => {
|
||||||
musicService.hasUser({ username, password });
|
musicService.hasUser({ username, password });
|
||||||
|
|
||||||
await sonosDriver
|
await sonosDriver
|
||||||
|
|||||||
188
tests/server.test.ts
Normal file
188
tests/server.test.ts
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
import request from "supertest";
|
||||||
|
import makeServer from "../src/server";
|
||||||
|
import { SONOS_DISABLED, Sonos, Device } from "../src/sonos";
|
||||||
|
|
||||||
|
import { aDevice, aService } from "./builders";
|
||||||
|
import { InMemoryMusicService } from "./in_memory_music_service";
|
||||||
|
|
||||||
|
describe("server", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("/", () => {
|
||||||
|
describe("when sonos integration is disabled", () => {
|
||||||
|
const server = makeServer(
|
||||||
|
SONOS_DISABLED,
|
||||||
|
aService(),
|
||||||
|
"http://localhost:1234",
|
||||||
|
new InMemoryMusicService()
|
||||||
|
);
|
||||||
|
|
||||||
|
describe("devices list", () => {
|
||||||
|
it("should be empty", async () => {
|
||||||
|
const res = await request(server).get("/").send();
|
||||||
|
|
||||||
|
expect(res.status).toEqual(200);
|
||||||
|
expect(res.text).not.toMatch(/class=device/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when there are 2 devices and bonob is not registered", () => {
|
||||||
|
const service1 = aService({
|
||||||
|
name: "s1",
|
||||||
|
sid: 1,
|
||||||
|
});
|
||||||
|
const service2 = aService({
|
||||||
|
name: "s2",
|
||||||
|
sid: 2,
|
||||||
|
});
|
||||||
|
const service3 = aService({
|
||||||
|
name: "s3",
|
||||||
|
sid: 3,
|
||||||
|
});
|
||||||
|
const service4 = aService({
|
||||||
|
name: "s4",
|
||||||
|
sid: 4,
|
||||||
|
});
|
||||||
|
const missingBonobService = aService({
|
||||||
|
name: "bonobMissing",
|
||||||
|
sid: 88,
|
||||||
|
});
|
||||||
|
|
||||||
|
const device1: Device = aDevice({
|
||||||
|
name: "device1",
|
||||||
|
ip: "172.0.0.1",
|
||||||
|
port: 4301,
|
||||||
|
});
|
||||||
|
|
||||||
|
const device2: Device = aDevice({
|
||||||
|
name: "device2",
|
||||||
|
ip: "172.0.0.2",
|
||||||
|
port: 4302,
|
||||||
|
});
|
||||||
|
|
||||||
|
const fakeSonos: Sonos = {
|
||||||
|
devices: () => Promise.resolve([device1, device2]),
|
||||||
|
services: () =>
|
||||||
|
Promise.resolve([service1, service2, service3, service4]),
|
||||||
|
register: () => Promise.resolve(false),
|
||||||
|
};
|
||||||
|
|
||||||
|
const server = makeServer(
|
||||||
|
fakeSonos,
|
||||||
|
missingBonobService,
|
||||||
|
"http://localhost:1234",
|
||||||
|
new InMemoryMusicService()
|
||||||
|
);
|
||||||
|
|
||||||
|
describe("devices list", () => {
|
||||||
|
it("should contain the devices returned from sonos", async () => {
|
||||||
|
const res = await request(server).get("/").send();
|
||||||
|
|
||||||
|
expect(res.status).toEqual(200);
|
||||||
|
expect(res.text).toMatch(/device1\s+\(172.0.0.1:4301\)/);
|
||||||
|
expect(res.text).toMatch(/device2\s+\(172.0.0.2:4302\)/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("services", () => {
|
||||||
|
it("should contain a list of services returned from sonos", async () => {
|
||||||
|
const res = await request(server).get("/").send();
|
||||||
|
|
||||||
|
expect(res.status).toEqual(200);
|
||||||
|
expect(res.text).toMatch(/Services\s+4/);
|
||||||
|
expect(res.text).toMatch(/s1\s+\(1\)/);
|
||||||
|
expect(res.text).toMatch(/s2\s+\(2\)/);
|
||||||
|
expect(res.text).toMatch(/s3\s+\(3\)/);
|
||||||
|
expect(res.text).toMatch(/s4\s+\(4\)/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("registration status", () => {
|
||||||
|
it("should be not-registered", async () => {
|
||||||
|
const res = await request(server).get("/").send();
|
||||||
|
expect(res.status).toEqual(200);
|
||||||
|
expect(res.text).toMatch(/No existing service registration/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when there are 2 devices and bonob is registered", () => {
|
||||||
|
const service1 = aService();
|
||||||
|
|
||||||
|
const service2 = aService();
|
||||||
|
|
||||||
|
const bonobService = aService({
|
||||||
|
name: "bonobNotMissing",
|
||||||
|
sid: 99,
|
||||||
|
});
|
||||||
|
|
||||||
|
const fakeSonos: Sonos = {
|
||||||
|
devices: () => Promise.resolve([]),
|
||||||
|
services: () => Promise.resolve([service1, service2, bonobService]),
|
||||||
|
register: () => Promise.resolve(false),
|
||||||
|
};
|
||||||
|
|
||||||
|
const server = makeServer(
|
||||||
|
fakeSonos,
|
||||||
|
bonobService,
|
||||||
|
"http://localhost:1234",
|
||||||
|
new InMemoryMusicService()
|
||||||
|
);
|
||||||
|
|
||||||
|
describe("registration status", () => {
|
||||||
|
it("should be registered", async () => {
|
||||||
|
const res = await request(server).get("/").send();
|
||||||
|
expect(res.status).toEqual(200);
|
||||||
|
expect(res.text).toMatch(/Existing service config/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("/register", () => {
|
||||||
|
const sonos = {
|
||||||
|
register: jest.fn(),
|
||||||
|
};
|
||||||
|
const theService = aService({
|
||||||
|
name: "We can all live a life of service",
|
||||||
|
sid: 999,
|
||||||
|
});
|
||||||
|
const server = makeServer(
|
||||||
|
(sonos as unknown) as Sonos,
|
||||||
|
theService,
|
||||||
|
"http://localhost:1234",
|
||||||
|
new InMemoryMusicService()
|
||||||
|
);
|
||||||
|
|
||||||
|
describe("when is succesfull", () => {
|
||||||
|
it("should return a nice message", async () => {
|
||||||
|
sonos.register.mockResolvedValue(true);
|
||||||
|
|
||||||
|
const res = await request(server).post("/register").send();
|
||||||
|
|
||||||
|
expect(res.status).toEqual(200);
|
||||||
|
expect(res.text).toMatch("Successfully registered");
|
||||||
|
|
||||||
|
expect(sonos.register.mock.calls.length).toEqual(1);
|
||||||
|
expect(sonos.register.mock.calls[0][0]).toBe(theService);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when is unsuccesfull", () => {
|
||||||
|
it("should return a failure message", async () => {
|
||||||
|
sonos.register.mockResolvedValue(false);
|
||||||
|
|
||||||
|
const res = await request(server).post("/register").send();
|
||||||
|
|
||||||
|
expect(res.status).toEqual(500);
|
||||||
|
expect(res.text).toMatch("Registration failed!");
|
||||||
|
|
||||||
|
expect(sonos.register.mock.calls.length).toEqual(1);
|
||||||
|
expect(sonos.register.mock.calls[0][0]).toBe(theService);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
<% layout('./layout', { title: "Login Successful" }) %>
|
|
||||||
|
|
||||||
<div id="content">
|
|
||||||
<h1>Login successful</h1>
|
|
||||||
</div>
|
|
||||||
5
web/views/success.eta
Normal file
5
web/views/success.eta
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
<% layout('./layout', { title: "Yippee" }) %>
|
||||||
|
|
||||||
|
<div id="content">
|
||||||
|
<h1><%= it.message %></h1>
|
||||||
|
</div>
|
||||||
Reference in New Issue
Block a user