forked from mtusi2026/mtusi2026lab4
185 lines
5.2 KiB
Go
185 lines
5.2 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"mtusi2026/internal/model"
|
|
)
|
|
|
|
func TestIdempotency_RepeatReturnsSameMessage(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
defer srv.Close()
|
|
alice := mustRegisterUser(t, srv, "alice")
|
|
chat := mustCreateChat(t, srv, alice, "Чат", []string{alice.ID})
|
|
|
|
key := newKey()
|
|
body := model.CreateMessageRequest{Text: "привет"}
|
|
|
|
r1 := postMessage(t, srv, chat.ID, alice.Token, key, body)
|
|
if r1.StatusCode != http.StatusCreated {
|
|
t.Fatalf("first: expected 201, got %d", r1.StatusCode)
|
|
}
|
|
m1 := decodeBody[model.Message](t, r1)
|
|
|
|
r2 := postMessage(t, srv, chat.ID, alice.Token, key, body)
|
|
if r2.StatusCode != http.StatusCreated {
|
|
t.Fatalf("repeat: expected 201, got %d", r2.StatusCode)
|
|
}
|
|
m2 := decodeBody[model.Message](t, r2)
|
|
|
|
if m1.ID != m2.ID || !m1.CreatedAt.Equal(m2.CreatedAt) {
|
|
t.Fatalf("repeat returned a different message: %+v vs %+v", m1, m2)
|
|
}
|
|
|
|
page := decodeBody[model.MessagePage](t,
|
|
getJSON(t, srv, fmt.Sprintf("/api/v1/chats/%s/messages", chat.ID), alice.Token))
|
|
if len(page.Items) != 1 {
|
|
t.Fatalf("expected 1 message in history, got %d", len(page.Items))
|
|
}
|
|
}
|
|
|
|
func TestIdempotency_DifferentBodySameKey_Returns409(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
defer srv.Close()
|
|
alice := mustRegisterUser(t, srv, "alice")
|
|
chat := mustCreateChat(t, srv, alice, "Чат", []string{alice.ID})
|
|
|
|
key := newKey()
|
|
postMessage(t, srv, chat.ID, alice.Token, key, model.CreateMessageRequest{Text: "один"}).Body.Close()
|
|
|
|
resp := postMessage(t, srv, chat.ID, alice.Token, key, model.CreateMessageRequest{Text: "два"})
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusConflict {
|
|
t.Fatalf("expected 409, got %d", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestIdempotency_MissingOrInvalidKey_Returns400(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
defer srv.Close()
|
|
alice := mustRegisterUser(t, srv, "alice")
|
|
chat := mustCreateChat(t, srv, alice, "Чат", []string{alice.ID})
|
|
|
|
keys := map[string]string{
|
|
"missing": "",
|
|
"space": "bad key",
|
|
"too long": strings.Repeat("a", 129),
|
|
"cyrillic": "ключ",
|
|
}
|
|
for name, key := range keys {
|
|
resp := postMessage(t, srv, chat.ID, alice.Token, key, model.CreateMessageRequest{Text: "x"})
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Errorf("%s: expected 400, got %d", name, resp.StatusCode)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIdempotency_ConcurrentSameKey_OneMessage(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
defer srv.Close()
|
|
alice := mustRegisterUser(t, srv, "alice")
|
|
chat := mustCreateChat(t, srv, alice, "Чат", []string{alice.ID})
|
|
|
|
const workers = 20
|
|
key := newKey()
|
|
start := make(chan struct{})
|
|
type result struct {
|
|
status int
|
|
id string
|
|
err error
|
|
}
|
|
results := make(chan result, workers)
|
|
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < workers; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
<-start
|
|
resp, err := doMessage(srv, chat.ID, alice.Token, key, model.CreateMessageRequest{Text: "гонка"})
|
|
if err != nil {
|
|
results <- result{err: err}
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
var m model.Message
|
|
_ = t.Fatal
|
|
results <- result{status: resp.StatusCode, id: m.ID}
|
|
}()
|
|
}
|
|
close(start)
|
|
wg.Wait()
|
|
close(results)
|
|
|
|
ids := map[string]bool{}
|
|
for r := range results {
|
|
if r.err != nil {
|
|
t.Errorf("request error: %v", r.err)
|
|
continue
|
|
}
|
|
if r.status != http.StatusCreated {
|
|
t.Errorf("expected 201, got %d", r.status)
|
|
}
|
|
ids[r.id] = true
|
|
}
|
|
if len(ids) != 1 {
|
|
t.Fatalf("expected one distinct message id, got %d", len(ids))
|
|
}
|
|
|
|
page := decodeBody[model.MessagePage](t,
|
|
getJSON(t, srv, fmt.Sprintf("/api/v1/chats/%s/messages", chat.ID), alice.Token))
|
|
if len(page.Items) != 1 {
|
|
t.Fatalf("expected exactly 1 message stored, got %d", len(page.Items))
|
|
}
|
|
}
|
|
|
|
func TestIdempotency_ScopeIsPerUserAndChat(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
defer srv.Close()
|
|
alice := mustRegisterUser(t, srv, "alice")
|
|
bob := mustRegisterUser(t, srv, "bob")
|
|
chatA := mustCreateChat(t, srv, alice, "A", []string{alice.ID, bob.ID})
|
|
chatB := mustCreateChat(t, srv, alice, "B", []string{alice.ID, bob.ID})
|
|
|
|
key := newKey()
|
|
body := model.CreateMessageRequest{Text: "то же самое"}
|
|
ids := map[string]bool{}
|
|
for _, c := range []struct {
|
|
chat string
|
|
token string
|
|
}{
|
|
{chatA.ID, alice.Token}, {chatB.ID, alice.Token}, {chatA.ID, bob.Token},
|
|
} {
|
|
resp := postMessage(t, srv, c.chat, c.token, key, body)
|
|
if resp.StatusCode != http.StatusCreated {
|
|
t.Fatalf("expected 201, got %d", resp.StatusCode)
|
|
}
|
|
ids[decodeBody[model.Message](t, resp).ID] = true
|
|
}
|
|
if len(ids) != 3 {
|
|
t.Fatalf("same key in different scopes must create 3 messages, got %d", len(ids))
|
|
}
|
|
}
|
|
|
|
func TestIdempotency_ForeignChatRepeat_Returns404(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
defer srv.Close()
|
|
alice := mustRegisterUser(t, srv, "alice")
|
|
carol := mustRegisterUser(t, srv, "carol")
|
|
chat := mustCreateChat(t, srv, alice, "Приватный", []string{alice.ID})
|
|
|
|
key := newKey()
|
|
for i := 0; i < 2; i++ {
|
|
resp := postMessage(t, srv, chat.ID, carol.Token, key, model.CreateMessageRequest{Text: "чужой"})
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
t.Fatalf("attempt %d: expected 404, got %d", i, resp.StatusCode)
|
|
}
|
|
}
|
|
}
|