Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

4.0 KB
Raw Blame
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:    "[email protected]",
		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, "[email protected]", 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:    "[email protected]",
		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))
		})
	}
}