|
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Text; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +using Altinn.Common.AccessToken.Services; |
| 9 | +using Altinn.Common.PEP.Interfaces; |
| 10 | +using Altinn.Platform.Events.Controllers; |
| 11 | +using Altinn.Platform.Events.Models; |
| 12 | +using Altinn.Platform.Events.Services.Interfaces; |
| 13 | +using Altinn.Platform.Events.Tests.Mocks; |
| 14 | +using Altinn.Platform.Events.Tests.Mocks.Authentication; |
| 15 | +using Altinn.Platform.Events.Tests.Utils; |
| 16 | +using Altinn.Platform.Events.UnitTest.Mocks; |
| 17 | +using AltinnCore.Authentication.JwtCookie; |
| 18 | + |
| 19 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 20 | +using Microsoft.AspNetCore.TestHost; |
| 21 | +using Microsoft.Extensions.Configuration; |
| 22 | +using Microsoft.Extensions.DependencyInjection; |
| 23 | +using Microsoft.Extensions.Options; |
| 24 | +using Moq; |
| 25 | +using Xunit; |
| 26 | + |
| 27 | +namespace Altinn.Platform.Events.Tests.TestingControllers; |
| 28 | + |
| 29 | +/// <summary> |
| 30 | +/// Reperesents a collection of integration tests. |
| 31 | +/// </summary> |
| 32 | +public partial class IntegrationTests |
| 33 | +{ |
| 34 | + /// <summary> |
| 35 | + /// Test class for LogsController |
| 36 | + /// </summary> |
| 37 | + public class LogsControllerTests : IClassFixture<WebApplicationFactory<LogsController>> |
| 38 | + { |
| 39 | + private const string _basePath = "/events/api/v1"; |
| 40 | + |
| 41 | + private readonly WebApplicationFactory<LogsController> _factory; |
| 42 | + |
| 43 | + private readonly Mock<ITraceLogService> _traceLogServiceMock = new(); |
| 44 | + |
| 45 | + public LogsControllerTests(WebApplicationFactory<LogsController> factory) |
| 46 | + { |
| 47 | + _factory = factory; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Scenario: |
| 52 | + /// Post a request that results in an exception being thrown by the trace log service instance. |
| 53 | + /// Expected result: |
| 54 | + /// Returns internal server error 500. |
| 55 | + /// Success criteria: |
| 56 | + /// The response has correct status code. |
| 57 | + /// </summary> |
| 58 | + [Fact] |
| 59 | + public async Task Logs_WhenExceptionIsThrownByTheService_IsCaughtAnd500IsReturned() |
| 60 | + { |
| 61 | + // Arrange |
| 62 | + string requestUri = $"{_basePath}/storage/events/logs"; |
| 63 | + string responseId = Guid.NewGuid().ToString(); |
| 64 | + |
| 65 | + Mock<IEventsService> eventsService = new(); |
| 66 | + Mock<ITraceLogService> traceLogService = new(); |
| 67 | + |
| 68 | + traceLogService.Setup(x => x.CreateWebhookResponseEntry(It.IsAny<LogEntryDto>())).Throws(new Exception()); |
| 69 | + |
| 70 | + HttpClient client = GetTestClient(eventsService.Object, traceLogService.Object); |
| 71 | + HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) |
| 72 | + { |
| 73 | + Content = new StringContent(JsonSerializer.Serialize(new LogEntryDto()), Encoding.UTF8, "application/cloudevents+json") |
| 74 | + }; |
| 75 | + |
| 76 | + httpRequestMessage.Headers.Add("PlatformAccessToken", PrincipalUtil.GetAccessToken("ttd", "endring-av-navn-v2")); |
| 77 | + |
| 78 | + // Act |
| 79 | + HttpResponseMessage response = await client.SendAsync(httpRequestMessage); |
| 80 | + |
| 81 | + // Assert |
| 82 | + Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Scenario: |
| 87 | + /// A possibly invalid log entry is posted to the logs endpoint. |
| 88 | + /// Expected result: |
| 89 | + /// 400 Bad request is returned |
| 90 | + /// Success criteria: |
| 91 | + /// Empty string returned from the service results in a bad request response |
| 92 | + /// </summary> |
| 93 | + /// <returns></returns> |
| 94 | + [Fact] |
| 95 | + public async Task Logs_WhenEmptyStringIsReturnedFromTheService_ReturnBadRequest() |
| 96 | + { |
| 97 | + // Arrange |
| 98 | + string requestUri = $"{_basePath}/storage/events/logs"; |
| 99 | + string responseId = string.Empty; |
| 100 | + Mock<IEventsService> eventsService = new(); |
| 101 | + Mock<ITraceLogService> traceLogService = new(); |
| 102 | + traceLogService.Setup(x => x.CreateWebhookResponseEntry(It.IsAny<LogEntryDto>())).ReturnsAsync(string.Empty); |
| 103 | + HttpClient client = GetTestClient(eventsService.Object, traceLogService.Object); |
| 104 | + HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) |
| 105 | + { |
| 106 | + Content = new StringContent(JsonSerializer.Serialize(new LogEntryDto()), Encoding.UTF8, "application/cloudevents+json") |
| 107 | + }; |
| 108 | + httpRequestMessage.Headers.Add("PlatformAccessToken", PrincipalUtil.GetAccessToken("ttd", "endring-av-navn-v2")); |
| 109 | + |
| 110 | + // Act |
| 111 | + HttpResponseMessage response = await client.SendAsync(httpRequestMessage); |
| 112 | + |
| 113 | + // Assert |
| 114 | + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Scenario: |
| 119 | + /// Post a valid logs request with a logEntryDto. |
| 120 | + /// Expected result: |
| 121 | + /// Returns HttpStatus Ok. |
| 122 | + /// Success criteria: |
| 123 | + /// The response has correct status code. |
| 124 | + /// </summary> |
| 125 | + [Fact] |
| 126 | + public async Task Logs_ValidLogEntryDto_ReturnsStatusCreated() |
| 127 | + { |
| 128 | + // Arrange |
| 129 | + string requestUri = $"{_basePath}/storage/events/logs"; |
| 130 | + string responseId = Guid.NewGuid().ToString(); |
| 131 | + |
| 132 | + Mock<IEventsService> eventsService = new Mock<IEventsService>(); |
| 133 | + Mock<ITraceLogService> traceLogService = new Mock<ITraceLogService>(); |
| 134 | + |
| 135 | + traceLogService.Setup(x => x.CreateWebhookResponseEntry(It.IsAny<LogEntryDto>())).ReturnsAsync(responseId); |
| 136 | + |
| 137 | + var client = GetTestClient(eventsService.Object, traceLogService.Object); |
| 138 | + |
| 139 | + string logEntryDto = JsonSerializer.Serialize(new LogEntryDto()); |
| 140 | + |
| 141 | + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) |
| 142 | + { |
| 143 | + Content = new StringContent(logEntryDto, Encoding.UTF8, "application/cloudevents+json") |
| 144 | + }; |
| 145 | + |
| 146 | + httpRequestMessage.Headers.Add("PlatformAccessToken", PrincipalUtil.GetAccessToken("ttd", "endring-av-navn-v2")); |
| 147 | + |
| 148 | + // Act |
| 149 | + var response = await client.SendAsync(httpRequestMessage); |
| 150 | + |
| 151 | + // Assert |
| 152 | + Assert.True(response.IsSuccessStatusCode); |
| 153 | + Assert.Equal(HttpStatusCode.Created, response.StatusCode); |
| 154 | + } |
| 155 | + |
| 156 | + private HttpClient GetTestClient(IEventsService eventsService, ITraceLogService traceLogService = null) |
| 157 | + { |
| 158 | + HttpClient client = _factory.WithWebHostBuilder(builder => |
| 159 | + { |
| 160 | + builder.ConfigureAppConfiguration((hostingContext, config) => |
| 161 | + { |
| 162 | + config.AddConfiguration(new ConfigurationBuilder().AddJsonFile("appsettings.unittest.json").Build()); |
| 163 | + }); |
| 164 | + |
| 165 | + builder.ConfigureTestServices(services => |
| 166 | + { |
| 167 | + services.AddSingleton(eventsService); |
| 168 | + |
| 169 | + if (traceLogService != null) |
| 170 | + { |
| 171 | + services.AddSingleton(traceLogService); |
| 172 | + } |
| 173 | + else |
| 174 | + { |
| 175 | + services.AddSingleton(_traceLogServiceMock.Object); |
| 176 | + } |
| 177 | + |
| 178 | + // Set up mock authentication so that not well known endpoint is used |
| 179 | + services.AddSingleton<IPostConfigureOptions<JwtCookieOptions>, JwtCookiePostConfigureOptionsStub>(); |
| 180 | + services.AddSingleton<IPublicSigningKeyProvider, PublicSigningKeyProviderMock>(); |
| 181 | + services.AddSingleton<IPDP, PepWithPDPAuthorizationMockSI>(); |
| 182 | + }); |
| 183 | + }).CreateClient(); |
| 184 | + |
| 185 | + return client; |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments