forked from cds-snc/smtp-proxy-for-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify_client_test.go
81 lines (68 loc) · 2.47 KB
/
notify_client_test.go
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
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSendEmail(t *testing.T) {
// Create a mock NotifyClient
client := &NotifyClient{
Hostname: "http://example.com",
ApiKey: "test-api-key",
Client: &http.Client{},
}
// Create a mock NotifyEmail
email := &NotifyEmail{
TemplateId: "test-template-id",
Personalisation: Body{
Subject: "Test Subject",
Body: "Test Body",
},
Attachments: []Attachment{
{
File: "test-file",
Filename: "test-filename",
SendingMethod: "test-sending-method",
},
},
Emails: []string{"[email protected]"},
}
// Create a mock response
mockResponse := `{"status": "success"}`
// Create a mock server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/v2/notifications/email", r.URL.Path)
assert.Equal(t, "POST", r.Method)
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Equal(t, "ApiKey-v1 test-api-key", r.Header.Get("Authorization"))
// Read the request body
body, err := io.ReadAll(r.Body)
assert.Nil(t, err)
// Unmarshal the request body
var requestPayload map[string]interface{}
err = json.Unmarshal(body, &requestPayload)
assert.Nil(t, err)
// Verify the request payload
assert.Equal(t, "test-template-id", requestPayload["template_id"])
assert.Equal(t, "Test Subject", requestPayload["personalisation"].(map[string]interface{})["subject"])
assert.Equal(t, "Test Body", requestPayload["personalisation"].(map[string]interface{})["body"])
assert.Equal(t, "test-file", requestPayload["personalisation"].(map[string]interface{})["attachment_0"].(map[string]interface{})["file"])
assert.Equal(t, "test-filename", requestPayload["personalisation"].(map[string]interface{})["attachment_0"].(map[string]interface{})["filename"])
assert.Equal(t, "test-sending-method", requestPayload["personalisation"].(map[string]interface{})["attachment_0"].(map[string]interface{})["sending_method"])
assert.Equal(t, "[email protected]", requestPayload["email_address"])
// Write the mock response
w.WriteHeader(http.StatusCreated)
_, err = w.Write([]byte(mockResponse))
assert.Nil(t, err)
}))
defer mockServer.Close()
// Set the mock server URL as the NotifyClient hostname
client.Hostname = mockServer.URL
// Call the sendEmail function
err := sendEmail(client, email)
// Verify the result
assert.Nil(t, err)
}