Files
components/LiteCharms.Features.Tests/HashServiceFeatureTests.cs
T
Khwezi Mngoma 5eb6dbc8b2
continuous-integration/drone/pr Build is passing
Refactored shasher payfast confirmation response handling
2026-06-01 16:36:33 +02:00

155 lines
4.6 KiB
C#

using LiteCharms.Features.Hasher;
using LiteCharms.Features.Models;
using static LiteCharms.Features.Extensions.Hash;
namespace LiteCharms.Features.Tests;
public class HashServiceFeatureTests(Fixture fixture) : IClassFixture<Fixture>
{
private readonly HashService hashService = fixture.Services.GetRequiredService<HashService>();
private readonly string payfastPassphrase = fixture.Configuration.GetSection("HasherSettings:PayfastPassphrase").Value!;
[Fact]
public void StringToSha256Hash_Should_GenerateHash()
{
var input = "We are the best";
var expectedHash = "96E17275B53F6BEB7A0D1C4F789F226D3C71CBE398585F25B3028F2B432E78AB";
var result = HashService.StringToSha256Hash(input);
Assert.NotNull(result);
Assert.True(HashService.IsSha256Hash(result));
Assert.Equal(expectedHash, result);
}
[Fact]
public void StreamToSha256Hash_Should_GenerateHash()
{
var input = "We are successful";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
var expectedHash = "C27872EE494B09D72203C98FC858268F3CD3492D62AA7B766A873520C2C73AFB";
var result = HashService.StreamToSha256Hash(stream);
Assert.NotNull(result);
Assert.True(HashService.IsSha256Hash(result));
Assert.Equal(expectedHash, result);
}
[Fact]
public void BytesToSha256Hash_Should_GenerateHash()
{
var inputBytes = Encoding.UTF8.GetBytes("We are wealthy");
var expectedHash = "3876BF98F6E4A8E42B22C40415687D6FF13F0E887F3F508B71852298FC665737";
var result = HashService.BytesToSha256Hash(inputBytes);
Assert.NotNull(result);
Assert.True(HashService.IsSha256Hash(result));
Assert.Equal(expectedHash, result);
}
[Fact]
public void ToMd5Hash_Should_GenerateHash()
{
var input = "We manifest our desired destiny";
var expectedMd5Lowercase = "6c7816869bcebe4634f7afe9c66dfa08";
var result = HashService.ToMd5Hash(input);
Assert.True(result.IsSuccess);
Assert.True(HashService.IsMd5Hash(result.Value));
Assert.Equal(expectedMd5Lowercase, result.Value);
}
[Fact]
public void VerifyPayfastWebhookSignature_Should_GenerateHash()
{
var paymentId = hashService.HashEncodeLongId(1001).Value;
var payload = new PayfastWebhookPayload
{
Amount = "350.00",
ItemName = "System Architecture Book",
MPaymentId = paymentId,
};
var rawPayload = payload.ToRawPayfastPayload(payfastPassphrase);
var generatedSignature = HashService.ToMd5Hash(rawPayload).Value;
var result = hashService.VerifyPayfastWebhookSignature(payload, generatedSignature);
Assert.True(result.IsSuccess);
Assert.True(result.Value);
}
[Fact]
public void HashEncodeHex_Should_GenerateHash()
{
var validHexInput = "DEADBEEF42";
var result = hashService.HashEncodeHex(validHexInput);
Assert.True(result.IsSuccess);
Assert.False(string.IsNullOrWhiteSpace(result.Value));
}
[Fact]
public void HashEncodeIntId_Should_GenerateHash()
{
int targetId = 42;
var result = hashService.HashEncodeIntId(targetId);
Assert.True(result.IsSuccess);
Assert.True(result.Value.Length >= 10);
}
[Fact]
public void HashEncodeLongId_Should_GenerateHash()
{
long targetId = 9904185012L;
var result = hashService.HashEncodeLongId(targetId);
Assert.True(result.IsSuccess);
Assert.True(result.Value.Length >= 10);
}
[Fact]
public void DecodeIntIdHash_Should_GenerateHash()
{
int originalId = 88041;
var hashedString = hashService.HashEncodeIntId(originalId).Value;
var result = hashService.DecodeIntIdHash(hashedString);
Assert.True(result.IsSuccess);
Assert.Equal(originalId, result.Value);
}
[Fact]
public void DecodeLongIdHash_Should_GenerateHash()
{
long originalId = 9081230491823L;
var hashedString = hashService.HashEncodeLongId(originalId).Value;
var result = hashService.DecodeLongIdHash(hashedString);
Assert.True(result.IsSuccess);
Assert.Equal(originalId, result.Value);
}
[Fact]
public void DecodeHexHash_Should_GenerateHash()
{
var originalHex = "ABCDEF12345";
var hashedString = hashService.HashEncodeHex(originalHex).Value;
var result = hashService.DecodeHexHash(hashedString);
Assert.True(result.IsSuccess);
Assert.Equal(originalHex, result.Value);
}
}