Skip to content

Commit

Permalink
adjust pipeline not to require azure credentials as they are mocked e…
Browse files Browse the repository at this point in the history
…ither way
  • Loading branch information
juuwel committed May 28, 2024
1 parent 6152c68 commit 5baa484
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions api/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace api;

public static class EnvironmentHelper
{
private static readonly List<string?> 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";
}
}
2 changes: 1 addition & 1 deletion api/Extensions/AddServicesAndRepositoriesExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void AddServicesAndRepositories(this IServiceCollection services)
services.AddSingleton<StatsService>();

// External services
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Testing")
if (EnvironmentHelper.IsTesting())
{
services.AddSingleton<IImageBackgroundRemoverService, MockImageBackgroundRemoverService>();
services.AddSingleton<IBlobStorageService, MockBlobStorageService>();
Expand Down
6 changes: 4 additions & 2 deletions api/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JwtOptions>(options =>
{
Expand All @@ -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<AzureVisionOptions>(options =>
{
options.RemoveBackgroundEndpoint = Environment.GetEnvironmentVariable("AZURE_VISION_REMOVE_BACKGROUND_ENDPOINT") ?? throw new Exception("Azure Vision endpoint is missing");
Expand Down
2 changes: 1 addition & 1 deletion api/GlobalExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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." };
}
Expand Down
9 changes: 3 additions & 6 deletions api/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +8,6 @@
using lib;
using Microsoft.EntityFrameworkCore;
using Serilog;
using Shared.Dtos;
using Shared.Exceptions;
using Shared.Models;
using Testcontainers.PostgreSql;
Expand All @@ -26,7 +24,6 @@ public static class Startup
nameof(ClientWantsToSignUp)
];

private static readonly List<string?> NonProdEnvironments = ["Development", "Testing"];
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
PropertyNameCaseInsensitive = true
Expand All @@ -52,7 +49,7 @@ public static async Task<WebApplication> StartApi(string[] args)

var builder = WebApplication.CreateBuilder(args);

if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Testing")
if (EnvironmentHelper.IsTesting())
{
var dbContainer =
new PostgreSqlBuilder()
Expand Down Expand Up @@ -92,15 +89,15 @@ public static async Task<WebApplication> StartApi(string[] args)
var scope = app.Services.CreateScope();
var db = await app.Services.GetRequiredService<IDbContextFactory<ApplicationDbContext>>().CreateDbContextAsync();

if (NonProdEnvironments.Contains(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")))
if (EnvironmentHelper.IsNonProd())
{
await db.Database.EnsureDeletedAsync();
}

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");
}
Expand Down

0 comments on commit 5baa484

Please sign in to comment.