128 lines
5.6 KiB
Go
128 lines
5.6 KiB
Go
package httpadapter
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/ogen-go/ogen"
|
|
|
|
"lincodex/backend/internal/adapter/primary/http/api"
|
|
)
|
|
|
|
// The four inbox operations follow the project conventions: the session
|
|
// cookie for a read, the session and the CSRF token for a mark, no-store on
|
|
// every success, the shared cursor parameters, the anti-enumeration 404 on
|
|
// everything keyed by an identifier, and no new problem code.
|
|
func TestNotificationOperationsFollowTheConventions(t *testing.T) {
|
|
spec := parsePublicContract(t)
|
|
for _, test := range []struct {
|
|
id string
|
|
path string
|
|
reads bool
|
|
parameters []string
|
|
statuses []string
|
|
absent []string
|
|
}{
|
|
{id: "ListMyNotifications", path: "/api/v1/me/notifications", reads: true,
|
|
parameters: []string{"PageLimit", "PageCursor"}, statuses: []string{"400", "401", "403"}, absent: []string{"404"}},
|
|
{id: "ListOrganizationNotifications", path: "/api/v1/organizations/{org_id}/notifications", reads: true,
|
|
parameters: []string{"OrganizationId", "PageLimit", "PageCursor"}, statuses: []string{"400", "401", "403", "404"}},
|
|
{id: "MarkMyNotificationRead", path: "/api/v1/me/notifications/{notification_id}/read",
|
|
parameters: []string{"NotificationId"}, statuses: []string{"400", "401", "403", "404", "429"}},
|
|
{id: "MarkOrganizationNotificationRead", path: "/api/v1/organizations/{org_id}/notifications/{notification_id}/read",
|
|
parameters: []string{"OrganizationId", "NotificationId"}, statuses: []string{"400", "401", "403", "404", "429"}},
|
|
} {
|
|
t.Run(test.id, func(t *testing.T) {
|
|
operation := operationByID(t, spec, test.id)
|
|
if spec.Paths[test.path] == nil {
|
|
t.Fatalf("the contract has no path %s", test.path)
|
|
}
|
|
if !slices.Contains(operation.Tags, "notification") {
|
|
t.Errorf("%s is not in the notification tag", test.id)
|
|
}
|
|
if !hasSecurity(operation, "sessionCookie") || hasSecurity(operation, "sessionCsrf") == test.reads {
|
|
t.Errorf("%s security = %v, want the session cookie and a CSRF token only for a mark", test.id, operation.Security)
|
|
}
|
|
for _, status := range append(test.statuses, "default") {
|
|
if operation.Responses[status] == nil {
|
|
t.Errorf("%s does not declare %s", test.id, status)
|
|
}
|
|
}
|
|
for _, status := range test.absent {
|
|
if operation.Responses[status] != nil {
|
|
t.Errorf("%s declares %s", test.id, status)
|
|
}
|
|
}
|
|
if success := operation.Responses["200"]; success == nil || success.Headers["Cache-Control"] == nil {
|
|
t.Errorf("%s does not mark its 200 response no-store", test.id)
|
|
}
|
|
if operation.RequestBody != nil {
|
|
t.Errorf("%s takes a body: the item and the recipient come from the path and the session", test.id)
|
|
}
|
|
names := make([]string, 0, len(operation.Parameters))
|
|
for _, parameter := range operation.Parameters {
|
|
names = append(names, parameterName(parameter))
|
|
}
|
|
if !slices.Equal(names, test.parameters) {
|
|
t.Errorf("%s parameters = %v, want %v", test.id, names, test.parameters)
|
|
}
|
|
})
|
|
}
|
|
// An idempotent command by its effect needs no key.
|
|
for _, id := range []string{"MarkMyNotificationRead", "MarkOrganizationNotificationRead"} {
|
|
for _, parameter := range operationByID(t, spec, id).Parameters {
|
|
if parameterName(parameter) == "IdempotencyKey" || parameterName(parameter) == "IfMatch" {
|
|
t.Errorf("%s declares %s", id, parameterName(parameter))
|
|
}
|
|
}
|
|
}
|
|
codes := enumValues(t, resolveSchema(t, spec, &ogen.Schema{Ref: "#/components/schemas/ProblemCode"}))
|
|
for _, code := range []string{"validation_failed", "unauthenticated", "forbidden", "csrf_check_failed", "not_found", "rate_limited"} {
|
|
if !slices.Contains(codes, code) {
|
|
t.Errorf("ProblemCode has no %s", code)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Every inbox object is closed and fully required, an item carries a
|
|
// reference and a state but no text of its own, and the text sits in the one
|
|
// nullable content object.
|
|
func TestNotificationSchemasAreClosedAndKeepTextInTheContent(t *testing.T) {
|
|
spec := parsePublicContract(t)
|
|
for _, pair := range []struct {
|
|
name string
|
|
want []string
|
|
}{
|
|
{"Notification", []string{"content", "delivered_at", "id", "kind", "organization_id", "read_at"}},
|
|
{"NotificationContent", []string{"link_path", "summary", "title"}},
|
|
{"NotificationList", []string{"items", "page"}},
|
|
{"NotificationReadReceipt", []string{"id", "read_at"}},
|
|
} {
|
|
schema := spec.Components.Schemas[pair.name]
|
|
if schema == nil {
|
|
t.Fatalf("the contract has no schema %s", pair.name)
|
|
}
|
|
if got := propertyNames(schema); !slices.Equal(got, pair.want) {
|
|
t.Errorf("%s properties = %v, want %v", pair.name, got, pair.want)
|
|
}
|
|
if schema.AdditionalProperties.Bool == nil || *schema.AdditionalProperties.Bool {
|
|
t.Errorf("%s accepts unknown properties", pair.name)
|
|
}
|
|
if required := slices.Sorted(slices.Values(schema.Required)); !slices.Equal(required, pair.want) {
|
|
t.Errorf("%s requires %v, want every property %v", pair.name, required, pair.want)
|
|
}
|
|
}
|
|
if got := enumValues(t, spec.Components.Schemas["NotificationKind"]); !slices.Equal(got, []string{"invitation_delivered", "synthetic"}) {
|
|
t.Errorf("NotificationKind = %v", got)
|
|
}
|
|
assertNoJSONFields(t, api.Notification{}, []string{
|
|
"title", "summary", "body", "text", "email", "name", "contact", "contacts", "recipient_user_id", "user_id", "candidate_id",
|
|
})
|
|
assertNoJSONFields(t, api.NotificationReadReceipt{}, []string{"content", "title", "summary", "kind", "organization_id"})
|
|
for _, property := range spec.Components.Schemas["NotificationList"].Properties {
|
|
if property.Name == "items" && (property.Schema.MaxItems == nil || *property.Schema.MaxItems != 100) {
|
|
t.Errorf("NotificationList.items maxItems = %v, want the page limit 100", property.Schema.MaxItems)
|
|
}
|
|
}
|
|
}
|