tercul-backend/test/fixtures/users.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

133 lines
3.5 KiB
Go

package fixtures
import (
"context"
"tercul/internal/domain"
"time"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
// User fixtures for testing
var (
// AdminUser is a fixture for an admin user
AdminUser = domain.User{
BaseModel: domain.BaseModel{
ID: uuid.MustParse("550e8400-e29b-41d4-a716-446655440101"),
CreatedAt: time.Now().Add(-30 * 24 * time.Hour),
UpdatedAt: time.Now().Add(-1 * 24 * time.Hour),
},
Username: "admin",
Email: "admin@tercul.com",
Password: hashPassword("admin123"),
FirstName: "Admin",
LastName: "User",
DisplayName: "Admin",
Bio: "Platform administrator",
Role: domain.UserRoleAdmin,
Verified: true,
Active: true,
}
// EditorUser is a fixture for an editor user
EditorUser = domain.User{
BaseModel: domain.BaseModel{
ID: uuid.MustParse("550e8400-e29b-41d4-a716-446655440102"),
CreatedAt: time.Now().Add(-20 * 24 * time.Hour),
UpdatedAt: time.Now().Add(-2 * 24 * time.Hour),
},
Username: "editor",
Email: "editor@tercul.com",
Password: hashPassword("editor123"),
FirstName: "Editor",
LastName: "User",
DisplayName: "Editor",
Bio: "Content editor",
Role: domain.UserRoleEditor,
Verified: true,
Active: true,
}
// ContributorUser is a fixture for a contributor user
ContributorUser = domain.User{
BaseModel: domain.BaseModel{
ID: uuid.MustParse("550e8400-e29b-41d4-a716-446655440103"),
CreatedAt: time.Now().Add(-15 * 24 * time.Hour),
UpdatedAt: time.Now().Add(-1 * time.Hour),
},
Username: "contributor",
Email: "contributor@tercul.com",
Password: hashPassword("contributor123"),
FirstName: "Contributor",
LastName: "User",
DisplayName: "Contributor",
Bio: "Active contributor",
Role: domain.UserRoleContributor,
Verified: true,
Active: true,
}
// ReaderUser is a fixture for a reader user
ReaderUser = domain.User{
BaseModel: domain.BaseModel{
ID: uuid.MustParse("550e8400-e29b-41d4-a716-446655440104"),
CreatedAt: time.Now().Add(-10 * 24 * time.Hour),
UpdatedAt: time.Now(),
},
Username: "reader",
Email: "reader@tercul.com",
Password: hashPassword("reader123"),
FirstName: "Reader",
LastName: "User",
DisplayName: "Reader",
Bio: "Book enthusiast",
Role: domain.UserRoleReader,
Verified: true,
Active: true,
}
// InactiveUser is a fixture for an inactive user
InactiveUser = domain.User{
BaseModel: domain.BaseModel{
ID: uuid.MustParse("550e8400-e29b-41d4-a716-446655440105"),
CreatedAt: time.Now().Add(-5 * 24 * time.Hour),
UpdatedAt: time.Now().Add(-3 * 24 * time.Hour),
},
Username: "inactive",
Email: "inactive@tercul.com",
Password: hashPassword("inactive123"),
Role: domain.UserRoleReader,
Verified: false,
Active: false,
}
)
// AllUsers returns all user fixtures
func AllUsers() []domain.User {
return []domain.User{
AdminUser,
EditorUser,
ContributorUser,
ReaderUser,
InactiveUser,
}
}
// hashPassword hashes a password for testing
func hashPassword(password string) string {
hash, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(hash)
}
// LoadUsers loads user fixtures into the database
func LoadUsers(ctx context.Context, db *gorm.DB) error {
for _, user := range AllUsers() {
if err := db.Create(&user).Error; err != nil {
return err
}
}
return nil
}