server_matrix_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package server
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "net/http"
  5. "net/http/httptest"
  6. "strings"
  7. "testing"
  8. )
  9. func TestMatrix_NewRequestFromMatrixJSON_Success(t *testing.T) {
  10. baseURL := "https://ntfy.sh"
  11. maxLength := 4096
  12. 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"}}`
  13. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
  14. newRequest, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
  15. require.Nil(t, err)
  16. require.Equal(t, "POST", newRequest.Method)
  17. require.Equal(t, "https://ntfy.sh/upABCDEFGHI?up=1", newRequest.URL.String())
  18. require.Equal(t, "https://ntfy.sh/upABCDEFGHI?up=1", newRequest.Header.Get("X-Matrix-Pushkey"))
  19. require.Equal(t, body, readAll(t, newRequest.Body))
  20. }
  21. func TestMatrix_NewRequestFromMatrixJSON_TooLarge(t *testing.T) {
  22. baseURL := "https://ntfy.sh"
  23. maxLength := 10 // Small
  24. 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"}}`
  25. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
  26. _, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
  27. require.Equal(t, errHTTPEntityTooLargeMatrixRequestTooLarge, err)
  28. }
  29. func TestMatrix_NewRequestFromMatrixJSON_InvalidJSON(t *testing.T) {
  30. baseURL := "https://ntfy.sh"
  31. maxLength := 4096
  32. body := `this is not json`
  33. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
  34. _, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
  35. require.Equal(t, errHTTPBadRequestMatrixMessageInvalid, err)
  36. }
  37. func TestMatrix_NewRequestFromMatrixJSON_NotAMatrixMessage(t *testing.T) {
  38. baseURL := "https://ntfy.sh"
  39. maxLength := 4096
  40. body := `{"message":"this is not a matrix message, but valid json"}`
  41. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
  42. _, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
  43. require.Equal(t, errHTTPBadRequestMatrixMessageInvalid, err)
  44. }
  45. func TestMatrix_NewRequestFromMatrixJSON_MismatchingPushKey(t *testing.T) {
  46. baseURL := "https://ntfy.sh" // Mismatch!
  47. maxLength := 4096
  48. 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"}}`
  49. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
  50. _, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
  51. matrixErr, ok := err.(*errMatrix)
  52. require.True(t, ok)
  53. 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())
  54. require.Equal(t, "https://ntfy.example.com/upABCDEFGHI?up=1", matrixErr.pushKey)
  55. }
  56. func TestMatrix_WriteMatrixDiscoveryResponse(t *testing.T) {
  57. w := httptest.NewRecorder()
  58. require.Nil(t, writeMatrixDiscoveryResponse(w))
  59. require.Equal(t, 200, w.Result().StatusCode)
  60. require.Equal(t, `{"unifiedpush":{"gateway":"matrix"}}`+"\n", w.Body.String())
  61. }
  62. func TestMatrix_WriteMatrixError(t *testing.T) {
  63. w := httptest.NewRecorder()
  64. r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", nil)
  65. v := newVisitor(newTestConfig(t), nil, "1.2.3.4")
  66. require.Nil(t, writeMatrixError(w, r, v, &errMatrix{"https://ntfy.example.com/upABCDEFGHI?up=1", errHTTPBadRequestMatrixPushkeyBaseURLMismatch}))
  67. require.Equal(t, 200, w.Result().StatusCode)
  68. require.Equal(t, `{"rejected":["https://ntfy.example.com/upABCDEFGHI?up=1"]}`+"\n", w.Body.String())
  69. }
  70. func TestMatrix_WriteMatrixSuccess(t *testing.T) {
  71. w := httptest.NewRecorder()
  72. require.Nil(t, writeMatrixSuccess(w))
  73. require.Equal(t, 200, w.Result().StatusCode)
  74. require.Equal(t, `{"rejected":[]}`+"\n", w.Body.String())
  75. }