forked from mtusi2026/mtusi2026lab3
104 lines
4.1 KiB
Go
104 lines
4.1 KiB
Go
package tests
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"messenger/src/backend/services"
|
|
)
|
|
|
|
func TestCaseVariantExtraFieldsReturn400WithoutSaving(t *testing.T) {
|
|
f := newTestFixture(t)
|
|
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
|
chat := createTestChat(t, f.chats, "Chat", []string{user.ID})
|
|
path := "/api/v1/chats/" + chat.ID + "/messages"
|
|
key := uuid.NewString()
|
|
send := func(path, body, key string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
|
|
r.Header.Set("Content-Type", "application/json")
|
|
r.Header.Set("Idempotency-Key", key)
|
|
authorizeTestRequest(t, f, r, user.ID)
|
|
w := httptest.NewRecorder()
|
|
f.router.ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
for _, test := range []struct{ path, body string }{
|
|
{path, `{"text":"first","TEXT":"second"}`},
|
|
{path, `{"TEXT":"second","text":"first"}`},
|
|
{path, `{"Text":"second"}`},
|
|
{path, `{"text":"first","Attachment_IDs":[]}`},
|
|
{"/api/v1/chats", fmt.Sprintf(`{"title":"Chat","TITLE":"other","member_ids":[%q]}`, user.ID)},
|
|
{"/api/v1/chats", fmt.Sprintf(`{"title":"Chat","MEMBER_IDS":[%q]}`, user.ID)},
|
|
{"/api/v1/users", `{"username":"new","USERNAME":"other","display_name":"New","password":"test-password"}`},
|
|
{"/api/v1/users", `{"username":"new","display_name":"New","Password":"test-password"}`},
|
|
} {
|
|
w := send(test.path, test.body, key)
|
|
if w.Code != 400 || !strings.Contains(w.Body.String(), `"code":"invalid_request"`) {
|
|
t.Fatalf("%s: want invalid_request 400, got %d %s", test.path, w.Code, w.Body.String())
|
|
}
|
|
}
|
|
if len(listTestMessages(t, f.messages, chat.ID)) != 0 {
|
|
t.Fatal("rejected request saved a message")
|
|
}
|
|
w := send(path, `{"text":"first"}`, key)
|
|
if w.Code != 201 {
|
|
t.Fatalf("corrected request with same key: %d %s", w.Code, w.Body.String())
|
|
}
|
|
if w = send(path, `{"text":"first","attachment_ids":[]}`, key); w.Code != 201 {
|
|
t.Fatalf("valid replay: %d %s", w.Code, w.Body.String())
|
|
}
|
|
messages := listTestMessages(t, f.messages, chat.ID)
|
|
if len(messages) != 1 || messages[0].Text != "first" {
|
|
t.Fatal("wrong message or duplicate saved")
|
|
}
|
|
}
|
|
|
|
func TestChatRejectsCaseVariantDuplicateMembersBeforeSQL(t *testing.T) {
|
|
const id = "abcdef01-abcd-4abc-8abc-abcdef012345"
|
|
for _, members := range [][]string{{id, strings.ToUpper(id)}, {strings.ToUpper(id), id}} {
|
|
_, err := services.NewChatService(nil).Create(context.Background(), id, "Chat", members)
|
|
var invalid services.InvalidInput
|
|
if !errors.As(err, &invalid) {
|
|
t.Fatalf("want InvalidInput before SQL, got %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestChatNormalizesMemberUUIDsHTTP(t *testing.T) {
|
|
f := newTestFixture(t)
|
|
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
|
for _, duplicate := range []bool{true, false} {
|
|
members := fmt.Sprintf(`[%q]`, strings.ToUpper(user.ID))
|
|
want := 201
|
|
if duplicate {
|
|
members = fmt.Sprintf(`[%q,%q]`, user.ID, strings.ToUpper(user.ID))
|
|
want = 400
|
|
}
|
|
r := httptest.NewRequest(http.MethodPost, "/api/v1/chats", strings.NewReader(`{"title":"Chat","member_ids":`+members+`}`))
|
|
r.Header.Set("Content-Type", "application/json")
|
|
authorizeTestRequest(t, f, r, user.ID)
|
|
w := httptest.NewRecorder()
|
|
f.router.ServeHTTP(w, r)
|
|
if w.Code != want {
|
|
t.Fatalf("duplicate=%v: want %d, got %d %s", duplicate, want, w.Code, w.Body.String())
|
|
}
|
|
}
|
|
var chats, members int
|
|
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM chats").Scan(&chats); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM chat_members").Scan(&members); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if chats != 1 || members != 1 {
|
|
t.Fatalf("unexpected persisted chats=%d members=%d", chats, members)
|
|
}
|
|
}
|