Spaces:
Paused
Paused
ML Intern deploy
Deploy httpSMS gateway to Hugging Face Spaces (Docker SDK): nginx :7860 -> Go API :8000, Nuxt SPA, Android APK, bootstrap-mode entrypoint
83a59ca Download api/pkg/emails/hermes_notification_email_factory_test.go from red-kit/httpsms-gateway: direct link, hf CLI and curl.
- Browser
- Download file 4.13 kB
-
https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/main/api/pkg/emails/hermes_notification_email_factory_test.go
- Command line
-
hf download hf://spaces/red-kit/httpsms-gateway/api/pkg/emails/hermes_notification_email_factory_test.go
-
curl -L -o hermes_notification_email_factory_test.go https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/main/api/pkg/emails/hermes_notification_email_factory_test.go
4.13 kB
| 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, `"message"`) | |
| 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)) | |
| }) | |
| } | |
| } | |