| 1 |
package zulip_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"net/http" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
"testing/synctest" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip/mocks" |
| 13 |
) |
| 14 |
|
| 15 |
const contentTypeForm = "application/x-www-form-urlencoded" |
| 16 |
|
| 17 |
func TestAPIURLFormatCompliance(t *testing.T) { |
| 18 |
t.Parallel() |
| 19 |
synctest.Test(t, func(t *testing.T) { |
| 20 |
mockClient := mocks.NewMockHTTPClient(t) |
| 21 |
msgResp := createMockResponse(http.StatusOK, `{"result": "success"}`) |
| 22 |
setupRegisterThenMessage(t, mockClient, msgResp) |
| 23 |
|
| 24 |
service := createTestService( |
| 25 |
t, |
| 26 |
"zulip://[email protected]:[email protected]?stream=general&topic=announcements", |
| 27 |
mockClient, |
| 28 |
) |
| 29 |
|
| 30 |
err := service.Send("Test message", nil) |
| 31 |
|
| 32 |
require.NoError(t, err) |
| 33 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 34 |
return req.URL.Scheme == "https" && |
| 35 |
req.URL.Host == "zulip.example.com" && |
| 36 |
req.URL.Path == "/api/v1/messages" |
| 37 |
}, "Zulip API URL format") |
| 38 |
|
| 39 |
mockClient.AssertExpectations(t) |
| 40 |
}) |
| 41 |
} |
| 42 |
|
| 43 |
func TestAPIPayloadStructureCompliance(t *testing.T) { |
| 44 |
t.Parallel() |
| 45 |
synctest.Test(t, func(t *testing.T) { |
| 46 |
mockClient := mocks.NewMockHTTPClient(t) |
| 47 |
service := createTestService( |
| 48 |
t, |
| 49 |
"zulip://[email protected]:[email protected]?stream=general&topic=announcements", |
| 50 |
mockClient, |
| 51 |
) |
| 52 |
|
| 53 |
msgResp := createMockResponse(http.StatusOK, `{"result": "success"}`) |
| 54 |
setupRegisterThenMessage(t, mockClient, msgResp) |
| 55 |
|
| 56 |
err := service.Send("Test message", nil) |
| 57 |
|
| 58 |
require.NoError(t, err) |
| 59 |
assertRequestContains(t, mockClient, "type=channel") |
| 60 |
assertRequestContains(t, mockClient, "to=general") |
| 61 |
assertRequestContains(t, mockClient, "topic=announcements") |
| 62 |
assertRequestContains(t, mockClient, "content=Test+message") |
| 63 |
|
| 64 |
mockClient.AssertExpectations(t) |
| 65 |
}) |
| 66 |
} |
| 67 |
|
| 68 |
func TestAPIPayloadDirectMessageCompliance(t *testing.T) { |
| 69 |
t.Parallel() |
| 70 |
synctest.Test(t, func(t *testing.T) { |
| 71 |
mockClient := mocks.NewMockHTTPClient(t) |
| 72 |
service := createTestService( |
| 73 |
t, |
| 74 |
"zulip://[email protected]:[email protected]", |
| 75 |
mockClient, |
| 76 |
) |
| 77 |
|
| 78 |
msgResp := createMockResponse(http.StatusOK, `{"result": "success"}`) |
| 79 |
setupRegisterThenMessage(t, mockClient, msgResp) |
| 80 |
|
| 81 |
params := createTestParams("type", "direct", "to", "[email protected],[email protected]") |
| 82 |
err := service.Send("DM test", params) |
| 83 |
|
| 84 |
require.NoError(t, err) |
| 85 |
// to should be JSON array |
| 86 |
assertRequestContains(t, mockClient, "type=direct") |
| 87 |
assertRequestContains(t, mockClient, `to=%5B%22user1%40example.com%22%2C%22user2%40example.com%22%5D`) |
| 88 |
assertRequestContains(t, mockClient, "content=DM+test") |
| 89 |
|
| 90 |
mockClient.AssertExpectations(t) |
| 91 |
}) |
| 92 |
} |
| 93 |
|
| 94 |
func TestAPIContentTypeHeaderCompliance(t *testing.T) { |
| 95 |
t.Parallel() |
| 96 |
synctest.Test(t, func(t *testing.T) { |
| 97 |
mockClient := mocks.NewMockHTTPClient(t) |
| 98 |
service := createTestService( |
| 99 |
t, |
| 100 |
"zulip://[email protected]:[email protected]?stream=general", |
| 101 |
mockClient, |
| 102 |
) |
| 103 |
|
| 104 |
msgResp := createMockResponse(http.StatusOK, `{"result": "success"}`) |
| 105 |
setupRegisterThenMessage(t, mockClient, msgResp) |
| 106 |
|
| 107 |
err := service.Send("Test message", nil) |
| 108 |
|
| 109 |
require.NoError(t, err) |
| 110 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 111 |
return req.Header.Get("Content-Type") == contentTypeForm |
| 112 |
}, "Content-Type header") |
| 113 |
|
| 114 |
mockClient.AssertExpectations(t) |
| 115 |
}) |
| 116 |
} |
| 117 |
|
| 118 |
func TestAPIMethodCompliance(t *testing.T) { |
| 119 |
t.Parallel() |
| 120 |
synctest.Test(t, func(t *testing.T) { |
| 121 |
mockClient := mocks.NewMockHTTPClient(t) |
| 122 |
service := createTestService( |
| 123 |
t, |
| 124 |
"zulip://[email protected]:[email protected]?stream=general", |
| 125 |
mockClient, |
| 126 |
) |
| 127 |
|
| 128 |
msgResp := createMockResponse(http.StatusOK, `{"result": "success"}`) |
| 129 |
setupRegisterThenMessage(t, mockClient, msgResp) |
| 130 |
|
| 131 |
err := service.Send("Test message", nil) |
| 132 |
|
| 133 |
require.NoError(t, err) |
| 134 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 135 |
return req.Method == http.MethodPost |
| 136 |
}, "POST method") |
| 137 |
|
| 138 |
mockClient.AssertExpectations(t) |
| 139 |
}) |
| 140 |
} |
| 141 |
|
| 142 |
func TestAPICredentialInURLCompliance(t *testing.T) { |
| 143 |
t.Parallel() |
| 144 |
synctest.Test(t, func(t *testing.T) { |
| 145 |
mockClient := mocks.NewMockHTTPClient(t) |
| 146 |
service := createTestService( |
| 147 |
t, |
| 148 |
"zulip://[email protected]:[email protected]?stream=general", |
| 149 |
mockClient, |
| 150 |
) |
| 151 |
|
| 152 |
msgResp := createMockResponse(http.StatusOK, `{"result": "success"}`) |
| 153 |
setupRegisterThenMessage(t, mockClient, msgResp) |
| 154 |
|
| 155 |
err := service.Send("Test message", nil) |
| 156 |
|
| 157 |
require.NoError(t, err) |
| 158 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 159 |
return req.URL.User != nil && |
| 160 |
req.URL.User.Username() == "[email protected]" |
| 161 |
}, "credentials in URL") |
| 162 |
|
| 163 |
mockClient.AssertExpectations(t) |
| 164 |
}) |
| 165 |
} |
| 166 |
|
| 167 |
func TestAPIHTTPSCompliance(t *testing.T) { |
| 168 |
t.Parallel() |
| 169 |
synctest.Test(t, func(t *testing.T) { |
| 170 |
mockClient := mocks.NewMockHTTPClient(t) |
| 171 |
service := createTestService( |
| 172 |
t, |
| 173 |
"zulip://[email protected]:[email protected]?stream=general", |
| 174 |
mockClient, |
| 175 |
) |
| 176 |
|
| 177 |
msgResp := createMockResponse(http.StatusOK, `{"result": "success"}`) |
| 178 |
setupRegisterThenMessage(t, mockClient, msgResp) |
| 179 |
|
| 180 |
err := service.Send("Test message", nil) |
| 181 |
|
| 182 |
require.NoError(t, err) |
| 183 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 184 |
return req.URL.Scheme == "https" |
| 185 |
}, "HTTPS scheme") |
| 186 |
|
| 187 |
mockClient.AssertExpectations(t) |
| 188 |
}) |
| 189 |
} |
| 190 |
|
| 191 |
func TestAPIWithoutTopicCompliance(t *testing.T) { |
| 192 |
t.Parallel() |
| 193 |
synctest.Test(t, func(t *testing.T) { |
| 194 |
mockClient := mocks.NewMockHTTPClient(t) |
| 195 |
service := createTestService( |
| 196 |
t, |
| 197 |
"zulip://[email protected]:[email protected]?stream=general", |
| 198 |
mockClient, |
| 199 |
) |
| 200 |
|
| 201 |
msgResp := createMockResponse(http.StatusOK, `{"result": "success"}`) |
| 202 |
setupRegisterThenMessage(t, mockClient, msgResp) |
| 203 |
|
| 204 |
err := service.Send("Test message", nil) |
| 205 |
|
| 206 |
require.NoError(t, err) |
| 207 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 208 |
body, readErr := io.ReadAll(req.Body) |
| 209 |
if readErr != nil { |
| 210 |
return false |
| 211 |
} |
| 212 |
|
| 213 |
req.Body = io.NopCloser(strings.NewReader(string(body))) |
| 214 |
|
| 215 |
return !strings.Contains(string(body), "topic=") |
| 216 |
}, "no topic when not configured") |
| 217 |
|
| 218 |
mockClient.AssertExpectations(t) |
| 219 |
}) |
| 220 |
} |
| 221 |
|
| 222 |
func TestAPIWithTopicCompliance(t *testing.T) { |
| 223 |
t.Parallel() |
| 224 |
synctest.Test(t, func(t *testing.T) { |
| 225 |
mockClient := mocks.NewMockHTTPClient(t) |
| 226 |
service := createTestService( |
| 227 |
t, |
| 228 |
"zulip://[email protected]:[email protected]?stream=general&topic=announcements", |
| 229 |
mockClient, |
| 230 |
) |
| 231 |
|
| 232 |
msgResp := createMockResponse(http.StatusOK, `{"result": "success"}`) |
| 233 |
setupRegisterThenMessage(t, mockClient, msgResp) |
| 234 |
|
| 235 |
err := service.Send("Test message", nil) |
| 236 |
|
| 237 |
require.NoError(t, err) |
| 238 |
assertRequestContains(t, mockClient, "topic=announcements") |
| 239 |
|
| 240 |
mockClient.AssertExpectations(t) |
| 241 |
}) |
| 242 |
} |
| 243 |
|
| 244 |
func TestAPIDirectWithToOnlyNoStream(t *testing.T) { |
| 245 |
t.Parallel() |
| 246 |
synctest.Test(t, func(t *testing.T) { |
| 247 |
mockClient := mocks.NewMockHTTPClient(t) |
| 248 |
service := createTestService( |
| 249 |
t, |
| 250 |
"zulip://[email protected]:[email protected]", |
| 251 |
mockClient, |
| 252 |
) |
| 253 |
|
| 254 |
msgResp := createMockResponse(http.StatusOK, `{"result": "success"}`) |
| 255 |
setupRegisterThenMessage(t, mockClient, msgResp) |
| 256 |
|
| 257 |
params := createTestParams("type", "direct", "to", "[email protected]") |
| 258 |
err := service.Send("DM only-to", params) |
| 259 |
|
| 260 |
require.NoError(t, err) |
| 261 |
assertRequestContains(t, mockClient, "type=direct") |
| 262 |
assertRequestContains(t, mockClient, "to=%5B%22dm-user%40example.com%22%5D") |
| 263 |
|
| 264 |
mockClient.AssertExpectations(t) |
| 265 |
}) |
| 266 |
} |