clone

a self-hosted multitenant nostr relay with sites, file storage, git hosting, and inbox and outbox delivery

feat/standalone-relay
tinyrelay / tinyclient / chat_pages_test.go
chat_pages_test.go 5.1K
1package tinyclient23import (4 "context"5 "encoding/json"6 "net/http"7 "net/http/httptest"8 "strings"9 "testing"1011 "github.com/FelineStateMachine/tinyrelay/internal/policy"12)1314type chatBackend struct {15 fakeBackend16 methods []string17 actors []string18 params []json.RawMessage19}2021func (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": ""}, nil30 case "browsedirectmessages":31 return map[string]any{"events": []any{}, "next_cursor": "next"}, nil32 default:33 return nil, nil34 }35}3637func 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, backend45}4647func 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 sees51 // 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}5960func 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}7677func 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}9293func 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]any110 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}114115func TestChatRouteRejectsUnsupportedMethodsAndPaths(t *testing.T) {116 app, _ := chatApp(t, roomOwner)117 for _, tc := range []struct {118 method, path string119 status int120 }{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}