Files

271 lines
8.3 KiB
Go

package fiber
import (
"net/http"
"net/url"
"strings"
"testing"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config"
)
// TestController_MessagesCreate checks message validation and identity-derived sender fields.
func TestController_MessagesCreate(t *testing.T) {
t.Run("Positive: Unicode at rune limit", func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
owner := user(t, ctrl)
id := chat(t, ctrl, owner)
actor := owner
text := strings.Repeat("界", 4000)
payload := mustJSON(t, map[string]string{"text": text})
// Act.
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusCreated, body)
if body["sender_id"] != owner || body["chat_id"] != id || body["text"] != text {
t.Fatalf("message fields changed: %v", body)
}
})
t.Run("Negative: empty text", func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
owner := user(t, ctrl)
id := chat(t, ctrl, owner)
actor := owner
payload := mustJSON(t, map[string]string{"text": ""})
// Act.
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusBadRequest, body)
assertErrorCode(t, body, "invalid_request")
})
t.Run("Negative: whitespace text", func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
owner := user(t, ctrl)
id := chat(t, ctrl, owner)
actor := owner
payload := mustJSON(t, map[string]string{"text": " \n\t"})
// Act.
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusBadRequest, body)
assertErrorCode(t, body, "invalid_request")
})
t.Run("Negative: text exceeds rune limit", func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
owner := user(t, ctrl)
id := chat(t, ctrl, owner)
actor := owner
payload := mustJSON(t, map[string]string{"text": strings.Repeat("界", 4001)})
// Act.
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusBadRequest, body)
assertErrorCode(t, body, "invalid_request")
})
t.Run("Negative: sender spoof", func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
owner := user(t, ctrl)
id := chat(t, ctrl, owner)
actor := owner
outsider := user(t, ctrl)
payload := mustJSON(t, map[string]string{"text": "hello", "sender_id": outsider})
// Act.
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusBadRequest, body)
assertErrorCode(t, body, "invalid_request")
})
t.Run("Negative: outsider", func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
owner := user(t, ctrl)
id := chat(t, ctrl, owner)
actor := user(t, ctrl)
payload := mustJSON(t, map[string]string{"text": "hello"})
// Act.
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusNotFound, body)
assertErrorCode(t, body, "not_found")
})
t.Run("Negative: authentication precedes JSON decoding", func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
owner := user(t, ctrl)
id := chat(t, ctrl, owner)
actor := ""
payload := []byte(`{`)
// Act.
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusUnauthorized, body)
assertErrorCode(t, body, "unauthorized")
})
}
// TestController_MessagesGetPage rejects invalid pagination separately from access failures.
func TestController_MessagesGetPage(t *testing.T) {
cases := []struct {
name string
query string
outsider bool
status int
code string
}{
{"Negative: zero limit", "limit=0", false, http.StatusBadRequest, "invalid_request"},
{"Negative: limit above maximum", "limit=101", false, http.StatusBadRequest, "invalid_request"},
{"Negative: negative limit", "limit=-1", false, http.StatusBadRequest, "invalid_request"},
{"Negative: nonnumeric limit", "limit=x", false, http.StatusBadRequest, "invalid_request"},
{"Negative: fractional limit", "limit=1.0", false, http.StatusBadRequest, "invalid_request"},
{"Negative: malformed cursor", "cursor=invalid", false, http.StatusBadRequest, "invalid_request"},
{"Negative: outsider", "", true, http.StatusNotFound, "not_found"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
actor := user(t, ctrl)
id := chat(t, ctrl, actor)
if tc.outsider {
actor = user(t, ctrl)
}
// Act.
status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+id+"/messages?"+tc.query, actor, nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, tc.status, body)
assertErrorCode(t, body, tc.code)
})
}
}
// TestController_MessageCursorScope rejects a continuation from another chat's history.
func TestController_MessageCursorScope(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
actor := user(t, ctrl)
first := chat(t, ctrl, actor)
second := chat(t, ctrl, actor)
path := "/api/v1/chats/" + first + "/messages"
for _, text := range []string{"first", "second"} {
call(t, ctrl, http.MethodPost, path, actor, map[string]string{"text": text}, http.StatusCreated)
}
page := call(t, ctrl, http.MethodGet, path+"?limit=1", actor, nil, http.StatusOK)
token := page["next_cursor"].(string)
// Act.
status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+second+"/messages?cursor="+url.QueryEscape(token), actor, nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusBadRequest, body)
assertErrorCode(t, body, "invalid_request")
}
// TestController_MessagePageLimitChange preserves continuation when the client changes page size.
func TestController_MessagePageLimitChange(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
actor := user(t, ctrl)
path := "/api/v1/chats/" + chat(t, ctrl, actor) + "/messages"
for _, text := range []string{"first", "second", "third"} {
call(t, ctrl, http.MethodPost, path, actor, map[string]string{"text": text}, http.StatusCreated)
}
first := call(t, ctrl, http.MethodGet, path+"?limit=1", actor, nil, http.StatusOK)
token := first["next_cursor"].(string)
// Act.
status, body, err := request(ctrl, http.MethodGet, path+"?limit=100&cursor="+url.QueryEscape(token), actor, nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusOK, body)
items := body["items"].([]any)
if len(items) != 2 || body["next_cursor"] != nil {
t.Fatalf("unexpected final page: %v", body)
}
if items[0].(map[string]any)["text"] != "second" || items[1].(map[string]any)["text"] != "third" {
t.Fatalf("lost order or skipped messages: %v", items)
}
}
// TestController_MessageCapacity preserves acknowledged data after rejecting an overflow write.
func TestController_MessageCapacity(t *testing.T) {
// Arrange.
ctrl := testController(t, func(cfg *config.Config) {
cfg.Memory.MaxMessages = 2
})
actor := user(t, ctrl)
path := "/api/v1/chats/" + chat(t, ctrl, actor) + "/messages"
first := call(t, ctrl, http.MethodPost, path, actor, map[string]string{"text": "first"}, http.StatusCreated)
second := call(t, ctrl, http.MethodPost, path, actor, map[string]string{"text": "second"}, http.StatusCreated)
// Act.
status, body, err := request(ctrl, http.MethodPost, path, actor, []byte(`{"text":"rejected"}`))
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusServiceUnavailable, body)
assertErrorCode(t, body, "unavailable")
items := history(t, ctrl, actor, path)
if len(items) != 2 || items[0].(map[string]any)["id"] != first["id"] || items[1].(map[string]any)["id"] != second["id"] {
t.Fatalf("overflow changed acknowledged messages: %v", items)
}
}