package server import ( "net/http" "net/http/httptest" "testing" ) func TestPolicyForRequest(t *testing.T) { tests := []struct { method, path, capability string target authorizationTargetKind }{ {http.MethodGet, "/guide/delete-is-a-title", "read", targetPath}, {http.MethodPost, "/guide/delete-is-a-title", "write", targetPath}, {http.MethodGet, "/e/-/guide", "write", targetPath}, {http.MethodGet, "/:/new", "create", targetDomain}, {http.MethodPost, "/:/new", "create", targetDomain}, {http.MethodGet, "/_/api/v1/object/create", "deny", targetDomain}, {http.MethodPost, "/_/api/v1/path/rename", "rename", targetDomain}, {http.MethodPost, "/_/api/v1/render/markdown", "read", targetDomain}, {http.MethodDelete, "/_/api/v1/assets", "manage", targetDomain}, {http.MethodGet, "/_/api/v1/assets", "manage", targetDomain}, {http.MethodGet, "/_/api/v1/notifications", "read", targetDomain}, {http.MethodPost, "/_/api/v1/notifications/read", "read", targetDomain}, {http.MethodPatch, "/_/api/v1/notifications/read", "read", targetDomain}, {http.MethodPost, "/_/api/v1/publication/requests", "manage", targetDomain}, {http.MethodPost, "/_/api/v1/publication/requests/request-1/decisions", "manage", targetDomain}, {http.MethodPut, "/_/api/v1/assets", "deny", targetDomain}, {http.MethodGet, "/_/api/v1/event/jobs", "manage", targetDomain}, {http.MethodGet, "/_/api/v1/queue", "manage", targetDomain}, {http.MethodPost, "/_/api/v1/event/run", "manage", targetDomain}, {http.MethodPost, "/_/api/v1/not-registered/delete", "deny", targetDomain}, } for _, tt := range tests { t.Run(tt.method+" "+tt.path, func(t *testing.T) { r := httptest.NewRequest(tt.method, tt.path, nil) got := policyForRequest(r) if got.Capability != tt.capability || got.Target != tt.target { t.Fatalf("policy = capability %q target %q; want %q %q", got.Capability, got.Target, tt.capability, tt.target) } }) } } func TestReadEndpointResolvers(t *testing.T) { tests := []struct { path string resolver endpointTargetResolver capability string }{ {"/v/raw/rev:123", resolveRaw, "read"}, {"/v/text/rev:123", resolveText, "read"}, {"/v/pdf-page", resolvePDF, "read"}, {"/v/preview-cache/hash.jpg", resolvePreview, "read"}, {"/v/preview/docs/page", resolveViewPath, "read"}, {"/o/e/uuid:123", resolveObjectURL, "write"}, {"/r/view/revision", resolveRevision, "read"}, } for _, tt := range tests { t.Run(tt.path, func(t *testing.T) { request := httptest.NewRequest(http.MethodGet, tt.path, nil) policy := policyForRequest(request) if policy.Resolver != tt.resolver || policy.Capability != tt.capability { t.Fatalf("resolver=%q capability=%q; want %q %q", policy.Resolver, policy.Capability, tt.resolver, tt.capability) } }) } } func TestEndpointPoliciesAreNamed(t *testing.T) { for route, policy := range endpointPolicies { if policy.Name == "" || policy.Capability == "" || policy.Target == "" { t.Errorf("incomplete policy for %s: %#v", route, policy) } } }