tercul-backend/internal/data/sql/auth_repository_test.go
Damir Mukimov d50722dad5
Some checks failed
Test / Integration Tests (push) Successful in 4s
Build / Build Binary (push) Failing after 2m9s
Docker Build / Build Docker Image (push) Failing after 2m32s
Test / Unit Tests (push) Failing after 3m12s
Lint / Go Lint (push) Failing after 1m0s
Refactor ID handling to use UUIDs across the application
- Updated database models and repositories to replace uint IDs with UUIDs.
- Modified test fixtures to generate and use UUIDs for authors, translations, users, and works.
- Adjusted mock implementations to align with the new UUID structure.
- Ensured all relevant functions and methods are updated to handle UUIDs correctly.
- Added necessary imports for UUID handling in various files.
2025-12-27 00:33:34 +01:00

92 lines
2.4 KiB
Go

package sql_test
import (
"context"
"tercul/internal/data/sql"
"tercul/internal/domain"
"tercul/internal/platform/config"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// newTestAuthRepoWithSQLite sets up an in-memory SQLite database for auth repository testing.
func newTestAuthRepoWithSQLite(t *testing.T) (domain.AuthRepository, *gorm.DB) {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
require.NoError(t, err)
// Auto-migrate the necessary schemas
err = db.AutoMigrate(
&domain.User{},
&domain.UserSession{},
)
require.NoError(t, err)
cfg := &config.Config{}
repo := sql.NewAuthRepository(db, cfg)
// Clean up the database after the test
t.Cleanup(func() {
sqlDB, err := db.DB()
require.NoError(t, err)
err = sqlDB.Close()
require.NoError(t, err)
})
return repo, db
}
func TestAuthRepository_StoreToken(t *testing.T) {
repo, db := newTestAuthRepoWithSQLite(t)
ctx := context.Background()
// Setup: Create a user
user := domain.User{Username: "authuser", Email: "auth@test.com"}
require.NoError(t, db.Create(&user).Error)
token := "my-secret-token"
expiresAt := time.Now().Add(1 * time.Hour)
err := repo.StoreToken(ctx, user.ID, token, expiresAt)
require.NoError(t, err)
var session domain.UserSession
err = db.Where("token = ?", token).First(&session).Error
require.NoError(t, err)
assert.Equal(t, user.ID, session.UserID)
assert.Equal(t, token, session.Token)
// Truncate to a reasonable precision for comparison
assert.WithinDuration(t, expiresAt, session.ExpiresAt, time.Second)
}
func TestAuthRepository_DeleteToken(t *testing.T) {
repo, db := newTestAuthRepoWithSQLite(t)
ctx := context.Background()
// Setup: Create a user and a session
user := domain.User{Username: "authuser2", Email: "auth2@test.com"}
require.NoError(t, db.Create(&user).Error)
token := "token-to-delete"
session := &domain.UserSession{
UserID: user.ID,
Token: token,
ExpiresAt: time.Now().Add(1 * time.Hour),
}
require.NoError(t, db.Create(session).Error)
// Delete the token
err := repo.DeleteToken(ctx, token)
require.NoError(t, err)
// Verify it's gone
var deletedSession domain.UserSession
err = db.Where("token = ?", token).First(&deletedSession).Error
assert.Error(t, err)
assert.Equal(t, gorm.ErrRecordNotFound, err)
}