forked from mtusi2026/mtusi2026lab3
fix: json decoder and uuid dubles
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestDecodeJSONBodyRequiresExactFieldNames(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
user, _ := createTestUser(t, fixture.users, "json_tester", "JSON tester")
|
||||
chat := createTestChat(t, fixture.chats, "JSON body tests", []string{user.ID})
|
||||
path := "/api/v1/chats/" + chat.ID + "/messages"
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
body string
|
||||
valid bool
|
||||
}{
|
||||
{"canonical", `{"text":"first","attachment_ids":[]}`, true},
|
||||
{"escaped canonical name", `{"te\u0078t":"first"}`, true},
|
||||
{"uppercase extra", `{"text":"first","TEXT":"second"}`, false},
|
||||
{"uppercase extra first", `{"TEXT":"second","text":"first"}`, false},
|
||||
{"mixed case", `{"Text":"second"}`, false},
|
||||
{"uppercase attachments", `{"text":"first","ATTACHMENT_IDS":[]}`, false},
|
||||
{"escaped uppercase", `{"text":"first","\u0054EXT":"second"}`, false},
|
||||
{"unknown field", `{"text":"first","sender_id":"other"}`, false},
|
||||
{"wrong type", `{"text":123}`, false},
|
||||
{"null", `null`, false},
|
||||
{"array", `[]`, false},
|
||||
{"trailing object", `{"text":"first"}{}`, false},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodPost, path, strings.NewReader(test.body))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
authorizeTestRequest(t, fixture, request, user.ID)
|
||||
response := httptest.NewRecorder()
|
||||
fixture.router.ServeHTTP(response, request)
|
||||
want := http.StatusBadRequest
|
||||
if test.valid {
|
||||
want = http.StatusCreated
|
||||
}
|
||||
if response.Code != want {
|
||||
t.Fatalf("want %d, got %d: %s", want, response.Code, response.Body.String())
|
||||
}
|
||||
if test.valid {
|
||||
var value map[string]any
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value["text"] != "first" {
|
||||
t.Fatal("text changed")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user