Tests for browsing of artists and albums

This commit is contained in:
simojenki
2021-03-01 12:46:23 +11:00
parent 0cb02707f1
commit 3b350c4402
12 changed files with 778 additions and 57 deletions

View File

@@ -1,6 +1,6 @@
import crypto from "crypto";
import request from "supertest";
import { createClientAsync } from "soap";
import { Client, createClientAsync } from "soap";
import { DOMParserImpl } from "xmldom-ts";
import * as xpath from "xpath-ts";
@@ -8,11 +8,12 @@ import * as xpath from "xpath-ts";
import { InMemoryLinkCodes, LinkCodes } from "../src/link_codes";
import makeServer from "../src/server";
import { bonobService, SONOS_DISABLED } from "../src/sonos";
import { STRINGS_ROUTE, LOGIN_ROUTE } from "../src/smapi";
import { STRINGS_ROUTE, LOGIN_ROUTE, getMetadataResult, container } from "../src/smapi";
import { aService, getAppLinkMessage } from "./builders";
import { aService, BLONDIE, BOB_MARLEY, getAppLinkMessage, someCredentials } from "./builders";
import { InMemoryMusicService } from "./in_memory_music_service";
import supersoap from "./supersoap";
import { AuthSuccess } from "../src/music_service";
const parseXML = (value: string) => new DOMParserImpl().parseFromString(value);
const select = xpath.useNamespaces({ sonos: "http://sonos.com/sonosapi" });
@@ -42,6 +43,31 @@ describe("service config", () => {
});
});
describe("getMetadataResult", () => {
describe("when there are a zero mediaCollections", () => {
it("should have zero count", () => {
const result = getMetadataResult({ mediaCollection: [] });
expect(result.getMetadataResult.count).toEqual(0);
expect(result.getMetadataResult.index).toEqual(0);
expect(result.getMetadataResult.total).toEqual(0);
expect(result.getMetadataResult.mediaCollection).toEqual([]);
});
});
describe("when there are a number of mediaCollections", () => {
it("should add correct counts", () => {
const mediaCollection = [{}, {}];
const result = getMetadataResult({ mediaCollection });
expect(result.getMetadataResult.count).toEqual(2);
expect(result.getMetadataResult.index).toEqual(0);
expect(result.getMetadataResult.total).toEqual(2);
expect(result.getMetadataResult.mediaCollection).toEqual(mediaCollection);
});
});
});
describe("api", () => {
const rootUrl = "http://localhost:1234";
const service = bonobService("test-api", 133, rootUrl, "AppLink");
@@ -171,20 +197,24 @@ describe("api", () => {
musicService,
linkCodes
);
describe("when there is a linkCode association", () => {
it("should return a device auth token", async () => {
const linkCode = linkCodes.mint();
const association = { authToken: "at", userId: "uid", nickname: "nn" };
const association = {
authToken: "at",
userId: "uid",
nickname: "nn",
};
linkCodes.associate(linkCode, association);
const ws = await createClientAsync(`${service.uri}?wsdl`, {
endpoint: service.uri,
httpClient: supersoap(server, rootUrl),
});
const result = await ws.getDeviceAuthTokenAsync({ linkCode });
expect(result[0]).toEqual({
getDeviceAuthTokenResult: {
authToken: association.authToken,
@@ -200,20 +230,20 @@ describe("api", () => {
});
});
});
describe("when there is no linkCode association", () => {
it("should return a device auth token", async () => {
const linkCode = "invalidLinkCode";
const ws = await createClientAsync(`${service.uri}?wsdl`, {
endpoint: service.uri,
httpClient: supersoap(server, rootUrl),
});
await ws
.getDeviceAuthTokenAsync({ linkCode })
.then(() => {
throw "Shouldnt get here";
fail("Shouldnt get here");
})
.catch((e: any) => {
expect(e.root.Envelope.Body.Fault).toEqual({
@@ -225,7 +255,151 @@ describe("api", () => {
});
});
});
});
describe("getMetadata", () => {
const server = makeServer(
SONOS_DISABLED,
service,
rootUrl,
musicService,
linkCodes
);
describe("when no credentials header provided", () => {
it("should return a fault of LoginUnauthorized", async () => {
const ws = await createClientAsync(`${service.uri}?wsdl`, {
endpoint: service.uri,
httpClient: supersoap(server, rootUrl),
});
await ws
.getMetadataAsync({ id: "root", index: 0, count: 0 })
.then(() => fail("shouldnt get here"))
.catch((e: any) => {
expect(e.root.Envelope.Body.Fault).toEqual({
faultcode: "Client.LoginUnsupported",
faultstring: "Missing credentials...",
});
});
});
});
describe("when invalid credentials are provided", () => {
it("should return a fault of LoginInvalid", async () => {
const username = "userThatGetsDeleted";
const password = "password1";
musicService.hasUser({ username, password });
const token = musicService.generateToken({
username,
password,
}) as AuthSuccess;
musicService.hasNoUsers();
const ws = await createClientAsync(`${service.uri}?wsdl`, {
endpoint: service.uri,
httpClient: supersoap(server, rootUrl),
});
ws.addSoapHeader({ credentials: someCredentials(token.authToken) });
await ws
.getMetadataAsync({ id: "root", index: 0, count: 0 })
.then(() => fail("shouldnt get here"))
.catch((e: any) => {
expect(e.root.Envelope.Body.Fault).toEqual({
faultcode: "Client.LoginUnauthorized",
faultstring: "Credentials not found...",
});
});
});
});
describe("when valid credentials are provided", () => {
const username = "validUser";
const password = "validPassword";
let token: AuthSuccess;
let ws: Client;
beforeEach(async () => {
musicService.hasUser({ username, password });
token = musicService.generateToken({
username,
password,
}) as AuthSuccess;
ws = await createClientAsync(`${service.uri}?wsdl`, {
endpoint: service.uri,
httpClient: supersoap(server, rootUrl),
});
ws.addSoapHeader({ credentials: someCredentials(token.authToken) });
});
describe("asking for the root container", () => {
it("should return it", async () => {
const root = await ws.getMetadataAsync({
id: "root",
index: 0,
count: 100,
});
expect(root[0]).toEqual(getMetadataResult({
mediaCollection: [
container({ id: "artists", title: "Artists" }),
container({ id: "albums", title: "Albums" }),
],
}));
});
});
describe("asking for artists", () => {
it("should return it", async () => {
musicService.hasArtists(BLONDIE, BOB_MARLEY);
const artists = await ws.getMetadataAsync({
id: "artists",
index: 0,
count: 100,
});
expect(artists[0]).toEqual(getMetadataResult({
mediaCollection: [BLONDIE, BOB_MARLEY].map(it => container({ id: `artist:${it.id}`, title: it.name })),
}));
});
});
describe("asking for all albums", () => {
it("should return it", async () => {
musicService.hasArtists(BLONDIE, BOB_MARLEY);
const albums = await ws.getMetadataAsync({
id: "albums",
index: 0,
count: 100,
});
expect(albums[0]).toEqual(getMetadataResult({
mediaCollection: [...BLONDIE.albums, ...BOB_MARLEY.albums].map(it => container({ id: `album:${it.id}`, title: it.name })),
}));
});
});
describe("asking for albums with paging", () => {
it("should return it", async () => {
musicService.hasArtists(BLONDIE, BOB_MARLEY);
expect(BLONDIE.albums.length).toEqual(2);
expect(BOB_MARLEY.albums.length).toEqual(3);
const albums = await ws.getMetadataAsync({
id: "albums",
index: 2,
count: 2,
});
expect(albums[0]).toEqual(getMetadataResult({
mediaCollection: [
container({ id: `album:${BOB_MARLEY.albums[0]!.id}`, title: BOB_MARLEY.albums[0]!.name }),
container({ id: `album:${BOB_MARLEY.albums[1]!.id}`, title: BOB_MARLEY.albums[1]!.name }),
],
}));
});
});
});
});
});
});