139 lines
4.7 KiB
Go
139 lines
4.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// setupAccountExtensionTestDB uses the shared setupTestDB helper.
|
|
// Account, User, AccountUser are already in defaultTestModels.
|
|
func setupAccountExtensionTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
return setupTestDB(t)
|
|
}
|
|
|
|
// createAccountExtTestAccount creates a minimal Account row.
|
|
func createAccountExtTestAccount(t *testing.T, db *gorm.DB) *model.Account {
|
|
t.Helper()
|
|
account := &model.Account{Name: "Account Extension Test Account"}
|
|
err := db.Create(account).Error
|
|
require.NoError(t, err, "failed to create test account")
|
|
return account
|
|
}
|
|
|
|
// createAccountExtTestUser creates a minimal User row.
|
|
func createAccountExtTestUser(t *testing.T, db *gorm.DB, accountID uint) *model.User {
|
|
t.Helper()
|
|
user := &model.User{Name: "Account Ext Test User", Email: "acct-ext-test@example.com", Password: "hashedpw", AccountID: accountID}
|
|
err := db.Create(user).Error
|
|
require.NoError(t, err, "failed to create test user")
|
|
return user
|
|
}
|
|
|
|
// createAccountExtTestAccountUser links a user to an account.
|
|
func createAccountExtTestAccountUser(t *testing.T, db *gorm.DB, accountID, userID uint) *model.AccountUser {
|
|
t.Helper()
|
|
au := &model.AccountUser{AccountID: accountID, UserID: userID, Role: "agent"}
|
|
err := db.Create(au).Error
|
|
require.NoError(t, err, "failed to create test account_user")
|
|
return au
|
|
}
|
|
|
|
// ===========================================================================
|
|
// UpdateActiveAt tests
|
|
// ===========================================================================
|
|
|
|
func TestAccountRepo_UpdateActiveAt(t *testing.T) {
|
|
db := setupAccountExtensionTestDB(t)
|
|
ctx := context.Background()
|
|
repo := NewAccountRepo(db)
|
|
|
|
account := createAccountExtTestAccount(t, db)
|
|
user := createAccountExtTestUser(t, db, account.ID)
|
|
au := createAccountExtTestAccountUser(t, db, account.ID, user.ID)
|
|
|
|
// Verify active_at is nil initially
|
|
assert.Nil(t, au.ActiveAt)
|
|
|
|
activeAt := time.Now()
|
|
err := repo.UpdateActiveAt(ctx, account.ID, user.ID, activeAt)
|
|
require.NoError(t, err)
|
|
|
|
// Verify active_at was updated
|
|
var refreshed model.AccountUser
|
|
require.NoError(t, db.Where("account_id = ? AND user_id = ?", account.ID, user.ID).First(&refreshed).Error)
|
|
assert.NotNil(t, refreshed.ActiveAt)
|
|
assert.WithinDuration(t, activeAt, *refreshed.ActiveAt, time.Second)
|
|
}
|
|
|
|
func TestAccountRepo_UpdateActiveAt_NotFound(t *testing.T) {
|
|
db := setupAccountExtensionTestDB(t)
|
|
ctx := context.Background()
|
|
repo := NewAccountRepo(db)
|
|
|
|
err := repo.UpdateActiveAt(ctx, 9999, 9999, time.Now())
|
|
// Should return ErrRecordNotFound when no matching account_user
|
|
assert.ErrorIs(t, err, gorm.ErrRecordNotFound)
|
|
}
|
|
|
|
func TestAccountRepo_UpdateActiveAt_OverwritesPrevious(t *testing.T) {
|
|
db := setupAccountExtensionTestDB(t)
|
|
ctx := context.Background()
|
|
repo := NewAccountRepo(db)
|
|
|
|
account := createAccountExtTestAccount(t, db)
|
|
user := createAccountExtTestUser(t, db, account.ID)
|
|
createAccountExtTestAccountUser(t, db, account.ID, user.ID)
|
|
|
|
// Set active_at once
|
|
firstTime := time.Now().Add(-1 * time.Hour)
|
|
err := repo.UpdateActiveAt(ctx, account.ID, user.ID, firstTime)
|
|
require.NoError(t, err)
|
|
|
|
// Set active_at again with a new timestamp
|
|
secondTime := time.Now()
|
|
err = repo.UpdateActiveAt(ctx, account.ID, user.ID, secondTime)
|
|
require.NoError(t, err)
|
|
|
|
// Verify the timestamp was overwritten
|
|
var refreshed model.AccountUser
|
|
require.NoError(t, db.Where("account_id = ? AND user_id = ?", account.ID, user.ID).First(&refreshed).Error)
|
|
assert.NotNil(t, refreshed.ActiveAt)
|
|
assert.WithinDuration(t, secondTime, *refreshed.ActiveAt, time.Second)
|
|
}
|
|
|
|
// ===========================================================================
|
|
// FindAccountUserByUserAndAccount tests
|
|
// ===========================================================================
|
|
|
|
func TestAccountRepo_FindAccountUserByUserAndAccount(t *testing.T) {
|
|
db := setupAccountExtensionTestDB(t)
|
|
ctx := context.Background()
|
|
repo := NewAccountRepo(db)
|
|
|
|
account := createAccountExtTestAccount(t, db)
|
|
user := createAccountExtTestUser(t, db, account.ID)
|
|
au := createAccountExtTestAccountUser(t, db, account.ID, user.ID)
|
|
|
|
found, err := repo.FindAccountUserByUserAndAccount(ctx, account.ID, user.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, au.ID, found.ID)
|
|
assert.Equal(t, account.ID, found.AccountID)
|
|
assert.Equal(t, user.ID, found.UserID)
|
|
}
|
|
|
|
func TestAccountRepo_FindAccountUserByUserAndAccount_NotFound(t *testing.T) {
|
|
db := setupAccountExtensionTestDB(t)
|
|
ctx := context.Background()
|
|
repo := NewAccountRepo(db)
|
|
|
|
_, err := repo.FindAccountUserByUserAndAccount(ctx, 9999, 9999)
|
|
assert.ErrorIs(t, err, gorm.ErrRecordNotFound)
|
|
} |