Skip to content

Commit 6e18c53

Browse files
fix: readded test, corrected pem mock
1 parent fa3deba commit 6e18c53

2 files changed

Lines changed: 104 additions & 17 deletions

File tree

tests/UnitTests/DemographicServicesTests/ManageCaasSubscriptionTests/MeshMailboxExtensionTests.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,29 @@ public class MeshMailboxExtensionTests
1111
[TestMethod]
1212
public async Task GetCACertificates_FromFilePath_ReturnsCollection()
1313
{
14-
var path = FindInParents("nems_certificate.pem");
15-
Assert.IsTrue(File.Exists(path), $"Test certificate not found at {path}");
16-
var certs = await MeshMailboxExtension.GetCACertificates(path, null);
17-
Assert.IsNotNull(certs);
18-
Assert.IsInstanceOfType(certs, typeof(X509Certificate2Collection));
19-
Assert.IsTrue(certs!.Count > 0);
14+
// Create a temporary self-signed certificate and write as PEM to a temp file
15+
using var rsa = RSA.Create(2048);
16+
var req = new CertificateRequest("CN=Test", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
17+
using var cert = req.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1));
18+
var der = cert.Export(X509ContentType.Cert);
19+
var pem = "-----BEGIN CERTIFICATE-----\n"
20+
+ Convert.ToBase64String(der, Base64FormattingOptions.InsertLineBreaks)
21+
+ "\n-----END CERTIFICATE-----\n";
22+
23+
var tempPath = Path.Combine(Path.GetTempPath(), $"test-cert-{Guid.NewGuid():N}.pem");
24+
await File.WriteAllTextAsync(tempPath, pem);
25+
26+
try
27+
{
28+
var certs = await MeshMailboxExtension.GetCACertificates(tempPath, null);
29+
Assert.IsNotNull(certs);
30+
Assert.IsInstanceOfType(certs, typeof(X509Certificate2Collection));
31+
Assert.IsTrue(certs!.Count > 0);
32+
}
33+
finally
34+
{
35+
if (File.Exists(tempPath)) File.Delete(tempPath);
36+
}
2037
}
2138

2239
[TestMethod]
@@ -25,15 +42,6 @@ public async Task GetCACertificates_NoInputs_ReturnsNull()
2542
var certs = await MeshMailboxExtension.GetCACertificates(null, null);
2643
Assert.IsNull(certs);
2744
}
28-
private static string FindInParents(string fileName)
29-
{
30-
var dir = new DirectoryInfo(AppContext.BaseDirectory);
31-
while (dir != null)
32-
{
33-
var candidate = Path.Combine(dir.FullName, fileName);
34-
if (File.Exists(candidate)) return candidate;
35-
dir = dir.Parent;
36-
}
37-
return fileName; // will fail later if not found
38-
}
45+
// kept for potential future use; not used after temp-cert approach
46+
private static string FindInParents(string fileName) => fileName;
3947
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
namespace NHS.CohortManager.Tests.UnitTests.DemographicServicesTests;
2+
3+
using System.Threading.Tasks;
4+
using Common;
5+
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.Options;
7+
using Moq;
8+
using NHS.MESH.Client.Contracts.Services;
9+
using NHS.MESH.Client.Models;
10+
11+
[TestClass]
12+
public class MeshSendCaasSubscribeTests
13+
{
14+
private readonly Mock<ILogger<MeshSendCaasSubscribe>> _logger = new();
15+
private readonly Mock<IMeshOutboxService> _meshOutbox = new();
16+
17+
private MeshSendCaasSubscribe CreateSut(string workflowId = "WF-CAAS-SUB")
18+
{
19+
var cfg = Options.Create(new MeshSendCaasSubscribeConfig { SendCaasWorkflowId = workflowId });
20+
return new MeshSendCaasSubscribe(_logger.Object, _meshOutbox.Object, cfg);
21+
}
22+
23+
[TestMethod]
24+
public async Task SendSubscriptionRequest_Success_SendsExpectedAttachment_AndReturnsMessageId()
25+
{
26+
// Arrange
27+
string? capturedFrom = null, capturedTo = null, capturedWorkflow = null;
28+
FileAttachment? capturedFile = null;
29+
_meshOutbox
30+
.Setup(m => m.SendCompressedMessageAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<FileAttachment>(), null, null, false))
31+
.Callback((string from, string to, string workflow, FileAttachment file, string? _, string? __, bool ___) =>
32+
{
33+
capturedFrom = from; capturedTo = to; capturedWorkflow = workflow; capturedFile = file;
34+
})
35+
.ReturnsAsync(new MeshResponse<SendMessageResponse>
36+
{
37+
IsSuccessful = true,
38+
Response = new SendMessageResponse { MessageId = "MSG123" }
39+
});
40+
41+
var sut = CreateSut();
42+
43+
// Act
44+
var result = await sut.SendSubscriptionRequest(9000000009L, "TO_BOX", "FROM_BOX");
45+
46+
// Assert
47+
Assert.AreEqual("MSG123", result);
48+
Assert.AreEqual("FROM_BOX", capturedFrom);
49+
Assert.AreEqual("TO_BOX", capturedTo);
50+
Assert.AreEqual("WF-CAAS-SUB", capturedWorkflow);
51+
Assert.IsNotNull(capturedFile);
52+
Assert.AreEqual("CaaSSubscribe.parquet", capturedFile!.FileName);
53+
Assert.AreEqual("application/octet-stream", capturedFile.ContentType);
54+
Assert.IsNotNull(capturedFile.Content);
55+
Assert.IsTrue(capturedFile.Content.Length > 0);
56+
}
57+
58+
[TestMethod]
59+
public async Task SendSubscriptionRequest_Failure_ReturnsNull()
60+
{
61+
// Arrange
62+
_meshOutbox
63+
.Setup(m => m.SendCompressedMessageAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<FileAttachment>(), null, null, false))
64+
.ReturnsAsync(new MeshResponse<SendMessageResponse>
65+
{
66+
IsSuccessful = false,
67+
Error = new APIErrorResponse { ErrorCode = "500", ErrorDescription = "boom" }
68+
});
69+
70+
var sut = CreateSut();
71+
72+
// Act
73+
var result = await sut.SendSubscriptionRequest(9000000009L, "TO_BOX", "FROM_BOX");
74+
75+
// Assert
76+
Assert.IsNull(result);
77+
}
78+
}
79+

0 commit comments

Comments
 (0)