Skip to content

Commit

Permalink
Add tests for IAM project
Browse files Browse the repository at this point in the history
  • Loading branch information
gkurbesov committed Nov 23, 2024
1 parent 1ef88f1 commit 8131ebd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions YaCloudKit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YaCloudKit.IAM", "src\Ident
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YaCloudKit.IAM.Examples", "samples\YaCloudKit.IAM.Examples\YaCloudKit.IAM.Examples.csproj", "{26E7F91F-B3A5-4EE7-A615-BB524ABFDA74}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YaCloudKit.IAM.Tests", "src\IdentityAccessManagement\YaCloudKit.IAM.Tests\YaCloudKit.IAM.Tests.csproj", "{E33BAB01-697B-4226-81ED-B47C96ABF0E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -83,6 +85,10 @@ Global
{26E7F91F-B3A5-4EE7-A615-BB524ABFDA74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26E7F91F-B3A5-4EE7-A615-BB524ABFDA74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26E7F91F-B3A5-4EE7-A615-BB524ABFDA74}.Release|Any CPU.Build.0 = Release|Any CPU
{E33BAB01-697B-4226-81ED-B47C96ABF0E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E33BAB01-697B-4226-81ED-B47C96ABF0E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E33BAB01-697B-4226-81ED-B47C96ABF0E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E33BAB01-697B-4226-81ED-B47C96ABF0E8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -99,6 +105,7 @@ Global
{8796825F-8B82-4EDA-B21C-9D552168407B} = {3B55C34B-BA59-4ABB-87C5-59153FF74CB5}
{6AF165C7-DAA3-4000-9352-994954A9C77D} = {A584AADB-F033-49CE-8740-D75C8B21D387}
{26E7F91F-B3A5-4EE7-A615-BB524ABFDA74} = {3B55C34B-BA59-4ABB-87C5-59153FF74CB5}
{E33BAB01-697B-4226-81ED-B47C96ABF0E8} = {A584AADB-F033-49CE-8740-D75C8B21D387}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EB60A6CA-F76D-40F9-BFDD-ACA1F097B83D}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1"/>
<PackageReference Include="MSTest.TestFramework" Version="3.1.1"/>
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\YaCloudKit.IAM\YaCloudKit.IAM.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\YaCloudKit.IAM\YaCloudKit.IAM.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using YaCloudKit.IAM.Rsa;

namespace YaCloudKit.IAM.Tests;

[TestClass]
public class YandexCachedPrivateKeyProviderTests
{
[TestMethod]
public async Task GetPrivateKeyAsync_WhenPrivateKeyIsNotCached_ShouldCallFuncAgain()
{
// Arrange
var count = 0;

var privateKeyFunc = new Func<CancellationToken, Task<char[]>>(async _ =>
{
count++;
return "privateKey".ToCharArray();
});

var privateKeyProvider = new YandexFuncPrivateKeyProvider(privateKeyFunc);

// Act
_ = privateKeyProvider.GetPrivateKeyAsync(CancellationToken.None);
_ = privateKeyProvider.GetPrivateKeyAsync(CancellationToken.None);

// Assert
Assert.AreEqual(2, count);
}

[TestMethod]
public async Task GetPrivateKeyAsync_WhenPrivateKeyIsCached_ShouldNotCallFuncAgain()
{
// Arrange
var count = 0;

var privateKeyFunc = new Func<CancellationToken, Task<char[]>>(async _ =>
{
count++;
return "privateKey".ToCharArray();
});

var privateKeyProvider = new YandexFuncPrivateKeyProvider(privateKeyFunc, true);

// Act
_ = privateKeyProvider.GetPrivateKeyAsync(CancellationToken.None);
_ = privateKeyProvider.GetPrivateKeyAsync(CancellationToken.None);

// Assert
Assert.AreEqual(1, count);
}
}

0 comments on commit 8131ebd

Please sign in to comment.