sonda/internal/api/http/opensubsonic_test.go
tgiachi 1ae8e77d81 feat: OpenSubsonic core, login with a device password
/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
2026-11-16 09:32:11 +01:00

110 lines
3.8 KiB
Go

package httpapi
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
// rest asks the API for a call of /rest and returns the answer inside subsonic-response.
func rest(t *testing.T, h http.Handler, method string, q url.Values) map[string]any {
t.Helper()
q.Set("f", "json")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("GET", "/rest/"+method+"?"+q.Encode(), nil))
var out struct {
R map[string]any `json:"subsonic-response"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil || out.R == nil {
t.Fatalf("%s: not an answer: %v: %s", method, err, rec.Body.String())
}
return out.R
}
func errorCode(r map[string]any) int {
e, _ := r["error"].(map[string]any)
if e == nil {
return -1
}
return int(e["code"].(float64))
}
func TestAnAppLogsInWithThePasswordOfADevice(t *testing.T) {
h := newTestAPI(t)
alice := registerAndLogin(t, h)
made := do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "Feishin"})
secret, _ := made.Body["secret"].(string)
id := itoa(int64(made.Body["id"].(float64)))
if r := rest(t, h, "ping", url.Values{"apiKey": {secret}}); r["status"] != "ok" {
t.Fatalf("API key: %v", r)
}
if r := rest(t, h, "ping", url.Values{"u": {"alice@example.org"}, "p": {secret}}); r["status"] != "ok" {
t.Fatalf("password: %v", r)
}
salt := "c19f3a"
sum := md5.Sum([]byte(secret + salt))
token := hex.EncodeToString(sum[:])
if r := rest(t, h, "ping", url.Values{"u": {"Alice@Example.org"}, "t": {token}, "s": {salt}}); r["status"] != "ok" {
t.Fatalf("token: %v", r)
}
if r := rest(t, h, "tokenInfo", url.Values{"apiKey": {secret}}); r["tokenInfo"].(map[string]any)["username"] != "alice@example.org" {
t.Fatalf("token info: %v", r)
}
// The use of the device shows in the list.
list := do(t, h, "GET", "/api/v1/me/devices", alice, nil).Body["devices"].([]any)
if _, used := list[0].(map[string]any)["last_used_at"]; !used {
t.Fatalf("the device was used but the list does not say: %v", list)
}
// The password of the account is not a password for apps.
if r := rest(t, h, "ping", url.Values{"u": {"alice@example.org"}, "p": {"longenough"}}); errorCode(r) != 40 {
t.Fatalf("account password: %v", r)
}
if r := rest(t, h, "ping", url.Values{"apiKey": {"longenough"}}); errorCode(r) != 44 {
t.Fatalf("account password as a key: %v", r)
}
// A device that is taken back stops working at once.
if r := do(t, h, "DELETE", "/api/v1/me/devices/"+id, alice, nil); r.Code != http.StatusNoContent {
t.Fatalf("revoke: %d", r.Code)
}
for name, q := range map[string]url.Values{
"key": {"apiKey": {secret}},
"password": {"u": {"alice@example.org"}, "p": {secret}},
"token": {"u": {"alice@example.org"}, "t": {token}, "s": {salt}},
} {
if r := rest(t, h, "ping", q); r["status"] != "failed" {
t.Errorf("%s after revoke: %v", name, r)
}
}
}
func TestADeviceOpensTheMusicAndNotTheAccount(t *testing.T) {
h := newTestAPI(t)
alice := registerAndLogin(t, h)
secret := do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "Phone"}).Body["secret"].(string)
// The secret is not a token of the main API, in any of the places a token can be.
if r := do(t, h, "GET", "/api/v1/me/devices", secret, nil); r.Code != http.StatusUnauthorized {
t.Fatalf("secret as a bearer token: %d", r.Code)
}
if r := do(t, h, "GET", "/api/v1/me/devices?access_token="+secret, "", nil); r.Code != http.StatusUnauthorized {
t.Fatalf("secret as an access_token: %d", r.Code)
}
// An empty library answers with empty lists.
r := rest(t, h, "getArtists", url.Values{"apiKey": {secret}})
if r["status"] != "ok" || len(r["artists"].(map[string]any)["index"].([]any)) != 0 {
t.Fatalf("artists of nothing: %v", r)
}
r = rest(t, h, "getGenres", url.Values{"apiKey": {secret}})
if r["status"] != "ok" {
t.Fatalf("genres of nothing: %v", r)
}
}