- dev auth flow (side-step OAuth) - db event processing integration tests - dev scripts (eg Makefile) - db / test db migration setup scripts.
134 lines
4.0 KiB
Go
134 lines
4.0 KiB
Go
package accounts_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"ruben/inventory2/consts"
|
|
"ruben/inventory2/domains/accounts"
|
|
"ruben/inventory2/internal/testdb"
|
|
)
|
|
|
|
func TestCreateAccount(t *testing.T) {
|
|
pool := testdb.Pool(t)
|
|
store := accounts.NewStore(testdb.Logger(), pool)
|
|
ctx := context.Background()
|
|
|
|
userID := testdb.NewUserID(t)
|
|
testdb.SeedOAuthUser(t, pool, userID)
|
|
|
|
email := userID + "@example.com"
|
|
|
|
acct, err := store.CreateAccount(ctx, userID, email)
|
|
if err != nil {
|
|
t.Fatalf("CreateAccount() error = %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
pool.Exec(context.Background(), "DELETE FROM accounts WHERE account_id = $1", acct.AccountID)
|
|
})
|
|
|
|
if acct.AccountID == 0 {
|
|
t.Error("CreateAccount() returned a zero AccountID")
|
|
}
|
|
if acct.UserID != userID {
|
|
t.Errorf("CreateAccount() UserID = %q, want %q", acct.UserID, userID)
|
|
}
|
|
if acct.Email != email {
|
|
t.Errorf("CreateAccount() Email = %q, want %q", acct.Email, email)
|
|
}
|
|
|
|
got, err := store.GetAccount(ctx, acct.AccountID)
|
|
if err != nil {
|
|
t.Fatalf("GetAccount() error = %v", err)
|
|
}
|
|
if got != acct {
|
|
t.Errorf("GetAccount() = %+v, want %+v", got, acct)
|
|
}
|
|
}
|
|
|
|
func TestCreateAccount_DuplicateUserIsConflict(t *testing.T) {
|
|
pool := testdb.Pool(t)
|
|
store := accounts.NewStore(testdb.Logger(), pool)
|
|
ctx := context.Background()
|
|
|
|
userID := testdb.NewUserID(t)
|
|
testdb.SeedOAuthUser(t, pool, userID)
|
|
|
|
acct, err := store.CreateAccount(ctx, userID, userID+"@example.com")
|
|
if err != nil {
|
|
t.Fatalf("first CreateAccount() error = %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
pool.Exec(context.Background(), "DELETE FROM accounts WHERE account_id = $1", acct.AccountID)
|
|
})
|
|
|
|
_, err = store.CreateAccount(ctx, userID, userID+"-other@example.com")
|
|
if !errors.Is(err, consts.ErrConflict) {
|
|
t.Fatalf("second CreateAccount() error = %v, want %v", err, consts.ErrConflict)
|
|
}
|
|
}
|
|
|
|
func TestGetAccount_NotFound(t *testing.T) {
|
|
pool := testdb.Pool(t)
|
|
store := accounts.NewStore(testdb.Logger(), pool)
|
|
ctx := context.Background()
|
|
|
|
_, err := store.GetAccount(ctx, -1)
|
|
if !errors.Is(err, consts.ErrNotFound) {
|
|
t.Fatalf("GetAccount() error = %v, want %v", err, consts.ErrNotFound)
|
|
}
|
|
}
|
|
|
|
func TestGetUserAndAccountByAccessToken(t *testing.T) {
|
|
pool := testdb.Pool(t)
|
|
store := accounts.NewStore(testdb.Logger(), pool)
|
|
ctx := context.Background()
|
|
|
|
userID := testdb.NewUserID(t)
|
|
accessToken := testdb.SeedOAuthSession(t, pool, userID)
|
|
|
|
// before an account exists: user resolves, account does not.
|
|
user, acct, err := store.GetUserAndAccountByAccessToken(ctx, accessToken)
|
|
if err != nil {
|
|
t.Fatalf("GetUserAndAccountByAccessToken() before account creation: error = %v", err)
|
|
}
|
|
if user.UserID != userID {
|
|
t.Errorf("GetUserAndAccountByAccessToken() UserID = %q, want %q", user.UserID, userID)
|
|
}
|
|
if acct != nil {
|
|
t.Errorf("GetUserAndAccountByAccessToken() Account = %+v, want nil before an account is created", acct)
|
|
}
|
|
|
|
created, err := store.CreateAccount(ctx, userID, userID+"@example.com")
|
|
if err != nil {
|
|
t.Fatalf("CreateAccount() error = %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
pool.Exec(context.Background(), "DELETE FROM accounts WHERE account_id = $1", created.AccountID)
|
|
})
|
|
|
|
// after an account exists: both resolve.
|
|
user, acct, err = store.GetUserAndAccountByAccessToken(ctx, accessToken)
|
|
if err != nil {
|
|
t.Fatalf("GetUserAndAccountByAccessToken() after account creation: error = %v", err)
|
|
}
|
|
if user.UserID != userID {
|
|
t.Errorf("GetUserAndAccountByAccessToken() UserID = %q, want %q", user.UserID, userID)
|
|
}
|
|
if acct == nil || acct.AccountID != created.AccountID {
|
|
t.Errorf("GetUserAndAccountByAccessToken() Account = %+v, want AccountID %d", acct, created.AccountID)
|
|
}
|
|
}
|
|
|
|
func TestGetUserAndAccountByAccessToken_UnknownToken(t *testing.T) {
|
|
pool := testdb.Pool(t)
|
|
store := accounts.NewStore(testdb.Logger(), pool)
|
|
ctx := context.Background()
|
|
|
|
_, _, err := store.GetUserAndAccountByAccessToken(ctx, "no-such-token-"+testdb.NewUserID(t))
|
|
if !errors.Is(err, consts.ErrNotFound) {
|
|
t.Fatalf("GetUserAndAccountByAccessToken() error = %v, want %v", err, consts.ErrNotFound)
|
|
}
|
|
}
|