feat: port verified messenger from lab 6
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
func TestCursorRejectsInvalidUUIDBeforeDatabaseAccess(t *testing.T) {
|
||||
const userID = "11111111-1111-4111-8111-111111111111"
|
||||
const chatID = "22222222-2222-4222-8222-222222222222"
|
||||
const messageID = "33333333-3333-4333-8333-333333333333"
|
||||
createdAt := time.Date(2026, 10, 10, 12, 0, 0, 0, time.UTC)
|
||||
for _, invalid := range []string{"not-a-uuid", "ffffffff-ffff-ffff-ffff-fffffffffffz"} {
|
||||
for _, field := range []string{"user_id", "chat_id"} {
|
||||
t.Run("chats/"+field+"/"+invalid, func(t *testing.T) {
|
||||
cursor := repositories.ChatCursor{UserID: userID, ChatID: chatID, CreatedAt: createdAt}
|
||||
if field == "user_id" {
|
||||
cursor.UserID = invalid
|
||||
} else {
|
||||
cursor.ChatID = invalid
|
||||
}
|
||||
token, err := services.EncodeChatCursor(cursor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := services.DecodeChatCursor(token); err == nil {
|
||||
t.Fatal("invalid UUID accepted")
|
||||
}
|
||||
// A nil store proves that validation finishes before any SQL call.
|
||||
_, err = services.NewChatService(nil).List(context.Background(), userID, 20, token)
|
||||
var invalidInput services.InvalidInput
|
||||
if !errors.As(err, &invalidInput) {
|
||||
t.Fatalf("want InvalidInput, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, field := range []string{"chat_id", "message_id"} {
|
||||
t.Run("messages/"+field+"/"+invalid, func(t *testing.T) {
|
||||
cursor := repositories.MessageCursor{ChatID: chatID, MessageID: messageID, CreatedAt: createdAt}
|
||||
if field == "chat_id" {
|
||||
cursor.ChatID = invalid
|
||||
} else {
|
||||
cursor.MessageID = invalid
|
||||
}
|
||||
token, err := services.EncodeMessageCursor(cursor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := services.DecodeMessageCursor(token); err == nil {
|
||||
t.Fatal("invalid UUID accepted")
|
||||
}
|
||||
chat := repositories.Chat{ID: chatID, MemberIDs: []string{userID}}
|
||||
_, err = services.NewMessageService(nil).List(context.Background(), userID, chat, 20, token)
|
||||
var invalidInput services.InvalidInput
|
||||
if !errors.As(err, &invalidInput) {
|
||||
t.Fatalf("want InvalidInput, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCursorPreservesPosition(t *testing.T) {
|
||||
original := repositories.ChatCursor{
|
||||
UserID: "11111111-1111-4111-8111-111111111111",
|
||||
ChatID: "22222222-2222-4222-8222-222222222222",
|
||||
CreatedAt: time.Date(2026, 10, 10, 12, 0, 0, 123000, time.UTC),
|
||||
}
|
||||
token, err := services.EncodeChatCursor(original)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoded, err := services.DecodeChatCursor(token)
|
||||
if err != nil || decoded != original {
|
||||
t.Fatalf("cursor changed: %+v, error: %v", decoded, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user