Skip to content

Commit

Permalink
Create KycWebHook lambda (#34)
Browse files Browse the repository at this point in the history
* Add `KycWebHook` lambda

* - install KYC.DataBase package
- add input model for lambda
- cw input

* update lambda function

* add tests

* fix test, cleanup tests

* Create nuget.config (#38)
  • Loading branch information
ArdenHide authored Oct 11, 2023
1 parent 3c57161 commit 4e7e09f
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 0 deletions.
14 changes: 14 additions & 0 deletions KYC.API.Proxy.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KYC.API.Proxy", "src\KYC.AP
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KYC.API.Proxy.Tests", "tests\KYC.API.Proxy.Tests\KYC.API.Proxy.Tests.csproj", "{0AB264DD-CE17-4ED3-BA4B-0539625C48F2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KycWebHook", "src\KYC.DataBase\KycWebHook\KycWebHook.csproj", "{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KycWebHook.Tests", "tests\KycWebHook.Tests\KycWebHook.Tests.csproj", "{41596964-623E-4796-9C70-DABA79EF7309}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,13 +29,23 @@ Global
{0AB264DD-CE17-4ED3-BA4B-0539625C48F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AB264DD-CE17-4ED3-BA4B-0539625C48F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AB264DD-CE17-4ED3-BA4B-0539625C48F2}.Release|Any CPU.Build.0 = Release|Any CPU
{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Release|Any CPU.Build.0 = Release|Any CPU
{41596964-623E-4796-9C70-DABA79EF7309}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41596964-623E-4796-9C70-DABA79EF7309}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41596964-623E-4796-9C70-DABA79EF7309}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41596964-623E-4796-9C70-DABA79EF7309}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{655F6E73-17FD-437B-B76B-C5C8CDB1BDC2} = {0E291973-D91C-486C-A9D2-8926C656A382}
{0AB264DD-CE17-4ED3-BA4B-0539625C48F2} = {753B9E81-B38A-4B54-830B-F80B129D5E6F}
{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230} = {0E291973-D91C-486C-A9D2-8926C656A382}
{41596964-623E-4796-9C70-DABA79EF7309} = {753B9E81-B38A-4B54-830B-F80B129D5E6F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0803A1E1-195C-4AC0-A580-70F2086DE96D}
Expand Down
13 changes: 13 additions & 0 deletions nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
<add key="Github" value="https://nuget.pkg.github.com/The-Poolz/index.json" />
</packageSources>
<packageSourceCredentials>
<Github>
<add key="Username" value="%GITHUB_PACKAGE_USER_NAME%" />
<add key="ClearTextPassword" value="%GITHUB_PACKAGE_TOKEN%" />
</Github>
</packageSourceCredentials>
</configuration>
18 changes: 18 additions & 0 deletions src/KYC.DataBase/KycWebHook/KycWebHook.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<!-- Generate ready to run images during publishing to improve cold start time. -->
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.1.0" />
<PackageReference Include="KYC.DataBase" Version="1.0.2" />
</ItemGroup>
</Project>
41 changes: 41 additions & 0 deletions src/KYC.DataBase/KycWebHook/LambdaFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Net;
using KYC.DataBase;
using KycWebHook.Models;
using Amazon.Lambda.Core;
using Newtonsoft.Json.Linq;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace KycWebHook;

public class LambdaFunction
{
private readonly KycDbContext context;

public LambdaFunction()
: this(new KycDbContext())
{ }

public LambdaFunction(KycDbContext context)
{
this.context = context;
}

public async Task<int> RunAsync(HttpResponse httpResponse)
{
Console.WriteLine(JToken.FromObject(httpResponse));

var user = context.Users.FirstOrDefault(x => x.RecordId == httpResponse.RecordId);
if (user == null)
{
context.Users.Add(httpResponse);
}
else
{
context.Users.Update(httpResponse);
}
await context.SaveChangesAsync();

return (int)HttpStatusCode.OK;
}
}
8 changes: 8 additions & 0 deletions src/KYC.DataBase/KycWebHook/Models/HttpResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using KYC.DataBase.Models;

namespace KycWebHook.Models;

public class HttpResponse : User
{
public string Event { get; set; } = null!;
}
10 changes: 10 additions & 0 deletions src/KYC.DataBase/KycWebHook/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"Mock Lambda Test Tool": {
"commandName": "Executable",
"commandLineArgs": "--port 5050",
"workingDirectory": ".\\bin\\$(Configuration)\\net6.0",
"executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-6.0.exe"
}
}
}
2 changes: 2 additions & 0 deletions src/KYC.DataBase/KycWebHook/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# KycWebHook

31 changes: 31 additions & 0 deletions tests/KycWebHook.Tests/KycWebHook.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>KycWebHook.Tests</RootNamespace>
<AssemblyName>KycWebHook.Tests</AssemblyName>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<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>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\KYC.DataBase\KycWebHook\KycWebHook.csproj" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions tests/KycWebHook.Tests/LambdaFunctionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using ConfiguredSqlConnection.Extensions;
using Xunit;
using FluentAssertions;
using KYC.DataBase;
using KycWebHook.Models;
using System.Net;

namespace KycWebHook.Tests;

public class LambdaFunctionTests
{
private readonly HttpResponse httpResponse = new()
{
Guid = "614967cde3227d00125ebc4f",
Status = "deleted",
ClientId = "client_id",
Event = "user.deleted",
RecordId = "5ffffb44baaaaf001236b1d1",
RefId = null
};

[Fact]
public async Task RunAsync_WhenUserNotExist()
{
var context = new DbContextFactory<KycDbContext>().Create(ContextOption.InMemory, Guid.NewGuid().ToString());

var result = await new LambdaFunction(context).RunAsync(httpResponse);

result.Should().Be((int)HttpStatusCode.OK);
context.Users.Should().HaveCount(1);
}

[Fact]
public async Task RunAsync_WhenUserExist()
{
var context = new DbContextFactory<KycDbContext>().Create(ContextOption.InMemory, Guid.NewGuid().ToString());
context.Users.Add(httpResponse);
await context.SaveChangesAsync();

var result = await new LambdaFunction(context).RunAsync(httpResponse);

result.Should().Be((int)HttpStatusCode.OK);
context.Users.First().Should().BeEquivalentTo(httpResponse);
}
}

0 comments on commit 4e7e09f

Please sign in to comment.