Skip to content

Commit

Permalink
Added test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffpatton1971 committed Oct 17, 2024
1 parent ffbfa84 commit 01c6138
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 39 deletions.
24 changes: 0 additions & 24 deletions CSharpProject.Tests/CSharpProject.Tests.csproj

This file was deleted.

15 changes: 0 additions & 15 deletions CSharpProject.Tests/UnitTest1.cs

This file was deleted.

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

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PasswordSafeClient\PasswordSafeClient\PasswordSafeClient.csproj" />
</ItemGroup>

</Project>
91 changes: 91 additions & 0 deletions PasswordSafeClient.Tests/Tests/CredentialService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using ModPosh.PasswordSafeClient.Services;
using ModPosh.PasswordSafeClient.Models;
using Moq;
using Moq.Protected;
using NUnit.Framework;
using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace ModPosh.PasswordSafeClient.Tests
{
[TestFixture]
public class CredentialsServiceTests : IDisposable
{
private Mock<HttpMessageHandler> _httpMessageHandlerMock;
private HttpClient _httpClient;
private CredentialsService _credentialsService;
private const string ValidAuthToken = "valid-auth-token";
private const int TestProjectId = 12345;
private const int TestCredentialId = 67890;

[SetUp]
public void Setup()
{
// Mock HttpMessageHandler to simulate HttpClient responses
_httpMessageHandlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);

// Allow Dispose to be called without throwing an exception
_httpMessageHandlerMock.Protected().Setup("Dispose", ItExpr.IsAny<bool>());

_httpClient = new HttpClient(_httpMessageHandlerMock.Object)
{
BaseAddress = new Uri("https://pwdsafe.rackspace.net")
};

// Initialize CredentialsService with mocked HttpClient and auth token
_credentialsService = new CredentialsService(_httpClient, ValidAuthToken);
}

[TearDown]
public void TearDown()
{
// Dispose of _httpClient to release resources after each test
_httpClient?.Dispose();
}

[Test]
public async Task GetCredentialAsync_ReturnsValidCredential()
{
// Arrange: Create a mock response
var expectedCredential = new Credential
{
Id = TestCredentialId,
Description = "Test Description",
Username = "test-user"
};

var jsonResponse = JsonSerializer.Serialize(new { credential = expectedCredential });

_httpMessageHandlerMock.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync", // Mock the protected SendAsync method
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(jsonResponse)
});

// Act: Call GetCredentialAsync
var result = await _credentialsService.GetCredentialAsync(TestProjectId, TestCredentialId);

// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Id, Is.EqualTo(TestCredentialId));
Assert.That(result.Description, Is.EqualTo("Test Description"));
}

// Other test methods...

public void Dispose()
{
// Ensure that resources are properly disposed of
_httpClient?.Dispose();
}
}
}
6 changes: 6 additions & 0 deletions PasswordSafeClient/PasswordSafeClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.8.34316.72
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PasswordSafeClient", "PasswordSafeClient\PasswordSafeClient.csproj", "{62A177F5-BB60-4EDC-A4D4-FA44BB8CA472}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PasswordSafeClient.Tests", "..\PasswordSafeClient.Tests\PasswordSafeClient.Tests.csproj", "{E679CAD4-2875-4C06-ADB0-A7FC3FE368AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{62A177F5-BB60-4EDC-A4D4-FA44BB8CA472}.Debug|Any CPU.Build.0 = Debug|Any CPU
{62A177F5-BB60-4EDC-A4D4-FA44BB8CA472}.Release|Any CPU.ActiveCfg = Release|Any CPU
{62A177F5-BB60-4EDC-A4D4-FA44BB8CA472}.Release|Any CPU.Build.0 = Release|Any CPU
{E679CAD4-2875-4C06-ADB0-A7FC3FE368AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E679CAD4-2875-4C06-ADB0-A7FC3FE368AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E679CAD4-2875-4C06-ADB0-A7FC3FE368AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E679CAD4-2875-4C06-ADB0-A7FC3FE368AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 01c6138

Please sign in to comment.