Ability to query artists from navidrome with paging

This commit is contained in:
simojenki
2021-03-01 22:31:37 +11:00
parent 7a28bc5288
commit 3aa1056aa5
12 changed files with 341 additions and 126 deletions

View File

@@ -23,7 +23,7 @@ describe("navidrome", () => {
const password = "pass1";
const salt = "saltysalty";
const navidrome = new Navidrome(url, encryption());
const navidrome = new Navidrome(url, encryption("secret"));
const mockedRandomString = (randomString as unknown) as jest.Mock;
@@ -35,6 +35,14 @@ describe("navidrome", () => {
mockedRandomString.mockReturnValue(salt);
});
const authParams = {
u: username,
t: t(password, salt),
s: salt,
v: "1.16.1",
c: "bonob",
};
describe("generateToken", () => {
describe("when the credentials are valid", () => {
it("should be able to generate a token and then login using it", async () => {
@@ -50,18 +58,9 @@ describe("navidrome", () => {
expect(token.nickname).toEqual(username);
expect(token.userId).toEqual(username);
expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/ping.view`,
{
params: {
u: username,
t: t(password, salt),
s: salt,
v: "1.16.1",
c: "bonob",
},
}
);
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/ping.view`, {
params: authParams,
});
});
});
@@ -80,4 +79,66 @@ describe("navidrome", () => {
});
});
});
describe("getArtists", () => {
beforeEach(() => {
(axios.get as jest.Mock).mockResolvedValue({
status: 200,
data: `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)">
<artists lastModified="1614586749000" ignoredArticles="The El La Los Las Le Les Os As O A">
<index name="#">
<artist id="2911b2d67a6b11eb804dd360a6225680" name="10 Planets" albumCount="22"></artist>
<artist id="3c0b9d7a7a6b11eb9773f398e6236ad6" name="1200 Ounces" albumCount="9"></artist>
</index>
<index name="A">
<artist id="3c5113007a6b11eb87173bfb9b07f9b1" name="AAAB" albumCount="2"></artist>
</index>
<index name="B">
<artist id="3ca781c27a6b11eb897ebbb5773603ad" name="BAAB" albumCount="2"></artist>
</index>
</artists>
</subsonic-response>`,
});
});
describe("when no paging specified", () => {
it("should return all the artists", async () => {
const artists = await navidrome
.generateToken({ username, password })
.then((it) => navidrome.login(it.authToken))
.then((it) => it.artists({}));
const expectedArtists = [
{ id: "2911b2d67a6b11eb804dd360a6225680", name: "10 Planets" },
{ id: "3c0b9d7a7a6b11eb9773f398e6236ad6", name: "1200 Ounces" },
{ id: "3c5113007a6b11eb87173bfb9b07f9b1", name: "AAAB" },
{ id: "3ca781c27a6b11eb897ebbb5773603ad", name: "BAAB" },
];
expect(artists).toEqual([expectedArtists, 4]);
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: authParams,
});
});
});
describe("when paging specified", () => {
it("should return only the correct page of artists", async () => {
const artists = await navidrome
.generateToken({ username, password })
.then((it) => navidrome.login(it.authToken))
.then((it) => it.artists({ _index: 1, _count: 2 }));
const expectedArtists = [
{ id: "3c0b9d7a7a6b11eb9773f398e6236ad6", name: "1200 Ounces" },
{ id: "3c5113007a6b11eb87173bfb9b07f9b1", name: "AAAB" },
];
expect(artists).toEqual([expectedArtists, 4]);
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: authParams,
});
});
});
});
});