(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestEmailSendValidateAndSubmit(t *testing.T) { |
| 14 | t.Parallel() |
| 15 | |
| 16 | scenarios := []struct { |
| 17 | template string |
| 18 | email string |
| 19 | collection string |
| 20 | expectedErrors []string |
| 21 | }{ |
| 22 | {"", "", "", []string{"template", "email"}}, |
| 23 | {"invalid", "test@example.com", "", []string{"template"}}, |
| 24 | {forms.TestTemplateVerification, "invalid", "", []string{"email"}}, |
| 25 | {forms.TestTemplateVerification, "test@example.com", "invalid", []string{"collection"}}, |
| 26 | {forms.TestTemplateVerification, "test@example.com", "demo1", []string{"collection"}}, |
| 27 | {forms.TestTemplateVerification, "test@example.com", "users", nil}, |
| 28 | {forms.TestTemplatePasswordReset, "test@example.com", "", nil}, |
| 29 | {forms.TestTemplateEmailChange, "test@example.com", "", nil}, |
| 30 | {forms.TestTemplateOTP, "test@example.com", "", nil}, |
| 31 | {forms.TestTemplateAuthAlert, "test@example.com", "", nil}, |
| 32 | } |
| 33 | |
| 34 | for i, s := range scenarios { |
| 35 | t.Run(fmt.Sprintf("%d_%s", i, s.template), func(t *testing.T) { |
| 36 | app, _ := tests.NewTestApp() |
| 37 | defer app.Cleanup() |
| 38 | |
| 39 | form := forms.NewTestEmailSend(app) |
| 40 | form.Email = s.email |
| 41 | form.Template = s.template |
| 42 | form.Collection = s.collection |
| 43 | |
| 44 | result := form.Submit() |
| 45 | |
| 46 | // parse errors |
| 47 | errs, ok := result.(validation.Errors) |
| 48 | if !ok && result != nil { |
| 49 | t.Fatalf("Failed to parse errors %v", result) |
| 50 | } |
| 51 | |
| 52 | // check errors |
| 53 | if len(errs) > len(s.expectedErrors) { |
| 54 | t.Fatalf("Expected error keys %v, got %v", s.expectedErrors, errs) |
| 55 | } |
| 56 | for _, k := range s.expectedErrors { |
| 57 | if _, ok := errs[k]; !ok { |
| 58 | t.Fatalf("Missing expected error key %q in %v", k, errs) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | expectedEmails := 1 |
| 63 | if len(s.expectedErrors) > 0 { |
| 64 | expectedEmails = 0 |
| 65 | } |
| 66 | |
| 67 | if app.TestMailer.TotalSend() != expectedEmails { |
| 68 | t.Fatalf("Expected %d email(s) to be sent, got %d", expectedEmails, app.TestMailer.TotalSend()) |
| 69 | } |
| 70 |
nothing calls this directly
no test coverage detected
searching dependent graphs…