forked from mtusi2026/mtusi2026lab2
313 lines
8.2 KiB
Go
313 lines
8.2 KiB
Go
package fiber
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// TestController_ChatsCreate checks membership and request validation in isolated chats.
|
|
func TestController_ChatsCreate(t *testing.T) {
|
|
t.Run("Positive: caller included", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
actor := user(t, ctrl)
|
|
payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{actor}})
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusCreated, body)
|
|
if body["title"] != "Team" || !reflect.DeepEqual(body["member_ids"], []any{actor}) {
|
|
t.Fatalf("chat fields changed: %v", body)
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: empty members", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
actor := user(t, ctrl)
|
|
payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{}})
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusBadRequest, body)
|
|
assertErrorCode(t, body, "invalid_request")
|
|
})
|
|
|
|
t.Run("Negative: duplicate members", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
actor := user(t, ctrl)
|
|
payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{actor, actor}})
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusBadRequest, body)
|
|
assertErrorCode(t, body, "invalid_request")
|
|
})
|
|
|
|
t.Run("Negative: caller omitted", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
actor := user(t, ctrl)
|
|
outsider := user(t, ctrl)
|
|
payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{outsider}})
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusBadRequest, body)
|
|
assertErrorCode(t, body, "invalid_request")
|
|
})
|
|
|
|
t.Run("Negative: unknown member", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
actor := user(t, ctrl)
|
|
payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{actor, uuid.NewString()}})
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusBadRequest, body)
|
|
assertErrorCode(t, body, "invalid_request")
|
|
})
|
|
|
|
t.Run("Negative: malformed member ID", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
actor := user(t, ctrl)
|
|
payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{actor, "invalid"}})
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusBadRequest, body)
|
|
assertErrorCode(t, body, "invalid_request")
|
|
})
|
|
|
|
t.Run("Negative: blank title", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
actor := user(t, ctrl)
|
|
payload := mustJSON(t, map[string]any{"title": " ", "member_ids": []string{actor}})
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusBadRequest, body)
|
|
assertErrorCode(t, body, "invalid_request")
|
|
})
|
|
|
|
t.Run("Negative: malformed JSON", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
actor := user(t, ctrl)
|
|
payload := []byte(`{`)
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusBadRequest, body)
|
|
assertErrorCode(t, body, "invalid_request")
|
|
})
|
|
}
|
|
|
|
// TestController_ChatsGetByID checks access and the returned chat identity.
|
|
func TestController_ChatsGetByID(t *testing.T) {
|
|
t.Run("Positive: member reads chat", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
owner := user(t, ctrl)
|
|
id := chat(t, ctrl, owner)
|
|
actor, target := owner, id
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+target, actor, nil)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusOK, body)
|
|
if body["id"] != id || body["title"] != "Чат" {
|
|
t.Fatalf("wrong chat: %v", body)
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: outsider", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
owner := user(t, ctrl)
|
|
id := chat(t, ctrl, owner)
|
|
actor, target := user(t, ctrl), id
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+target, actor, nil)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusNotFound, body)
|
|
assertErrorCode(t, body, "not_found")
|
|
})
|
|
|
|
t.Run("Negative: unknown chat", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
owner := user(t, ctrl)
|
|
actor, target := owner, uuid.NewString()
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+target, actor, nil)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusNotFound, body)
|
|
assertErrorCode(t, body, "not_found")
|
|
})
|
|
|
|
t.Run("Negative: malformed chat ID", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
owner := user(t, ctrl)
|
|
actor, target := owner, "invalid"
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+target, actor, nil)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusBadRequest, body)
|
|
assertErrorCode(t, body, "invalid_request")
|
|
})
|
|
|
|
t.Run("Negative: authentication precedes ID parsing", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
actor, target := "", "invalid"
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+target, actor, nil)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusUnauthorized, body)
|
|
assertErrorCode(t, body, "unauthorized")
|
|
})
|
|
}
|
|
|
|
// TestController_ChatsGetPage checks that foreign memberships remain absent from lists.
|
|
func TestController_ChatsGetPage(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
owner := user(t, ctrl)
|
|
chat(t, ctrl, owner)
|
|
outsider := user(t, ctrl)
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats", outsider, nil)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusOK, body)
|
|
if len(body["items"].([]any)) != 0 || body["next_cursor"] != nil {
|
|
t.Fatalf("foreign chat leaked: %v", body)
|
|
}
|
|
}
|
|
|
|
// TestController_ChatPagination recovers every chat exactly once across page boundaries.
|
|
func TestController_ChatPagination(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
actor := user(t, ctrl)
|
|
expected := make(map[string]bool)
|
|
for range 15 {
|
|
expected[chat(t, ctrl, actor)] = true
|
|
}
|
|
|
|
// Act.
|
|
items := history(t, ctrl, actor, "/api/v1/chats")
|
|
|
|
// Assert.
|
|
if len(items) != len(expected) {
|
|
t.Fatalf("got %d chats, want %d", len(items), len(expected))
|
|
}
|
|
for _, item := range items {
|
|
id := item.(map[string]any)["id"].(string)
|
|
if !expected[id] {
|
|
t.Fatalf("duplicate or unexpected chat %s", id)
|
|
}
|
|
delete(expected, id)
|
|
}
|
|
}
|
|
|
|
// TestController_ChatCursorScope rejects a continuation issued for another user's list.
|
|
func TestController_ChatCursorScope(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
owner := user(t, ctrl)
|
|
outsider := user(t, ctrl)
|
|
chat(t, ctrl, owner)
|
|
chat(t, ctrl, owner)
|
|
page := call(t, ctrl, http.MethodGet, "/api/v1/chats?limit=1", owner, nil, http.StatusOK)
|
|
token := page["next_cursor"].(string)
|
|
|
|
// Act.
|
|
status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats?cursor="+url.QueryEscape(token), outsider, nil)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertHTTPStatus(t, status, http.StatusBadRequest, body)
|
|
assertErrorCode(t, body, "invalid_request")
|
|
}
|