/rest speaks the OpenSubsonic API (Subsonic 1.16.1) so that other apps can browse the music of a server. A call logs in with the password of a device: an apiKey, Subsonic's token (u, t, s) or the password itself (u, p). The account password is never accepted there. - devices: ByKey, ByToken, ByPassword and the note of last use - opensubsonic package: JSON and XML answers, ping, getLicense, getOpenSubsonicExtensions, tokenInfo, getMusicFolders, getArtists, getArtist, getAlbum, getSong, getGenres - storage.SubsonicRepo: year, genres, size, bitrate, suffix and added date
118 lines
4.2 KiB
Go
118 lines
4.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/devices"
|
|
)
|
|
|
|
func TestDevicesAreKeptListedAndTakenBack(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewDevicesRepo(db)
|
|
alice, bob := seedUser(t, db, "a@example.org"), seedUser(t, db, "b@example.org")
|
|
at := time.Unix(1_700_000_000, 0)
|
|
|
|
first, err := repo.Create(ctx, devices.Row{UserID: alice, Name: "Phone", KeyHash: "h1", Sealed: []byte{1, 2, 3}, CreatedAt: at})
|
|
if err != nil || first.ID == 0 {
|
|
t.Fatalf("Create = %+v, %v", first, err)
|
|
}
|
|
if _, err := repo.Create(ctx, devices.Row{UserID: bob, Name: "Tablet", KeyHash: "h2", Sealed: []byte{4}, CreatedAt: at}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := repo.List(ctx, alice)
|
|
if err != nil || len(got) != 1 {
|
|
t.Fatalf("List = %+v, %v", got, err)
|
|
}
|
|
if g := got[0]; g.ID != first.ID || g.Name != "Phone" || g.KeyHash != "h1" || string(g.Sealed) != "\x01\x02\x03" || !g.CreatedAt.Equal(at) || g.LastUsedAt != nil {
|
|
t.Fatalf("row %+v", g)
|
|
}
|
|
|
|
if err := repo.Delete(ctx, bob, first.ID); !errors.Is(err, devices.ErrNotFound) {
|
|
t.Fatalf("someone else's device: %v", err)
|
|
}
|
|
if err := repo.Delete(ctx, alice, first.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := repo.Delete(ctx, alice, first.ID); !errors.Is(err, devices.ErrNotFound) {
|
|
t.Fatalf("twice: %v", err)
|
|
}
|
|
if got, _ := repo.List(ctx, alice); len(got) != 0 {
|
|
t.Fatalf("after delete: %+v", got)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTwoDevicesCannotShareASecret(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewDevicesRepo(db)
|
|
uid := seedUser(t, db, "a@example.org")
|
|
row := devices.Row{UserID: uid, Name: "A", KeyHash: "same", Sealed: []byte{1}, CreatedAt: time.Unix(1, 0)}
|
|
if _, err := repo.Create(ctx, row); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := repo.Create(ctx, row); err == nil {
|
|
t.Fatal("the same secret was kept twice")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestDevicesGoWithTheirPerson(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewDevicesRepo(db)
|
|
uid := seedUser(t, db, "a@example.org")
|
|
if _, err := repo.Create(ctx, devices.Row{UserID: uid, Name: "A", KeyHash: "h", Sealed: []byte{1}, CreatedAt: time.Unix(1, 0)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := db.ExecContext(ctx, "DELETE FROM users WHERE id = ?", uid); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var n int
|
|
if err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM device_passwords").Scan(&n); err != nil || n != 0 {
|
|
t.Fatalf("%d devices left, %v", n, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestADeviceIsFoundByItsKeyAndByTheAddressOfItsPerson(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewDevicesRepo(db)
|
|
alice, bob := seedUser(t, db, "alice@example.org"), seedUser(t, db, "bob@example.org")
|
|
at := time.Unix(1_700_000_000, 0)
|
|
a1, _ := repo.Create(ctx, devices.Row{UserID: alice, Name: "Phone", KeyHash: "h1", Sealed: []byte{1}, CreatedAt: at})
|
|
_, _ = repo.Create(ctx, devices.Row{UserID: alice, Name: "Laptop", KeyHash: "h2", Sealed: []byte{2}, CreatedAt: at})
|
|
_, _ = repo.Create(ctx, devices.Row{UserID: bob, Name: "Tablet", KeyHash: "h3", Sealed: []byte{3}, CreatedAt: at})
|
|
|
|
got, err := repo.ByKeyHash(ctx, "h1")
|
|
if err != nil || got.ID != a1.ID || got.UserID != alice || got.Email != "alice@example.org" {
|
|
t.Fatalf("ByKeyHash = %+v, %v", got, err)
|
|
}
|
|
if _, err := repo.ByKeyHash(ctx, "nope"); !errors.Is(err, devices.ErrNotFound) {
|
|
t.Fatalf("an unknown key: %v", err)
|
|
}
|
|
rows, err := repo.ByEmail(ctx, "alice@example.org")
|
|
if err != nil || len(rows) != 2 || rows[0].Email != "alice@example.org" {
|
|
t.Fatalf("ByEmail = %+v, %v", rows, err)
|
|
}
|
|
if rows, err := repo.ByEmail(ctx, "zed@example.org"); err != nil || len(rows) != 0 {
|
|
t.Fatalf("an unknown address: %+v, %v", rows, err)
|
|
}
|
|
|
|
used := at.Add(time.Hour)
|
|
if err := repo.Touch(ctx, a1.ID, used); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ = repo.ByKeyHash(ctx, "h1")
|
|
if got.LastUsedAt == nil || !got.LastUsedAt.Equal(used) {
|
|
t.Fatalf("last used = %v", got.LastUsedAt)
|
|
}
|
|
})
|
|
}
|