feat: moved code from lab3
This commit is contained in:
@@ -0,0 +1,276 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateMessageBlankText(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore, messageStore := fixture.users, fixture.chats, fixture.messages
|
||||
|
||||
user, created := createTestUser(t, userStore, "alice", "Алиса")
|
||||
if !created {
|
||||
t.Fatal("expected user to be created")
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Тестовый чат",
|
||||
[]string{user.ID},
|
||||
)
|
||||
|
||||
handler := fixture.router
|
||||
|
||||
request := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/chats/"+chat.ID+"/messages",
|
||||
strings.NewReader(`{"text":" \t\n"}`),
|
||||
)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
authorizeTestRequest(t, fixture, request, user.ID)
|
||||
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf(
|
||||
"expected status %d, got %d; body: %s",
|
||||
http.StatusBadRequest,
|
||||
response.Code,
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
if !strings.Contains(
|
||||
response.Body.String(),
|
||||
`"code":"invalid_request"`,
|
||||
) {
|
||||
t.Fatalf(
|
||||
"expected invalid_request error code, got body: %s",
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
messages := listTestMessages(t, messageStore, chat.ID)
|
||||
|
||||
if len(messages) != 0 {
|
||||
t.Fatalf(
|
||||
"expected no messages to be stored, got %d",
|
||||
len(messages),
|
||||
)
|
||||
}
|
||||
}
|
||||
func TestCreateMessageFromChatOutsider(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore, messageStore := fixture.users, fixture.chats, fixture.messages
|
||||
|
||||
alice, created := createTestUser(t, userStore, "alice", "Алиса")
|
||||
if !created {
|
||||
t.Fatal("expected Alice to be created")
|
||||
}
|
||||
|
||||
bob, created := createTestUser(t, userStore, "bob", "Боб")
|
||||
if !created {
|
||||
t.Fatal("expected Bob to be created")
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Чат Алисы",
|
||||
[]string{alice.ID},
|
||||
)
|
||||
|
||||
handler := fixture.router
|
||||
|
||||
request := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/chats/"+chat.ID+"/messages",
|
||||
strings.NewReader(`{"text":"Сообщение чужому чату"}`),
|
||||
)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
authorizeTestRequest(t, fixture, request, bob.ID)
|
||||
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusNotFound {
|
||||
t.Fatalf(
|
||||
"expected status %d, got %d; body: %s",
|
||||
http.StatusNotFound,
|
||||
response.Code,
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
if !strings.Contains(
|
||||
response.Body.String(),
|
||||
`"code":"not_found"`,
|
||||
) {
|
||||
t.Fatalf(
|
||||
"expected not_found error code, got body: %s",
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
messages := listTestMessages(t, messageStore, chat.ID)
|
||||
|
||||
if len(messages) != 0 {
|
||||
t.Fatalf(
|
||||
"expected no messages to be stored, got %d",
|
||||
len(messages),
|
||||
)
|
||||
}
|
||||
}
|
||||
func TestListMessagesPagination(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore, messageStore := fixture.users, fixture.chats, fixture.messages
|
||||
|
||||
user, created := createTestUser(t, userStore, "alice", "Алиса")
|
||||
if !created {
|
||||
t.Fatal("expected user to be created")
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Тестовый чат",
|
||||
[]string{user.ID},
|
||||
)
|
||||
|
||||
first := createTestMessage(t, messageStore, chat.ID, user.ID, "Первое сообщение")
|
||||
second := createTestMessage(t, messageStore, chat.ID, user.ID, "Второе сообщение")
|
||||
third := createTestMessage(t, messageStore, chat.ID, user.ID, "Третье сообщение")
|
||||
|
||||
expectedIDs := map[string]bool{
|
||||
first.ID: true,
|
||||
second.ID: true,
|
||||
third.ID: true,
|
||||
}
|
||||
|
||||
handler := fixture.router
|
||||
|
||||
// Запрашиваем только два сообщения из трёх.
|
||||
firstRequest := httptest.NewRequest(
|
||||
http.MethodGet,
|
||||
"/api/v1/chats/"+chat.ID+"/messages?limit=2",
|
||||
nil,
|
||||
)
|
||||
authorizeTestRequest(t, fixture, firstRequest, user.ID)
|
||||
|
||||
firstResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(firstResponse, firstRequest)
|
||||
|
||||
if firstResponse.Code != http.StatusOK {
|
||||
t.Fatalf(
|
||||
"expected first page status %d, got %d; body: %s",
|
||||
http.StatusOK,
|
||||
firstResponse.Code,
|
||||
firstResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
var firstPage messagePage
|
||||
|
||||
if err := json.NewDecoder(firstResponse.Body).Decode(&firstPage); err != nil {
|
||||
t.Fatalf("failed to decode first page: %v", err)
|
||||
}
|
||||
|
||||
if len(firstPage.Items) != 2 {
|
||||
t.Fatalf(
|
||||
"expected 2 messages on first page, got %d",
|
||||
len(firstPage.Items),
|
||||
)
|
||||
}
|
||||
|
||||
if firstPage.NextCursor == nil || *firstPage.NextCursor == "" {
|
||||
t.Fatal("expected non-empty next cursor on first page")
|
||||
}
|
||||
|
||||
// Проверяем, что сообщения на первой странице уникальны
|
||||
// и принадлежат исходному набору.
|
||||
seenIDs := make(map[string]bool)
|
||||
|
||||
for _, message := range firstPage.Items {
|
||||
if !expectedIDs[message.ID] {
|
||||
t.Fatalf("unexpected message ID on first page: %s", message.ID)
|
||||
}
|
||||
|
||||
if seenIDs[message.ID] {
|
||||
t.Fatalf("duplicate message on first page: %s", message.ID)
|
||||
}
|
||||
|
||||
seenIDs[message.ID] = true
|
||||
}
|
||||
|
||||
// Используем cursor первой страницы для получения второй.
|
||||
secondRequest := httptest.NewRequest(
|
||||
http.MethodGet,
|
||||
"/api/v1/chats/"+chat.ID+"/messages?limit=2&cursor="+
|
||||
url.QueryEscape(*firstPage.NextCursor),
|
||||
nil,
|
||||
)
|
||||
authorizeTestRequest(t, fixture, secondRequest, user.ID)
|
||||
|
||||
secondResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(secondResponse, secondRequest)
|
||||
|
||||
if secondResponse.Code != http.StatusOK {
|
||||
t.Fatalf(
|
||||
"expected second page status %d, got %d; body: %s",
|
||||
http.StatusOK,
|
||||
secondResponse.Code,
|
||||
secondResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
var secondPage messagePage
|
||||
|
||||
if err := json.NewDecoder(secondResponse.Body).Decode(&secondPage); err != nil {
|
||||
t.Fatalf("failed to decode second page: %v", err)
|
||||
}
|
||||
|
||||
if len(secondPage.Items) != 1 {
|
||||
t.Fatalf(
|
||||
"expected 1 message on second page, got %d",
|
||||
len(secondPage.Items),
|
||||
)
|
||||
}
|
||||
|
||||
if secondPage.NextCursor != nil {
|
||||
t.Fatalf("expected second page to be final, got cursor %q", *secondPage.NextCursor)
|
||||
}
|
||||
|
||||
// Проверяем, что вторая страница не повторяет первую.
|
||||
for _, message := range secondPage.Items {
|
||||
if seenIDs[message.ID] {
|
||||
t.Fatalf("message repeated on second page: %s", message.ID)
|
||||
}
|
||||
|
||||
if !expectedIDs[message.ID] {
|
||||
t.Fatalf("unexpected message ID on second page: %s", message.ID)
|
||||
}
|
||||
|
||||
seenIDs[message.ID] = true
|
||||
}
|
||||
|
||||
if len(seenIDs) != len(expectedIDs) {
|
||||
t.Fatalf(
|
||||
"pagination lost messages: expected %d unique messages, got %d",
|
||||
len(expectedIDs),
|
||||
len(seenIDs),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user