Files
phishingclub/backend/service/smtpConfiguration_db_test.go
2026-12-01 21:25:01 +01:00

73 lines
2.5 KiB
Go

package service
import (
"testing"
"github.com/oapi-codegen/nullable"
"github.com/phishingclub/phishingclub/model"
"github.com/phishingclub/phishingclub/repository"
"github.com/phishingclub/phishingclub/vo"
)
// an update that sets the public sending IPs saves them, and an update that
// leaves the field out keeps them
func TestSMTPConfigurationUpdateSavesPublicSendingIPs(t *testing.T) {
f := newAllowListingFixture(t)
conf := &model.SMTPConfiguration{
Name: nullable.NewNullableWithValue(*vo.NewString127Must("ips")),
Host: nullable.NewNullableWithValue(*vo.NewString255Must("smtp.example.test")),
Port: nullable.NewNullableWithValue(*vo.NewPortMust(587)),
Username: nullable.NewNullableWithValue(*vo.NewOptionalString255Must("")),
Password: nullable.NewNullableWithValue(*vo.NewOptionalString255Must("")),
IgnoreCertErrors: nullable.NewNullableWithValue(false),
}
id, err := f.smtp.Create(f.ctx, f.session, conf)
if err != nil {
t.Fatalf("create: %v", err)
}
read := func() string {
got, err := f.smtp.SMTPConfigurationRepository.GetByID(f.ctx, id, &repository.SMTPConfigurationOption{})
if err != nil {
t.Fatalf("read: %v", err)
}
return got.PublicSendingIPs.MustGet()
}
if got := read(); got != "" {
t.Fatalf("new config: got %q", got)
}
update := *conf
update.PublicSendingIPs = nullable.NewNullableWithValue("203.0.113.5, 2001:DB8::5")
if err := f.smtp.UpdateByID(f.ctx, f.session, id, &update); err != nil {
t.Fatalf("update: %v", err)
}
if got := read(); got != "203.0.113.5\n2001:db8::5" {
t.Fatalf("after update: got %q", got)
}
// an update without the field, as a package update sends, keeps the value
keep := *conf
keep.Host = nullable.NewNullableWithValue(*vo.NewString255Must("smtp2.example.test"))
if err := f.smtp.UpdateByID(f.ctx, f.session, id, &keep); err != nil {
t.Fatalf("update without field: %v", err)
}
if got := read(); got != "203.0.113.5\n2001:db8::5" {
t.Fatalf("after update without field: got %q", got)
}
invalid := *conf
invalid.PublicSendingIPs = nullable.NewNullableWithValue("mail.example.test")
if err := f.smtp.UpdateByID(f.ctx, f.session, id, &invalid); err == nil {
t.Fatal("a hostname must be refused")
}
cleared := *conf
cleared.PublicSendingIPs = nullable.NewNullableWithValue("")
if err := f.smtp.UpdateByID(f.ctx, f.session, id, &cleared); err != nil {
t.Fatalf("clear: %v", err)
}
if got := read(); got != "" {
t.Fatalf("after clear: got %q", got)
}
}