mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 10:03:46 +01:00
174 lines
6.7 KiB
Go
174 lines
6.7 KiB
Go
package service
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/oapi-codegen/nullable"
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"github.com/phishingclub/phishingclub/model"
|
|
"github.com/phishingclub/phishingclub/repository"
|
|
"github.com/phishingclub/phishingclub/vo"
|
|
)
|
|
|
|
// recipientService returns a recipient service on the fixture database
|
|
func (f *poolFixture) recipientService() *Recipient {
|
|
return &Recipient{
|
|
Common: f.svc.Common,
|
|
RecipientRepository: &repository.Recipient{DB: f.db},
|
|
}
|
|
}
|
|
|
|
// storedLanguage returns the language column of a recipient, "<nil>" for null
|
|
func (f *poolFixture) storedLanguage(id uuid.UUID) string {
|
|
f.t.Helper()
|
|
var row database.Recipient
|
|
if err := f.db.Where("id = ?", id).First(&row).Error; err != nil {
|
|
f.t.Fatalf("read recipient: %v", err)
|
|
}
|
|
if row.Language == nil {
|
|
return "<nil>"
|
|
}
|
|
return *row.Language
|
|
}
|
|
|
|
// languageRecipient stores a recipient in groupID with a language, nil for
|
|
// none, and returns its id
|
|
func (f *poolFixture) languageRecipient(groupID uuid.UUID, language *string) uuid.UUID {
|
|
f.t.Helper()
|
|
rid := uuid.New()
|
|
email := rid.String() + "@language.test"
|
|
if err := f.db.Create(&database.Recipient{
|
|
ID: &rid, CreatedAt: &f.now, UpdatedAt: &f.now, Email: &email, Language: language,
|
|
}).Error; err != nil {
|
|
f.t.Fatalf("insert recipient: %v", err)
|
|
}
|
|
if err := f.db.Create(&database.RecipientGroupRecipient{RecipientID: &rid, RecipientGroupID: &groupID}).Error; err != nil {
|
|
f.t.Fatalf("insert group member: %v", err)
|
|
}
|
|
return rid
|
|
}
|
|
|
|
func strPtr(v string) *string { return &v }
|
|
|
|
func recipientWithLanguage(email string, language string) *model.Recipient {
|
|
return &model.Recipient{
|
|
Email: nullable.NewNullableWithValue(*vo.NewEmailMust(email)),
|
|
Language: nullable.NewNullableWithValue(language),
|
|
}
|
|
}
|
|
|
|
// TestRecipientLanguageCreateAndUpdate checks the form and API path: a code,
|
|
// tag or name is stored as the code, an unknown value is refused on the
|
|
// language field, an update without the field keeps it and an empty value
|
|
// clears it
|
|
func TestRecipientLanguageCreateAndUpdate(t *testing.T) {
|
|
f := newPoolFixture(t)
|
|
svc := f.recipientService()
|
|
id, err := svc.Create(f.ctx, f.session, recipientWithLanguage("dane@language.test", " Danish "))
|
|
if err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
if got := f.storedLanguage(*id); got != "da" {
|
|
t.Fatalf("create stored %s, want da", got)
|
|
}
|
|
_, err = svc.Create(f.ctx, f.session, recipientWithLanguage("klingon@language.test", "Klingon"))
|
|
if !isValidationError(err) || !strings.Contains(err.Error(), "language") || !strings.Contains(err.Error(), "Klingon") {
|
|
t.Fatalf("unknown language: want a validation error naming the value, got %v", err)
|
|
}
|
|
if _, err := svc.Create(f.ctx, f.session, recipientWithLanguage("none@language.test", "")); err != nil {
|
|
t.Fatalf("create without a language: %v", err)
|
|
}
|
|
|
|
update := func(r *model.Recipient) error {
|
|
return svc.UpdateByID(f.ctx, f.session, id, r)
|
|
}
|
|
if err := update(&model.Recipient{Language: nullable.NewNullableWithValue("de-AT")}); err != nil {
|
|
t.Fatalf("update with a tag: %v", err)
|
|
}
|
|
if got := f.storedLanguage(*id); got != "de" {
|
|
t.Fatalf("update stored %s, want de", got)
|
|
}
|
|
firstName := nullable.NewNullableWithValue(*vo.NewOptionalString127Must("Dane"))
|
|
if err := update(&model.Recipient{FirstName: firstName}); err != nil {
|
|
t.Fatalf("update without language: %v", err)
|
|
}
|
|
if got := f.storedLanguage(*id); got != "de" {
|
|
t.Fatalf("an update without the field changed it to %s", got)
|
|
}
|
|
if err := update(&model.Recipient{Language: nullable.NewNullableWithValue("xx")}); !isValidationError(err) {
|
|
t.Fatalf("update with an unknown language: want a validation error, got %v", err)
|
|
}
|
|
if got := f.storedLanguage(*id); got != "de" {
|
|
t.Fatalf("a refused update changed the language to %s", got)
|
|
}
|
|
if err := update(&model.Recipient{Language: nullable.NewNullableWithValue("")}); err != nil {
|
|
t.Fatalf("clear: %v", err)
|
|
}
|
|
if got := f.storedLanguage(*id); got != "<nil>" {
|
|
t.Fatalf("an empty value stored %s, want null", got)
|
|
}
|
|
got, err := svc.GetByID(f.ctx, f.session, id, &repository.RecipientOption{})
|
|
if err != nil || got.Language.MustGet() != "" {
|
|
t.Fatalf("read back %v %v, want an empty language", got.Language, err)
|
|
}
|
|
}
|
|
|
|
// TestRecipientLanguageImport checks the CSV import path: each row is read
|
|
// on its own, an unknown language fails only its row with the reason, and an
|
|
// empty value keeps or clears the stored language as the overwrite option says
|
|
func TestRecipientLanguageImport(t *testing.T) {
|
|
f := newPoolFixture(t)
|
|
svc := f.recipientService()
|
|
rows := []*model.Recipient{
|
|
recipientWithLanguage("a@import.test", "da-DK"),
|
|
recipientWithLanguage("b@import.test", "Elvish"),
|
|
recipientWithLanguage("c@import.test", "german"),
|
|
}
|
|
result, err := svc.Import(f.ctx, f.session, rows, false, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("import: %v", err)
|
|
}
|
|
if result.Summary.Created != 2 || result.Summary.Failed != 1 || len(result.Failures) != 1 {
|
|
t.Fatalf("summary %+v, want 2 created and 1 failed", result.Summary)
|
|
}
|
|
failure := result.Failures[0]
|
|
if failure.Index != 1 || !strings.Contains(failure.Reason, "unknown language") || !strings.Contains(failure.Reason, "Elvish") {
|
|
t.Fatalf("failure %+v, want row 1 with the unknown language", failure)
|
|
}
|
|
byEmail := func(email string) uuid.UUID {
|
|
var row database.Recipient
|
|
if err := f.db.Where("email = ?", email).First(&row).Error; err != nil {
|
|
t.Fatalf("read %s: %v", email, err)
|
|
}
|
|
return *row.ID
|
|
}
|
|
a, c := byEmail("a@import.test"), byEmail("c@import.test")
|
|
if f.storedLanguage(a) != "da" || f.storedLanguage(c) != "de" {
|
|
t.Fatalf("stored %s and %s, want da and de", f.storedLanguage(a), f.storedLanguage(c))
|
|
}
|
|
|
|
// an empty language on an update keeps the stored one when empty fields
|
|
// are ignored, else it clears it, the same as the other fields
|
|
if _, err := svc.Import(f.ctx, f.session, []*model.Recipient{recipientWithLanguage("a@import.test", "")}, true, nil, nil); err != nil {
|
|
t.Fatalf("import keeping empty fields: %v", err)
|
|
}
|
|
if got := f.storedLanguage(a); got != "da" {
|
|
t.Fatalf("ignored empty value changed the language to %s", got)
|
|
}
|
|
if _, err := svc.Import(f.ctx, f.session, []*model.Recipient{recipientWithLanguage("a@import.test", " ")}, false, nil, nil); err != nil {
|
|
t.Fatalf("import overwriting empty fields: %v", err)
|
|
}
|
|
if got := f.storedLanguage(a); got != "<nil>" {
|
|
t.Fatalf("overwritten empty value stored %s, want null", got)
|
|
}
|
|
// a new value on an update replaces the stored one
|
|
if _, err := svc.Import(f.ctx, f.session, []*model.Recipient{recipientWithLanguage("c@import.test", "Swedish")}, false, nil, nil); err != nil {
|
|
t.Fatalf("import update: %v", err)
|
|
}
|
|
if got := f.storedLanguage(c); got != "sv" {
|
|
t.Fatalf("update stored %s, want sv", got)
|
|
}
|
|
}
|