forked from mtusi2026/mtusi2026lab2
chore: port lab 1 solution
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
//go:build contract
|
||||
|
||||
package studenttests_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
|
||||
func TestInvalidJSON(t *testing.T) {
|
||||
resp := doRequest(t, http.MethodPost, "/api/v1/users", uuid.Nil, []byte(`{"username": "broken",`))
|
||||
requireStatus(t, resp, http.StatusBadRequest)
|
||||
|
||||
var apiErr dto.ErrorResponse
|
||||
mustDecode(t, resp, &apiErr)
|
||||
|
||||
if apiErr.Error.Code != dto.ErrorCodeInvalidRequest {
|
||||
t.Errorf("expected code %q, got %q", dto.ErrorCodeInvalidRequest, apiErr.Error.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestForeignChat(t *testing.T) {
|
||||
user1 := createUser(t)
|
||||
user2 := createUser(t)
|
||||
user3 := createUser(t)
|
||||
|
||||
chat := createChat(t, user1, user1.ID, user2.ID)
|
||||
|
||||
resp := doRequest(t, http.MethodGet, "/api/v1/chats/"+chat.ID.String(), user3.ID, nil)
|
||||
requireStatus(t, resp, http.StatusNotFound)
|
||||
|
||||
resp = sendMessage(t, user3, chat.ID, "should not land")
|
||||
requireStatus(t, resp, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func TestEmptyMessage(t *testing.T) {
|
||||
user := createUser(t)
|
||||
chat := createChat(t, user, user.ID)
|
||||
|
||||
for _, text := range []string{"", " ", "\t\n"} {
|
||||
resp := sendMessage(t, user, chat.ID, text)
|
||||
requireStatus(t, resp, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDuplicateUsername(t *testing.T) {
|
||||
user := createUser(t)
|
||||
|
||||
body, err := json.Marshal(map[string]string{
|
||||
"username": user.Username,
|
||||
"display_name": "Someone else",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal body: %v", err)
|
||||
}
|
||||
|
||||
resp := doRequest(t, http.MethodPost, "/api/v1/users", uuid.Nil, body)
|
||||
requireStatus(t, resp, http.StatusConflict)
|
||||
|
||||
var apiErr dto.ErrorResponse
|
||||
mustDecode(t, resp, &apiErr)
|
||||
|
||||
if apiErr.Error.Code != dto.ErrorCodeConflict {
|
||||
t.Errorf("expected code %q, got %q", dto.ErrorCodeConflict, apiErr.Error.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagination(t *testing.T) {
|
||||
user := createUser(t)
|
||||
chat := createChat(t, user, user.ID)
|
||||
|
||||
const total = 25
|
||||
const pageSize = 4
|
||||
|
||||
sent := make(map[string]bool, total)
|
||||
for i := 0; i < total; i++ {
|
||||
resp := sendMessage(t, user, chat.ID, "msg")
|
||||
requireStatus(t, resp, http.StatusCreated)
|
||||
|
||||
var msg dto.MessageResponse
|
||||
mustDecode(t, resp, &msg)
|
||||
sent[msg.ID.String()] = true
|
||||
}
|
||||
|
||||
seen := make(map[string]bool, total)
|
||||
cursor := ""
|
||||
pages := 0
|
||||
|
||||
for {
|
||||
path := "/api/v1/chats/" + chat.ID.String() + "/messages?limit=" + strconv.Itoa(pageSize)
|
||||
if cursor != "" {
|
||||
path += "&cursor=" + url.QueryEscape(cursor)
|
||||
}
|
||||
|
||||
resp := doRequest(t, http.MethodGet, path, user.ID, nil)
|
||||
requireStatus(t, resp, http.StatusOK)
|
||||
|
||||
var page dto.MessagePageResponse
|
||||
mustDecode(t, resp, &page)
|
||||
pages++
|
||||
|
||||
for _, msg := range page.Items {
|
||||
id := msg.ID.String()
|
||||
if seen[id] {
|
||||
t.Fatalf("duplicate message id across pages: %s", id)
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
|
||||
if page.NextCursor == nil {
|
||||
break
|
||||
}
|
||||
cursor = *page.NextCursor
|
||||
|
||||
if pages > total {
|
||||
t.Fatalf("too many pages: got %d, expected at most %d", pages, total)
|
||||
}
|
||||
}
|
||||
|
||||
if len(seen) != total {
|
||||
t.Fatalf("expected %d unique messages across all pages, got %d", total, len(seen))
|
||||
}
|
||||
for id := range sent {
|
||||
if !seen[id] {
|
||||
t.Fatalf("message %s sent but never seen while paginating", id)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user