Skip to content

Commit d5dadbc

Browse files
authored
feat: Policy section unit testing (#30)
1 parent c41637d commit d5dadbc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1534
-259
lines changed

example/source/ApiOperationPolicy.cs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,31 @@ namespace Contoso.Apis;
88
[Document("echo-api_retrieve-resource")]
99
public class ApiOperationPolicy : IDocument
1010
{
11-
public void Inbound(IInboundContext c)
11+
public void Inbound(IInboundContext context)
1212
{
13-
c.Base();
14-
if (IsFromCompanyIp(c.ExpressionContext))
13+
context.Base();
14+
if (IsFromCompanyIp(context.ExpressionContext))
1515
{
16-
c.Cors(new CorsConfig
17-
{
18-
AllowCredentials = true,
19-
AllowedOrigins = ["http://internal.contoso.example"],
20-
AllowedHeaders = ["*"],
21-
AllowedMethods = ["*"],
22-
ExposeHeaders = ["*"],
23-
});
24-
c.InlinePolicy("<set-backend-service base-url=\"https://internal.contoso.example\" />");
25-
c.AuthenticationBasic("{{username}}", "{{password}}");
16+
context.AuthenticationBasic("{{username}}", "{{password}}");
2617
}
2718
else
2819
{
29-
c.AuthenticationManagedIdentity(new ManagedIdentityAuthenticationConfig()
20+
context.AuthenticationManagedIdentity(new ManagedIdentityAuthenticationConfig()
3021
{
31-
Resource = "https://management.azure.com/",
32-
OutputTokenVariableName = "testToken",
22+
Resource = "https://management.azure.com/",
3323
});
34-
c.SetHeader("Authorization", Bearer(c.ExpressionContext));
3524
}
3625
}
3726

38-
public void Outbound(IOutboundContext c)
27+
public void Outbound(IOutboundContext context)
3928
{
40-
c.Base();
41-
c.SetBody(FilterSecrets(c.ExpressionContext));
29+
context.Base();
30+
context.SetBody(FilterSecrets(context.ExpressionContext));
4231
}
4332

4433
public bool IsFromCompanyIp(IExpressionContext context)
4534
=> context.Request.IpAddress.StartsWith("10.0.0.");
4635

47-
public string Bearer(IExpressionContext context)
48-
=> $"Bearer {context.Variables["testToken"]}";
49-
5036
[Expression]
5137
public string FilterSecrets(IExpressionContext context)
5238
{

example/source/Source.Example.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="Azure.ApiManagement.PolicyToolkit.Authoring" Version="0.0.1" />
15-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
15+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1616
</ItemGroup>
1717
</Project>

example/test/ApiOperationPolicyTest.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using Contoso.Apis;
1+
using System.Text;
22

3+
using Azure.ApiManagement.PolicyToolkit.Testing;
4+
using Azure.ApiManagement.PolicyToolkit.Testing.Document;
35
using Azure.ApiManagement.PolicyToolkit.Testing.Expressions;
46

7+
using Contoso.Apis;
8+
59
using Newtonsoft.Json.Linq;
610

711
namespace Contoso.Test.Apis;
@@ -38,4 +42,53 @@ public void FilterSecrets()
3842
)
3943
);
4044
}
45+
46+
[TestMethod]
47+
public void TestInboundInternalIp()
48+
{
49+
var policyDocument = new ApiOperationPolicy();
50+
var testDocument = new TestDocument(policyDocument)
51+
{
52+
Context = {
53+
Request = { IpAddress = "10.0.0.1" }
54+
}
55+
};
56+
57+
testDocument.RunInbound();
58+
59+
var headers = testDocument.Context.Request.Headers;
60+
var value = headers.Should().ContainKey("Authorization")
61+
.WhoseValue.Should().ContainSingle()
62+
.Subject;
63+
value.Should().StartWith("Basic ");
64+
DecodeBasicAuthorization(value).Should().Be("{{username}}:{{password}}");
65+
}
66+
67+
[TestMethod]
68+
public void TestInboundExternalIp()
69+
{
70+
var policyDocument = new ApiOperationPolicy();
71+
var testDocument = new TestDocument(policyDocument)
72+
{
73+
Context = {
74+
Request = { IpAddress = "11.0.0.1" }
75+
}
76+
};
77+
testDocument.InInbound().AuthenticationManagedIdentity().ReturnsToken("myToken");
78+
79+
testDocument.RunInbound();
80+
81+
var headers = testDocument.Context.Request.Headers;
82+
var value = headers.Should().ContainKey("Authorization")
83+
.WhoseValue.Should().ContainSingle()
84+
.Subject;
85+
value.Should().StartWith("Bearer ");
86+
value["Bearer ".Length..].Should().Be("myToken");
87+
}
88+
89+
private string DecodeBasicAuthorization(string value)
90+
{
91+
var token = value["Basic ".Length..];
92+
return Encoding.UTF8.GetString(Convert.FromBase64String(token));
93+
}
4194
}

