Files

126 lines
4.4 KiB
Go

package service
import (
"context"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/phishingclub/phishingclub/data"
"github.com/phishingclub/phishingclub/model"
"github.com/phishingclub/phishingclub/vo"
)
func TestAuditOptionValueRedactsSSOSecret(t *testing.T) {
secret := "super-secret-value-123"
value := `{"enabled":true,"providerType":"entraid","clientID":"cid","tenantID":"tid","clientSecret":"` + secret + `","redirectURL":"https://example.test/cb"}`
got := auditOptionValue(data.OptionKeyAdminSSOLogin, value)
if strings.Contains(got, secret) {
t.Fatalf("audit value contains the client secret: %s", got)
}
if !strings.Contains(got, "[redacted]") {
t.Fatalf("audit value should mark the secret as redacted: %s", got)
}
if !strings.Contains(got, `"clientID":"cid"`) {
t.Fatalf("audit value should keep non secret fields: %s", got)
}
}
func TestAuditOptionValueUnparsableSSOIsWithheld(t *testing.T) {
got := auditOptionValue(data.OptionKeyAdminSSOLogin, `{"clientSecret":"abc"`)
if got != "[redacted]" {
t.Fatalf("expected whole value withheld, got %s", got)
}
}
func TestAuditOptionValueOtherKeysUnchanged(t *testing.T) {
for _, v := range []string{data.OptionValueDisplayModeWhitebox, data.OptionValueDisplayModeAll} {
if got := auditOptionValue(data.OptionKeyDisplayMode, v); got != v {
t.Fatalf("expected value %s unchanged, got %s", v, got)
}
}
}
func TestIsValidDisplayMode(t *testing.T) {
for _, v := range []string{
data.OptionValueDisplayModeWhitebox,
data.OptionValueDisplayModeBlackbox,
data.OptionValueDisplayModeAll,
} {
if !isValidDisplayMode(v) {
t.Fatalf("expected %q to be a valid display mode", v)
}
}
for _, v := range []string{"", "ALL", "both", "whitebox "} {
if isValidDisplayMode(v) {
t.Fatalf("expected %q to be rejected", v)
}
}
}
// a converted service function: an invalid option key is a validation
// failure after the permission check
func TestSetOptionByKeyFailedIsAudited(t *testing.T) {
logger, buf := bufferLogger(0)
o := &Option{Common: Common{Logger: logger}}
session := adminSession(uuid.New(), data.RoleSuperAdministrator)
err := o.SetOptionByKey(context.Background(), session, &model.Option{
Key: *vo.NewString127Must("no_such_option"),
Value: *vo.NewOptionalString1MBMust("x"),
})
if err == nil {
t.Fatal("expected an invalid key to fail")
}
out := buf.String()
if strings.Count(out, `"name":"Option.SetOptionByKey"`) != 1 ||
!strings.Contains(out, `"result":"failed"`) || !strings.Contains(out, `"errorClass":"validation"`) {
t.Fatalf("failed option event: %q", out)
}
}
// the value of an unknown option key is not written, it could be a secret
func TestSetOptionByKeyUnknownKeyHidesValue(t *testing.T) {
logger, buf := bufferLogger(0)
o := &Option{Common: Common{Logger: logger}}
session := adminSession(uuid.New(), data.RoleSuperAdministrator)
_ = o.SetOptionByKey(context.Background(), session, &model.Option{
Key: *vo.NewString127Must("no_such_option"),
Value: *vo.NewOptionalString1MBMust("pasted-secret-value"),
})
if strings.Contains(buf.String(), "pasted-secret-value") {
t.Fatalf("the value of an unknown key reached the audit log: %q", buf.String())
}
}
func TestInstallTimeZone(t *testing.T) {
t.Cleanup(func() { installLocation.Store(nil) })
if err := SetInstallTimeZone(""); err != nil || InstallLocation() != time.Local {
t.Fatalf("an empty zone must be the server zone, got %v %v", InstallLocation(), err)
}
for _, bad := range []string{"Local", "Mars/Base", "Europe/Nowhere", "../etc"} {
if err := SetInstallTimeZone(bad); err == nil {
t.Errorf("%q must be rejected", bad)
}
}
// a zone 14 hours ahead of UTC is a day ahead of every other zone for 10
// hours a day, so the date function must print its own day
if err := SetInstallTimeZone("Pacific/Kiritimati"); err != nil {
t.Fatal(err)
}
want := time.Now().In(InstallLocation()).Format("2006-01-02 15")
date := TemplateFuncs()["date"].(func(string, ...int) string)
if got := date("Y-m-d H"); got != want {
t.Errorf("date printed %q, want %q in the install zone", got, want)
}
// 2025-12-31 12:00 UTC is already the next day in Kiritimati
dateUnix := TemplateFuncs()["dateUnix"].(func(string, int64) string)
if got := dateUnix("Y-m-d H:i", 1767182400); got != "2026-01-01 02:00" {
t.Errorf("dateUnix printed %q, want the install zone time", got)
}
if got := dateUnix("Y-m-d", 0); got != "" {
t.Errorf("dateUnix of 0 must print nothing, got %q", got)
}
}