Registration, login and the admin's create-user call take `email` instead of `username`, and the user object returns `email`. The address is checked for its shape (a plain address, no display name, at most 254 bytes), trimmed, lowercased and unique. Sonda sends no mail and does not verify it. - Migration 0018 renames the users column (and the PostgreSQL index); values are kept, so an account made before keeps its plain name and still logs in with it (login does not check the shape). - Error code username_taken becomes email_taken; the JWT claim is now `email`, so access tokens issued before the upgrade are refused once and clients refresh. - OpenAPI spec, README, the web login form (type email), strings (en, it) and the generated API types follow. Closes #65
189 lines
7.5 KiB
Go
189 lines
7.5 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
func id(n int64) string { return strconv.FormatInt(n, 10) }
|
|
|
|
// gamma runs Discover once so that Last.fm's answer is stored and Gamma (an artist
|
|
// with no files) exists, and returns its id.
|
|
func (e *discoverEnv) gamma(t *testing.T) int64 {
|
|
t.Helper()
|
|
r := e.get(t, "?"+e.seed(e.alpha))
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("discover: %d %v", r.Code, r.Body)
|
|
}
|
|
return int64(itemFor(t, r, "Gamma")["artist"].(map[string]any)["id"].(float64))
|
|
}
|
|
|
|
func (e *discoverEnv) bob(t *testing.T) string {
|
|
t.Helper()
|
|
body := map[string]any{"email": "bob@example.org", "password": "longenough"}
|
|
if r := do(t, e.h, "POST", "/api/v1/users", e.admin, body); r.Code != http.StatusCreated {
|
|
t.Fatalf("create bob: %d %v", r.Code, r.Body)
|
|
}
|
|
tok, _ := do(t, e.h, "POST", "/api/v1/auth/login", "", body).Body["access_token"].(string)
|
|
return tok
|
|
}
|
|
|
|
func TestFeedbackRoutesNeedASignedInUser(t *testing.T) {
|
|
e := newDiscoverEnv(t)
|
|
for _, rt := range []struct{ method, path string }{
|
|
{"GET", "/api/v1/wishlist"}, {"PUT", "/api/v1/wishlist/1"}, {"DELETE", "/api/v1/wishlist/1"},
|
|
{"GET", "/api/v1/discover/dismissed"}, {"POST", "/api/v1/discover/1/dismiss"}, {"DELETE", "/api/v1/discover/1/dismiss"},
|
|
} {
|
|
if r := do(t, e.h, rt.method, rt.path, "", nil); r.Code != http.StatusUnauthorized {
|
|
t.Errorf("%s %s without a token: %d", rt.method, rt.path, r.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWishlist(t *testing.T) {
|
|
e := newDiscoverEnv(t)
|
|
e.enableLastfm(t)
|
|
gamma := e.gamma(t)
|
|
|
|
if r := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil); r.Code != http.StatusOK || len(r.Body["items"].([]any)) != 0 || r.Body["total"] != float64(0) {
|
|
t.Fatalf("empty wishlist: %d %v", r.Code, r.Body)
|
|
}
|
|
for i := 0; i < 2; i++ { // twice: the same answer
|
|
if r := do(t, e.h, "PUT", "/api/v1/wishlist/"+id(gamma), e.admin, nil); r.Code != http.StatusNoContent {
|
|
t.Fatalf("PUT %d: %d %v", i, r.Code, r.Body)
|
|
}
|
|
}
|
|
if r := do(t, e.h, "PUT", "/api/v1/wishlist/"+id(e.beta), e.admin, nil); r.Code != http.StatusNoContent {
|
|
t.Fatalf("PUT beta: %d %v", r.Code, r.Body)
|
|
}
|
|
r := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil)
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("%d %v", r.Code, r.Body)
|
|
}
|
|
sameKeys(t, "page", r.Body, "items", "total", "limit", "offset")
|
|
items := r.Body["items"].([]any)
|
|
if r.Body["total"] != float64(2) || len(items) != 2 || r.Body["limit"] != float64(50) || r.Body["offset"] != float64(0) {
|
|
t.Fatalf("wishlist = %v", r.Body)
|
|
}
|
|
first := items[0].(map[string]any)
|
|
sameKeys(t, "entry", first, "artist", "in_library", "marked_at")
|
|
sameKeys(t, "artist", first["artist"].(map[string]any), "id", "name")
|
|
byName := map[string]map[string]any{}
|
|
for _, it := range items {
|
|
m := it.(map[string]any)
|
|
byName[m["artist"].(map[string]any)["name"].(string)] = m
|
|
}
|
|
if byName["Gamma"]["in_library"] != false || byName["Beta"]["in_library"] != true {
|
|
t.Errorf("in_library: Gamma %v, Beta %v", byName["Gamma"]["in_library"], byName["Beta"]["in_library"])
|
|
}
|
|
|
|
// Discover says which suggestions are wished for.
|
|
d := e.get(t, "?"+e.seed(e.alpha))
|
|
if itemFor(t, d, "Gamma")["wishlisted"] != true || itemFor(t, d, "Beta")["wishlisted"] != true {
|
|
t.Errorf("wishlisted flags: %v", d.Body["items"])
|
|
}
|
|
|
|
// Take it off, twice.
|
|
for i := 0; i < 2; i++ {
|
|
if r := do(t, e.h, "DELETE", "/api/v1/wishlist/"+id(gamma), e.admin, nil); r.Code != http.StatusNoContent {
|
|
t.Fatalf("DELETE %d: %d %v", i, r.Code, r.Body)
|
|
}
|
|
}
|
|
if r := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil); r.Body["total"] != float64(1) {
|
|
t.Errorf("after DELETE: %v", r.Body)
|
|
}
|
|
if d = e.get(t, "?"+e.seed(e.alpha)); itemFor(t, d, "Gamma")["wishlisted"] != false {
|
|
t.Error("Gamma is still marked as wished")
|
|
}
|
|
}
|
|
|
|
func TestWishlistRefusesWhatIsNotAnArtistAndPagesStrictly(t *testing.T) {
|
|
e := newDiscoverEnv(t)
|
|
for _, rt := range []struct {
|
|
method, path string
|
|
code int
|
|
err string
|
|
}{
|
|
{"PUT", "/api/v1/wishlist/99999", 404, "artist_not_found"},
|
|
{"DELETE", "/api/v1/wishlist/99999", 404, "artist_not_found"},
|
|
{"PUT", "/api/v1/wishlist/abc", 400, "invalid_input"},
|
|
{"PUT", "/api/v1/wishlist/0", 400, "invalid_input"},
|
|
{"DELETE", "/api/v1/wishlist/-2", 400, "invalid_input"},
|
|
{"GET", "/api/v1/wishlist?limit=x", 400, "invalid_input"},
|
|
{"GET", "/api/v1/wishlist?limit=-1", 400, "invalid_input"},
|
|
{"GET", "/api/v1/wishlist?offset=-1", 400, "invalid_input"},
|
|
{"POST", "/api/v1/discover/99999/dismiss", 404, "artist_not_found"},
|
|
{"POST", "/api/v1/discover/abc/dismiss", 400, "invalid_input"},
|
|
{"DELETE", "/api/v1/discover/99999/dismiss", 404, "artist_not_found"},
|
|
{"GET", "/api/v1/discover/dismissed?limit=-1", 400, "invalid_input"},
|
|
} {
|
|
r := do(t, e.h, rt.method, rt.path, e.admin, nil)
|
|
if r.Code != rt.code || errCode(r) != rt.err {
|
|
t.Errorf("%s %s: %d %v, want %d %s", rt.method, rt.path, r.Code, r.Body, rt.code, rt.err)
|
|
}
|
|
}
|
|
if r := do(t, e.h, "GET", "/api/v1/wishlist?limit=100000", e.admin, nil); r.Code != http.StatusOK || r.Body["limit"] != float64(200) {
|
|
t.Errorf("a large limit is lowered: %d %v", r.Code, r.Body)
|
|
}
|
|
}
|
|
|
|
func TestDismissHidesAnArtistFromDiscoverAndCanBeUndone(t *testing.T) {
|
|
e := newDiscoverEnv(t)
|
|
e.enableLastfm(t)
|
|
gamma := e.gamma(t)
|
|
|
|
if r := do(t, e.h, "POST", "/api/v1/discover/"+id(gamma)+"/dismiss", e.admin, nil); r.Code != http.StatusNoContent {
|
|
t.Fatalf("dismiss: %d %v", r.Code, r.Body)
|
|
}
|
|
d := e.get(t, "?"+e.seed(e.alpha))
|
|
for _, it := range d.Body["items"].([]any) {
|
|
if it.(map[string]any)["artist"].(map[string]any)["name"] == "Gamma" {
|
|
t.Fatal("a dismissed artist is still suggested")
|
|
}
|
|
}
|
|
r := do(t, e.h, "GET", "/api/v1/discover/dismissed", e.admin, nil)
|
|
if r.Code != http.StatusOK || r.Body["total"] != float64(1) {
|
|
t.Fatalf("dismissed = %d %v", r.Code, r.Body)
|
|
}
|
|
if r.Body["items"].([]any)[0].(map[string]any)["artist"].(map[string]any)["name"] != "Gamma" {
|
|
t.Errorf("dismissed = %v", r.Body)
|
|
}
|
|
// It is not on the wishlist, and wishing for it replaces the dismissal.
|
|
if w := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil); w.Body["total"] != float64(0) {
|
|
t.Errorf("wishlist = %v", w.Body)
|
|
}
|
|
if r := do(t, e.h, "PUT", "/api/v1/wishlist/"+id(gamma), e.admin, nil); r.Code != http.StatusNoContent {
|
|
t.Fatalf("wish: %d", r.Code)
|
|
}
|
|
if r := do(t, e.h, "GET", "/api/v1/discover/dismissed", e.admin, nil); r.Body["total"] != float64(0) {
|
|
t.Errorf("a wish must replace the dismissal: %v", r.Body)
|
|
}
|
|
// Dismiss again, then undo it.
|
|
do(t, e.h, "POST", "/api/v1/discover/"+id(gamma)+"/dismiss", e.admin, nil)
|
|
if r := do(t, e.h, "DELETE", "/api/v1/discover/"+id(gamma)+"/dismiss", e.admin, nil); r.Code != http.StatusNoContent {
|
|
t.Fatalf("undo: %d", r.Code)
|
|
}
|
|
d = e.get(t, "?"+e.seed(e.alpha))
|
|
itemFor(t, d, "Gamma") // back
|
|
}
|
|
|
|
func TestFeedbackIsPrivate(t *testing.T) {
|
|
e := newDiscoverEnv(t)
|
|
e.enableLastfm(t)
|
|
gamma := e.gamma(t)
|
|
bob := e.bob(t)
|
|
do(t, e.h, "PUT", "/api/v1/wishlist/"+id(gamma), e.admin, nil)
|
|
do(t, e.h, "POST", "/api/v1/discover/"+id(e.beta)+"/dismiss", e.admin, nil)
|
|
|
|
if r := do(t, e.h, "GET", "/api/v1/wishlist", bob, nil); r.Body["total"] != float64(0) {
|
|
t.Errorf("bob sees alice's wishlist: %v", r.Body)
|
|
}
|
|
if r := do(t, e.h, "GET", "/api/v1/discover/dismissed", bob, nil); r.Body["total"] != float64(0) {
|
|
t.Errorf("bob sees alice's dismissals: %v", r.Body)
|
|
}
|
|
d := do(t, e.h, "GET", "/api/v1/discover?"+e.seed(e.alpha), bob, nil)
|
|
if d.Code != http.StatusOK || itemFor(t, d, "Beta") == nil || itemFor(t, d, "Gamma")["wishlisted"] != false {
|
|
t.Errorf("bob's Discover is affected by alice's choices: %v", d.Body)
|
|
}
|
|
}
|