example/test/Test.Example.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
19-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
20-
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
21-
<PackageReference Include="coverlet.collector" Version="3.1.2" />
18+
<PackageReference Include="FluentAssertions" Version="7.0.0" />
19+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
20+
<PackageReference Include="MSTest.TestAdapter" Version="3.6.4" />
21+
<PackageReference Include="MSTest.TestFramework" Version="3.6.4" />
22+
<PackageReference Include="coverlet.collector" Version="6.0.2" />
2223
</ItemGroup>
2324

2425
<ItemGroup>
2526
<PackageReference Include="Azure.ApiManagement.PolicyToolkit.Testing" Version="0.0.1" />
26-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
27+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
2728
</ItemGroup>
2829

2930
<ItemGroup>

example/test/Usings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
global using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
global using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
global using FluentAssertions;

src/Analyzers/Analyzers.csproj

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<TargetFramework>.net8</TargetFramework>
5-
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
7-
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
8-
<RootNamespace>Azure.ApiManagement.PolicyToolkit.Analyzers</RootNamespace>
9-
10-
<IsPackable>false</IsPackable>
11-
</PropertyGroup>
12-
13-
<ItemGroup>
14-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0"/>
15-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
16-
</ItemGroup>
17-
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>.net8</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
8+
<RootNamespace>Azure.ApiManagement.PolicyToolkit.Analyzers</RootNamespace>
9+
10+
<IsPackable>false</IsPackable>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0"/>
15+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
16+
</ItemGroup>
17+
1818
</Project>

src/Authoring/Configs/StatusConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public record StatusConfig
1111
/// <summary>
1212
/// The HTTP status code to return. Policy expressions are allowed.
1313
/// </summary>
14-
public required uint Code { get; init; }
14+
public required int Code { get; init; }
1515

1616
/// <summary>
1717
/// A description of the reason for returning the status code. Policy expressions are allowed.

