Skip to content

Commit

Permalink
testing on ci should now use secrets instead of appsettings
Browse files Browse the repository at this point in the history
  • Loading branch information
juuwel committed Apr 26, 2024
1 parent 8d439cf commit 9b01ace
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 20 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,20 @@ jobs:
dotnet-version: 8.0.x

- name: Run tests
env:
CI: true
JWT_KEY: ${{ secrets.JWT_KEY }}
JWT_ISSUER: ${{ secrets.JWT_ISSUER }}
JWT_AUDIENCE: ${{ secrets.JWT_AUDIENCE }}
JWT_EXPIRY: ${{ secrets.JWT_EXPIRY }}

MQTT_BROKER: ${{ secrets.MQTT_BROKER }}
MQTT_PORT: ${{ secrets.MQTT_PORT }}
MQTT_USERNAME: ${{ secrets.MQTT_USERNAME }}
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ obj/
riderModule.iml
/_ReSharper.Caches/
appsettings.json
appsettings.Development.json
appsettings.Development.json
appsettings.Production.json
appsettings.Testing.json
7 changes: 7 additions & 0 deletions Core/Options/AzureVisionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Core.Options;

public class AzureVisionOptions
{
public string Endpoint { get; set; } = null!;
public string Key { get; set; } = null!;
}
2 changes: 0 additions & 2 deletions Tests/AuthTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Reactive.Linq;
using api.Events.Auth.Client;
using api.Events.Auth.Server;
using api.Events.Global;
using lib;
using Shared.Dtos.FromClient;
using Shared.Dtos.FromClient.Identity;

namespace Tests;
Expand Down
6 changes: 3 additions & 3 deletions Tests/PlantTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ private CreatePlantDto GenerateRandomCreatePlantDto(string email)
ImageUrl = "https://realurl.com",
CreateRequirementsDto = new CreateRequirementsDto
{
SoilMoisture = RequirementLevel.Low,
SoilMoistureLevel = RequirementLevel.Low,
LightLevel = RequirementLevel.Medium,
Temperature = RequirementLevel.High,
Humidity = RequirementLevel.Low
TemperatureLevel = RequirementLevel.High,
HumidityLevel = RequirementLevel.Low
}
};
return createPlantDto;
Expand Down
14 changes: 9 additions & 5 deletions api/Extensions/AddServicesAndRepositoriesExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ public static class AddServicesAndRepositoriesExtension
{
public static void AddServicesAndRepositories(this IServiceCollection services)
{
// Repositories
services.AddSingleton<UserRepository>();
services.AddSingleton<PlantRepository>();
services.AddSingleton<RequirementsRepository>();
services.AddSingleton<ConditionsLogsRepository>();
services.AddSingleton<CollectionsRepository>();

// Services
services.AddSingleton<WebSocketConnectionService>();
services.AddSingleton<JwtService>();
services.AddSingleton<UserService>();
services.AddSingleton<PlantService>();
services.AddSingleton<ConditionsLogsService>();
services.AddSingleton<RequirementService>();
services.AddSingleton<MqttSubscriberService>();

// Repositories
services.AddSingleton<UserRepository>();
services.AddSingleton<PlantRepository>();
services.AddSingleton<RequirementsRepository>();
services.AddSingleton<MqttPublisherService>();
}
}
33 changes: 32 additions & 1 deletion api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,40 @@ public static async Task<WebApplication> StartApi(string[] args)
options.UseNpgsql(connectionString ?? throw new Exception("Connection string cannot be null"));
});
}

builder.Services.Configure<JwtOptions>(builder.Configuration.GetSection("JWT"));
builder.Services.Configure<MqttOptions>(builder.Configuration.GetSection("MQTT"));
builder.Services.Configure<AzureVisionOptions>(builder.Configuration.GetSection("AzureVision"));

// On ci options are stored as repository secrets
if (args.Contains("ENVIRONMENT=Testing") && Environment.GetEnvironmentVariable("CI") is not null)
{
builder.Services.Configure<JwtOptions>(options =>
{
options.Key = Environment.GetEnvironmentVariable("JWT_KEY") ?? throw new Exception("JWT key is missing");
options.Issuer = Environment.GetEnvironmentVariable("JWT_ISSUER") ?? throw new Exception("JWT issuer is missing");
options.Audience = Environment.GetEnvironmentVariable("JWT_AUDIENCE") ?? throw new Exception("JWT audience is missing");
options.ExpirationMinutes = int.Parse(Environment.GetEnvironmentVariable("JWT_EXPIRY") ?? throw new Exception("JWT expiration minutes is missing"));
});

builder.Services.Configure<MqttOptions>(options =>
{
options.Server = Environment.GetEnvironmentVariable("MQTT_BROKER") ?? throw new Exception("MQTT broker is missing");
options.Port = int.Parse(Environment.GetEnvironmentVariable("MQTT_PORT") ?? throw new Exception("MQTT port is missing"));
options.ClientId = Environment.GetEnvironmentVariable("MQTT_CLIENT_ID") ?? throw new Exception("MQTT client id is missing");
options.Username = Environment.GetEnvironmentVariable("MQTT_USERNAME") ?? throw new Exception("MQTT username is missing");
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");
});

builder.Services.Configure<AzureVisionOptions>(options =>
{
options.Endpoint = Environment.GetEnvironmentVariable("AZURE_VISION_ENDPOINT") ?? throw new Exception("Azure vision endpoint is missing");
options.Key = Environment.GetEnvironmentVariable("AZURE_VISION_KEY") ?? throw new Exception("Azure vision key is missing");
});
}


builder.Services.AddServicesAndRepositories();

var services = builder.FindAndInjectClientEventHandlers(Assembly.GetExecutingAssembly());
Expand Down
8 changes: 0 additions & 8 deletions api/appsettings.Testing.json

This file was deleted.

0 comments on commit 9b01ace

Please sign in to comment.