|
| 1 | +using System.Net; |
| 2 | +using System.Text.Json; |
| 3 | +using Termii; |
| 4 | +using Termii.Tests.Infrastructure; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Termii.Tests; |
| 8 | + |
| 9 | +public sealed class TermiiEmailClientTests |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public async Task SendProductEmailAsyncPostsTemplateEmailBody() |
| 13 | + { |
| 14 | + using var handler = new TestHttpMessageHandler( |
| 15 | + HttpStatusCode.OK, |
| 16 | + """{"code":"ok","message":"Email accepted","message_id":"email-123","status":"queued"}"""); |
| 17 | + var client = TestTermiiClientFactory.Create(handler); |
| 18 | + |
| 19 | + var response = await client.Emails.SendProductEmailAsync( |
| 20 | + new SendProductEmailRequest |
| 21 | + { |
| 22 | + EmailAddress = "person@example.com", |
| 23 | + TemplateId = "template-123", |
| 24 | + Subject = "Order update", |
| 25 | + From = "noreply@example.com", |
| 26 | + SenderName = "Example Store", |
| 27 | + Data = new Dictionary<string, JsonElement> |
| 28 | + { |
| 29 | + ["first_name"] = JsonDocument.Parse("\"Ada\"").RootElement.Clone(), |
| 30 | + ["order_id"] = JsonDocument.Parse("\"ORD-123\"").RootElement.Clone(), |
| 31 | + }, |
| 32 | + }, |
| 33 | + CancellationToken.None); |
| 34 | + |
| 35 | + var request = handler.LastRequest; |
| 36 | + Assert.NotNull(request); |
| 37 | + using var body = await request.ReadJsonBodyAsync(CancellationToken.None); |
| 38 | + |
| 39 | + Assert.Equal(HttpMethod.Post, request.Method); |
| 40 | + Assert.Equal("https://example.test/api/templates/send-email", request.RequestUri!.ToString()); |
| 41 | + Assert.Equal("test-api-key", body.RootElement.GetProperty("api_key").GetString()); |
| 42 | + Assert.Equal("person@example.com", body.RootElement.GetProperty("email_address").GetString()); |
| 43 | + Assert.Equal("template-123", body.RootElement.GetProperty("template_id").GetString()); |
| 44 | + Assert.Equal("Order update", body.RootElement.GetProperty("subject").GetString()); |
| 45 | + Assert.Equal("noreply@example.com", body.RootElement.GetProperty("from").GetString()); |
| 46 | + Assert.Equal("Example Store", body.RootElement.GetProperty("sender_name").GetString()); |
| 47 | + Assert.Equal("Ada", body.RootElement.GetProperty("data").GetProperty("first_name").GetString()); |
| 48 | + Assert.Equal("ORD-123", body.RootElement.GetProperty("data").GetProperty("order_id").GetString()); |
| 49 | + |
| 50 | + Assert.Equal("ok", response.Code); |
| 51 | + Assert.Equal("Email accepted", response.Message); |
| 52 | + Assert.Equal("email-123", response.MessageId); |
| 53 | + Assert.Equal("queued", response.Status); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public async Task SendProductEmailAsyncRejectsMissingRequiredFields() |
| 58 | + { |
| 59 | + using var handler = new TestHttpMessageHandler(); |
| 60 | + var client = TestTermiiClientFactory.Create(handler); |
| 61 | + |
| 62 | + await Assert.ThrowsAsync<ArgumentException>(() => client.Emails.SendProductEmailAsync( |
| 63 | + new SendProductEmailRequest |
| 64 | + { |
| 65 | + EmailAddress = "", |
| 66 | + TemplateId = "template-123", |
| 67 | + }, |
| 68 | + CancellationToken.None)); |
| 69 | + } |
| 70 | +} |
0 commit comments