Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
145 lines
5.4 KiB
Go
145 lines
5.4 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/mastodon"
|
|
)
|
|
|
|
func seedUser(t *testing.T, db *bun.DB, email string) int64 {
|
|
t.Helper()
|
|
var id int64
|
|
if err := db.QueryRow("INSERT INTO users (email, password_hash, is_admin, created_at) VALUES (?, 'x', ?, 0) RETURNING id", email, false).Scan(&id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
func TestAMastodonAccountIsKeptReplacedAndForgotten(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewMastodonRepo(db)
|
|
uid := seedUser(t, db, "a@example.org")
|
|
if _, found, err := repo.Get(ctx, uid); err != nil || found {
|
|
t.Fatalf("before any save: found %v, %v", found, err)
|
|
}
|
|
first := mastodon.Credentials{Instance: "https://mastodon.social", Token: "one", Handle: "@a@mastodon.social"}
|
|
if err := repo.Save(ctx, uid, first); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, found, err := repo.Get(ctx, uid); err != nil || !found || got != first {
|
|
t.Fatalf("Get = %+v, %v, %v", got, found, err)
|
|
}
|
|
second := mastodon.Credentials{Instance: "https://mastodon.uno", Token: "two", Handle: "@a@mastodon.uno"}
|
|
if err := repo.Save(ctx, uid, second); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _, _ := repo.Get(ctx, uid); got != second {
|
|
t.Errorf("after the second save: %+v", got)
|
|
}
|
|
if err := repo.Delete(ctx, uid); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, found, _ := repo.Get(ctx, uid); found {
|
|
t.Error("still kept after Delete")
|
|
}
|
|
if err := repo.Delete(ctx, uid); err != nil {
|
|
t.Errorf("deleting nothing is not an error: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestAMastodonAccountBelongsToOnePersonAndGoesWithThem(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewMastodonRepo(db)
|
|
a, b := seedUser(t, db, "a@example.org"), seedUser(t, db, "b@example.org")
|
|
_ = repo.Save(ctx, a, mastodon.Credentials{Instance: "https://mastodon.social", Token: "a", Handle: "@a@x"})
|
|
if _, found, _ := repo.Get(ctx, b); found {
|
|
t.Error("somebody else's account was found")
|
|
}
|
|
if _, err := db.Exec("DELETE FROM users WHERE id = ?", a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, found, _ := repo.Get(ctx, a); found {
|
|
t.Error("the account of a deleted person is still kept")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTheTrackOfAPostHasItsTitleItsMainArtistsAndItsAlbum(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewMastodonRepo(db)
|
|
var artist1, artist2, feat, album, track, bare int64
|
|
for name, dst := range map[string]*int64{"Nora Valdés": &artist1, "Teo": &artist2, "Guest": &feat} {
|
|
if err := db.QueryRow("INSERT INTO artists (name) VALUES (?) RETURNING id", name).Scan(dst); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := db.QueryRow("INSERT INTO albums (title) VALUES ('Luce Sommersa') RETURNING id").Scan(&album); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.QueryRow("INSERT INTO tracks (title, album_id) VALUES ('Sonar', ?) RETURNING id", album).Scan(&track); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.QueryRow("INSERT INTO tracks (title) VALUES ('Bare') RETURNING id").Scan(&bare); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, a := range []struct {
|
|
id int64
|
|
role string
|
|
}{{artist1, "main"}, {artist2, "main"}, {feat, "featured"}} {
|
|
if _, err := db.Exec("INSERT INTO track_artists (track_id, artist_id, role) VALUES (?, ?, ?)", track, a.id, a.role); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
got, found, err := repo.Track(ctx, track)
|
|
if err != nil || !found {
|
|
t.Fatalf("Track = %+v, %v, %v", got, found, err)
|
|
}
|
|
if got.Title != "Sonar" || got.Album != "Luce Sommersa" || len(got.Artists) != 2 || got.Artists[0] != "Nora Valdés" && got.Artists[1] != "Nora Valdés" {
|
|
t.Errorf("track = %+v", got)
|
|
}
|
|
for _, name := range got.Artists {
|
|
if name == "Guest" {
|
|
t.Error("a featured artist is not one of the main ones")
|
|
}
|
|
}
|
|
if got, found, _ := repo.Track(ctx, bare); !found || got.Title != "Bare" || got.Album != "" || len(got.Artists) != 0 {
|
|
t.Errorf("a track with no album and no artist: %+v, %v", got, found)
|
|
}
|
|
if _, found, err := repo.Track(ctx, 99999); err != nil || found {
|
|
t.Errorf("unknown track: found %v, %v", found, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestAProblemWithTheTokenIsKeptUntilTheAccountIsSavedAgain(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewMastodonRepo(db)
|
|
uid := seedUser(t, db, "a@example.org")
|
|
if err := repo.SetProblem(ctx, uid, mastodon.ProblemTokenRefused); err != nil {
|
|
t.Fatalf("a person with no account is not an error: %v", err)
|
|
}
|
|
_ = repo.Save(ctx, uid, mastodon.Credentials{Instance: "https://mastodon.social", Token: "one", Handle: "@a@x"})
|
|
if got, _, _ := repo.Get(ctx, uid); got.Problem != "" {
|
|
t.Fatalf("a new account starts with no problem: %q", got.Problem)
|
|
}
|
|
if err := repo.SetProblem(ctx, uid, mastodon.ProblemTokenRefused); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _, _ := repo.Get(ctx, uid)
|
|
if got.Problem != mastodon.ProblemTokenRefused || got.Token != "one" {
|
|
t.Fatalf("after SetProblem: %+v", got)
|
|
}
|
|
_ = repo.Save(ctx, uid, mastodon.Credentials{Instance: "https://mastodon.social", Token: "two", Handle: "@a@x"})
|
|
if got, _, _ := repo.Get(ctx, uid); got.Problem != "" || got.Token != "two" {
|
|
t.Errorf("saving again clears the problem: %+v", got)
|
|
}
|
|
})
|
|
}
|