Files
lincodex/backend/cmd/company-fixture/notifications_test.go
T

87 lines
3.9 KiB
Go

//go:build e2e
package main
import (
"context"
"encoding/json"
"net/http"
"strings"
"testing"
"github.com/google/uuid"
"lincodex/backend/internal/adapter/secondary/postgres"
"lincodex/backend/internal/port/outbound"
)
func TestNoticeEndpointsNeedTheBearerAndDecodeStrictly(t *testing.T) {
recipientID, organizationID, eventID := uuid.New(), uuid.New(), uuid.New()
var published, hidden int
handler := &fixtureHandler{
token: strings.Repeat("ab", 32),
notices: &noticeFixture{
publish: func(_ context.Context, gotRecipient, gotOrganization uuid.UUID) (uuid.UUID, error) {
published++
if gotRecipient != recipientID || gotOrganization != organizationID {
t.Fatalf("publish IDs = %s/%s, want %s/%s", gotRecipient, gotOrganization, recipientID, organizationID)
}
return eventID, nil
},
replay: func(_ context.Context, got uuid.UUID) (noticeReplayResult, error) {
if got != eventID {
t.Fatalf("replay event = %s, want %s", got, eventID)
}
return noticeReplayResult{RenamedApplied: true}, nil
},
hide: func(context.Context, uuid.UUID, uuid.UUID) error {
hidden++
return postgres.ErrCompanyFixtureScope
},
},
}
bearer := "Bearer " + handler.token
body := `{"recipient_user_id":"` + recipientID.String() + `","organization_id":"` + organizationID.String() + `"}`
if response := fixtureRequest(handler, http.MethodPost, noticeEndpoint, body, "Bearer "+strings.Repeat("cd", 32), "application/json"); response.Code != http.StatusUnauthorized || published != 0 {
t.Fatalf("unauthorized publish = %d, published %d", response.Code, published)
}
response := fixtureRequest(handler, http.MethodPost, noticeEndpoint, body, bearer, "application/json")
var created struct {
EventID uuid.UUID `json:"event_id"`
}
if response.Code != http.StatusCreated || json.Unmarshal(response.Body.Bytes(), &created) != nil || created.EventID != eventID {
t.Fatalf("publish = %d %s, want 201 with the event ID", response.Code, response.Body.String())
}
replay := fixtureRequest(handler, http.MethodPost, noticeReplayEndpoint, `{"event_id":"`+eventID.String()+`"}`, bearer, "application/json")
var result noticeReplayResult
if replay.Code != http.StatusOK || json.Unmarshal(replay.Body.Bytes(), &result) != nil || result.PrimaryApplied || !result.RenamedApplied {
t.Fatalf("replay = %d %s, want 200 with a duplicate for the primary consumer", replay.Code, replay.Body.String())
}
if hide := fixtureRequest(handler, http.MethodPost, noticeHideEndpoint, body, bearer, "application/json"); hide.Code != http.StatusNotFound || hidden != 1 {
t.Fatalf("hide outside the fixture scope = %d, want 404", hide.Code)
}
for name, test := range map[string]struct {
path, body string
want int
}{
"unknown field": {noticeEndpoint, `{"recipient_user_id":"` + recipientID.String() + `","organization_id":"` + organizationID.String() + `","email":"x"}`, http.StatusBadRequest},
"bad recipient": {noticeEndpoint, `{"recipient_user_id":"x","organization_id":"` + organizationID.String() + `"}`, http.StatusBadRequest},
"nil event": {noticeReplayEndpoint, `{"event_id":"` + uuid.Nil.String() + `"}`, http.StatusBadRequest},
"trailing content": {noticeEndpoint, body + " {}", http.StatusBadRequest},
} {
if got := fixtureRequest(handler, http.MethodPost, test.path, test.body, bearer, "application/json"); got.Code != test.want {
t.Errorf("%s = %d, want %d", name, got.Code, test.want)
}
}
if got := fixtureRequest(handler, http.MethodGet, noticeEndpoint, "", bearer, ""); got.Code != http.StatusMethodNotAllowed {
t.Errorf("GET = %d, want 405", got.Code)
}
if statusForNoticeError(outbound.ErrOrganizationNotFound) != http.StatusNotFound {
t.Error("an unknown organization must answer 404")
}
if got := fixtureRequest(&fixtureHandler{token: handler.token}, http.MethodPost, noticeEndpoint, body, bearer, "application/json"); got.Code != http.StatusNotFound {
t.Errorf("a handler without notices = %d, want 404", got.Code)
}
}