first commit
This commit is contained in:
56
internal/user/repo_test.go
Normal file
56
internal/user/repo_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/noderunners/nip05api/internal/db"
|
||||
)
|
||||
|
||||
func newTestRepo(t *testing.T) *Repo {
|
||||
t.Helper()
|
||||
d, err := db.Open(filepath.Join(t.TempDir(), "test.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { d.Close() })
|
||||
if err := d.Migrate(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return NewRepo(d)
|
||||
}
|
||||
|
||||
func TestRepo_InsertAndFetch(t *testing.T) {
|
||||
repo := newTestRepo(t)
|
||||
ctx := context.Background()
|
||||
exp := time.Now().Add(365 * 24 * time.Hour)
|
||||
u := &User{
|
||||
Pubkey: "0e8c41ebcd55a8d8db2e0a8c3a4b9c5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c",
|
||||
Username: "alice",
|
||||
SubscriptionType: SubYearly,
|
||||
ExpiresAt: &exp,
|
||||
IsActive: true,
|
||||
}
|
||||
if err := repo.Insert(ctx, u); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
got, err := repo.GetByPubkey(ctx, u.Pubkey)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if got.Username != "alice" {
|
||||
t.Errorf("got username %q", got.Username)
|
||||
}
|
||||
if !got.IsActive {
|
||||
t.Error("expected active")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepo_GetByUsername_NotFound(t *testing.T) {
|
||||
repo := newTestRepo(t)
|
||||
if _, err := repo.GetByUsername(context.Background(), "nope"); err != ErrUserNotFound {
|
||||
t.Errorf("got %v want ErrUserNotFound", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user