Skip to content

Commit

Permalink
GitHub Secrets setup test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikfri committed Nov 5, 2024
1 parent 8548dbd commit 2a0411a
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 212 deletions.
76 changes: 76 additions & 0 deletions .github/workflows/DB-Angora.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Build and deploy .NET Core application to Web App DB-Angora with API Management Service DB-AngoraManagerAPI-DB-AngoraAPI
on:
push:
branches:
- master
env:
AZURE_WEBAPP_NAME: DB-Angora
AZURE_WEBAPP_PACKAGE_PATH: DB-AngoraREST\publish
AZURE_APIM_RESOURCE_PATH: /
AZURE_APIM_RESOURCEGROUP: MikksRG
AZURE_APIM_SERVICENAME: DB-AngoraManagerAPI
AZURE_APIM_API_ID: DB-AngoraAPI
AZURE_APIM_APPSERVICEURL: https://db-angora.azurewebsites.net
SWASHBUCLE_ASPNET_CORE_CLI_PACKAGE_VERSION: 5.6.3
SWASHBUCKLE_DOTNET_CORE_VERSION: 3.1.x
API_IMPORT_SPECIFICATION_PATH: DB-AngoraREST\publish\swagger.json
API_IMPORT_DLL: DB-AngoraREST\bin\Release\net8.0\DB-AngoraREST.dll
API_IMPORT_VERSION: v1
CONFIGURATION: Release
DOTNET_CORE_VERSION: 8.0.x
WORKING_DIRECTORY: DB-AngoraREST
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Setup .NET SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_CORE_VERSION }}
- name: Setup SwashBuckle .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.SWASHBUCKLE_DOTNET_CORE_VERSION }}
- name: Restore
run: dotnet restore ${{ env.WORKING_DIRECTORY }}
- name: Build
run: dotnet build ${{ env.WORKING_DIRECTORY }} --configuration ${{ env.CONFIGURATION }} --no-restore
- name: Test
run: dotnet test ${{ env.WORKING_DIRECTORY }} --no-build
- name: Publish
run: dotnet publish ${{ env.WORKING_DIRECTORY }} --configuration ${{ env.CONFIGURATION }} --no-build --output ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
- name: Install Swashbuckle CLI .NET Global Tool
run: dotnet tool install --global Swashbuckle.AspNetCore.Cli --version ${{ env.SWASHBUCLE_ASPNET_CORE_CLI_PACKAGE_VERSION }}
working-directory: ${{ env.WORKING_DIRECTORY }}
- name: Generate Open API Specification Document
run: swagger tofile --output "${{ env.API_IMPORT_SPECIFICATION_PATH }}" "${{ env.API_IMPORT_DLL }}" "${{ env.API_IMPORT_VERSION }}"
- name: Publish Artifacts
uses: actions/upload-artifact@v3
with:
name: webapp
path: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
deploy:
runs-on: windows-latest
needs: build
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v3
with:
name: webapp
path: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
- name: Deploy to Azure WebApp
uses: azure/webapps-deploy@v2
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
publish-profile: ${{ secrets.DB_Angora_2FD5 }}
- name: Azure Login
uses: azure/login@v1
with:
creds: ${{ secrets.DB_AngoraManagerAPI_SPN }}
- name: Import API into Azure API Management
run: az apim api import --path "${{ env.AZURE_APIM_RESOURCE_PATH }}" --resource-group "${{ env.AZURE_APIM_RESOURCEGROUP }}" --service-name "${{ env.AZURE_APIM_SERVICENAME }}" --api-id "${{ env.AZURE_APIM_API_ID }}" --service-url "${{ env.AZURE_APIM_APPSERVICEURL }}" --specification-path "${{ env.API_IMPORT_SPECIFICATION_PATH }}" --specification-format OpenApi --subscription-required false
- name: logout
run: >
az logout
79 changes: 23 additions & 56 deletions DB-AngoraMST/Services_InMemTest/RabbitService_MST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DB_AngoraLib.MockData;
using DB_AngoraLib.Models;
using DB_AngoraLib.Repository;
using DB_AngoraLib.SeededData;
using DB_AngoraLib.Services.AccountService;
using DB_AngoraLib.Services.BreederService;
using DB_AngoraLib.Services.EmailService;
Expand All @@ -24,69 +25,32 @@ namespace DB_AngoraMST.Services_InMemTest
[TestClass]
public class RabbitServices_MST
{

private IRabbitService _rabbitService;
private IBreederService _breederService;
private Mock<IBreederService> _breederServiceMock;
private Mock<Rabbit_Validator> _validatorServiceMock;
private DB_AngoraContext _context;

public RabbitServices_MST()
[TestInitialize]
public void Setup()
{
// Setup in-memory database
// Configure in-memory database
var options = new DbContextOptionsBuilder<DB_AngoraContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.UseInMemoryDatabase(databaseName: "TestDatabase")
.Options;

_context = new DB_AngoraContext(options);
_context.Database.EnsureCreated();

// Create repositories
var rabbitRepository = new GRepository<Rabbit>(_context);
var breederRepository = new GRepository<Breeder>(_context);

// Initialize services with all required parameters
_breederService = new BreederServices(breederRepository);
var validatorService = new Rabbit_Validator();
_rabbitService = new RabbitServices(rabbitRepository, _breederService, validatorService);
}
// Seed the database with initial data
SeedDatabase(_context);

[TestInitialize]
public void Setup()
{
// Add mock data to in-memory database
var mockUsersWithRoles = MockUsers.GetMockUsersWithRoles();
foreach (var mockUserWithRole in mockUsersWithRoles)
{
_context.Users.Add(mockUserWithRole.User);
_context.SaveChanges();

foreach (var role in mockUserWithRole.Roles)
{
var roleId = MockRoles.GetMockRoles().First(r => r.Name == role).Id;

// Assign role to user
var userRole = new IdentityUserRole<string>
{
UserId = mockUserWithRole.User.Id,
RoleId = roleId
};
_context.UserRoles.Add(userRole);

// Assign claims to user
var roleClaims = RoleClaims.Get_AspNetRoleClaims();
var claimsForRole = roleClaims.Where(rc => rc.RoleId == roleId).ToList();
foreach (var claim in claimsForRole)
{
var userClaim = new IdentityUserClaim<string>
{
UserId = mockUserWithRole.User.Id,
ClaimType = claim.ClaimType,
ClaimValue = claim.ClaimValue
};
_context.UserClaims.Add(userClaim);
}
}
}
// Mock dependencies
_breederServiceMock = new Mock<IBreederService>();
_validatorServiceMock = new Mock<Rabbit_Validator>();

_context.SaveChanges();
// Initialize RabbitServices with mocked dependencies
var repository = new GRepository<Rabbit>(_context);
_rabbitService = new RabbitServices(repository, _breederServiceMock.Object, _validatorServiceMock.Object);
}

[TestCleanup]
Expand All @@ -96,6 +60,12 @@ public void Cleanup()
_context.Dispose();
}

