forked from mtusi2026/mtusi2026lab3
feat: add shared idempotency and replica identity
This commit is contained in:
@@ -3,6 +3,7 @@ package messenger
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@@ -11,7 +12,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/model"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab3/internal/model"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab3/internal/postgres"
|
||||
)
|
||||
|
||||
var errDependency = errors.New("dependency unavailable")
|
||||
@@ -22,6 +24,9 @@ type messengerStoreStub struct {
|
||||
userByIDErr error
|
||||
credentials model.UserCredentials
|
||||
credentialsErr error
|
||||
message model.Message
|
||||
messageReplay bool
|
||||
messageErr error
|
||||
}
|
||||
|
||||
func (s messengerStoreStub) Ping(context.Context) error { return s.pingErr }
|
||||
@@ -43,8 +48,8 @@ func (messengerStoreStub) ChatForUser(context.Context, string, string) (model.Ch
|
||||
func (messengerStoreStub) ListChats(context.Context, string, *model.Position, int) ([]model.Chat, bool, error) {
|
||||
return nil, false, nil
|
||||
}
|
||||
func (messengerStoreStub) CreateMessage(context.Context, string, string, string) (model.Message, error) {
|
||||
return model.Message{}, nil
|
||||
func (s messengerStoreStub) CreateMessage(context.Context, string, string, string, string, time.Duration) (model.Message, bool, error) {
|
||||
return s.message, s.messageReplay, s.messageErr
|
||||
}
|
||||
func (messengerStoreStub) ListMessages(context.Context, string, string, *model.Position, int) ([]model.Message, bool, error) {
|
||||
return nil, false, nil
|
||||
@@ -157,9 +162,66 @@ func TestRegistrationRejectsWhitespacePassword(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceHeaderIsSetOnSuccessAndError(t *testing.T) {
|
||||
server := newTestServer(messengerStoreStub{}, sessionStub{}, passwordsStub{})
|
||||
for _, response := range []*httptest.ResponseRecorder{
|
||||
serveRequest(server, http.MethodGet, "/health/live", ""),
|
||||
serveRequest(server, http.MethodGet, "/api/v1/users/me", ""),
|
||||
} {
|
||||
if value := response.Header().Get("X-Instance-Id"); value != "test-instance" {
|
||||
t.Fatalf("X-Instance-Id = %q, want test-instance", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageCreationValidatesIdempotencyKey(t *testing.T) {
|
||||
store := messengerStoreStub{userByID: model.User{ID: "user-id"}}
|
||||
server := newTestServer(store, sessionStub{userID: "user-id"}, passwordsStub{})
|
||||
tests := []struct {
|
||||
name string
|
||||
key string
|
||||
}{
|
||||
{name: "missing"},
|
||||
{name: "invalid", key: "invalid key"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/chats/00000000-0000-0000-0000-000000000001/messages", strings.NewReader(`{"text":"message"}`))
|
||||
request.Header.Set("Authorization", "Bearer valid-token")
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
if tt.key != "" {
|
||||
request.Header.Set("Idempotency-Key", tt.key)
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusBadRequest)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageCreationReturnsConflictForReusedKeyWithDifferentBody(t *testing.T) {
|
||||
store := messengerStoreStub{
|
||||
userByID: model.User{ID: "user-id"},
|
||||
messageErr: fmt.Errorf("create message: %w", postgres.ErrIdempotencyConflict),
|
||||
}
|
||||
server := newTestServer(store, sessionStub{userID: "user-id"}, passwordsStub{})
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/chats/00000000-0000-0000-0000-000000000001/messages", strings.NewReader(`{"text":"different"}`))
|
||||
request.Header.Set("Authorization", "Bearer valid-token")
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Idempotency-Key", "same-key")
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
if response.Code != http.StatusConflict {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusConflict)
|
||||
}
|
||||
}
|
||||
|
||||
func newTestServer(messages MessengerStore, sessions SessionStore, passwords PasswordService) *Server {
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
return NewServer(logger, messages, sessions, passwords, time.Hour, time.Second, "8081")
|
||||
return NewServer(logger, messages, sessions, passwords, time.Hour, 24*time.Hour, time.Second, "test-instance", "8081")
|
||||
}
|
||||
|
||||
func serveRequest(server *Server, method, path, body string) *httptest.ResponseRecorder {
|
||||
|
||||
Reference in New Issue
Block a user