diff --git a/server/server_test.go b/server/server_test.go index 8ab5e12f..9ba65697 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -1229,8 +1229,14 @@ func TestServer_MatrixGateway_Discovery_Failure_Unconfigured(t *testing.T) { func TestServer_MatrixGateway_Push_Success(t *testing.T) { s := newTestServer(t, newTestConfig(t)) + + response := request(t, s, "GET", "/mytopic/json?poll=1", "", map[string]string{ + "Rate-Topics": "mytopic", + }) + require.Equal(t, 200, response.Code) + notification := `{"notification":{"devices":[{"pushkey":"http://127.0.0.1:12345/mytopic?up=1"}]}}` - response := request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil) + response = request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil) require.Equal(t, 200, response.Code) require.Equal(t, `{"rejected":[]}`+"\n", response.Body.String()) @@ -1240,6 +1246,36 @@ func TestServer_MatrixGateway_Push_Success(t *testing.T) { require.Equal(t, notification, m.Message) } +func TestServer_MatrixGateway_Push_DailyLimit(t *testing.T) { + c := newTestConfig(t) + c.VisitorMessageDailyLimit = 3 + s := newTestServer(t, c) + + response := request(t, s, "GET", "/mytopic/json?poll=1", "", map[string]string{ + "Rate-Topics": "mytopic", + }) + require.Equal(t, 200, response.Code) + + notification := `{"notification":{"devices":[{"pushkey":"http://127.0.0.1:12345/mytopic?up=1"}]}}` + for i := 0; i < 3; i++ { + response = request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil) + require.Equal(t, 200, response.Code) + require.Equal(t, `{"rejected":[]}`+"\n", response.Body.String()) + } + + response = request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil) + require.Equal(t, 429, response.Code) + require.Equal(t, "42908", response.Header().Get("X-Ntfy-Error-Code")) +} + +func TestServer_MatrixGateway_Push_NoSubscriber(t *testing.T) { + s := newTestServer(t, newTestConfig(t)) + notification := `{"notification":{"devices":[{"pushkey":"http://127.0.0.1:12345/mytopic?up=1"}]}}` + response := request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil) + require.Equal(t, 507, response.Code) + require.Equal(t, `{"rejected":[]}`+"\n", response.Body.String()) +} + func TestServer_MatrixGateway_Push_Failure_InvalidPushkey(t *testing.T) { s := newTestServer(t, newTestConfig(t)) notification := `{"notification":{"devices":[{"pushkey":"http://wrong-base-url.com/mytopic?up=1"}]}}`