private void SeedDatabase(DB_AngoraContext context)
{
var modelBuilder = new ModelBuilder(new Microsoft.EntityFrameworkCore.Metadata.Conventions.ConventionSet());
SeedData.Seed(modelBuilder);
context.Database.EnsureCreated();
}

//-------------------------: ADD TESTS
[TestMethod]
Expand Down Expand Up @@ -469,9 +439,6 @@ await Assert.ThrowsExceptionAsync<InvalidOperationException>(
var deletedRabbitOwned = await _context.Rabbits
.FirstOrDefaultAsync(r => r.RightEarId == mockRabbitOwned.RightEarId && r.LeftEarId == mockRabbitOwned.LeftEarId);
Assert.IsNull(deletedRabbitOwned);
}



}
}
}
154 changes: 0 additions & 154 deletions DB-AngoraREST/DB_DataStarter/DbInitializer.cs

This file was deleted.

11 changes: 9 additions & 2 deletions DB-AngoraREST/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using DB_AngoraLib.Services.TokenService;
using DB_AngoraLib.Services.TransferService;
using DB_AngoraLib.Services.ValidationService;
using DB_AngoraREST.DB_DataStarter;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
Expand All @@ -20,9 +19,16 @@
using System.Text.Json.Serialization;
using DB_AngoraLib.Services.BreederBrandService;
using DB_AngoraLib.Services.BreederService;
using DB_AngoraREST.Settings;

var builder = WebApplication.CreateBuilder(args);

// Load secrets from environment variables
builder.Configuration.AddEnvironmentVariables();
builder.Services.Configure<ConnectionStrings>(builder.Configuration.GetSection("ConnectionStrings"));
builder.Services.Configure<JwtSettings>(builder.Configuration.GetSection("Jwt"));
builder.Services.Configure<EmailSettings>(builder.Configuration.GetSection("EmailSettings"));


// Add services to the container.
//-----------------: DB-AngoraLib Services
Expand Down Expand Up @@ -68,7 +74,8 @@

// -----------------: DB CONNECTION-STRING & MIGRATION SETUP
builder.Services.AddDbContext<DB_AngoraContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
//options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
options.UseSqlServer(builder.Configuration.GetConnectionString("SecretConnection"),
b => b.MigrationsAssembly("DB-AngoraREST")));


Expand Down
Loading

0 comments on commit 2a0411a

Please sign in to comment.