Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
katherinelc321 committed May 13, 2024
1 parent df2befe commit cb995d9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions frontend/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
PathSegmentSubscriptionID = "subscriptionid"
PathSegmentResourceGroupName = "resourcegroupname"
PathSegmentResourceName = "resourcename"
PathSegmentDeploymentName = "deploymentname"
PathSegmentActionName = "actionname"
PathSegmentOperationID = "operationId"
PathSegmentResourceType = "resourceType"
Expand Down
3 changes: 2 additions & 1 deletion frontend/middleware_validatestatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 25 additions & 5 deletions frontend/middleware_validatestatic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit cb995d9

Please sign in to comment.