Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AuthService.Test/AuthService.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.6" />
</ItemGroup>

<ItemGroup>
Expand Down
29 changes: 24 additions & 5 deletions AuthService.Test/AuthServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using AuthService.DataAccess;
using System.Collections.Generic;
using AuthService.DataAccess.EF;
using AuthService.Domain;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
Expand All @@ -11,21 +14,37 @@ namespace AuthService.Test;
public class AuthServiceTests
{
private readonly Domain.AuthService authService;
private readonly InsuranceAgentsInMemoryDb agentsDb;
private readonly IInsuranceAgents agentsRepository;
private readonly AppSettings appSettings;
private readonly ITestOutputHelper output;

public AuthServiceTests(ITestOutputHelper output)
{
this.output = output;
agentsDb = new InsuranceAgentsInMemoryDb();

// Setup EF Core in-memory database
var options = new DbContextOptionsBuilder<AuthDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;

var dbContext = new AuthDbContext(options);
agentsRepository = new InsuranceAgentRepository(dbContext);

// Seed test data
agentsRepository.Add(new InsuranceAgent("jimmy.solid", "secret", "static/avatars/jimmy_solid.png",
new List<string> { "TRI", "HSI", "FAI", "CAR" }));
agentsRepository.Add(new InsuranceAgent("danny.solid", "secret", "static/avatars/danny.solid.png",
new List<string> { "TRI", "HSI", "FAI", "CAR" }));
agentsRepository.Add(new InsuranceAgent("admin", "admin", "static/avatars/admin.png",
new List<string> { "TRI", "HSI", "FAI", "CAR" }));

appSettings = new AppSettings
{
Secret = "ThisIsASecretKeyForJWTTokenGeneration123456789"
};
var options = Options.Create(appSettings);
var appSettingsOptions = Options.Create(appSettings);

authService = new Domain.AuthService(agentsDb, options);
authService = new Domain.AuthService(agentsRepository, appSettingsOptions);
}

[Theory]
Expand Down
2 changes: 2 additions & 0 deletions AuthService/AuthService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.6" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
Expand Down
37 changes: 37 additions & 0 deletions AuthService/DataAccess/EF/AuthDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using AuthService.Domain;

namespace AuthService.DataAccess.EF;

public class AuthDbContext : DbContext
{
public AuthDbContext(DbContextOptions<AuthDbContext> options) : base(options)
{
}
Comment on lines +10 to +12
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

используй primary конструктор


public DbSet<InsuranceAgent> InsuranceAgents { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.EnableSensitiveDataLogging();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<InsuranceAgent>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasIndex(e => e.Login).IsUnique();
entity.Property(e => e.Login).IsRequired();
entity.Property(e => e.Password).IsRequired();
entity.Property(e => e.Avatar).IsRequired();
entity.Property(e => e.AvailableProducts)
.HasConversion(
v => string.Join(',', v),
v => v.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList()
);
});
}
}
20 changes: 20 additions & 0 deletions AuthService/DataAccess/EF/EFInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using AuthService.Domain;

namespace AuthService.DataAccess.EF;

public static class EFInstaller
{
public static IServiceCollection AddEFConfiguration(this IServiceCollection services, IConfiguration configuration)
{
Comment on lines +8 to +11
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

предпочтительно EfInstaller и AddEfConfiguration

services.AddDbContext<AuthDbContext>(options =>
{
options.UseInMemoryDatabase("InsuranceAgents");
});

services.AddScoped<IInsuranceAgents, InsuranceAgentRepository>();
return services;
}
}
27 changes: 27 additions & 0 deletions AuthService/DataAccess/EF/InsuranceAgentRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using AuthService.Domain;

namespace AuthService.DataAccess.EF;

public class InsuranceAgentRepository : IInsuranceAgents
{
private readonly AuthDbContext dbContext;

public InsuranceAgentRepository(AuthDbContext dbContext)
{
this.dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}

public void Add(InsuranceAgent agent)
{
dbContext.InsuranceAgents.Add(agent);
}
Comment on lines +17 to +20
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не хватает сохранения контекста


public InsuranceAgent FindByLogin(string login)
{
return dbContext.InsuranceAgents
.FirstOrDefault(a => a.Login == login);
}
}
30 changes: 0 additions & 30 deletions AuthService/DataAccess/InsuranceAgentsInMemoryDb.cs

This file was deleted.

16 changes: 11 additions & 5 deletions AuthService/Domain/InsuranceAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ namespace AuthService.Domain;

public class InsuranceAgent
{
// EF Core requires a parameterless constructor
private InsuranceAgent()
{
}

public InsuranceAgent(string login, string password, string avatar, List<string> availableProducts)
{
Login = login;
Expand All @@ -12,13 +17,14 @@ public InsuranceAgent(string login, string password, string avatar, List<string>
AvailableProducts = availableProducts;
}

public string Login { get; }
public string Password { get; }
public string Avatar { get; }
public List<string> AvailableProducts { get; }
public int Id { get; private set; }
public string Login { get; private set; }
public string Password { get; private set; }
public string Avatar { get; private set; }
public List<string> AvailableProducts { get; private set; }

public bool PasswordMatches(string passwordToTest)
{
return Password == passwordToTest;
}
}
}
16 changes: 16 additions & 0 deletions AuthService/Init/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace AuthService.Init;

public static class ApplicationBuilderExtensions
{
public static void UseInitializer(this IApplicationBuilder app)
{
using (var scope = app.ApplicationServices.CreateScope())
{
var initializer = scope.ServiceProvider.GetService<DataLoader>();
initializer.Seed();
}
}
}
31 changes: 31 additions & 0 deletions AuthService/Init/DataLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using AuthService.DataAccess.EF;
using AuthService.Domain;

namespace AuthService.Init;

public class DataLoader
{
private readonly AuthDbContext dbContext;

public DataLoader(AuthDbContext context)
{
dbContext = context;
}

public void Seed()
{
dbContext.Database.EnsureCreated();
if (dbContext.InsuranceAgents.Any()) return;

dbContext.InsuranceAgents.Add(new InsuranceAgent("jimmy.solid", "secret", "static/avatars/jimmy_solid.png",
new List<string> { "TRI", "HSI", "FAI", "CAR" }));
dbContext.InsuranceAgents.Add(new InsuranceAgent("danny.solid", "secret", "static/avatars/danny.solid.png",
new List<string> { "TRI", "HSI", "FAI", "CAR" }));
dbContext.InsuranceAgents.Add(new InsuranceAgent("admin", "admin", "static/avatars/admin.png",
new List<string> { "TRI", "HSI", "FAI", "CAR" }));

dbContext.SaveChanges();
}
}
12 changes: 12 additions & 0 deletions AuthService/Init/DataLoaderInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.DependencyInjection;

namespace AuthService.Init;

public static class DataLoaderInstaller
{
public static IServiceCollection AddAuthDemoInitializer(this IServiceCollection services)
{
services.AddScoped<DataLoader>();
return services;
}
}
9 changes: 7 additions & 2 deletions AuthService/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;
using AuthService.DataAccess;
using AuthService.DataAccess.EF;
using AuthService.Domain;
using AuthService.Init;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -59,7 +60,8 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddSingleton<Domain.AuthService>();
services.AddSingleton<IInsuranceAgents, InsuranceAgentsInMemoryDb>();
services.AddEFConfiguration(Configuration);
services.AddAuthDemoInitializer();
services.AddSwaggerGen();
}

Expand All @@ -84,6 +86,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseAuthorization();

app.UseHttpsRedirection();

// Ensure initializer is awaited so seeding completes before the app starts handling requests
app.UseInitializer();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
}