src/Compiling/Compiling.csproj

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<PackageId>Azure.ApiManagement.PolicyToolkit.Compiling</PackageId>
5-
<PackageVersion>0.0.1</PackageVersion>
6-
<FileVersion>0.0.1</FileVersion>
7-
<AssemblyVersion>0.0.1</AssemblyVersion>
8-
<InformationalVersion>0.0.1</InformationalVersion>
9-
<Authors>Microsoft</Authors>
10-
<PackageLicenseExpression>MIT</PackageLicenseExpression>
11-
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
12-
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
13-
<PackageProjectUrl>https://github.com/Azure/azure-api-management-policy-toolkit</PackageProjectUrl>
14-
<RepositoryUrl>https://github.com/Azure/azure-api-management-policy-toolkit</RepositoryUrl>
15-
<Description>
16-
Azure API Management Policy Toolkit Compiling is a dotnet tool allowing you to transform policy document(s) from C# code to XML.
17-
Read more about it at https://github.com/Azure/azure-api-management-policy-toolkit
18-
</Description>
19-
<PackageTags>Azure;Azure API Management;API Gateway;API Management;Policy;Policies;Policy Toolkit;Compiling;Policy Toolkit Compiling</PackageTags>
20-
</PropertyGroup>
21-
22-
<PropertyGroup>
23-
<OutputType>Exe</OutputType>
24-
<TargetFramework>.net8</TargetFramework>
25-
<ImplicitUsings>enable</ImplicitUsings>
26-
<Nullable>enable</Nullable>
27-
<RootNamespace>Azure.ApiManagement.PolicyToolkit.Compiling</RootNamespace>
28-
29-
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
30-
<PackAsTool>true</PackAsTool>
31-
<ToolCommandName>azure-apim-policy-compiler</ToolCommandName>
32-
<PackageOutputPath>..\..\output</PackageOutputPath>
33-
</PropertyGroup>
34-
35-
<ItemGroup>
36-
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="9.0.0"/>
37-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
38-
</ItemGroup>
39-
40-
<ItemGroup>
41-
<ProjectReference Include="..\Core\Core.csproj"/>
42-
</ItemGroup>
43-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<PackageId>Azure.ApiManagement.PolicyToolkit.Compiling</PackageId>
5+
<PackageVersion>0.0.1</PackageVersion>
6+
<FileVersion>0.0.1</FileVersion>
7+
<AssemblyVersion>0.0.1</AssemblyVersion>
8+
<InformationalVersion>0.0.1</InformationalVersion>
9+
<Authors>Microsoft</Authors>
10+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
11+
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
12+
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
13+
<PackageProjectUrl>https://github.com/Azure/azure-api-management-policy-toolkit</PackageProjectUrl>
14+
<RepositoryUrl>https://github.com/Azure/azure-api-management-policy-toolkit</RepositoryUrl>
15+
<Description>
16+
Azure API Management Policy Toolkit Compiling is a dotnet tool allowing you to transform policy document(s) from C# code to XML.
17+
Read more about it at https://github.com/Azure/azure-api-management-policy-toolkit
18+
</Description>
19+
<PackageTags>Azure;Azure API Management;API Gateway;API Management;Policy;Policies;Policy Toolkit;Compiling;Policy Toolkit Compiling</PackageTags>
20+
</PropertyGroup>
21+
22+
<PropertyGroup>
23+
<OutputType>Exe</OutputType>
24+
<TargetFramework>.net8</TargetFramework>
25+
<ImplicitUsings>enable</ImplicitUsings>
26+
<Nullable>enable</Nullable>
27+
<RootNamespace>Azure.ApiManagement.PolicyToolkit.Compiling</RootNamespace>
28+
29+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
30+
<PackAsTool>true</PackAsTool>
31+
<ToolCommandName>azure-apim-policy-compiler</ToolCommandName>
32+
<PackageOutputPath>..\..\output</PackageOutputPath>
33+
</PropertyGroup>
34+
35+
<ItemGroup>
36+
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="9.0.0"/>
37+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
38+
</ItemGroup>
39+
40+
<ItemGroup>
41+
<ProjectReference Include="..\Core\Core.csproj"/>
42+
</ItemGroup>
43+
</Project>

src/Core/Core.csproj

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<PropertyGroup>
3-
<TargetFramework>.net8</TargetFramework>
4-
<ImplicitUsings>enable</ImplicitUsings>
5-
<Nullable>enable</Nullable>
6-
7-
<IsPackable>false</IsPackable>
8-
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
9-
<RootNamespace>Azure.ApiManagement.PolicyToolkit</RootNamespace>
10-
</PropertyGroup>
11-
12-
<ItemGroup>
13-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
14-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0"/>
15-
</ItemGroup>
16-
17-
<ItemGroup>
18-
<ProjectReference Include="..\Authoring\Authoring.csproj"/>
19-
</ItemGroup>
20-
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>.net8</TargetFramework>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
9+
<RootNamespace>Azure.ApiManagement.PolicyToolkit</RootNamespace>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
14+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0"/>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\Authoring\Authoring.csproj"/>
19+
</ItemGroup>
20+
2121
</Project>

src/Testing/ApimRuntimeEmulator.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)