Skip to content

Commit

Permalink
tests: add a DelegatedResources block parsing test
Browse files Browse the repository at this point in the history
  • Loading branch information
aristosvo committed Jan 12, 2024
1 parent ae94480 commit fa3de03
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,100 @@ func TestModelsWithASystemOrUserAssignedMapIdentityInlined(t *testing.T) {
}
}

func TestModelsWithASystemOrUserAssignedMapIdentityWithDelegatedResources(t *testing.T) {
// this handles the scenario of a System Assigned & User Assigned model having a DelegatedResources block
result, err := ParseSwaggerFileForTesting(t, "model_system_or_user_assigned_map_with_delegated_resources.json")
if err != nil {
t.Fatalf("parsing: %+v", err)
}
if result == nil {
t.Fatal("result was nil")
}
if len(result.Resources) != 1 {
t.Fatalf("expected 1 resource but got %d", len(result.Resources))
}

hello, ok := result.Resources["Hello"]
if !ok {
t.Fatalf("no resources were output with the tag Hello")
}

if len(hello.Constants) != 0 {
t.Fatalf("expected no Constants but got %d", len(hello.Constants))
}
if len(hello.Models) != 1 {
t.Fatalf("expected 1 Model but got %d", len(hello.Models))
}
if len(hello.Operations) != 1 {
t.Fatalf("expected 1 Operation but got %d", len(hello.Operations))
}
if len(hello.ResourceIds) != 0 {
t.Fatalf("expected no ResourceIds but got %d", len(hello.ResourceIds))
}

world, ok := hello.Operations["GetWorld"]
if !ok {
t.Fatalf("no resources were output with the name GetWorld")
}
if world.Method != "GET" {
t.Fatalf("expected a GET operation but got %q", world.Method)
}
if len(world.ExpectedStatusCodes) != 1 {
t.Fatalf("expected 1 status code but got %d", len(world.ExpectedStatusCodes))
}
if world.ExpectedStatusCodes[0] != 200 {
t.Fatalf("expected the status code to be 200 but got %d", world.ExpectedStatusCodes[0])
}
if world.RequestObject != nil {
t.Fatalf("expected no request object but got %+v", *world.RequestObject)
}
if world.ResponseObject == nil {
t.Fatal("expected a response object but didn't get one")
}
if world.ResponseObject.Type != models.ObjectDefinitionReference {
t.Fatalf("expected the response object to be a reference but got %q", string(world.ResponseObject.Type))
}
if *world.ResponseObject.ReferenceName != "Example" {
t.Fatalf("expected the response object to be 'Example' but got %q", *world.ResponseObject.ReferenceName)
}
if world.ResourceIdName != nil {
t.Fatalf("expected no ResourceId but got %q", *world.ResourceIdName)
}
if world.UriSuffix == nil {
t.Fatal("expected world.UriSuffix to have a value")
}
if *world.UriSuffix != "/things" {
t.Fatalf("expected world.UriSuffix to be `/things` but got %q", *world.UriSuffix)
}
if world.LongRunning {
t.Fatal("expected a non-long running operation but it was long running")
}

exampleModel, ok := hello.Models["Example"]
if !ok {
t.Fatalf("expected there to be a model called Example")
}
if len(exampleModel.Fields) != 2 {
t.Fatalf("expected the model Example to have 2 field but got %d", len(exampleModel.Fields))
}
if _, ok := exampleModel.Fields["Name"]; !ok {
t.Fatalf("expected there to be a field called Name")
}
identityField, ok := exampleModel.Fields["Identity"]
if !ok {
t.Fatalf("expected there to be a field called Identity")
}
if identityField.CustomFieldType == nil {
t.Fatalf("expected the field Identity to have a CustomFieldType")
}
if *identityField.CustomFieldType != models.CustomFieldTypeSystemOrUserAssignedIdentityMap {
t.Fatalf("expected the field Identity to have a CustomFieldType of SystemOrUserAssignedIdentityMap")
}
if identityField.ObjectDefinition != nil {
t.Fatalf("expected the field Identity to have no ObjectDefinition")
}
}

func TestModelsWithAUserAssignedListIdentity(t *testing.T) {
result, err := ParseSwaggerFileForTesting(t, "model_user_assigned_list.json")
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"swagger": "2.0",
"info": {
"title": "Example",
"description": "Example",
"version": "2020-01-01"
},
"host": "management.mysite.com",
"schemes": [
"https"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"security": [],
"securityDefinitions": {},
"paths": {
"/things": {
"get": {
"tags": [
"Hello"
],
"operationId": "Hello_GetWorld",
"description": "A GET request with a body returned.",
"parameters": [],
"responses": {
"200": {
"description": "Success.",
"schema": {
"$ref": "#/definitions/Example"
}
}
}
}
}
},
"definitions": {
"Example": {
"properties": {
"name": {
"type": "string"
},
"identity": {
"$ref": "#/definitions/SystemAssignedUserAssignedIdentity"
}
},
"type": "object",
"title": "Example"
},
"SystemAssignedUserAssignedIdentity": {
"properties": {
"type": {
"type": "string",
"enum": [
"None",
"SystemAssigned",
"UserAssigned"
],
"x-ms-enum": {
"name": "IdentityType1",
"modelAsString": true
}
},
"principalId": {
"type": "string",
"readOnly": true
},
"tenantId": {
"type": "string",
"readOnly": true
},
"userAssignedIdentities": {
"$ref": "#/definitions/SystemAssignedUserAssignedIdentityMap"
},
"delegatedResources": {
"$ref": "#/definitions/DelegatedResources"
}
}
},
"SystemAssignedUserAssignedIdentityMap": {
"additionalProperties": {
"type": "object",
"properties": {
"clientId": {
"type": "string"
},
"principalId": {
"type": "string"
}
}
}
},
"DelegatedResources": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/DelegatedResource"
}
},
"DelegatedResource": {
"type": "object",
"properties": {
"resourceId": {
"type": "string"
},
"tenantId": {
"format": "uuid",
"type": "string"
},
"referralResource": {
"type": "string"
},
"location": {
"type": "string"
}
}
}
},
"parameters": {}
}

0 comments on commit fa3de03

Please sign in to comment.