Files
wehub-resource-sync 75f3dd141c
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CI and Release / lint-frontend (push) Has been cancelled
CI and Release / dockerfile-scan (push) Has been cancelled
CI and Release / test-frontend (push) Has been cancelled
CI and Release / lint-verification-agent (push) Has been cancelled
CI and Release / test-verification-agent (push) Has been cancelled
CI and Release / e2e-verification-agent (push) Has been cancelled
CI and Release / test-backend (push) Has been cancelled
CI and Release / build-dev-image (push) Has been cancelled
CI and Release / push-dev-image (push) Has been cancelled
CI and Release / build-image (push) Has been cancelled
CI and Release / build-verification-image (push) Has been cancelled
CI and Release / determine-version (push) Has been cancelled
CI and Release / push-image (push) Has been cancelled
CI and Release / push-verification-image (12) (push) Has been cancelled
CI and Release / lint-backend (push) Has been cancelled
CI and Release / push-verification-image (17) (push) Has been cancelled
CI and Release / push-verification-image (18) (push) Has been cancelled
CI and Release / release (push) Has been cancelled
CI and Release / publish-helm-chart (push) Has been cancelled
CI and Release / push-verification-image (13) (push) Has been cancelled
CI and Release / push-verification-image (14) (push) Has been cancelled
CI and Release / push-verification-image (15) (push) Has been cancelled
CI and Release / push-verification-image (16) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:11:07 +08:00

79 lines
1.9 KiB
Go

package users_testing
import (
"fmt"
"strings"
"time"
"github.com/google/uuid"
users_dto "databasus-backend/internal/features/users/dto"
users_enums "databasus-backend/internal/features/users/enums"
users_models "databasus-backend/internal/features/users/models"
users_repositories "databasus-backend/internal/features/users/repositories"
users_services "databasus-backend/internal/features/users/services"
)
func CreateTestUser(role users_enums.UserRole) *users_dto.SignInResponseDTO {
userID := uuid.New()
email := fmt.Sprintf("%s-%s@test.com", strings.ToLower(string(role)), userID.String()[:8])
hashedPassword := "$2a$10$test"
user := &users_models.User{
ID: userID,
Email: email,
Name: "Test User",
HashedPassword: &hashedPassword,
PasswordCreationTime: time.Now().UTC(),
CreatedAt: time.Now().UTC(),
Role: role,
Status: users_enums.UserStatusActive,
}
userRepository := &users_repositories.UserRepository{}
err := userRepository.CreateUser(user)
if err != nil {
panic(err)
}
response, err := users_services.GetUserService().GenerateAccessToken(user)
if err != nil {
panic(err)
}
response.Email = user.Email
return response
}
func ReacreateInitAdminAndGetAccess() *users_dto.SignInResponseDTO {
RecreateInitialAdmin()
userRepository := &users_repositories.UserRepository{}
user, err := userRepository.GetUserByEmail("admin")
if err != nil {
panic(err)
}
response, err := users_services.GetUserService().GenerateAccessToken(user)
if err != nil {
panic(err)
}
return response
}
func RecreateInitialAdmin() {
userRepository := &users_repositories.UserRepository{}
err := userRepository.RenameUserEmailForTests("admin", "admin-"+uuid.New().String())
if err != nil {
panic(err)
}
userService := users_services.GetUserService()
err = userService.CreateInitialAdmin()
if err != nil {
panic(err)
}
}