From 51b4808ae6679913112df23f81b186a37ec8ed17 Mon Sep 17 00:00:00 2001 From: katherinelc321 Date: Mon, 13 May 2024 14:28:06 -0400 Subject: [PATCH] add test cases --- frontend/middleware_validatestatic.go | 3 ++- frontend/middleware_validatestatic_test.go | 30 ++++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/frontend/middleware_validatestatic.go b/frontend/middleware_validatestatic.go index cb79a739b..1af6e10fa 100644 --- a/frontend/middleware_validatestatic.go +++ b/frontend/middleware_validatestatic.go @@ -17,6 +17,7 @@ import ( // Referenced in https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules#microsoftresources var rxResourceGroupName = regexp.MustCompile(`^[-a-z0-9_().]{0,89}[-a-z0-9_()]$`) +var rxResourceName = regexp.MustCompile(`^[a-zA-Z0-9-]{3,24}$`) func MiddlewareValidateStatic(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { @@ -58,7 +59,7 @@ func MiddlewareValidateStatic(w http.ResponseWriter, r *http.Request, next http. } if resourceName != "" { - if !rxResourceGroupName.MatchString(resourceName) { + if !rxResourceName.MatchString(resourceName) { arm.WriteError(w, http.StatusBadRequest, arm.CloudErrorCodeResourceNotFound, "", "The Resource '%s/%s/%s' under resource group '%s' is invalid.", resourceProviderNamespace, resourceType, resourceName, resourceGroupName) return } diff --git a/frontend/middleware_validatestatic_test.go b/frontend/middleware_validatestatic_test.go index 23058c035..40b45c96d 100644 --- a/frontend/middleware_validatestatic_test.go +++ b/frontend/middleware_validatestatic_test.go @@ -27,6 +27,9 @@ func TestMiddlewareValidateStatic(t *testing.T) { subscriptionID string resourceGroupName string resourceProviderNamespace string + resourceType string + resourceName string + operationsId string expectedStatusCode int expectedBody string }{ @@ -45,21 +48,35 @@ func TestMiddlewareValidateStatic(t *testing.T) { }, { name: "Invalid resource group name", - path: "/subscriptions/42d9eac4-d29a-4d6e-9e26-3439758b1491/resourcegroups/resourcegroup!", - subscriptionID: "42d9eac4-d29a-4d6e-9e26-3439758b1491", + path: "/resourcegroups/resourcegroup!", resourceGroupName: "resourcegroup!", expectedStatusCode: http.StatusBadRequest, expectedBody: "Resource group 'resourcegroup!' is invalid.", }, { name: "Invalid resource provider namespace", - path: "/subscriptions/42d9eac4-d29a-4d6e-9e26-3439758b1491/resourcegroups/resourcegroup/providers/invalid", - subscriptionID: "42d9eac4-d29a-4d6e-9e26-3439758b1491", - resourceGroupName: "resourcegroup", + path: "/providers/invalid", resourceProviderNamespace: "invalid", expectedStatusCode: http.StatusBadRequest, expectedBody: "The resource namespace 'invalid' is invalid.", }, + { + name: "Invalid resource name", + path: "/resourcegroup/providers/microsoft.redhatopenshift/hcpopenshiftcluster/$", + resourceGroupName: "resourcegroup", + resourceProviderNamespace: "microsoft.redhatopenshift", + resourceType: "hcpopenshiftcluster", + resourceName: "$", + expectedStatusCode: http.StatusBadRequest, + expectedBody: "The Resource 'microsoft.redhatopenshift/hcpopenshiftcluster/$' under resource group 'resourcegroup' is invalid.", + }, + { + name: "Invalid operation id", + path: "/operations/abc", + operationsId: "abc", + expectedStatusCode: http.StatusBadRequest, + expectedBody: "The provided operation identifier 'abc' is malformed or invalid.", + }, } for _, tc := range tests { @@ -72,6 +89,9 @@ func TestMiddlewareValidateStatic(t *testing.T) { req.SetPathValue(PathSegmentSubscriptionID, tc.subscriptionID) req.SetPathValue(PathSegmentResourceGroupName, tc.resourceGroupName) req.SetPathValue(PathSegmentResourceProviderNamespace, tc.resourceProviderNamespace) + req.SetPathValue(PathSegmentResourceType, tc.resourceType) + req.SetPathValue(PathSegmentResourceName, tc.resourceName) + req.SetPathValue(PathSegmentOperationID, tc.operationsId) // Execute the middleware MiddlewareValidateStatic(w, req, nextHandler)