mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-21 17:33:29 +01:00
Tests for browsing of artists and albums
This commit is contained in:
@@ -3,8 +3,18 @@ import { Express } from "express";
|
||||
|
||||
import request from "supertest";
|
||||
|
||||
import { GetAppLinkResult, GetDeviceAuthTokenResult } from "../src/smapi";
|
||||
import { getAppLinkMessage } from "./builders";
|
||||
import {
|
||||
GetAppLinkResult,
|
||||
GetDeviceAuthTokenResult,
|
||||
GetMetadataResponse,
|
||||
} from "../src/smapi";
|
||||
import {
|
||||
BLONDIE,
|
||||
BOB_MARLEY,
|
||||
getAppLinkMessage,
|
||||
MADONNA,
|
||||
someCredentials,
|
||||
} from "./builders";
|
||||
import { InMemoryMusicService } from "./in_memory_music_service";
|
||||
import { InMemoryLinkCodes } from "../src/link_codes";
|
||||
import { Credentials } from "../src/music_service";
|
||||
@@ -12,13 +22,53 @@ import makeServer from "../src/server";
|
||||
import { Service, bonobService, SONOS_DISABLED } from "../src/sonos";
|
||||
import supersoap from "./supersoap";
|
||||
|
||||
class FooDriver {
|
||||
class LoggedInSonosDriver {
|
||||
client: Client;
|
||||
token: GetDeviceAuthTokenResult;
|
||||
currentMetadata?: GetMetadataResponse = undefined;
|
||||
|
||||
constructor(client: Client, token: GetDeviceAuthTokenResult) {
|
||||
this.client = client;
|
||||
this.token = token;
|
||||
this.client.addSoapHeader({
|
||||
credentials: someCredentials(
|
||||
this.token.getDeviceAuthTokenResult.authToken
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
async navigate(...path: string[]) {
|
||||
let next = path.shift();
|
||||
while (next) {
|
||||
if (next != "root") {
|
||||
const childIds = this.currentMetadata!.getMetadataResult.mediaCollection.map(
|
||||
(it) => it.id
|
||||
);
|
||||
if (!childIds.includes(next)) {
|
||||
throw `Expected to find a child element with id=${next} in order to browse, but found only ${childIds}`;
|
||||
}
|
||||
}
|
||||
this.currentMetadata = (await this.getMetadata(next))[0];
|
||||
next = path.shift();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
expectTitles(titles: string[]) {
|
||||
expect(
|
||||
this.currentMetadata!.getMetadataResult.mediaCollection.map(
|
||||
(it) => it.title
|
||||
)
|
||||
).toEqual(titles);
|
||||
return this;
|
||||
}
|
||||
|
||||
async getMetadata(id: string) {
|
||||
return await this.client.getMetadataAsync({
|
||||
id,
|
||||
index: 0,
|
||||
count: 100,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +124,10 @@ class SonosDriver {
|
||||
|
||||
return client
|
||||
.getDeviceAuthTokenAsync({ linkCode })
|
||||
.then((authToken: [GetDeviceAuthTokenResult, any]) => new FooDriver(client, authToken[0]));
|
||||
.then(
|
||||
(authToken: [GetDeviceAuthTokenResult, any]) =>
|
||||
new LoggedInSonosDriver(client, authToken[0])
|
||||
);
|
||||
},
|
||||
expectFailure: () => {
|
||||
expect(response.status).toEqual(403);
|
||||
@@ -89,7 +142,10 @@ class SonosDriver {
|
||||
describe("scenarios", () => {
|
||||
const bonobUrl = "http://localhost:1234";
|
||||
const bonob = bonobService("bonob", 123, bonobUrl);
|
||||
const musicService = new InMemoryMusicService();
|
||||
const musicService = new InMemoryMusicService().hasArtists(
|
||||
BOB_MARLEY,
|
||||
BLONDIE
|
||||
);
|
||||
const linkCodes = new InMemoryLinkCodes();
|
||||
const server = makeServer(
|
||||
SONOS_DISABLED,
|
||||
@@ -107,22 +163,6 @@ describe("scenarios", () => {
|
||||
});
|
||||
|
||||
describe("adding the service", () => {
|
||||
describe("when the user exists within the music service", () => {
|
||||
const username = "validuser";
|
||||
const password = "validpassword";
|
||||
|
||||
it("should successfuly sign up", async () => {
|
||||
musicService.hasUser({ username, password });
|
||||
|
||||
await sonosDriver
|
||||
.addService()
|
||||
.then((it) => it.login({ username, password }))
|
||||
.then((it) => it.expectSuccess());
|
||||
|
||||
expect(linkCodes.count()).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when the user doesnt exists within the music service", () => {
|
||||
const username = "invaliduser";
|
||||
const password = "invalidpassword";
|
||||
@@ -138,5 +178,54 @@ describe("scenarios", () => {
|
||||
expect(linkCodes.count()).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when the user exists within the music service", () => {
|
||||
const username = "validuser";
|
||||
const password = "validpassword";
|
||||
|
||||
beforeEach(() => {
|
||||
musicService.hasUser({ username, password });
|
||||
musicService.hasArtists(BLONDIE, BOB_MARLEY, MADONNA);
|
||||
});
|
||||
|
||||
it("should successfuly sign up", async () => {
|
||||
await sonosDriver
|
||||
.addService()
|
||||
.then((it) => it.login({ username, password }))
|
||||
.then((it) => it.expectSuccess());
|
||||
|
||||
expect(linkCodes.count()).toEqual(1);
|
||||
});
|
||||
|
||||
it("should be able to list the artists", async () => {
|
||||
await sonosDriver
|
||||
.addService()
|
||||
.then((it) => it.login({ username, password }))
|
||||
.then((it) => it.expectSuccess())
|
||||
.then((it) => it.navigate("root", "artists"))
|
||||
.then((it) =>
|
||||
it.expectTitles(
|
||||
[BLONDIE, BOB_MARLEY, MADONNA].map(
|
||||
(it) => it.name
|
||||
)
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it("should be able to list the albums", async () => {
|
||||
await sonosDriver
|
||||
.addService()
|
||||
.then((it) => it.login({ username, password }))
|
||||
.then((it) => it.expectSuccess())
|
||||
.then((it) => it.navigate("root", "albums"))
|
||||
.then((it) =>
|
||||
it.expectTitles(
|
||||
[...BLONDIE.albums, ...BOB_MARLEY.albums, ...MADONNA.albums].map(
|
||||
(it) => it.name
|
||||
)
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user