Debian Codegraph @

testing/integration/twilio/send_test.go

raw
1 package twilio_test
2
3 import (
4 "net/http"
5 "testing"
6 "testing/synctest"
7
8 "github.com/stretchr/testify/mock"
9 "github.com/stretchr/testify/require"
10
11 "github.com/nicholas-fedor/shoutrrr/pkg/types"
12 )
13
14 func TestSendBasicMessage(t *testing.T) {
15 t.Parallel()
16 synctest.Test(t, func(t *testing.T) {
17 mockClient := &MockHTTPClient{}
18 service := createTestService(t, validTwilioURL, mockClient)
19
20 mockClient.On("Do", mock.Anything).
21 Return(createMockResponse(http.StatusCreated, `{"sid": "SM123"}`), nil).
22 Once()
23
24 err := service.Send("Hello from Twilio integration test", nil)
25 require.NoError(t, err)
26
27 assertRequestContains(t, mockClient, "Body=Hello+from+Twilio+integration+test")
28
29 mockClient.AssertExpectations(t)
30 })
31 }
32
33 func TestSendToMultipleRecipients(t *testing.T) {
34 t.Parallel()
35 synctest.Test(t, func(t *testing.T) {
36 mockClient := &MockHTTPClient{}
37 service := createTestService(
38 t,
39 "twilio://ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:authToken@+15551234567/+15559876543/+15551111111",
40 mockClient,
41 )
42
43 mockClient.On("Do", mock.Anything).
44 Return(createMockResponse(http.StatusCreated, `{"sid": "SM123"}`), nil).
45 Times(2)
46
47 err := service.Send("Message to multiple recipients", nil)
48 require.NoError(t, err)
49
50 mockClient.AssertNumberOfCalls(t, "Do", 2)
51 mockClient.AssertExpectations(t)
52 })
53 }
54
55 func TestSendWithTitle(t *testing.T) {
56 t.Parallel()
57 synctest.Test(t, func(t *testing.T) {
58 mockClient := &MockHTTPClient{}
59 service := createTestService(
60 t,
61 validTwilioURL+"?title=Alert",
62 mockClient,
63 )
64
65 mockClient.On("Do", mock.Anything).
66 Return(createMockResponse(http.StatusCreated, `{"sid": "SM123"}`), nil).
67 Once()
68
69 err := service.Send("Something happened", nil)
70 require.NoError(t, err)
71
72 // Title should be prepended to the body
73 assertRequestContains(t, mockClient, "Body=Alert")
74 assertRequestContains(t, mockClient, "Something")
75
76 mockClient.AssertExpectations(t)
77 })
78 }
79
80 func TestSendWithTitleParam(t *testing.T) {
81 t.Parallel()
82 synctest.Test(t, func(t *testing.T) {
83 mockClient := &MockHTTPClient{}
84 service := createTestService(t, validTwilioURL, mockClient)
85
86 mockClient.On("Do", mock.Anything).
87 Return(createMockResponse(http.StatusCreated, `{"sid": "SM123"}`), nil).
88 Once()
89
90 params := types.Params{"title": "Dynamic Title"}
91 err := service.Send("Message body", &params)
92 require.NoError(t, err)
93
94 assertRequestContains(t, mockClient, "Body=Dynamic+Title")
95 assertRequestContains(t, mockClient, "Message")
96
97 mockClient.AssertExpectations(t)
98 })
99 }
100
101 func TestSendWithUnicodeContent(t *testing.T) {
102 t.Parallel()
103 synctest.Test(t, func(t *testing.T) {
104 mockClient := &MockHTTPClient{}
105 service := createTestService(t, validTwilioURL, mockClient)
106
107 mockClient.On("Do", mock.Anything).
108 Return(createMockResponse(http.StatusCreated, `{"sid": "SM123"}`), nil).
109 Once()
110
111 err := service.Send("Hello World", nil)
112 require.NoError(t, err)
113
114 mockClient.AssertExpectations(t)
115 })
116 }
117
118 func TestSendWithEmptyMessage(t *testing.T) {
119 t.Parallel()
120 synctest.Test(t, func(t *testing.T) {
121 mockClient := &MockHTTPClient{}
122 service := createTestService(t, validTwilioURL, mockClient)
123
124 mockClient.On("Do", mock.Anything).
125 Return(createMockResponse(http.StatusCreated, `{"sid": "SM123"}`), nil).
126 Once()
127
128 err := service.Send("", nil)
129 require.NoError(t, err)
130
131 mockClient.AssertExpectations(t)
132 })
133 }
134
135 func TestServiceInitialization(t *testing.T) {
136 t.Parallel()
137 synctest.Test(t, func(t *testing.T) {
138 mockClient := &MockHTTPClient{}
139 service := createTestService(t, validTwilioURL, mockClient)
140
141 require.NotNil(t, service)
142 require.Equal(t, "twilio", service.GetID())
143
144 mockClient.AssertExpectations(t)
145 })
146 }