|
| 1 | +package emails |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/NdoleStudio/httpsms/pkg/entities" |
| 8 | + "github.com/NdoleStudio/httpsms/pkg/events" |
| 9 | + "github.com/google/uuid" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func testNotificationEmailFactory() NotificationEmailFactory { |
| 15 | + return NewHermesNotificationEmailFactory(&HermesGeneratorConfig{ |
| 16 | + AppURL: "https://httpsms.com", |
| 17 | + AppName: "httpSMS", |
| 18 | + AppLogoURL: "https://httpsms.com/logo.png", |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +func TestWebhookSendFailedFormatsOnlyEventPayload(t *testing.T) { |
| 23 | + statusCode := 500 |
| 24 | + factory := testNotificationEmailFactory() |
| 25 | + user := &entities.User{ |
| 26 | + Email: "name@email.com", |
| 27 | + Timezone: "UTC", |
| 28 | + } |
| 29 | + payload := &events.WebhookSendFailedPayload{ |
| 30 | + WebhookID: uuid.New(), |
| 31 | + WebhookURL: "https://example.com/webhooks", |
| 32 | + Owner: "+237612345678", |
| 33 | + EventID: "event-id", |
| 34 | + EventType: "message.phone.received", |
| 35 | + EventPayload: `{"message":"hello","retry":false}`, |
| 36 | + HTTPResponseStatusCode: &statusCode, |
| 37 | + ErrorMessage: "plain failure response", |
| 38 | + } |
| 39 | + |
| 40 | + email, err := factory.WebhookSendFailed(user, payload) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + assert.Equal(t, "name@email.com", email.ToEmail) |
| 44 | + assert.Equal(t, "📢 We could not forward a webhook event to your server", email.Subject) |
| 45 | + assert.Contains(t, email.HTML, `<pre style=`) |
| 46 | + assert.Equal(t, 1, strings.Count(email.HTML, `<pre style=`)) |
| 47 | + assert.Contains(t, email.HTML, `"message"`) |
| 48 | + assert.Contains(t, email.HTML, `plain failure response`) |
| 49 | + assert.Contains(t, email.Text, `"message": "hello"`) |
| 50 | + assert.Contains(t, email.Text, `"retry": false`) |
| 51 | + assert.NotContains(t, email.Text, "<pre") |
| 52 | +} |
| 53 | + |
| 54 | +func TestWebhookSendFailedPreservesNonJSONEventPayload(t *testing.T) { |
| 55 | + factory := testNotificationEmailFactory() |
| 56 | + user := &entities.User{ |
| 57 | + Email: "name@email.com", |
| 58 | + Timezone: "UTC", |
| 59 | + } |
| 60 | + payload := &events.WebhookSendFailedPayload{ |
| 61 | + WebhookID: uuid.New(), |
| 62 | + WebhookURL: "https://example.com/webhooks", |
| 63 | + Owner: "+237612345678", |
| 64 | + EventID: "event-id", |
| 65 | + EventType: "message.phone.received", |
| 66 | + EventPayload: "line one\n line two", |
| 67 | + ErrorMessage: "plain failure response", |
| 68 | + } |
| 69 | + |
| 70 | + email, err := factory.WebhookSendFailed(user, payload) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + assert.Contains(t, email.HTML, "line one\n line two") |
| 74 | + assert.NotContains(t, email.HTML, `<span style="color:`) |
| 75 | + assert.Contains(t, email.Text, "line one\n line two") |
| 76 | +} |
0 commit comments