server_matrix_test.go 4.8 KB

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