A friend's server works out the peaks of a track in a shared library and gives
them to a signed friend (GET /peer/tracks/{id}/peaks). This server asks for them
and passes them on to the listener after checking them: 1 to 1000 levels from 0
to 255, as many as the count says (GET /friends/{friend}/tracks/{id}/peaks). The
player uses it for a friend's track, so its bar is no longer flat.
148 lines
5 KiB
Go
148 lines
5 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"codeberg.org/squidd/sonda/internal/federation"
|
|
"codeberg.org/squidd/sonda/internal/storage"
|
|
)
|
|
|
|
// realAudio puts a real MP3 where the shared library of the fixture keeps its song, so that its shape can be worked out.
|
|
func (f *peerFixture) realAudio(t *testing.T) {
|
|
t.Helper()
|
|
data, err := os.ReadFile(filepath.Join("..", "..", "waveform", "testdata", "ramp.mp3"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(f.root, "shared", "song.mp3"), data, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// untilPeaks asks again, as the page does, until the answer is not "working", and gives back the last one.
|
|
func untilPeaks(t *testing.T, ask func() resp) resp {
|
|
t.Helper()
|
|
for range 40 {
|
|
r := ask()
|
|
if r.Code != http.StatusAccepted {
|
|
return r
|
|
}
|
|
time.Sleep(250 * time.Millisecond)
|
|
}
|
|
t.Fatal("the shape was never ready")
|
|
return resp{}
|
|
}
|
|
|
|
func TestAFriendReadsThePeaksOfASharedTrackOnly(t *testing.T) {
|
|
f := newPeerFixture(t)
|
|
f.befriend(t)
|
|
f.share(t, f.shared, true)
|
|
shared := strconv.FormatInt(f.track["Shared Song"], 10)
|
|
private := strconv.FormatInt(f.track["Private Song"], 10)
|
|
|
|
// The files of the fixture are not audio: the friend is told so, in words.
|
|
r := f.get(t, "/tracks/"+shared+"/peaks")
|
|
if r.Code != 202 && !(r.Code == 404 && errCode(r) == "waveform_unavailable") {
|
|
t.Fatalf("a file that is not audio: %d %v", r.Code, r.Body)
|
|
}
|
|
r = untilPeaks(t, func() resp { return f.get(t, "/tracks/"+shared+"/peaks") })
|
|
if r.Code != 404 || errCode(r) != "waveform_unavailable" {
|
|
t.Errorf("a file that is not audio: %d %v", r.Code, r.Body)
|
|
}
|
|
|
|
f.realAudio(t)
|
|
r = untilPeaks(t, func() resp { return f.get(t, "/tracks/"+shared+"/peaks") })
|
|
if r.Code != 200 {
|
|
t.Fatalf("a shared track: %d %v", r.Code, r.Body)
|
|
}
|
|
sameKeys(t, "peaks", r.Body, "count", "peaks")
|
|
if r.Body["count"] != float64(120) || len(r.Body["peaks"].([]any)) != 120 {
|
|
t.Errorf("count = %v", r.Body["count"])
|
|
}
|
|
|
|
if r := f.get(t, "/tracks/"+private+"/peaks"); r.Code != 404 || errCode(r) != "track_not_found" {
|
|
t.Errorf("a track that is not shared: %d %v", r.Code, r.Body)
|
|
}
|
|
if rec := f.serve(f.signed("GET", "/tracks/"+shared+"/peaks", func(r *http.Request) { r.Header.Del(federation.HeaderSignature) })); rec.Code != 401 {
|
|
t.Errorf("without a signature: %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestAListenerSeesThePeaksOfAFriendsTrackThroughThisServer(t *testing.T) {
|
|
f := newBrowseFixture(t)
|
|
track := f.sharedTrack(t)
|
|
path := "/friends/" + f.friendID + "/tracks/" + track + "/peaks"
|
|
|
|
if rec := f.listen(path, func(r *http.Request) { r.Header.Del("Authorization") }); rec.Code != 401 {
|
|
t.Errorf("without a login: %d", rec.Code)
|
|
}
|
|
r := untilPeaks(t, func() resp { return f.get(t, path) })
|
|
if r.Code != 404 || errCode(r) != "waveform_unavailable" {
|
|
t.Errorf("a file that is not audio: %d %v", r.Code, r.Body)
|
|
}
|
|
|
|
f.p.realAudio(t)
|
|
r = untilPeaks(t, func() resp { return f.get(t, path) })
|
|
if r.Code != 200 {
|
|
t.Fatalf("peaks: %d %v", r.Code, r.Body)
|
|
}
|
|
sameKeys(t, "peaks", r.Body, "count", "peaks")
|
|
if r.Body["count"] != float64(120) {
|
|
t.Errorf("count = %v", r.Body["count"])
|
|
}
|
|
if r := f.get(t, "/friends/"+f.friendID+"/tracks/999999/peaks"); r.Code != 404 || errCode(r) != "track_not_found" {
|
|
t.Errorf("a track the friend does not have: %d %v", r.Code, r.Body)
|
|
}
|
|
if r := f.get(t, "/friends/999/tracks/"+track+"/peaks"); r.Code != 404 || errCode(r) != "friend_not_found" {
|
|
t.Errorf("a friend that is not there: %d %v", r.Code, r.Body)
|
|
}
|
|
}
|
|
|
|
func TestWhatAFriendSendsAsPeaksIsCheckedBeforeItIsPassedOn(t *testing.T) {
|
|
for name, tc := range map[string]struct {
|
|
body string
|
|
ok bool
|
|
}{
|
|
"good, with something added": {`{"count":3,"peaks":[0,128,255],"path":"/srv/music/x"}`, true},
|
|
"the count is not the length": {`{"count":4,"peaks":[0,128,255]}`, false},
|
|
"a level over 255": {`{"count":1,"peaks":[256]}`, false},
|
|
"a level under 0": {`{"count":1,"peaks":[-1]}`, false},
|
|
"no levels": {`{"count":0,"peaks":[]}`, false},
|
|
"far too many": {`{"count":1,"peaks":[` + manyLevels(5000) + `]}`, false},
|
|
"html": {`<html>hello</html>`, false},
|
|
"empty": {``, false},
|
|
} {
|
|
h, _ := newTestEnvDB(t, func(d *Deps, db *bun.DB) {
|
|
d.Federation = crafted{federationService: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()), body: tc.body}
|
|
})
|
|
tok := registerAndLogin(t, h)
|
|
r := do(t, h, "GET", "/api/v1/friends/1/tracks/1/peaks", tok, nil)
|
|
if tc.ok {
|
|
if r.Code != 200 {
|
|
t.Errorf("%s: %d %v", name, r.Code, r.Body)
|
|
continue
|
|
}
|
|
sameKeys(t, name, r.Body, "count", "peaks")
|
|
} else if r.Code != http.StatusBadGateway || errCode(r) != "friend_unreachable" {
|
|
t.Errorf("%s: %d %v", name, r.Code, r.Body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func manyLevels(n int) string {
|
|
out := make([]byte, 0, n*2)
|
|
for i := range n {
|
|
if i > 0 {
|
|
out = append(out, ',')
|
|
}
|
|
out = append(out, '1')
|
|
}
|
|
return string(out)
|
|
}
|