File: server_matrix_test.go

package info (click to toggle)
ntfy 2.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,360 kB
  • sloc: javascript: 16,782; makefile: 282; sh: 105; php: 21; python: 19
file content (83 lines) | stat: -rw-r--r-- 4,909 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//go:build ignore
package server

import (
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestMatrix_NewRequestFromMatrixJSON_Success(t *testing.T) {
	baseURL := "https://ntfy.sh"
	maxLength := 4096
	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"}}`
	r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
	newRequest, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
	require.Nil(t, err)
	require.Equal(t, "POST", newRequest.Method)
	require.Equal(t, "https://ntfy.sh/upABCDEFGHI?up=1", newRequest.URL.String())
	require.Equal(t, body, readAll(t, newRequest.Body))
}

func TestMatrix_NewRequestFromMatrixJSON_TooLarge(t *testing.T) {
	baseURL := "https://ntfy.sh"
	maxLength := 10 // Small
	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"}}`
	r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
	_, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
	require.Equal(t, errHTTPEntityTooLargeMatrixRequest, err)
}

func TestMatrix_NewRequestFromMatrixJSON_InvalidJSON(t *testing.T) {
	baseURL := "https://ntfy.sh"
	maxLength := 4096
	body := `this is not json`
	r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
	_, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
	require.Equal(t, errHTTPBadRequestMatrixMessageInvalid, err)
}

func TestMatrix_NewRequestFromMatrixJSON_NotAMatrixMessage(t *testing.T) {
	baseURL := "https://ntfy.sh"
	maxLength := 4096
	body := `{"message":"this is not a matrix message, but valid json"}`
	r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
	_, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
	require.Equal(t, errHTTPBadRequestMatrixMessageInvalid, err)
}

func TestMatrix_NewRequestFromMatrixJSON_MismatchingPushKey(t *testing.T) {
	baseURL := "https://ntfy.sh" // Mismatch!
	maxLength := 4096
	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"}}`
	r, _ := http.NewRequest("POST", "http://ntfy.example.com/_matrix/push/v1/notify", strings.NewReader(body))
	_, err := newRequestFromMatrixJSON(r, baseURL, maxLength)
	matrixErr, ok := err.(*errMatrixPushkeyRejected)
	require.True(t, ok)
	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())
	require.Equal(t, "https://ntfy.example.com/upABCDEFGHI?up=1", matrixErr.rejectedPushKey)
}

func TestMatrix_WriteMatrixDiscoveryResponse(t *testing.T) {
	w := httptest.NewRecorder()
	require.Nil(t, writeMatrixDiscoveryResponse(w))
	require.Equal(t, 200, w.Result().StatusCode)
	require.Equal(t, `{"unifiedpush":{"gateway":"matrix"}}`+"\n", w.Body.String())
}

func TestMatrix_WriteMatrixError(t *testing.T) {
	w := httptest.NewRecorder()
	require.Nil(t, writeMatrixResponse(w, "https://ntfy.example.com/upABCDEFGHI?up=1"))
	require.Equal(t, 200, w.Result().StatusCode)
	require.Equal(t, `{"rejected":["https://ntfy.example.com/upABCDEFGHI?up=1"]}`+"\n", w.Body.String())
}

func TestMatrix_WriteMatrixSuccess(t *testing.T) {
	w := httptest.NewRecorder()
	require.Nil(t, writeMatrixSuccess(w))
	require.Equal(t, 200, w.Result().StatusCode)
	require.Equal(t, `{"rejected":[]}`+"\n", w.Body.String())
}