Skip to content

Commit

Permalink
Add ReportRequestConverterTests
Browse files Browse the repository at this point in the history
  • Loading branch information
trejjam committed Jun 17, 2023
1 parent 864626f commit 56e912e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ReportingApi.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReportingApi", "src\ReportingApi\ReportingApi.csproj", "{03785889-0A06-4001-9144-36C1A2278B94}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReportingApi.Tests", "src\ReportingApi.Tests\ReportingApi.Tests.csproj", "{6001C0E3-D5B9-4537-ACE2-1187D28799BF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{03785889-0A06-4001-9144-36C1A2278B94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03785889-0A06-4001-9144-36C1A2278B94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03785889-0A06-4001-9144-36C1A2278B94}.Release|Any CPU.Build.0 = Release|Any CPU
{6001C0E3-D5B9-4537-ACE2-1187D28799BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6001C0E3-D5B9-4537-ACE2-1187D28799BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6001C0E3-D5B9-4537-ACE2-1187D28799BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6001C0E3-D5B9-4537-ACE2-1187D28799BF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
43 changes: 43 additions & 0 deletions src/ReportingApi.Tests/ReportRequestConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using ReportingApi.Models;
using System.Text.Json;
using Xunit;

namespace ReportingApi.Tests;

public class ReportRequestConverterTests
{
[Fact]
public void CspViolationTest()
{
var sourceJson = """
{
"age":1,
"body": {
"blockedURL":"https://csplite.com/tst/media/7_del.png",
"disposition":"enforce",
"documentURL":"https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d",
"effectiveDirective":"img-src",
"lineNumber":9,
"originalPolicy":"default-src 'none'; report-to endpoint-csp;",
"referrer":"https://csplite.com/test229/",
"sourceFile":"https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d",
"statusCode":0
},
"type":"csp-violation",
"url":"https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d",
"user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
}
""";

var reportRequest = JsonSerializer.Deserialize<ReportRequest>(sourceJson);

Assert.NotNull(reportRequest);
Assert.Equal(1, reportRequest.Age);
Assert.Equal("https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d", reportRequest.Url);
Assert.Equal("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36", reportRequest.UserAgent);

var cspReport = Assert.IsType<ReportRequest<CspReport>>(reportRequest);
Assert.Equal("csp-violation", cspReport.Type);
Assert.NotNull(cspReport.Body);
}
}
29 changes: 29 additions & 0 deletions src/ReportingApi.Tests/ReportingApi.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<PackageLicense>MIT</PackageLicense>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ReportingApi\ReportingApi.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>


</Project>

0 comments on commit 56e912e

Please sign in to comment.