Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
orionstudt committed Sep 4, 2024
1 parent 11b6b81 commit 8f7d42b
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ obj/
*.nupkg
*.csproj.user

# Rider
.idea/*

# Local Settings
*.sln.DotSettings.user

# Ignore NuGet Packages
*.nupkg

Expand Down
96 changes: 96 additions & 0 deletions DuoSecurity.Auth.Http.Tests/DuoAuthClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using DuoSecurity.Auth.Http.Results;
using FluentAssertions;
using NUnit.Framework;

namespace DuoSecurity.Auth.Http.Tests;

[TestFixture]
public class DuoAuthClientTests
{
protected static DuoAuthClient Client => TestSetup.Client;

protected static string UserName => TestSetup.UserName;

[Test]
public async Task Ping_works_as_expected()
{
var response = await Client.PingAsync();

response.Should().NotBeNull();
response.IsSuccessful.Should().BeTrue();
response.Result.Should().NotBeNull();
response.Error.Should().BeNull();
}

[Test]
public async Task Check_works_as_expected()
{
var response = await Client.CheckAsync();

response.Should().NotBeNull();
response.IsSuccessful.Should().BeTrue();
response.Result.Should().NotBeNull();
response.Error.Should().BeNull();
}

[Test(Description = "Note that this test requires that your account has a logo configured.")]
public async Task Logo_works_as_expected()
{
var response = await Client.LogoAsync();

response.Should().NotBeNull();
response.IsSuccessful.Should().BeTrue();
response.Result.Should().NotBeNull();
response.Error.Should().BeNull();
}

[Test(Description = "Note that this test will create an actual user on your account.")]
public async Task Enroll_and_EnrollStatus_works_as_expected()
{
var first = await Client.EnrollAsync(valid_secs: 30);

first.Should().NotBeNull();
first.IsSuccessful.Should().BeTrue();
first.Result.Should().NotBeNull();
first.Error.Should().BeNull();

var enrollment = first.Result!;
var userId = enrollment.UserId!;

await Task.Delay(TimeSpan.FromSeconds(2));
var second = await Client.EnrollStatusAsync(
userId, enrollment.ActivationCode);

second.Should().NotBeNull();
second.IsSuccessful.Should().BeTrue();
second.Result.Should().NotBeNull();
second.Error.Should().BeNull();

second.Result.Status.Should().Be(EnrollStatus.Waiting);
}

[Test(Description = "The given username should be one that is already enrolled.")]
public async Task PreAuth_works_as_expected()
{
var response = await Client.PreAuthByUsernameAsync(UserName);

response.Should().NotBeNull();
response.IsSuccessful.Should().BeTrue();
response.Result.Should().NotBeNull();
response.Error.Should().BeNull();

var result = response.Result!;
result.Devices.Should().NotBeEmpty();
}

[Test(Description = "The given username should be one that is already enrolled. This test will require auth to complete.")]
public async Task Auth_works_as_expected()
{
var response = await Client.AuthAutoByUsernameAsync(UserName);

response.Should().NotBeNull();
response.IsSuccessful.Should().BeTrue();
response.Result.Should().NotBeNull();
response.Error.Should().BeNull();
}
}
27 changes: 27 additions & 0 deletions DuoSecurity.Auth.Http.Tests/DuoSecurity.Auth.Http.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<UserSecretsId>9d9d08ac-5704-4b9c-8165-338ce75ac6be</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="NUnit" Version="3.13.3"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
<PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DuoSecurity.Auth.Http\DuoSecurity.Auth.Http.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions DuoSecurity.Auth.Http.Tests/TestSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Extensions.Configuration;
using NUnit.Framework;

namespace DuoSecurity.Auth.Http.Tests;

[SetUpFixture]
public static class TestSetup
{
public static DuoAuthClient Client { get; private set; } = null!;

public static string UserName { get; private set; } = null!;

[OneTimeSetUp]
public static void OneTimeSetUp()
{
var configuration = new ConfigurationBuilder()
.AddUserSecrets(typeof(TestSetup).Assembly)
.Build().Get<TestConfig>()!;

var duoConfig = new DuoAuthConfig(
configuration.HostName,
configuration.IntegrationKey,
configuration.SecretKey);

Client = new DuoAuthClient(duoConfig);
UserName = configuration.UserName;
}

[OneTimeTearDown]
public static void OneTimeTearDown()
{
Client?.Dispose();
}

private record TestConfig(string IntegrationKey, string SecretKey, string HostName, string UserName);
}
6 changes: 6 additions & 0 deletions DuoSecurity.Auth.Http.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DuoSecurity.Auth.Http.Tests", "DuoSecurity.Auth.Http.Tests\DuoSecurity.Auth.Http.Tests.csproj", "{E56A13CA-18F6-4C16-BB36-EE9008846265}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{4D8F9F67-1DFE-44E2-8C16-2EECEDF7EF16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D8F9F67-1DFE-44E2-8C16-2EECEDF7EF16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D8F9F67-1DFE-44E2-8C16-2EECEDF7EF16}.Release|Any CPU.Build.0 = Release|Any CPU
{E56A13CA-18F6-4C16-BB36-EE9008846265}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E56A13CA-18F6-4C16-BB36-EE9008846265}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E56A13CA-18F6-4C16-BB36-EE9008846265}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E56A13CA-18F6-4C16-BB36-EE9008846265}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion DuoSecurity.Auth.Http/DuoAuthConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class DuoAuthConfig
public string IntegrationKey { get; set; }

public string SecretKey { get; set; }

public DuoAuthConfig(string host, string integrationKey, string secretKey)
{
Host = host;
Expand Down

0 comments on commit 8f7d42b

Please sign in to comment.