|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http.Json; |
| 3 | +using System.Text; |
| 4 | +using System.Text.Json; |
| 5 | +using FluentAssertions; |
| 6 | +using Moq; |
| 7 | +using OrderMonitor.Api.Controllers; |
| 8 | +using OrderMonitor.Core.Interfaces; |
| 9 | + |
| 10 | +namespace OrderMonitor.IntegrationTests; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Integration tests for AlertsController endpoints. |
| 14 | +/// Tests HTTP request/response handling for alert functionality. |
| 15 | +/// </summary> |
| 16 | +public class AlertsControllerIntegrationTests : IClassFixture<CustomWebApplicationFactory> |
| 17 | +{ |
| 18 | + private readonly CustomWebApplicationFactory _factory; |
| 19 | + |
| 20 | + public AlertsControllerIntegrationTests(CustomWebApplicationFactory factory) |
| 21 | + { |
| 22 | + _factory = factory; |
| 23 | + } |
| 24 | + |
| 25 | + #region POST /api/alerts/test Tests |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public async Task SendTestAlert_WithValidEmail_ReturnsOk() |
| 29 | + { |
| 30 | + // Arrange |
| 31 | + _factory.SetupDefaultMocks(); |
| 32 | + var request = new TestAlertRequest { Email = "test@example.com" }; |
| 33 | + var content = new StringContent( |
| 34 | + JsonSerializer.Serialize(request), |
| 35 | + Encoding.UTF8, |
| 36 | + "application/json"); |
| 37 | + using var client = _factory.CreateClient(); |
| 38 | + |
| 39 | + // Act |
| 40 | + var response = await client.PostAsync("/api/alerts/test", content); |
| 41 | + |
| 42 | + // Assert |
| 43 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 44 | + |
| 45 | + var result = await response.Content.ReadFromJsonAsync<TestAlertResponse>(); |
| 46 | + result.Should().NotBeNull(); |
| 47 | + result!.Success.Should().BeTrue(); |
| 48 | + result.Message.Should().Contain("test@example.com"); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task SendTestAlert_CallsAlertService() |
| 53 | + { |
| 54 | + // Arrange |
| 55 | + _factory.SetupDefaultMocks(); |
| 56 | + var request = new TestAlertRequest { Email = "verify@example.com" }; |
| 57 | + var content = new StringContent( |
| 58 | + JsonSerializer.Serialize(request), |
| 59 | + Encoding.UTF8, |
| 60 | + "application/json"); |
| 61 | + using var client = _factory.CreateClient(); |
| 62 | + |
| 63 | + // Act |
| 64 | + await client.PostAsync("/api/alerts/test", content); |
| 65 | + |
| 66 | + // Assert |
| 67 | + _factory.MockAlertService.Verify( |
| 68 | + s => s.SendTestAlertAsync("verify@example.com", It.IsAny<CancellationToken>()), |
| 69 | + Times.Once); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public async Task SendTestAlert_WithEmptyEmail_ReturnsBadRequest() |
| 74 | + { |
| 75 | + // Arrange |
| 76 | + _factory.SetupDefaultMocks(); |
| 77 | + var request = new TestAlertRequest { Email = "" }; |
| 78 | + var content = new StringContent( |
| 79 | + JsonSerializer.Serialize(request), |
| 80 | + Encoding.UTF8, |
| 81 | + "application/json"); |
| 82 | + using var client = _factory.CreateClient(); |
| 83 | + |
| 84 | + // Act |
| 85 | + var response = await client.PostAsync("/api/alerts/test", content); |
| 86 | + |
| 87 | + // Assert |
| 88 | + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 89 | + |
| 90 | + // Verify the service was NOT called (validation should prevent it) |
| 91 | + _factory.MockAlertService.Verify( |
| 92 | + s => s.SendTestAlertAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), |
| 93 | + Times.Never); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public async Task SendTestAlert_WithNullEmail_ReturnsBadRequest() |
| 98 | + { |
| 99 | + // Arrange |
| 100 | + _factory.SetupDefaultMocks(); |
| 101 | + var content = new StringContent( |
| 102 | + "{}", |
| 103 | + Encoding.UTF8, |
| 104 | + "application/json"); |
| 105 | + using var client = _factory.CreateClient(); |
| 106 | + |
| 107 | + // Act |
| 108 | + var response = await client.PostAsync("/api/alerts/test", content); |
| 109 | + |
| 110 | + // Assert |
| 111 | + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 112 | + } |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public async Task SendTestAlert_WithWhitespaceEmail_ReturnsBadRequest() |
| 116 | + { |
| 117 | + // Arrange |
| 118 | + _factory.SetupDefaultMocks(); |
| 119 | + var request = new TestAlertRequest { Email = " " }; |
| 120 | + var content = new StringContent( |
| 121 | + JsonSerializer.Serialize(request), |
| 122 | + Encoding.UTF8, |
| 123 | + "application/json"); |
| 124 | + using var client = _factory.CreateClient(); |
| 125 | + |
| 126 | + // Act |
| 127 | + var response = await client.PostAsync("/api/alerts/test", content); |
| 128 | + |
| 129 | + // Assert |
| 130 | + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public async Task SendTestAlert_WhenSmtpPasswordNotConfigured_Returns500WithMessage() |
| 135 | + { |
| 136 | + // Arrange |
| 137 | + _factory.SetupDefaultMocks(); |
| 138 | + _factory.MockAlertService |
| 139 | + .Setup(s => s.SendTestAlertAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 140 | + .ThrowsAsync(new InvalidOperationException("SMTP password not configured")); |
| 141 | + |
| 142 | + var request = new TestAlertRequest { Email = "test@example.com" }; |
| 143 | + var content = new StringContent( |
| 144 | + JsonSerializer.Serialize(request), |
| 145 | + Encoding.UTF8, |
| 146 | + "application/json"); |
| 147 | + using var client = _factory.CreateClient(); |
| 148 | + |
| 149 | + // Act |
| 150 | + var response = await client.PostAsync("/api/alerts/test", content); |
| 151 | + |
| 152 | + // Assert |
| 153 | + response.StatusCode.Should().Be(HttpStatusCode.InternalServerError); |
| 154 | + |
| 155 | + var responseBody = await response.Content.ReadAsStringAsync(); |
| 156 | + responseBody.Should().Contain("SMTP password not configured"); |
| 157 | + } |
| 158 | + |
| 159 | + [Fact] |
| 160 | + public async Task SendTestAlert_WhenSmtpFails_Returns500() |
| 161 | + { |
| 162 | + // Arrange |
| 163 | + _factory.SetupDefaultMocks(); |
| 164 | + _factory.MockAlertService |
| 165 | + .Setup(s => s.SendTestAlertAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 166 | + .ThrowsAsync(new Exception("SMTP server unreachable")); |
| 167 | + |
| 168 | + var request = new TestAlertRequest { Email = "test@example.com" }; |
| 169 | + var content = new StringContent( |
| 170 | + JsonSerializer.Serialize(request), |
| 171 | + Encoding.UTF8, |
| 172 | + "application/json"); |
| 173 | + using var client = _factory.CreateClient(); |
| 174 | + |
| 175 | + // Act |
| 176 | + var response = await client.PostAsync("/api/alerts/test", content); |
| 177 | + |
| 178 | + // Assert |
| 179 | + response.StatusCode.Should().Be(HttpStatusCode.InternalServerError); |
| 180 | + } |
| 181 | + |
| 182 | + [Fact] |
| 183 | + public async Task SendTestAlert_ReturnsTimestamp() |
| 184 | + { |
| 185 | + // Arrange |
| 186 | + _factory.SetupDefaultMocks(); |
| 187 | + var request = new TestAlertRequest { Email = "test@example.com" }; |
| 188 | + var content = new StringContent( |
| 189 | + JsonSerializer.Serialize(request), |
| 190 | + Encoding.UTF8, |
| 191 | + "application/json"); |
| 192 | + var beforeRequest = DateTime.UtcNow; |
| 193 | + using var client = _factory.CreateClient(); |
| 194 | + |
| 195 | + // Act |
| 196 | + var response = await client.PostAsync("/api/alerts/test", content); |
| 197 | + |
| 198 | + // Assert |
| 199 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 200 | + |
| 201 | + var result = await response.Content.ReadFromJsonAsync<TestAlertResponse>(); |
| 202 | + result.Should().NotBeNull(); |
| 203 | + result!.SentAt.Should().BeOnOrAfter(beforeRequest); |
| 204 | + result.SentAt.Should().BeOnOrBefore(DateTime.UtcNow.AddSeconds(1)); |
| 205 | + } |
| 206 | + |
| 207 | + [Fact] |
| 208 | + public async Task SendTestAlert_ReturnsJsonContentType() |
| 209 | + { |
| 210 | + // Arrange |
| 211 | + _factory.SetupDefaultMocks(); |
| 212 | + var request = new TestAlertRequest { Email = "test@example.com" }; |
| 213 | + var content = new StringContent( |
| 214 | + JsonSerializer.Serialize(request), |
| 215 | + Encoding.UTF8, |
| 216 | + "application/json"); |
| 217 | + using var client = _factory.CreateClient(); |
| 218 | + |
| 219 | + // Act |
| 220 | + var response = await client.PostAsync("/api/alerts/test", content); |
| 221 | + |
| 222 | + // Assert |
| 223 | + response.Content.Headers.ContentType!.MediaType.Should().Be("application/json"); |
| 224 | + } |
| 225 | + |
| 226 | + #endregion |
| 227 | +} |
0 commit comments