server_matrix_test.go 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package server
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "net/netip"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestMatrix_NewRequestFromMatrixJSON_Success(t *testing.T) {
  11. baseURL := "https://ntfy.sh"
  12. maxLength := 4096
  13. body := `{"notification":{"content":{"body":"I'm floating in a most peculiar way.","msgtype":"m.text"},"counts":{"missed_calls":1,"unread":2},"devices":[{"app_id":"org.matrix.matrixConsole.ios","data":{},"pushkey":"https://ntfy.sh/upABCDEFGHI?up=1","pushkey_ts":12345678,"tweaks":{"sound":"bing"}}],"event_id":"$3957tyerfgewrf384","prio":"high","room_alias":"#exampleroom:matrix.org","room_id":"!slw48wfj34rtnrf:example.com","room_name":"Mission Control","sender":"@exampleuser:matrix.org","sender_display_name":"Major Tom","type":"m.room.message"}}`
  14. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
  15. newRequest, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
  16. require.Nil(t, err)
  17. require.Equal(t, "POST", newRequest.Method)
  18. require.Equal(t, "https://ntfy.sh/upABCDEFGHI?up=1", newRequest.URL.String())
  19. require.Equal(t, "https://ntfy.sh/upABCDEFGHI?up=1", newRequest.Header.Get("X-Matrix-Pushkey"))
  20. require.Equal(t, body, readAll(t, newRequest.Body))
  21. }
  22. func TestMatrix_NewRequestFromMatrixJSON_TooLarge(t *testing.T) {
  23. baseURL := "https://ntfy.sh"
  24. maxLength := 10 // Small
  25. body := `{"notification":{"content":{"body":"I'm floating in a most peculiar way.","msgtype":"m.text"},"counts":{"missed_calls":1,"unread":2},"devices":[{"app_id":"org.matrix.matrixConsole.ios","data":{},"pushkey":"https://ntfy.sh/upABCDEFGHI?up=1","pushkey_ts":12345678,"tweaks":{"sound":"bing"}}],"event_id":"$3957tyerfgewrf384","prio":"high","room_alias":"#exampleroom:matrix.org","room_id":"!slw48wfj34rtnrf:example.com","room_name":"Mission Control","sender":"@exampleuser:matrix.org","sender_display_name":"Major Tom","type":"m.room.message"}}`
  26. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
  27. _, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
  28. require.Equal(t, errHTTPEntityTooLargeMatrixRequest, err)
  29. }
  30. func TestMatrix_NewRequestFromMatrixJSON_InvalidJSON(t *testing.T) {
  31. baseURL := "https://ntfy.sh"
  32. maxLength := 4096
  33. body := `this is not json`
  34. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
  35. _, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
  36. require.Equal(t, errHTTPBadRequestMatrixMessageInvalid, err)
  37. }
  38. func TestMatrix_NewRequestFromMatrixJSON_NotAMatrixMessage(t *testing.T) {
  39. baseURL := "https://ntfy.sh"
  40. maxLength := 4096
  41. body := `{"message":"this is not a matrix message, but valid json"}`
  42. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
  43. _, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
  44. require.Equal(t, errHTTPBadRequestMatrixMessageInvalid, err)
  45. }
  46. func TestMatrix_NewRequestFromMatrixJSON_MismatchingPushKey(t *testing.T) {
  47. baseURL := "https://ntfy.sh" // Mismatch!
  48. maxLength := 4096
  49. body := `{"notification":{"content":{"body":"I'm floating in a most peculiar way.","msgtype":"m.text"},"counts":{"missed_calls":1,"unread":2},"devices":[{"app_id":"org.matrix.matrixConsole.ios","data":{},"pushkey":"https://ntfy.example.com/upABCDEFGHI?up=1","pushkey_ts":12345678,"tweaks":{"sound":"bing"}}],"event_id":"$3957tyerfgewrf384","prio":"high","room_alias":"#exampleroom:matrix.org","room_id":"!slw48wfj34rtnrf:example.com","room_name":"Mission Control","sender":"@exampleuser:matrix.org","sender_display_name":"Major Tom","type":"m.room.message"}}`
  50. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
  51. _, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
  52. matrixErr, ok := err.(*errMatrix)
  53. require.True(t, ok)
  54. require.Equal(t, "invalid request: push key must be prefixed with base URL, received push key: https://ntfy.example.com/upABCDEFGHI?up=1, configured base URL: https://ntfy.sh", matrixErr.err.Error())
  55. require.Equal(t, "https://ntfy.example.com/upABCDEFGHI?up=1", matrixErr.pushKey)
  56. }
  57. func TestMatrix_WriteMatrixDiscoveryResponse(t *testing.T) {
  58. w := httptest.NewRecorder()
  59. require.Nil(t, writeMatrixDiscoveryResponse(w))
  60. require.Equal(t, 200, w.Result().StatusCode)
  61. require.Equal(t, `{"unifiedpush":{"gateway":"matrix"}}`+"\n", w.Body.String())
  62. }
  63. func TestMatrix_WriteMatrixError(t *testing.T) {
  64. w := httptest.NewRecorder()
  65. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", nil)
  66. v := newVisitor(newTestConfig(t), nil, nil, netip.MustParseAddr("1.2.3.4"), nil)
  67. require.Nil(t, writeMatrixError(w, r, v, &errMatrix{"https://ntfy.example.com/upABCDEFGHI?up=1", errHTTPBadRequestMatrixPushkeyBaseURLMismatch}))
  68. require.Equal(t, 200, w.Result().StatusCode)
  69. require.Equal(t, `{"rejected":["https://ntfy.example.com/upABCDEFGHI?up=1"]}`+"\n", w.Body.String())
  70. }
  71. func TestMatrix_WriteMatrixSuccess(t *testing.T) {
  72. w := httptest.NewRecorder()
  73. require.Nil(t, writeMatrixSuccess(w))
  74. require.Equal(t, 200, w.Result().StatusCode)
  75. require.Equal(t, `{"rejected":[]}`+"\n", w.Body.String())
  76. }