package accounts_test import ( "context" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "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) require.NoError(t, err, "CreateAccount()") t.Cleanup(func() { pool.Exec(context.Background(), "DELETE FROM accounts WHERE account_id = $1", acct.AccountID) }) assert.NotZero(t, acct.AccountID, "CreateAccount() returned a zero AccountID") assert.Equal(t, userID, acct.UserID, "CreateAccount() UserID") assert.Equal(t, email, acct.Email, "CreateAccount() Email") got, err := store.GetAccount(ctx, acct.AccountID) require.NoError(t, err, "GetAccount()") assert.Equal(t, acct, got, "GetAccount()") } 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") require.NoError(t, err, "first CreateAccount()") 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") require.ErrorIs(t, err, consts.ErrConflict, "second CreateAccount()") } func TestGetAccount_NotFound(t *testing.T) { pool := testdb.Pool(t) store := accounts.NewStore(testdb.Logger(), pool) ctx := context.Background() _, err := store.GetAccount(ctx, -1) require.ErrorIs(t, err, consts.ErrNotFound, "GetAccount()") } 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) require.NoError(t, err, "GetUserAndAccountByAccessToken() before account creation") assert.Equal(t, userID, user.UserID, "GetUserAndAccountByAccessToken() UserID") assert.Nil(t, acct, "GetUserAndAccountByAccessToken() Account should be nil before an account is created") created, err := store.CreateAccount(ctx, userID, userID+"@example.com") require.NoError(t, err, "CreateAccount()") 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) require.NoError(t, err, "GetUserAndAccountByAccessToken() after account creation") assert.Equal(t, userID, user.UserID, "GetUserAndAccountByAccessToken() UserID") if assert.NotNil(t, acct, "GetUserAndAccountByAccessToken() Account should be set after an account is created") { assert.Equal(t, created.AccountID, acct.AccountID, "GetUserAndAccountByAccessToken() Account.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)) require.ErrorIs(t, err, consts.ErrNotFound, "GetUserAndAccountByAccessToken()") }