From 5baa4842f28d93d79ade659156b118f590e8caf8 Mon Sep 17 00:00:00 2001 From: Julia Ilasova <1julka1il@gmail.com> Date: Tue, 28 May 2024 14:28:29 +0200 Subject: [PATCH] adjust pipeline not to require azure credentials as they are mocked either way --- .github/workflows/test.yaml | 3 --- api/EnvironmentHelper.cs | 26 +++++++++++++++++++ .../AddServicesAndRepositoriesExtension.cs | 2 +- api/Extensions/ConfigureExtensions.cs | 6 +++-- api/GlobalExceptionHandler.cs | 2 +- api/Program.cs | 9 +++---- 6 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 api/EnvironmentHelper.cs diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 828f1cf..06377de 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -35,7 +35,4 @@ jobs: MQTT_CLIENT_ID: ${{ secrets.MQTT_CLIENT_ID }} MQTT_SUBSCRIBE_TOPIC: ${{ secrets.MQTT_SUBSCRIBE_TOPIC }} MQTT_PUBLISH_TOPIC: ${{ secrets.MQTT_PUBLISH_TOPIC }} - - AZURE_VISION_KEY: ${{ secrets.AZURE_VISION_KEY }} - AZURE_VISION_ENDPOINT: ${{ secrets.AZURE_VISION_ENDPOINT }} run: cd Tests && dotnet test \ No newline at end of file diff --git a/api/EnvironmentHelper.cs b/api/EnvironmentHelper.cs new file mode 100644 index 0000000..8546b43 --- /dev/null +++ b/api/EnvironmentHelper.cs @@ -0,0 +1,26 @@ +namespace api; + +public static class EnvironmentHelper +{ + private static readonly List NonProdEnvironments = ["Development", "Testing"]; + + public static bool IsTesting() + { + return Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Testing"; + } + + public static bool IsDevelopment() + { + return Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development"; + } + + public static bool IsNonProd() + { + return NonProdEnvironments.Contains(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")); + } + + public static bool IsCi() + { + return Environment.GetEnvironmentVariable("CI") == "true"; + } +} \ No newline at end of file diff --git a/api/Extensions/AddServicesAndRepositoriesExtension.cs b/api/Extensions/AddServicesAndRepositoriesExtension.cs index a341e3c..3206523 100644 --- a/api/Extensions/AddServicesAndRepositoriesExtension.cs +++ b/api/Extensions/AddServicesAndRepositoriesExtension.cs @@ -29,7 +29,7 @@ public static void AddServicesAndRepositories(this IServiceCollection services) services.AddSingleton(); // External services - if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Testing") + if (EnvironmentHelper.IsTesting()) { services.AddSingleton(); services.AddSingleton(); diff --git a/api/Extensions/ConfigureExtensions.cs b/api/Extensions/ConfigureExtensions.cs index e3c2ee4..43bfcd2 100644 --- a/api/Extensions/ConfigureExtensions.cs +++ b/api/Extensions/ConfigureExtensions.cs @@ -6,7 +6,7 @@ public static class ConfigureExtensions { public static void ConfigureOptions(this WebApplicationBuilder builder) { - if (Environment.GetEnvironmentVariable("CI") is not null) + if (EnvironmentHelper.IsCi()) { builder.Services.Configure(options => { @@ -25,7 +25,9 @@ public static void ConfigureOptions(this WebApplicationBuilder builder) options.SubscribeTopic = Environment.GetEnvironmentVariable("MQTT_SUBSCRIBE_TOPIC") ?? throw new Exception("MQTT subscribe topic is missing"); options.PublishTopic = Environment.GetEnvironmentVariable("MQTT_PUBLISH_TOPIC") ?? throw new Exception("MQTT publish topic is missing"); }); - + + if (EnvironmentHelper.IsTesting()) return; + builder.Services.Configure(options => { options.RemoveBackgroundEndpoint = Environment.GetEnvironmentVariable("AZURE_VISION_REMOVE_BACKGROUND_ENDPOINT") ?? throw new Exception("Azure Vision endpoint is missing"); diff --git a/api/GlobalExceptionHandler.cs b/api/GlobalExceptionHandler.cs index 99cd37c..2d878c0 100644 --- a/api/GlobalExceptionHandler.cs +++ b/api/GlobalExceptionHandler.cs @@ -29,7 +29,7 @@ public static void Handle(this Exception ex, IWebSocketConnection socket, string }; else { - serverResponse = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Testing" + serverResponse = EnvironmentHelper.IsTesting() ? new ServerSendsErrorMessage { Error = ex.Message } : new ServerSendsErrorMessage { Error = "Something went wrong. Please try again later." }; } diff --git a/api/Program.cs b/api/Program.cs index c32f851..d3df78d 100644 --- a/api/Program.cs +++ b/api/Program.cs @@ -1,6 +1,5 @@ using System.Reflection; using System.Text.Json; -using api.Core.Options; using api.Core.Services; using api.Events.Auth.Client; using api.Extensions; @@ -9,7 +8,6 @@ using lib; using Microsoft.EntityFrameworkCore; using Serilog; -using Shared.Dtos; using Shared.Exceptions; using Shared.Models; using Testcontainers.PostgreSql; @@ -26,7 +24,6 @@ public static class Startup nameof(ClientWantsToSignUp) ]; - private static readonly List NonProdEnvironments = ["Development", "Testing"]; private static readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true @@ -52,7 +49,7 @@ public static async Task StartApi(string[] args) var builder = WebApplication.CreateBuilder(args); - if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Testing") + if (EnvironmentHelper.IsTesting()) { var dbContainer = new PostgreSqlBuilder() @@ -92,7 +89,7 @@ public static async Task StartApi(string[] args) var scope = app.Services.CreateScope(); var db = await app.Services.GetRequiredService>().CreateDbContextAsync(); - if (NonProdEnvironments.Contains(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))) + if (EnvironmentHelper.IsNonProd()) { await db.Database.EnsureDeletedAsync(); } @@ -100,7 +97,7 @@ public static async Task StartApi(string[] args) await db.Database.EnsureCreatedAsync(); await db.Database.MigrateAsync(); - if (NonProdEnvironments.Contains(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))) + if (EnvironmentHelper.IsNonProd()) { await db.SeedDevelopmentDataAsync(scope, app.Configuration["AzureBlob:DefaultPlantImageUrl"] ?? "https://example.com"); }