-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kej
committed
Nov 2, 2022
0 parents
commit 2ed6f2b
Showing
10 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Description |
29 changes: 29 additions & 0 deletions
29
ReceiptVerifierEndpoint.Tests/ReceiptVerifierEndpoint.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="Moq" Version="4.18.2" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ReceiptVerifierEndpoint\ReceiptVerifierEndpoint.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
133 changes: 133 additions & 0 deletions
133
ReceiptVerifierEndpoint.Tests/ReceiptVerifierEndpointTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using AppleReceiptVerifierNET; | ||
using AppleReceiptVerifierNET.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
using Moq; | ||
using ReceiptVerifierEndpoint.Middlewares; | ||
using Environment = AppleReceiptVerifierNET.Models.Environment; | ||
|
||
namespace ReceiptVerifierEndpoint.Tests; | ||
|
||
public class ReceiptVerifierEndpointTests | ||
{ | ||
[Fact] | ||
public async Task Ensure_Invoke_Async_Writes_Response_Body() | ||
{ | ||
const string requestUrl = "/api/receiptInfo"; | ||
|
||
var options = new OptionsWrapper<ReceiptVerifierMiddlewareOptions>(new ReceiptVerifierMiddlewareOptions | ||
{ | ||
Path = requestUrl | ||
}); | ||
|
||
var context = new DefaultHttpContext | ||
{ | ||
Request = | ||
{ | ||
Path = requestUrl, | ||
Body = new MemoryStream(Encoding.ASCII.GetBytes("{\"Receipt\":\"abc\"}")) | ||
}, | ||
Response = | ||
{ | ||
Body = new MemoryStream() | ||
} | ||
}; | ||
|
||
var receiptVerifierMock = new Mock<IAppleReceiptVerifier>(MockBehavior.Strict); | ||
receiptVerifierMock.Setup(v => v.VerifyReceiptAsync("abc", false)) | ||
.ReturnsAsync(new VerifyReceiptResponse(null, null, null, null, null, null!, 0) | ||
{ | ||
RawJson = "abc" | ||
}); | ||
var receiptVerifierEndpoint = | ||
new ReceiptVerifierEndpointMiddleware(null!, receiptVerifierMock.Object, options); | ||
|
||
await receiptVerifierEndpoint.InvokeAsync(context); | ||
|
||
var responseBody = context.Response.Body; | ||
responseBody.Position = 0; | ||
var responseStr = await new StreamReader(responseBody).ReadToEndAsync(); | ||
Assert.Equal("abc", responseStr); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_Next_Delegate_Executes_If_Wrong_Request_Url() | ||
{ | ||
const string requestUrl = "/api/receiptInfo"; | ||
const string wrongRequestUrl = "/receiptInfo"; | ||
|
||
var options = new OptionsWrapper<ReceiptVerifierMiddlewareOptions>(new ReceiptVerifierMiddlewareOptions | ||
{ | ||
Path = requestUrl | ||
}); | ||
|
||
var context = new DefaultHttpContext | ||
{ | ||
Request = | ||
{ | ||
Path = wrongRequestUrl, | ||
Body = new MemoryStream(Encoding.ASCII.GetBytes("{\"Receipt\":\"abc\"}")) | ||
}, | ||
Response = | ||
{ | ||
Body = new MemoryStream() | ||
} | ||
}; | ||
|
||
var receiptVerifierMock = new Mock<IAppleReceiptVerifier>(MockBehavior.Strict); | ||
receiptVerifierMock.Setup(v => v.VerifyReceiptAsync("abc", false)) | ||
.ReturnsAsync(new VerifyReceiptResponse(null, null, null, null, null, null!, 0) | ||
{ | ||
RawJson = "abc" | ||
}); | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
requestDelegateMock.Setup(x => x(context)); | ||
var receiptVerifierEndpoint = | ||
new ReceiptVerifierEndpointMiddleware(requestDelegateMock.Object, receiptVerifierMock.Object, options); | ||
|
||
await receiptVerifierEndpoint.InvokeAsync(context); | ||
|
||
requestDelegateMock.Verify(x=>x(context), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_Next_Delegate_Not_Executes_If_Request_Url_Is_Correct() | ||
{ | ||
const string requestUrl = "/api/receiptInfo"; | ||
|
||
var options = new OptionsWrapper<ReceiptVerifierMiddlewareOptions>(new ReceiptVerifierMiddlewareOptions | ||
{ | ||
Path = requestUrl | ||
}); | ||
|
||
var context = new DefaultHttpContext | ||
{ | ||
Request = | ||
{ | ||
Path = requestUrl, | ||
Body = new MemoryStream(Encoding.ASCII.GetBytes("{\"Receipt\":\"abc\"}")) | ||
}, | ||
Response = | ||
{ | ||
Body = new MemoryStream() | ||
} | ||
}; | ||
|
||
var receiptVerifierMock = new Mock<IAppleReceiptVerifier>(MockBehavior.Strict); | ||
receiptVerifierMock.Setup(v => v.VerifyReceiptAsync("abc", false)) | ||
.ReturnsAsync(new VerifyReceiptResponse(null, null, null, null, null, null!, 0) | ||
{ | ||
RawJson = "abc" | ||
}); | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
requestDelegateMock.Setup(x => x(context)); | ||
var receiptVerifierEndpoint = | ||
new ReceiptVerifierEndpointMiddleware(requestDelegateMock.Object, receiptVerifierMock.Object, options); | ||
|
||
await receiptVerifierEndpoint.InvokeAsync(context); | ||
|
||
requestDelegateMock.Verify(x=>x(context), Times.Never); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReceiptVerifierEndpoint", "ReceiptVerifierEndpoint\ReceiptVerifierEndpoint.csproj", "{8EAD6645-46DF-4D5B-8849-06368D5208A7}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReceiptVerifierEndpoint.Tests", "ReceiptVerifierEndpoint.Tests\ReceiptVerifierEndpoint.Tests.csproj", "{31C0CBA0-0F68-406E-A178-9DA0C04B486F}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{8EAD6645-46DF-4D5B-8849-06368D5208A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{8EAD6645-46DF-4D5B-8849-06368D5208A7}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{8EAD6645-46DF-4D5B-8849-06368D5208A7}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{8EAD6645-46DF-4D5B-8849-06368D5208A7}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{31C0CBA0-0F68-406E-A178-9DA0C04B486F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{31C0CBA0-0F68-406E-A178-9DA0C04B486F}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{31C0CBA0-0F68-406E-A178-9DA0C04B486F}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{31C0CBA0-0F68-406E-A178-9DA0C04B486F}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using ReceiptVerifierEndpoint.Middlewares; | ||
using AppleReceiptVerifierNET; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace ReceiptVerifierEndpoint; | ||
|
||
public static class HostingExtensions | ||
{ | ||
public static IServiceCollection AddApnsVerifierEndpoint(this IServiceCollection srv, | ||
Action<ReceiptVerifierMiddlewareOptions> configureEndpoint, Action<AppleReceiptVerifierOptions>? configureVerifier = null) | ||
{ | ||
srv.AddOptions<ReceiptVerifierMiddlewareOptions>() | ||
.Configure(configureEndpoint); | ||
|
||
if (configureVerifier != null) | ||
srv.AddAppleReceiptVerifier(configureVerifier); | ||
|
||
return srv; | ||
} | ||
|
||
public static IApplicationBuilder UseApnsVerifierEndpoint(this IApplicationBuilder app) | ||
{ | ||
var verifier = app.ApplicationServices.GetService<IAppleReceiptVerifier>(); | ||
if (verifier == null) | ||
throw new InvalidOperationException("а кто регать верифаер будет"); | ||
|
||
return app.UseMiddleware<ReceiptVerifierEndpointMiddleware>(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
ReceiptVerifierEndpoint/Middlewares/ReceiptVerifierEndpointMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using ReceiptVerifierEndpoint.Models; | ||
using AppleReceiptVerifierNET; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace ReceiptVerifierEndpoint.Middlewares; | ||
|
||
public class ReceiptVerifierEndpointMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
readonly IAppleReceiptVerifier _receiptVerifier; | ||
private readonly ReceiptVerifierMiddlewareOptions _options; | ||
private static readonly JsonSerializerOptions SerializerOptions = new(JsonSerializerDefaults.Web); | ||
|
||
public ReceiptVerifierEndpointMiddleware(RequestDelegate next, IAppleReceiptVerifier receiptVerifier, IOptions<ReceiptVerifierMiddlewareOptions> options) | ||
{ | ||
_next = next; | ||
_receiptVerifier = receiptVerifier; | ||
_options = options.Value; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
if (context.Request.Path.Equals(_options.Path, StringComparison.InvariantCultureIgnoreCase)) | ||
{ | ||
var body = await JsonSerializer.DeserializeAsync<ReceiptDto>(context.Request.Body, | ||
SerializerOptions); | ||
if (body == null) | ||
{ | ||
|
||
} | ||
else | ||
{ | ||
var receiptInfo = await _receiptVerifier.VerifyReceiptAsync(body.Receipt); | ||
await context.Response.WriteAsync(receiptInfo.RawJson); | ||
} | ||
} | ||
else | ||
{ | ||
await _next(context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace ReceiptVerifierEndpoint.Models; | ||
|
||
public record ReceiptDto(string Receipt); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<OutputType>Library</OutputType> | ||
<IsPackable>true</IsPackable> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<StaticWebAssetsEnabled>false</StaticWebAssetsEnabled> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AppleReceiptVerifier.NET" Version="1.0.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace ReceiptVerifierEndpoint; | ||
|
||
public class ReceiptVerifierMiddlewareOptions | ||
{ | ||
public string Path { get; set; } = null!; | ||
} |