74 lines
2.5 KiB
Go
74 lines
2.5 KiB
Go
|
|
package server
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/user-management-system/internal/cache"
|
||
|
|
"github.com/user-management-system/internal/config"
|
||
|
|
"github.com/user-management-system/internal/service"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestResolveJWTAccessTokenExpire_UsesExpireHourFallback(t *testing.T) {
|
||
|
|
cfg := &config.Config{}
|
||
|
|
cfg.JWT.ExpireHour = 24
|
||
|
|
cfg.JWT.AccessTokenExpireMinutes = 0
|
||
|
|
|
||
|
|
expire := resolveJWTAccessTokenExpire(cfg)
|
||
|
|
|
||
|
|
if expire != 24*time.Hour {
|
||
|
|
t.Fatalf("resolveJWTAccessTokenExpire() = %v, want %v", expire, 24*time.Hour)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestResolveJWTAccessTokenExpire_PrefersMinuteOverride(t *testing.T) {
|
||
|
|
cfg := &config.Config{}
|
||
|
|
cfg.JWT.ExpireHour = 24
|
||
|
|
cfg.JWT.AccessTokenExpireMinutes = 90
|
||
|
|
|
||
|
|
expire := resolveJWTAccessTokenExpire(cfg)
|
||
|
|
|
||
|
|
if expire != 90*time.Minute {
|
||
|
|
t.Fatalf("resolveJWTAccessTokenExpire() = %v, want %v", expire, 90*time.Minute)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestConfigureAuthEmailServices_UsesSMTPEnvironment(t *testing.T) {
|
||
|
|
t.Setenv("EMAIL_HOST", "127.0.0.1")
|
||
|
|
t.Setenv("EMAIL_PORT", "2525")
|
||
|
|
t.Setenv("EMAIL_FROM_EMAIL", "noreply@test.local")
|
||
|
|
t.Setenv("EMAIL_FROM_NAME", "UMS E2E")
|
||
|
|
t.Setenv("EMAIL_USER", "smtp-user")
|
||
|
|
t.Setenv("EMAIL_PASS", "smtp-pass")
|
||
|
|
|
||
|
|
cfg := &config.Config{}
|
||
|
|
cfg.Server.FrontendURL = "http://127.0.0.1:3000"
|
||
|
|
cfg.Log.ServiceName = "UMS E2E"
|
||
|
|
|
||
|
|
cacheManager := cache.NewCacheManager(cache.NewL1Cache(), cache.NewRedisCache(false))
|
||
|
|
authService := service.NewAuthService(nil, nil, nil, cacheManager, 8, 5, time.Minute)
|
||
|
|
passwordResetConfig := service.DefaultPasswordResetConfig()
|
||
|
|
|
||
|
|
if err := configureAuthEmailServices(cfg, cacheManager, authService, passwordResetConfig); err != nil {
|
||
|
|
t.Fatalf("configureAuthEmailServices() error = %v", err)
|
||
|
|
}
|
||
|
|
if !authService.SupportsEmailActivation() {
|
||
|
|
t.Fatal("SupportsEmailActivation() = false, want true")
|
||
|
|
}
|
||
|
|
if !authService.HasEmailCodeService() {
|
||
|
|
t.Fatal("HasEmailCodeService() = false, want true")
|
||
|
|
}
|
||
|
|
if passwordResetConfig.SMTPHost != "127.0.0.1" {
|
||
|
|
t.Fatalf("password reset SMTP host = %q, want %q", passwordResetConfig.SMTPHost, "127.0.0.1")
|
||
|
|
}
|
||
|
|
if passwordResetConfig.SMTPPort != 2525 {
|
||
|
|
t.Fatalf("password reset SMTP port = %d, want %d", passwordResetConfig.SMTPPort, 2525)
|
||
|
|
}
|
||
|
|
if passwordResetConfig.FromEmail != "noreply@test.local" {
|
||
|
|
t.Fatalf("password reset FromEmail = %q, want %q", passwordResetConfig.FromEmail, "noreply@test.local")
|
||
|
|
}
|
||
|
|
if passwordResetConfig.SiteURL != "http://127.0.0.1:3000" {
|
||
|
|
t.Fatalf("password reset SiteURL = %q, want %q", passwordResetConfig.SiteURL, "http://127.0.0.1:3000")
|
||
|
|
}
|
||
|
|
}
|