Feature/flavour in subsonic token (#83)

* Add type of subsonic clone to serviceToken so can specialise client for navidrome

* Ability to add bearer token to subsonic credentials for flavours of subsonic
This commit is contained in:
Simon J
2021-12-03 13:17:03 +11:00
committed by GitHub
parent 8a0140b728
commit 075538f029
5 changed files with 110 additions and 21 deletions

View File

@@ -19,6 +19,9 @@ import {
artistImageURN,
images,
song,
PingResponse,
parseToken,
asToken,
} from "../src/subsonic";
import axios from "axios";
@@ -558,7 +561,19 @@ const FAILURE = {
},
};
const PING_OK = subsonicOK({});
const pingJson = (pingResponse: Partial<PingResponse> = {}) => ({
"subsonic-response": {
status: "ok",
version: "1.16.1",
type: "navidrome",
serverVersion: "0.45.1 (c55e6590)",
...pingResponse
}
})
const PING_OK = pingJson({ status: "ok" });
describe("artistURN", () => {
describe("when artist URL is", () => {
@@ -729,6 +744,29 @@ describe("Subsonic", () => {
expect(token.nickname).toEqual(username);
expect(token.userId).toEqual(username);
expect(parseToken(token.serviceToken)).toEqual({ username, password, type: PING_OK["subsonic-response"].type })
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/ping.view`, {
params: asURLSearchParams(authParamsPlusJson),
headers,
});
});
it("should store the type of the subsonic server on the token", async () => {
const type = "someSubsonicClone";
(axios.get as jest.Mock).mockResolvedValue(ok(pingJson({ type })));
const token = (await navidrome.generateToken({
username,
password,
})) as AuthSuccess;
expect(token.serviceToken).toBeDefined();
expect(token.nickname).toEqual(username);
expect(token.userId).toEqual(username);
expect(parseToken(token.serviceToken)).toEqual({ username, password, type })
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/ping.view`, {
params: asURLSearchParams(authParamsPlusJson),
headers,
@@ -751,6 +789,29 @@ describe("Subsonic", () => {
});
});
describe("login", () => {
describe("when the token is for generic subsonic", () => {
it("should return a subsonic client", async () => {
const client = await navidrome.login(asToken({ username: "foo", password: "bar", type: "subsonic", bearer: undefined }));
expect(client.flavour()).toEqual("subsonic");
});
});
describe("when the token is for navidrome", () => {
it("should return a navidrome client", async () => {
const client = await navidrome.login(asToken({ username: "foo", password: "bar", type: "navidrome", bearer: undefined }));
expect(client.flavour()).toEqual("navidrome");
});
});
describe("when the token is for gonic", () => {
it("should return a subsonic client", async () => {
const client = await navidrome.login(asToken({ username: "foo", password: "bar", type: "gonic", bearer: undefined }));
expect(client.flavour()).toEqual("subsonic");
});
});
});
describe("getting genres", () => {
describe("when there are none", () => {
beforeEach(() => {