tinyrelayfile
tinyrelay/tinyclient/chat_pages_test.go/

tinyclient/chat_pages_test.go raw
1package tinyclient
2
3import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10
11 "github.com/FelineStateMachine/tinyrelay/internal/policy"
12)
13
14type chatBackend struct {
15 fakeBackend
16 methods []string
17 actors []string
18 params []json.RawMessage
19}
20
21func (b *chatBackend) Query(_ context.Context, method string, params []json.RawMessage, actor string) (any, error) {
22 b.methods = append(b.methods, method)
23 b.actors = append(b.actors, actor)
24 if len(params) != 0 {
25 b.params = append(b.params, params[0])
26 }
27 switch method {
28 case "browserooms":
29 return map[string]any{"items": []any{roomRecord("general", "General", "open", "")}, "next_cursor": ""}, nil
30 case "browsedirectmessages":
31 return map[string]any{"events": []any{}, "next_cursor": "next"}, nil
32 default:
33 return nil, nil
34 }
35}
36
37func chatApp(t *testing.T, actor string) (*App, *chatBackend) {
38 t.Helper()
39 backend := &chatBackend{fakeBackend: fakeBackend{policy: policy.Defaults(roomOwner)}}
40 app, err := New(backend, Options{Actor: func(*http.Request) (string, error) { return actor, nil }})
41 if err != nil {
42 t.Fatal(err)
43 }
44 return app, backend
45}
46
47func TestRoomsRedirectToChatPreservesPrefixAndQuery(t *testing.T) {
48 app, _ := chatApp(t, roomOwner)
49 req := httptest.NewRequest(http.MethodGet, "/rooms?cursor=older", nil)
50 // Tenant routing normally strips the /r/<slug> prefix before webui sees
51 // the URL. Keep the original request URI here to cover the redirect helper.
52 req.RequestURI = "/r/demo/rooms?cursor=older"
53 res := httptest.NewRecorder()
54 app.ServeHTTP(res, req)
55 if res.Code != http.StatusMovedPermanently || res.Header().Get("Location") != "/r/demo/chat?cursor=older" {
56 t.Fatalf("redirect = %d %q", res.Code, res.Header().Get("Location"))
57 }
58}
59
60func TestChatPageRendersGroupsAndGuestSignin(t *testing.T) {
61 app, _ := chatApp(t, roomOwner)
62 res := httptest.NewRecorder()
63 app.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/chat", nil))
64 body := res.Body.String()
65 if res.Code != http.StatusOK || !strings.Contains(body, "<h1>Chat</h1>") || !strings.Contains(body, "Direct chats") || !strings.Contains(body, `href="/rooms/general"`) {
66 t.Fatalf("signed chat page: %d %s", res.Code, body)
67 }
68 guest, _ := chatApp(t, "")
69 res = httptest.NewRecorder()
70 guest.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/chat", nil))
71 body = res.Body.String()
72 if res.Code != http.StatusOK || !strings.Contains(body, `href="/signin?next=%2Fchat">Sign in</a> to open your direct chats.`) || strings.Contains(body, "<direct-chats") || strings.Contains(body, "<direct-start") {
73 t.Fatalf("guest chat page: %d %s", res.Code, body)
74 }
75}
76
77func TestDirectChatPageDoesNotRenderCiphertextForGuest(t *testing.T) {
78 app, backend := chatApp(t, "")
79 peer := strings.Repeat("d", 64)
80 res := httptest.NewRecorder()
81 app.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/chat/dm/"+peer, nil))
82 body := res.Body.String()
83 if res.Code != http.StatusOK || !strings.Contains(body, "Private conversation") || !strings.Contains(body, `href="/signin?next=%2Fchat%2Fdm%2F`+peer) || strings.Contains(body, "<direct-thread") || strings.Contains(body, "ciphertext") {
84 t.Fatalf("guest direct chat: %d %s", res.Code, body)
85 }
86 for _, method := range backend.methods {
87 if method == "browsedirectmessages" {
88 t.Fatal("guest direct chat queried encrypted messages")
89 }
90 }
91}
92
93func TestDirectMessagesEndpointRequiresActorAndForwardsCursor(t *testing.T) {
94 guest, _ := chatApp(t, "")
95 res := httptest.NewRecorder()
96 guest.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/chat/events?cursor=abc", nil))
97 if res.Code != http.StatusUnauthorized || res.Header().Get("Cache-Control") != "private, no-store" {
98 t.Fatalf("guest events endpoint: %d %q", res.Code, res.Header().Get("Cache-Control"))
99 }
100 app, backend := chatApp(t, roomOwner)
101 res = httptest.NewRecorder()
102 app.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/chat/events?cursor=abc", nil))
103 if res.Code != http.StatusOK || res.Header().Get("Cache-Control") != "private, no-store" || res.Header().Get("Content-Type") != "application/json; charset=utf-8" {
104 t.Fatalf("events endpoint: %d headers=%v body=%s", res.Code, res.Header(), res.Body.String())
105 }
106 if len(backend.methods) != 1 || backend.methods[0] != "browsedirectmessages" || backend.actors[0] != roomOwner {
107 t.Fatalf("backend calls = %#v %#v", backend.methods, backend.actors)
108 }
109 var query map[string]any
110 if err := json.Unmarshal(backend.params[0], &query); err != nil || query["cursor"] != "abc" || query["limit"] != float64(50) {
111 t.Fatalf("query = %#v", query)
112 }
113}
114
115func TestChatRouteRejectsUnsupportedMethodsAndPaths(t *testing.T) {
116 app, _ := chatApp(t, roomOwner)
117 for _, tc := range []struct {
118 method, path string
119 status int
120 }{
121 {http.MethodPost, "/chat/events", http.StatusMethodNotAllowed},
122 {http.MethodGet, "/chat/events/extra", http.StatusNotFound},
123 {http.MethodGet, "/chat/dm/not-a-pubkey", http.StatusNotFound},
124 } {
125 res := httptest.NewRecorder()
126 app.ServeHTTP(res, httptest.NewRequest(tc.method, tc.path, nil))
127 if res.Code != tc.status {
128 t.Errorf("%s %s = %d, want %d", tc.method, tc.path, res.Code, tc.status)
129 }
130 }
131}

About

ownerc01503e8035f
visibilitypublic
headd730679639bf

Maintainers

  • c01503e8035f owner
  • ba1b5beed5b5 agent hermes

Clone

  • https://012.run/npub1cq2s86qrtadf36eqq3epm38wmhdynrrcwxf84eldk3f94dkv7ttsrulrz5/tinyrelay.git

Refs