Skip to content

Commit 4e7e09f

Browse files
authored
Create KycWebHook lambda (#34)
* 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)
1 parent 3c57161 commit 4e7e09f

9 files changed

Lines changed: 182 additions & 0 deletions

File tree

KYC.API.Proxy.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KYC.API.Proxy", "src\KYC.AP
1111
EndProject
1212
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}"
1313
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KycWebHook", "src\KYC.DataBase\KycWebHook\KycWebHook.csproj", "{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}"
15+
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KycWebHook.Tests", "tests\KycWebHook.Tests\KycWebHook.Tests.csproj", "{41596964-623E-4796-9C70-DABA79EF7309}"
17+
EndProject
1418
Global
1519
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1620
Debug|Any CPU = Debug|Any CPU
@@ -25,13 +29,23 @@ Global
2529
{0AB264DD-CE17-4ED3-BA4B-0539625C48F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
2630
{0AB264DD-CE17-4ED3-BA4B-0539625C48F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
2731
{0AB264DD-CE17-4ED3-BA4B-0539625C48F2}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{41596964-623E-4796-9C70-DABA79EF7309}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{41596964-623E-4796-9C70-DABA79EF7309}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{41596964-623E-4796-9C70-DABA79EF7309}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{41596964-623E-4796-9C70-DABA79EF7309}.Release|Any CPU.Build.0 = Release|Any CPU
2840
EndGlobalSection
2941
GlobalSection(SolutionProperties) = preSolution
3042
HideSolutionNode = FALSE
3143
EndGlobalSection
3244
GlobalSection(NestedProjects) = preSolution
3345
{655F6E73-17FD-437B-B76B-C5C8CDB1BDC2} = {0E291973-D91C-486C-A9D2-8926C656A382}
3446
{0AB264DD-CE17-4ED3-BA4B-0539625C48F2} = {753B9E81-B38A-4B54-830B-F80B129D5E6F}
47+
{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230} = {0E291973-D91C-486C-A9D2-8926C656A382}
48+
{41596964-623E-4796-9C70-DABA79EF7309} = {753B9E81-B38A-4B54-830B-F80B129D5E6F}
3549
EndGlobalSection
3650
GlobalSection(ExtensibilityGlobals) = postSolution
3751
SolutionGuid = {0803A1E1-195C-4AC0-A580-70F2086DE96D}

nuget.config

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
5+
<add key="Github" value="https://nuget.pkg.github.com/The-Poolz/index.json" />
6+
</packageSources>
7+
<packageSourceCredentials>
8+
<Github>
9+
<add key="Username" value="%GITHUB_PACKAGE_USER_NAME%" />
10+
<add key="ClearTextPassword" value="%GITHUB_PACKAGE_TOKEN%" />
11+
</Github>
12+
</packageSourceCredentials>
13+
</configuration>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net6.0</TargetFramework>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
7+
<AWSProjectType>Lambda</AWSProjectType>
8+
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
9+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
10+
<!-- Generate ready to run images during publishing to improve cold start time. -->
11+
<PublishReadyToRun>true</PublishReadyToRun>
12+
</PropertyGroup>
13+
<ItemGroup>
14+
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
15+
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.1.0" />
16+
<PackageReference Include="KYC.DataBase" Version="1.0.2" />
17+
</ItemGroup>
18+
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Net;
2+
using KYC.DataBase;
3+
using KycWebHook.Models;
4+
using Amazon.Lambda.Core;
5+
using Newtonsoft.Json.Linq;
6+
7+
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
8+
9+
namespace KycWebHook;
10+
11+
public class LambdaFunction
12+
{
13+
private readonly KycDbContext context;
14+
15+
public LambdaFunction()
16+
: this(new KycDbContext())
17+
{ }
18+
19+
public LambdaFunction(KycDbContext context)
20+
{
21+
this.context = context;
22+
}
23+
24+
public async Task<int> RunAsync(HttpResponse httpResponse)
25+
{
26+
Console.WriteLine(JToken.FromObject(httpResponse));
27+
28+
var user = context.Users.FirstOrDefault(x => x.RecordId == httpResponse.RecordId);
29+
if (user == null)
30+
{
31+
context.Users.Add(httpResponse);
32+
}
33+
else
34+
{
35+
context.Users.Update(httpResponse);
36+
}
37+
await context.SaveChangesAsync();
38+
39+
return (int)HttpStatusCode.OK;
40+
}
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using KYC.DataBase.Models;
2+
3+
namespace KycWebHook.Models;
4+
5+
public class HttpResponse : User
6+
{
7+
public string Event { get; set; } = null!;
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"Mock Lambda Test Tool": {
4+
"commandName": "Executable",
5+
"commandLineArgs": "--port 5050",
6+
"workingDirectory": ".\\bin\\$(Configuration)\\net6.0",
7+
"executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-6.0.exe"
8+
}
9+
}
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# KycWebHook
2+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<RootNamespace>KycWebHook.Tests</RootNamespace>
8+
<AssemblyName>KycWebHook.Tests</AssemblyName>
9+
<IsPackable>false</IsPackable>
10+
<IsTestProject>true</IsTestProject>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="FluentAssertions" Version="6.11.0" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
16+
<PackageReference Include="xunit" Version="2.4.2" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
<PackageReference Include="coverlet.collector" Version="3.2.0">
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
<PrivateAssets>all</PrivateAssets>
24+
</PackageReference>
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<ProjectReference Include="..\..\src\KYC.DataBase\KycWebHook\KycWebHook.csproj" />
29+
</ItemGroup>
30+
31+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using ConfiguredSqlConnection.Extensions;
2+
using Xunit;
3+
using FluentAssertions;
4+
using KYC.DataBase;
5+
using KycWebHook.Models;
6+
using System.Net;
7+
8+
namespace KycWebHook.Tests;
9+
10+
public class LambdaFunctionTests
11+
{
12+
private readonly HttpResponse httpResponse = new()
13+
{
14+
Guid = "614967cde3227d00125ebc4f",
15+
Status = "deleted",
16+
ClientId = "client_id",
17+
Event = "user.deleted",
18+
RecordId = "5ffffb44baaaaf001236b1d1",
19+
RefId = null
20+
};
21+
22+
[Fact]
23+
public async Task RunAsync_WhenUserNotExist()
24+
{
25+
var context = new DbContextFactory<KycDbContext>().Create(ContextOption.InMemory, Guid.NewGuid().ToString());
26+
27+
var result = await new LambdaFunction(context).RunAsync(httpResponse);
28+
29+
result.Should().Be((int)HttpStatusCode.OK);
30+
context.Users.Should().HaveCount(1);
31+
}
32+
33+
[Fact]
34+
public async Task RunAsync_WhenUserExist()
35+
{
36+
var context = new DbContextFactory<KycDbContext>().Create(ContextOption.InMemory, Guid.NewGuid().ToString());
37+
context.Users.Add(httpResponse);
38+
await context.SaveChangesAsync();
39+
40+
var result = await new LambdaFunction(context).RunAsync(httpResponse);
41+
42+
result.Should().Be((int)HttpStatusCode.OK);
43+
context.Users.First().Should().BeEquivalentTo(httpResponse);
44+
}
45+
}

0 commit comments

Comments
 (0)