diff --git a/arm-ttk/testcases/deploymentTemplate/URIs-Should-Be-Properly-Constructed.test.ps1 b/arm-ttk/testcases/deploymentTemplate/URIs-Should-Be-Properly-Constructed.test.ps1 new file mode 100644 index 00000000..ee7ad880 --- /dev/null +++ b/arm-ttk/testcases/deploymentTemplate/URIs-Should-Be-Properly-Constructed.test.ps1 @@ -0,0 +1,38 @@ +<# +.Synopsis + Ensures that URIs are properly constructed +.Description + Ensures that properties named URI or URL are properly constructed, and do not use and -FunctionNotAllowedInUri +#> +param( + # The template object + [Parameter(Mandatory = $true, Position = 0)] + [PSObject] + $TemplateObject, + + # A list of functions not allowed in the uri. By default, format and concat. + [string[]] + $FunctionNotAllowedInUri = @('format', 'concat') +) + +# commenting out the test due to # 417 +$foundObjects = Find-JsonContent -InputObject $TemplateObject -Key 'ur[il]$' -Match + +foreach ($found in $foundObjects) { # Walk over each found object + foreach ($prop in $found.psobject.properties) { # then walk thru each property + if ($prop.Name -notmatch 'ur[il]$') { continue } # skipping ones that are not uri/url + if (-not $prop.value | ?) { continue } # and ones that do not contain an expression. + + + + # If the value contained expressions, but not the function uri + $foundBadFunction = $prop.Value | ? -FunctionName ($FunctionNotAllowedInUri -join '|') + $foundUriFunction = $prop.Value | ? -FunctionName uri + if ( + ($foundBadFunction -and -not $foundUriFunction) -or + $foundBadFunction.Index -lt $foundUriFunction.Index + ) { + Write-Error "Function'$($foundBadFunction.Groups['FunctionName'].Value)' found within '$($prop.Name)" -TargetObject $found -ErrorId "URI.Improperly.Constructed" + } + } +} diff --git a/unit-tests/URIs-Should-Be-Properly-Constructed/Fail/URI-Using-Concat.json b/unit-tests/URIs-Should-Be-Properly-Constructed/Fail/URI-Using-Concat.json new file mode 100644 index 00000000..314eb931 --- /dev/null +++ b/unit-tests/URIs-Should-Be-Properly-Constructed/Fail/URI-Using-Concat.json @@ -0,0 +1,9 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "resources": [ + { + "aUri": "[concat('foo', 'bar')]" + } + ] +} + diff --git a/unit-tests/URIs-Should-Be-Properly-Constructed/Fail/URI-Using-Format.json b/unit-tests/URIs-Should-Be-Properly-Constructed/Fail/URI-Using-Format.json new file mode 100644 index 00000000..66b33288 --- /dev/null +++ b/unit-tests/URIs-Should-Be-Properly-Constructed/Fail/URI-Using-Format.json @@ -0,0 +1,9 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "resources": [ + { + "aUri": "[format('foo {0}', 'bar')]" + } + ] +} + diff --git a/unit-tests/URIs-Should-Be-Properly-Constructed/Pass/Concat-in-URL.json b/unit-tests/URIs-Should-Be-Properly-Constructed/Pass/Concat-in-URL.json new file mode 100644 index 00000000..7dadd6d0 --- /dev/null +++ b/unit-tests/URIs-Should-Be-Properly-Constructed/Pass/Concat-in-URL.json @@ -0,0 +1,9 @@ +{ +"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "outputs": { + "ServerUrl": { + "type": "string", + "value": "[uri(concat('http://', reference(variables('publicIPAddressName')).dnsSettings.fqdn), 'somepath')]" + } + } +} diff --git a/unit-tests/URIs-Should-Be-Properly-Constructed/Pass/MarketplaceSample.path.txt b/unit-tests/URIs-Should-Be-Properly-Constructed/Pass/MarketplaceSample.path.txt new file mode 100644 index 00000000..3e51b74e --- /dev/null +++ b/unit-tests/URIs-Should-Be-Properly-Constructed/Pass/MarketplaceSample.path.txt @@ -0,0 +1 @@ +..\..\Common\Pass\100-marketplace-sample diff --git a/unit-tests/URIs-Should-Be-Properly-Constructed/URIs-Should-Be-Properly-Constructed.tests.ps1 b/unit-tests/URIs-Should-Be-Properly-Constructed/URIs-Should-Be-Properly-Constructed.tests.ps1 new file mode 100644 index 00000000..d9e0b2ec --- /dev/null +++ b/unit-tests/URIs-Should-Be-Properly-Constructed/URIs-Should-Be-Properly-Constructed.tests.ps1 @@ -0,0 +1,6 @@ + +#requires -module arm-ttk +. $PSScriptRoot\..\arm-ttk.test.functions.ps1 +Test-TTK $psScriptRoot +return +