lab4: исправления по ревью
This commit is contained in:
@@ -105,7 +105,9 @@ func (e *Extensions) authenticate(r *http.Request) (uuid.UUID, error) {
|
||||
|
||||
user, err := e.sessions.Resolve(r.Context(), token)
|
||||
if err != nil {
|
||||
return uuid.Nil, fmt.Errorf("%w: %w", domain.ErrUnauthenticated, err)
|
||||
// session.Resolve already distinguishes an invalid session (401) from a
|
||||
// database outage (503); do not collapse both into 401 here.
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
return user.ID, nil
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
)
|
||||
|
||||
type resolverFunc func(ctx context.Context, token string) (domain.User, error)
|
||||
|
||||
func (f resolverFunc) Resolve(ctx context.Context, token string) (domain.User, error) {
|
||||
return f(ctx, token)
|
||||
}
|
||||
|
||||
func TestExtensionPreservesDependencyErrors(t *testing.T) {
|
||||
extensions := NewExtensions(nil, resolverFunc(func(context.Context, string) (domain.User, error) {
|
||||
return domain.User{}, domain.ErrUnavailable
|
||||
}), nil)
|
||||
|
||||
request := httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodPost, "/api/v1/chats/11111111-1111-4111-8111-111111111111/attachments/presign", nil,
|
||||
)
|
||||
request.Header.Set("Authorization", "Bearer token")
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
extensions.Presign().ServeHTTP(recorder, request)
|
||||
|
||||
require.Equal(t, http.StatusServiceUnavailable, recorder.Code)
|
||||
}
|
||||
|
||||
func TestExtensionInvalidSessionIsUnauthorized(t *testing.T) {
|
||||
extensions := NewExtensions(nil, resolverFunc(func(context.Context, string) (domain.User, error) {
|
||||
return domain.User{}, domain.ErrUnauthenticated
|
||||
}), nil)
|
||||
|
||||
request := httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodPost, "/api/v1/chats/11111111-1111-4111-8111-111111111111/attachments/presign", nil,
|
||||
)
|
||||
request.Header.Set("Authorization", "Bearer token")
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
extensions.Presign().ServeHTTP(recorder, request)
|
||||
|
||||
require.Equal(t, http.StatusUnauthorized, recorder.Code)
|
||||
}
|
||||
|
||||
func TestExtensionWrongMethodIsNotAllowed(t *testing.T) {
|
||||
extensions := NewExtensions(nil, nil, nil)
|
||||
|
||||
request := httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodGet, "/api/v1/chats/11111111-1111-4111-8111-111111111111/attachments/presign", nil,
|
||||
)
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
extensions.Presign().ServeHTTP(recorder, request)
|
||||
|
||||
require.Equal(t, http.StatusMethodNotAllowed, recorder.Code)
|
||||
require.Equal(t, http.MethodPost, recorder.Header().Get("Allow"))
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -271,15 +270,15 @@ func (h *Handler) CreateMessage(
|
||||
}, nil
|
||||
}
|
||||
|
||||
// messageFingerprint canonicalises the create-message payload so that the key
|
||||
// order of the client JSON cannot change the fingerprint, while a different
|
||||
// payload with the same Idempotency-Key can (contract 1.3.0).
|
||||
// messageFingerprint canonicalises the create-message payload so that JSON key
|
||||
// order cannot change the fingerprint, while a different payload (including a
|
||||
// different attachment order) with the same Idempotency-Key can. Absent
|
||||
// attachment_ids and [] are equivalent, as required by the contract.
|
||||
func messageFingerprint(req *oas.CreateMessage) ([sha256.Size]byte, error) {
|
||||
ids := make([]uuid.UUID, 0, len(req.AttachmentIds))
|
||||
ids = append(ids, req.AttachmentIds...)
|
||||
slices.SortFunc(ids, func(left, right uuid.UUID) int {
|
||||
return bytes.Compare(left[:], right[:])
|
||||
})
|
||||
ids := req.AttachmentIds
|
||||
if ids == nil {
|
||||
ids = []uuid.UUID{}
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(struct {
|
||||
Text string `json:"text"`
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"mtusi-messenger/internal/transport/rest/internal/oas"
|
||||
)
|
||||
|
||||
func TestMessageFingerprintPreservesAttachmentOrder(t *testing.T) {
|
||||
first, second := uuid.New(), uuid.New()
|
||||
|
||||
forward, err := messageFingerprint(&oas.CreateMessage{
|
||||
Text: "x",
|
||||
AttachmentIds: []uuid.UUID{first, second},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
reverse, err := messageFingerprint(&oas.CreateMessage{
|
||||
Text: "x",
|
||||
AttachmentIds: []uuid.UUID{second, first},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NotEqual(t, forward, reverse,
|
||||
"reordered attachment_ids are a different payload and must not replay")
|
||||
}
|
||||
|
||||
func TestMessageFingerprintTreatsAbsentAndEmptyAsEqual(t *testing.T) {
|
||||
absent, err := messageFingerprint(&oas.CreateMessage{Text: "x"})
|
||||
require.NoError(t, err)
|
||||
|
||||
empty, err := messageFingerprint(&oas.CreateMessage{Text: "x", AttachmentIds: []uuid.UUID{}})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, absent, empty)
|
||||
}
|
||||
Reference in New Issue
Block a user