From a36223bd0ab27012d93023b1c9cd1b8397b12c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Mon, 25 Nov 2024 09:23:04 +0100 Subject: [PATCH 1/3] Update PullRequestStatusCheckController.cs --- .../PullRequestStatusCheckController.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs b/Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs index a6dd661..2ea6894 100644 --- a/Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs +++ b/Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs @@ -67,7 +67,20 @@ public async Task OnPullRequestChanged( return BadRequest($"Only public repositories from '{string.Join(", ", RepositoryOwners)}' are supported!"); } - var bearerToken = _configuration.GetValue("GithubBearerToken"); + var bearerToken = pullRequestModel.Repository.Owner.Login switch + { + "Testably" => _configuration.GetValue("testablyToken"), + "aweXpect" => _configuration.GetValue("aweXpectToken"), + _ => "" + } + if (string.IsNullOrEmpty(bearerToken)) + { + _logger.LogWarning("Could not find valid bearer token for {Organization}", + pullRequestModel.Repository.Owner.Login); + return StatusCode(StatusCodes.Status403Forbidden, + $"Could not find valid bearer token for {pullRequestModel.Repository.Owner.Login}"); + } + using var client = _clientFactory.CreateClient("Proxied"); client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Testably", Assembly.GetExecutingAssembly().GetName().Version.ToString())); @@ -85,6 +98,7 @@ public async Task OnPullRequestChanged( if (!response.IsSuccessStatusCode) { var responseContent = await response.Content.ReadAsStringAsync(); + _logger.LogWarning("GitHub API '{RequestUri}' not available: {ResponseContent}", requestUri, responseContent); return StatusCode(StatusCodes.Status500InternalServerError, $"GitHub API '{requestUri}' not available: {responseContent}"); } @@ -96,6 +110,7 @@ await response.Content.ReadAsStreamAsync(cancellationToken), if (!jsonDocument.RootElement.TryGetProperty("title", out var titleProperty) || titleProperty.GetString() == null) { + _logger.LogWarning("GitHub API '{RequestUri}' returned an invalid response (missing title).", requestUri); return StatusCode(StatusCodes.Status500InternalServerError, $"GitHub API '{requestUri}' returned an invalid response (missing title)."); } @@ -104,6 +119,7 @@ await response.Content.ReadAsStreamAsync(cancellationToken), !headProperty.TryGetProperty("sha", out var shaProperty) || shaProperty.GetString() == null) { + _logger.LogWarning("GitHub API '{RequestUri}' returned an invalid response (missing head.sha).", requestUri); return StatusCode(StatusCodes.Status500InternalServerError, $"GitHub API '{requestUri}' returned an invalid response (missing head.sha)."); } From e16fbd25d86df3340fd522e7a5565dc62772aed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Mon, 25 Nov 2024 09:24:40 +0100 Subject: [PATCH 2/3] Update PullRequestStatusCheckController.cs --- .../Controllers/PullRequestStatusCheckController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs b/Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs index 2ea6894..b4a6941 100644 --- a/Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs +++ b/Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs @@ -72,7 +72,7 @@ public async Task OnPullRequestChanged( "Testably" => _configuration.GetValue("testablyToken"), "aweXpect" => _configuration.GetValue("aweXpectToken"), _ => "" - } + }; if (string.IsNullOrEmpty(bearerToken)) { _logger.LogWarning("Could not find valid bearer token for {Organization}", From f87398f7fa1cd232e4dab7ca890391dce2f7accc Mon Sep 17 00:00:00 2001 From: Valentin Date: Mon, 25 Nov 2024 09:38:25 +0100 Subject: [PATCH 3/3] Fix tests --- .idea/.idea.Testably.Server/.idea/.gitignore | 13 ++++++++++ .../.idea.Testably.Server/.idea/encodings.xml | 4 +++ .idea/.idea.Testably.Server/.idea/vcs.xml | 4 +++ Testably.Server.sln.DotSettings | 1 + Tests/Testably.Server.Tests/TestFactory.cs | 25 ++++++++++++++++--- Tests/Testably.Server.Tests/WebhookTests.cs | 3 ++- 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 .idea/.idea.Testably.Server/.idea/.gitignore create mode 100644 .idea/.idea.Testably.Server/.idea/encodings.xml create mode 100644 .idea/.idea.Testably.Server/.idea/vcs.xml diff --git a/.idea/.idea.Testably.Server/.idea/.gitignore b/.idea/.idea.Testably.Server/.idea/.gitignore new file mode 100644 index 0000000..882e90d --- /dev/null +++ b/.idea/.idea.Testably.Server/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/contentModel.xml +/.idea.Testably.Server.iml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.Testably.Server/.idea/encodings.xml b/.idea/.idea.Testably.Server/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.Testably.Server/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.Testably.Server/.idea/vcs.xml b/.idea/.idea.Testably.Server/.idea/vcs.xml new file mode 100644 index 0000000..d843f34 --- /dev/null +++ b/.idea/.idea.Testably.Server/.idea/vcs.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Testably.Server.sln.DotSettings b/Testably.Server.sln.DotSettings index d084548..fd38711 100644 --- a/Testably.Server.sln.DotSettings +++ b/Testably.Server.sln.DotSettings @@ -449,6 +449,7 @@ True True True + True True True True diff --git a/Tests/Testably.Server.Tests/TestFactory.cs b/Tests/Testably.Server.Tests/TestFactory.cs index bbf0ebe..ee78766 100644 --- a/Tests/Testably.Server.Tests/TestFactory.cs +++ b/Tests/Testably.Server.Tests/TestFactory.cs @@ -1,17 +1,25 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace Testably.Server.Tests; public class TestFactory : WebApplicationFactory { + private readonly IConfiguration _configuration; private readonly IHttpClientFactory? _httpClientFactory; - public TestFactory(IHttpClientFactory? httpClientFactory = null) + public TestFactory(IHttpClientFactory? httpClientFactory = null, + Action>? configuration = null) { _httpClientFactory = httpClientFactory; + var builder = new ConfigurationBuilder(); + var inMemoryConfiguration = new Dictionary(); + configuration?.Invoke(inMemoryConfiguration); + _configuration = builder.AddInMemoryCollection(inMemoryConfiguration!).Build(); } /// @@ -22,8 +30,8 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) { if (_httpClientFactory != null) { - var descriptor = - services.SingleOrDefault(d => d.ServiceType == _httpClientFactory.GetType()); + var descriptor = services + .SingleOrDefault(d => d.ServiceType == _httpClientFactory.GetType()); if (descriptor != null) { services.Remove(descriptor); @@ -31,6 +39,17 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) services.AddSingleton(_httpClientFactory); } + + services.AddSingleton(_configuration); }); } + + /// + /// https://stackoverflow.com/a/69825605 + /// + protected override IHost CreateHost(IHostBuilder builder) + { + builder.UseContentRoot(Directory.GetCurrentDirectory()); + return base.CreateHost(builder); + } } \ No newline at end of file diff --git a/Tests/Testably.Server.Tests/WebhookTests.cs b/Tests/Testably.Server.Tests/WebhookTests.cs index 00c4a07..3ce7214 100644 --- a/Tests/Testably.Server.Tests/WebhookTests.cs +++ b/Tests/Testably.Server.Tests/WebhookTests.cs @@ -33,7 +33,8 @@ public async Task HappyCase_ShouldSucceed(string title, string status) sentStatusCheck = c; return ""; })); - await using var factory = new TestFactory(httpClientFactoryMock.Object); + await using var factory = new TestFactory(httpClientFactoryMock.Object, + c => c.Add("testablyToken", "foo")); using var client = factory.CreateClient(); client.DefaultRequestHeaders.Add("x-github-event", "pull_request"); using var content = new StringContent(webhookPayload, mediaType: "application/json",