File size: 4,134 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package emails

import (
	"strings"
	"testing"

	"github.com/NdoleStudio/httpsms/pkg/entities"
	"github.com/NdoleStudio/httpsms/pkg/events"
	"github.com/google/uuid"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func testNotificationEmailFactory() NotificationEmailFactory {
	return NewHermesNotificationEmailFactory(&HermesGeneratorConfig{
		AppURL:     "https://httpsms.com",
		AppName:    "httpSMS",
		AppLogoURL: "https://httpsms.com/logo.png",
	})
}

func TestWebhookSendFailedFormatsOnlyEventPayload(t *testing.T) {
	statusCode := 500
	factory := testNotificationEmailFactory()
	user := &entities.User{
		Email:    "name@email.com",
		Timezone: "UTC",
	}
	payload := &events.WebhookSendFailedPayload{
		WebhookID:              uuid.New(),
		WebhookURL:             "https://example.com/webhooks",
		Owner:                  "+237612345678",
		EventID:                "event-id",
		EventType:              "message.phone.received",
		EventPayload:           `{"message":"hello","retry":false}`,
		HTTPResponseStatusCode: &statusCode,
		ErrorMessage:           "plain failure response",
	}

	email, err := factory.WebhookSendFailed(user, payload)
	require.NoError(t, err)

	assert.Equal(t, "name@email.com", email.ToEmail)
	assert.Equal(t, "📢 We could not forward a webhook event to your server", email.Subject)
	assert.Contains(t, email.HTML, `<pre style=`)
	assert.Equal(t, 1, strings.Count(email.HTML, `<pre style=`))
	assert.Contains(t, email.HTML, `&#34;message&#34;`)
	assert.Contains(t, email.HTML, `plain failure response`)
	assert.Contains(t, email.Text, `"message": "hello"`)
	assert.Contains(t, email.Text, `"retry": false`)
	assert.NotContains(t, email.Text, "<pre")
	assert.NotContains(t, email.Text, webhookSendFailedEventPayloadPlaceholder)
	assert.NotContains(t, email.HTML, webhookSendFailedEventPayloadPlaceholder)
}

func TestWebhookSendFailedPreservesNonJSONEventPayload(t *testing.T) {
	factory := testNotificationEmailFactory()
	user := &entities.User{
		Email:    "name@email.com",
		Timezone: "UTC",
	}
	payload := &events.WebhookSendFailedPayload{
		WebhookID:    uuid.New(),
		WebhookURL:   "https://example.com/webhooks",
		Owner:        "+237612345678",
		EventID:      "event-id",
		EventType:    "message.phone.received",
		EventPayload: "line one\n  line two",
		ErrorMessage: "plain failure response",
	}

	email, err := factory.WebhookSendFailed(user, payload)
	require.NoError(t, err)

	assert.Contains(t, email.HTML, "line one\n  line two")
	assert.NotContains(t, email.HTML, `<span style="color:`)
	assert.Contains(t, email.Text, "line one\n  line two")
}

func TestGenerateWebhookSendFailedPlainTextFallsBackToFormattedPayload(t *testing.T) {
	formattedPayload := "{\n  \"message\": \"hello\"\n}"
	values := make([]string, 0, 2)
	generate := func(value string) (string, error) {
		values = append(values, value)
		if len(values) == 1 {
			return "plain text without the placeholder", nil
		}
		return value, nil
	}

	text, err := generateWebhookSendFailedPlainText(generate, formattedPayload)

	require.NoError(t, err)
	assert.Equal(t, formattedPayload, text)
	assert.Equal(t, []string{webhookSendFailedEventPayloadPlaceholder, formattedPayload}, values)
}

func TestReplaceWebhookSendFailedEventPayloadPlaceholder(t *testing.T) {
	tests := []struct {
		name             string
		text             string
		formattedPayload string
		want             string
	}{
		{
			name:             "replaces placeholder and preserves remaining text",
			text:             "before\n" + webhookSendFailedEventPayloadPlaceholder + "\nafter",
			formattedPayload: "{\n  \"message\": \"hello\"\n}",
			want:             "before\n{\n  \"message\": \"hello\"\n}\nafter",
		},
		{
			name:             "returns original text when placeholder is missing",
			text:             "before\nafter",
			formattedPayload: "{\n  \"message\": \"hello\"\n}",
			want:             "before\nafter",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.want, replaceWebhookSendFailedEventPayloadPlaceholder(tt.text, tt.formattedPayload))
		})
	}
}