Change ND genre ids to b64 encoded strings of genre, so as to differentiate between genre name and id (#54)

This commit is contained in:
Simon J
2021-09-21 10:53:02 +10:00
committed by GitHub
parent be4fcdff24
commit d508eaebcf
8 changed files with 68 additions and 53 deletions

17
tests/b64.test.ts Normal file
View File

@@ -0,0 +1,17 @@
import { b64Encode, b64Decode } from "../src/b64";
describe("b64", () => {
const value = "foobar100";
const encoded = Buffer.from(value).toString("base64");
describe("encode", () => {
it("should encode", () => {
expect(b64Encode(value)).toEqual(encoded);
});
});
describe("decode", () => {
it("should decode", () => {
expect(b64Decode(encoded)).toEqual(value);
});
});
});

View File

@@ -5,6 +5,7 @@ import { Credentials } from "../src/smapi";
import { Service, Device } from "../src/sonos";
import { Album, Artist, Track, albumToAlbumSummary, artistToArtistSummary, PlaylistSummary, Playlist } from "../src/music_service";
import randomString from "../src/random_string";
import { b64Encode } from "../src/b64";
const randomInt = (max: number) => Math.floor(Math.random() * Math.floor(max));
const randomIpAddress = () => `127.0.${randomInt(255)}.${randomInt(255)}`;
@@ -111,16 +112,18 @@ export function anArtist(fields: Partial<Artist> = {}): Artist {
return artist;
}
export const HIP_HOP = { id: "genre_hip_hop", name: "Hip-Hop" };
export const METAL = { id: "genre_metal", name: "Metal" };
export const NEW_WAVE = { id: "genre_new_wave", name: "New Wave" };
export const POP = { id: "genre_pop", name: "Pop" };
export const POP_ROCK = { id: "genre_pop_rock", name: "Pop Rock" };
export const REGGAE = { id: "genre_reggae", name: "Reggae" };
export const ROCK = { id: "genre_rock", name: "Rock" };
export const SKA = { id: "genre_ska", name: "Ska" };
export const PUNK = { id: "genre_punk", name: "Punk" };
export const TRIP_HOP = { id: "genre_trip_hop", name: "Trip Hop" };
export const aGenre = (name: string) => ({ id: b64Encode(name), name })
export const HIP_HOP = aGenre("Hip-Hop");
export const METAL = aGenre("Metal");
export const NEW_WAVE = aGenre("New Wave");
export const POP = aGenre("Pop");
export const POP_ROCK = aGenre("Pop Rock");
export const REGGAE = aGenre("Reggae");
export const ROCK = aGenre("Rock");
export const SKA = aGenre("Ska");
export const PUNK = aGenre("Punk");
export const TRIP_HOP = aGenre("Trip Hop");
export const SAMPLE_GENRES = [HIP_HOP, METAL, NEW_WAVE, POP, POP_ROCK, REGGAE, ROCK, SKA];
export const randomGenre = () => SAMPLE_GENRES[randomInt(SAMPLE_GENRES.length)];

View File

@@ -467,9 +467,9 @@ describe("InMemoryMusicService", () => {
it("should provide an array of artists", async () => {
expect(await musicLibrary.genres()).toEqual([
HIP_HOP,
SKA,
POP,
ROCK,
SKA,
]);
});
});

View File

@@ -5,6 +5,8 @@ import { pipe } from "fp-ts/lib/function";
import { ordString, fromCompare } from "fp-ts/lib/Ord";
import { shuffle } from "underscore";
import { b64Encode, b64Decode } from "../src/b64";
import {
MusicService,
Credentials,
@@ -37,9 +39,7 @@ export class InMemoryMusicService implements MusicService {
this.users[username] == password
) {
return Promise.resolve({
authToken: Buffer.from(JSON.stringify({ username, password })).toString(
"base64"
),
authToken: b64Encode(JSON.stringify({ username, password })),
userId: username,
nickname: username,
});
@@ -49,9 +49,7 @@ export class InMemoryMusicService implements MusicService {
}
login(token: string): Promise<MusicLibrary> {
const credentials = JSON.parse(
Buffer.from(token, "base64").toString("ascii")
) as Credentials;
const credentials = JSON.parse(b64Decode(token)) as Credentials;
if (this.users[credentials.username] != credentials.password)
return Promise.reject("Invalid auth token");

View File

@@ -38,12 +38,14 @@ import {
SimilarArtist,
} from "../src/music_service";
import {
aGenre,
anAlbum,
anArtist,
aPlaylist,
aPlaylistSummary,
aTrack,
} from "./builders";
import { b64Encode } from "../src/b64";
describe("t", () => {
it("should be an md5 of the password and the salt", () => {
@@ -547,7 +549,7 @@ describe("Navidrome", () => {
.then((it) => navidrome.login(it.authToken))
.then((it) => it.genres());
expect(result).toEqual([{ id: "genre1", name: "genre1" }]);
expect(result).toEqual([{ id: b64Encode("genre1"), name: "genre1" }]);
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getGenres`, {
params: asURLSearchParams(authParams),
@@ -579,10 +581,10 @@ describe("Navidrome", () => {
.then((it) => it.genres());
expect(result).toEqual([
{ id: "g1", name: "g1" },
{ id: "g2", name: "g2" },
{ id: "g3", name: "g3" },
{ id: "g4", name: "g4" },
{ id: b64Encode("g1"), name: "g1" },
{ id: b64Encode("g2"), name: "g2" },
{ id: b64Encode("g3"), name: "g3" },
{ id: b64Encode("g4"), name: "g4" },
]);
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getGenres`, {
@@ -1234,11 +1236,11 @@ describe("Navidrome", () => {
);
});
it("should pass the filter to navidrome", async () => {
it("should map the 64 encoded genre back into the subsonic genre", async () => {
const q: AlbumQuery = {
_index: 0,
_count: 100,
genre: "Pop",
genre: b64Encode("Pop"),
type: "byGenre",
};
const result = await navidrome
@@ -2300,7 +2302,7 @@ describe("Navidrome", () => {
describe("streaming a track", () => {
const trackId = uuid();
const genre = { id: "foo", name: "foo" };
const genre = aGenre("foo");
const album = anAlbum({ genre });
const artist = anArtist({
@@ -3535,7 +3537,7 @@ describe("Navidrome", () => {
it("should return true", async () => {
const album = anAlbum({
name: "foo woo",
genre: { id: "pop", name: "pop" },
genre: { id: b64Encode("pop"), name: "pop" },
});
const artist = anArtist({ name: "#1", albums: [album] });
@@ -3570,13 +3572,13 @@ describe("Navidrome", () => {
it("should return true", async () => {
const album1 = anAlbum({
name: "album1",
genre: { id: "pop", name: "pop" },
genre: { id: b64Encode("pop"), name: "pop" },
});
const artist1 = anArtist({ name: "artist1", albums: [album1] });
const album2 = anAlbum({
name: "album2",
genre: { id: "pop", name: "pop" },
genre: { id: b64Encode("pop"), name: "pop" },
});
const artist2 = anArtist({ name: "artist2", albums: [album2] });
@@ -3904,11 +3906,11 @@ describe("Navidrome", () => {
const id = uuid();
const name = "Great Playlist";
const track1 = aTrack({
genre: { id: "pop", name: "pop" },
genre: { id: b64Encode("pop"), name: "pop" },
number: 66,
});
const track2 = aTrack({
genre: { id: "rock", name: "rock" },
genre: { id: b64Encode("rock"), name: "rock" },
number: 77,
});