65 lines
2.3 KiB
Go
65 lines
2.3 KiB
Go
package test
|
|
|
|
import (
|
|
"RenewCMS/api/handlers/user"
|
|
"RenewCMS/internal/domain/author"
|
|
domain "RenewCMS/internal/domain/user"
|
|
"RenewCMS/internal/infrastructure/config"
|
|
"RenewCMS/internal/infrastructure/mailer"
|
|
"net/http"
|
|
"os"
|
|
"reflect"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func readWebSource(t *testing.T, path string) string {
|
|
t.Helper()
|
|
raw, err := os.ReadFile("../web/src/" + path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return string(raw)
|
|
}
|
|
|
|
func TestAccountDeleteLiteralParity(t *testing.T) {
|
|
t.Run("Deleted user is the same name in the domain, the audit trail and the SPA", func(t *testing.T) {
|
|
if domain.DeletedUserName != author.DeletedName {
|
|
t.Errorf("user %q and author %q disagree", domain.DeletedUserName, author.DeletedName)
|
|
}
|
|
if !strings.Contains(readWebSource(t, "lib/deletedUser.ts"), "DELETED_USER_NAME = '"+domain.DeletedUserName+"'") {
|
|
t.Errorf("web/src/lib/deletedUser.ts does not name the user %q", domain.DeletedUserName)
|
|
}
|
|
if !strings.Contains(readWebSource(t, "i18n/messages.ts"), "'"+domain.DeletedUserName+"':") {
|
|
t.Errorf("the i18n catalogue has no entry for %q", domain.DeletedUserName)
|
|
}
|
|
})
|
|
|
|
t.Run("The confirmation word is the same in the handler and in the SPA", func(t *testing.T) {
|
|
field, _ := reflect.TypeOf(user.DeleteAccountRequest{}).FieldByName("Confirmation")
|
|
matched := regexp.MustCompile(`eq=(\w+)`).FindStringSubmatch(field.Tag.Get("validate"))
|
|
if matched == nil {
|
|
t.Fatalf("the Confirmation tag has no eq= rule: %q", field.Tag)
|
|
}
|
|
if !strings.Contains(readWebSource(t, "lib/accountDeletion.ts"), "DELETE_WORD = '"+matched[1]+"'") {
|
|
t.Errorf("web/src/lib/accountDeletion.ts does not use the word %q", matched[1])
|
|
}
|
|
})
|
|
|
|
t.Run("The template the use case sends is the one the mailer renders", func(t *testing.T) {
|
|
f := newDeleteAccountFixture(t)
|
|
|
|
rec := f.request(t, 7, deleteAccountBody)
|
|
|
|
if rec.Code != http.StatusNoContent || len(f.mail.SentEmails) != 1 {
|
|
t.Fatalf("unexpected outcome %d with %d mails", rec.Code, len(f.mail.SentEmails))
|
|
}
|
|
sent := f.mail.SentEmails[0]
|
|
subject, body, err := mailer.NewMailRepository(config.SmtpSettings{}).Render(sent.Template, sent.Data)
|
|
if err != nil || body == "" || subject == "RenewCMS | Action required" {
|
|
t.Errorf("the mailer does not know %q: subject %q err %v", sent.Template, subject, err)
|
|
}
|
|
})
|
|
}
|