-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffbfa84
commit 01c6138
Showing
6 changed files
with
128 additions
and
39 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters