forked from mtusi2026/mtusi2026lab2
116 lines
4.4 KiB
Go
116 lines
4.4 KiB
Go
package http
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/api/http/dto"
|
|
)
|
|
|
|
func TestAccess_ForeignChat(t *testing.T) {
|
|
r := newTestRig(t)
|
|
_, _, eve, chat := bootstrap(r)
|
|
base := "/api/v1/chats/" + chat.ID
|
|
|
|
cases := []struct {
|
|
method, path string
|
|
body any
|
|
}{
|
|
{http.MethodGet, base, nil},
|
|
{http.MethodGet, base + "/messages?limit=10", nil},
|
|
{http.MethodPost, base + "/messages", dto.CreateMessageRequest{Text: "вторжение"}},
|
|
}
|
|
for _, tc := range cases {
|
|
var resp *http.Response
|
|
if tc.body == nil {
|
|
resp = r.do(tc.method, tc.path, eve.ID, nil, nil)
|
|
} else {
|
|
resp = r.doJSON(tc.method, tc.path, eve.ID, tc.body)
|
|
}
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
data, _ := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
t.Fatalf("%s %s expected 404, got %d; body=%s", tc.method, tc.path, resp.StatusCode, string(data))
|
|
}
|
|
e := readError(t, resp)
|
|
if e.Error.Code != CodeNotFound {
|
|
t.Fatalf("%s %s expected code %q, got %q", tc.method, tc.path, CodeNotFound, e.Error.Code)
|
|
}
|
|
}
|
|
|
|
resp := r.do(http.MethodGet, "/api/v1/chats?limit=100", eve.ID, nil, nil)
|
|
expectStatus(t, resp, http.StatusOK)
|
|
var page dto.ChatsPageResponse
|
|
readJSON(t, resp, &page)
|
|
for _, item := range page.Items {
|
|
if item.ID == chat.ID {
|
|
t.Fatalf("чат утёк в список чужого пользователя")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAccess_AuthBeforeContentType(t *testing.T) {
|
|
r := newTestRig(t)
|
|
resp := r.do(http.MethodPost, "/api/v1/chats", "",
|
|
map[string]string{"Content-Type": "text/plain"}, strings.NewReader("x"))
|
|
expectStatus(t, resp, http.StatusUnauthorized)
|
|
resp.Body.Close()
|
|
|
|
resp = r.do(http.MethodPost, "/api/v1/nope", "", nil, strings.NewReader("x"))
|
|
expectStatus(t, resp, http.StatusNotFound)
|
|
if resp.Header.Get("X-Request-Id") == "" {
|
|
t.Fatal("404 without X-Request-Id")
|
|
}
|
|
resp.Body.Close()
|
|
}
|
|
|
|
func TestAccess_RouterErrors(t *testing.T) {
|
|
r := newTestRig(t)
|
|
alice, _, _, chat := bootstrap(r)
|
|
chatPath := "/api/v1/chats/" + chat.ID
|
|
|
|
cases := []struct {
|
|
method, path, actor string
|
|
status int
|
|
code, message string
|
|
}{
|
|
{http.MethodGet, "/nope", "", http.StatusNotFound, CodeNotFound, "Object not found"},
|
|
{http.MethodGet, "/health/live/", "", http.StatusNotFound, CodeNotFound, "Object not found"},
|
|
{http.MethodGet, "/api/v1/nope", alice.ID, http.StatusNotFound, CodeNotFound, "Object not found"},
|
|
{http.MethodPost, "/health/live", "", http.StatusMethodNotAllowed, CodeInvalidRequest, "Method not allowed"},
|
|
{http.MethodGet, "/api/v1/users", "", http.StatusMethodNotAllowed, CodeInvalidRequest, "Method not allowed"},
|
|
{http.MethodPost, "/api/v1/users/me", "", http.StatusMethodNotAllowed, CodeInvalidRequest, "Method not allowed"},
|
|
{http.MethodDelete, "/api/v1/users/me", alice.ID, http.StatusMethodNotAllowed, CodeInvalidRequest, "Method not allowed"},
|
|
{http.MethodDelete, "/api/v1/chats", alice.ID, http.StatusMethodNotAllowed, CodeInvalidRequest, "Method not allowed"},
|
|
{http.MethodPut, chatPath, alice.ID, http.StatusMethodNotAllowed, CodeInvalidRequest, "Method not allowed"},
|
|
{http.MethodPatch, chatPath + "/messages", alice.ID, http.StatusMethodNotAllowed, CodeInvalidRequest, "Method not allowed"},
|
|
}
|
|
for _, tc := range cases {
|
|
var body io.Reader
|
|
headers := map[string]string{}
|
|
if tc.method == http.MethodPost || tc.method == http.MethodPut || tc.method == http.MethodPatch {
|
|
body = bytes.NewReader([]byte(`{}`))
|
|
headers["Content-Type"] = "application/json"
|
|
}
|
|
resp := r.do(tc.method, tc.path, tc.actor, headers, body)
|
|
if resp.StatusCode != tc.status {
|
|
data, _ := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
t.Fatalf("%s %s: expected %d, got %d; body=%s", tc.method, tc.path, tc.status, resp.StatusCode, string(data))
|
|
}
|
|
if tc.status == http.StatusMethodNotAllowed && resp.Header.Get("Allow") == "" {
|
|
t.Fatalf("%s %s: 405 без заголовка Allow", tc.method, tc.path)
|
|
}
|
|
e := readError(t, resp)
|
|
if e.Error.Code != tc.code || e.Error.Message != tc.message {
|
|
t.Fatalf("%s %s: expected %q/%q, got %q/%q", tc.method, tc.path, tc.code, tc.message, e.Error.Code, e.Error.Message)
|
|
}
|
|
if e.Error.RequestID == "" || e.Error.RequestID != resp.Header.Get("X-Request-Id") {
|
|
t.Fatalf("%s %s: request_id %q не совпадает с заголовком %q", tc.method, tc.path, e.Error.RequestID, resp.Header.Get("X-Request-Id"))
|
|
}
|
|
}
|
|
}
|