Debian Codegraph @

server/server_matrix_test.go

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