Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Db DI. #26

Merged
merged 17 commits into from
Aug 19, 2024
Merged
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
6 changes: 3 additions & 3 deletions RelationAnalysis.Migrations/InitialRecordsCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ public async Task AddInitialRecords()
{
Name = "Admin",
Permissions =
"'[\"/api/Access/GetPermissions\",\"/api/Admin/GetUser/{id}\",\"/api/Admin/GetAllUser\",\"/api/Admin/GetAllRoles\",\"/api/Admin/UpdateUser/{id}\",\"/api/Admin/UpdatePassword/{id}\",\"/api/Admin/DeleteUser/{id}\",\"/api/Admin/CreateUser\",\"/api/Admin/UpdateRoles/{id}\",\"/api/Auth/Login\", \"/api/User/GetUser\",\"/api/User/UpdateUser\",\"/api/User/UpdatePassword\",\"/api/User/Logout\"]'",
"[\"/api/Access/GetPermissions\",\"/api/Admin/GetUser/{id}\",\"/api/Admin/GetAllUser\",\"/api/Admin/GetAllRoles\",\"/api/Admin/UpdateUser/{id}\",\"/api/Admin/UpdatePassword/{id}\",\"/api/Admin/DeleteUser/{id}\",\"/api/Admin/CreateUser\",\"/api/Admin/UpdateRoles/{id}\",\"/api/Auth/Login\", \"/api/User/GetUser\",\"/api/User/UpdateUser\",\"/api/User/UpdatePassword\",\"/api/User/Logout\"]",
Id = 1
},
new Role()
{
Name = "DataAdmin",
Permissions =
"'[\"/api/Access/GetPermissions\",\"/api/Auth/Login\",\"/api/User/GetUser\",\"/api/User/UpdateUser\",\"/api/User/UpdatePassword\",\"/api/User/Logout\"]'",
"[\"/api/Access/GetPermissions\",\"/api/Auth/Login\",\"/api/User/GetUser\",\"/api/User/UpdateUser\",\"/api/User/UpdatePassword\",\"/api/User/Logout\"]",
Id = 2
},
new Role()
{
Name = "DataAnalyst",
Permissions =
"'[\"/api/Access/GetPermissions\",\"/api/Auth/Login\",\"/api/User/GetUser\",\"/api/User/UpdateUser\",\"/api/User/UpdatePassword\",\"/api/User/Logout\"]'",
"[\"/api/Access/GetPermissions\",\"/api/Auth/Login\",\"/api/User/GetUser\",\"/api/User/UpdateUser\",\"/api/User/UpdatePassword\",\"/api/User/Logout\"]",
Id = 3
}
};
Expand Down
10 changes: 8 additions & 2 deletions RelationshipAnalysis.Test/Services/LoginServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Dto;
Expand All @@ -20,7 +21,7 @@ public class LoginServiceTests
private readonly Mock<IJwtTokenGenerator> _mockJwtTokenGenerator;
private readonly Mock<IPasswordVerifier> _mockPasswordVerifier;
private readonly Mock<HttpResponse> _mockHttpResponse;

private readonly IServiceProvider _serviceProvider;
public LoginServiceTests()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
Expand All @@ -38,8 +39,13 @@ public LoginServiceTests()
_mockHttpResponse = new Mock<HttpResponse>();
_mockHttpResponse.SetupGet(r => r.Cookies).Returns(mockResponseCookies.Object);

var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped(_ => _context);

_serviceProvider = serviceCollection.BuildServiceProvider();

_sut = new LoginService(
_context,
_serviceProvider,
_mockCookieSetter.Object,
_mockJwtTokenGenerator.Object,
_mockPasswordVerifier.Object
Expand Down
47 changes: 29 additions & 18 deletions RelationshipAnalysis.Test/Services/PermissionServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Security.Claims;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Newtonsoft.Json;
using RelationshipAnalysis.Context;
Expand All @@ -13,39 +14,46 @@ namespace RelationshipAnalysis.Test.Services;
public class PermissionServiceTests
{
private readonly PermissionService _sut;
private readonly ApplicationDbContext _context;
private readonly List<string> _userRoles = ["Read", "Write"];
private readonly List<string> _adminRoles = ["Delete", "Write"];
private readonly IServiceProvider _serviceProvider;
private readonly List<string> _userRoles = new List<string> { "Read", "Write" };
private readonly List<string> _adminRoles = new List<string> { "Delete", "Write" };

public PermissionServiceTests()
{
var serviceCollection = new ServiceCollection();

var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "TestDatabase")
.Options;
serviceCollection.AddScoped(_ => new ApplicationDbContext(options));

_context = new ApplicationDbContext(options);
_serviceProvider = serviceCollection.BuildServiceProvider();

SeedDatabase();

_sut = new PermissionService(_context);
_sut = new PermissionService(_serviceProvider);
}

private void SeedDatabase()
{
_context.Roles.AddRange(new List<Role>
using (var scope = _serviceProvider.CreateScope())
{
new Role
{
Name = "User",
Permissions = JsonConvert.SerializeObject(_userRoles)
},
new Role
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Roles.AddRange(new List<Role>
{
Name = "Admin",
Permissions = JsonConvert.SerializeObject(_adminRoles)
}
});
_context.SaveChanges();
new Role
{
Name = "User",
Permissions = JsonConvert.SerializeObject(_userRoles)
},
new Role
{
Name = "Admin",
Permissions = JsonConvert.SerializeObject(_adminRoles)
}
});
context.SaveChanges();
}
}

[Fact]
Expand All @@ -60,10 +68,13 @@ public async Task GetPermissionsAsync_ShouldReturnPermissions_WhenRolesExist()

// Act
var result = await _sut.GetPermissionsAsync(userClaims);

var expectedResult = _userRoles.Union(_adminRoles);
var expectedPermissions = JsonConvert.SerializeObject(expectedResult);

// Assert
var expectedPermissions = JsonConvert.SerializeObject(expectedResult);
Assert.NotNull(result);
Assert.NotNull(result.Data);
Assert.Equal(expectedPermissions, result.Data.Permissions);
}
}
11 changes: 9 additions & 2 deletions RelationshipAnalysis.Test/Services/UserPasswordServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Dto;
Expand All @@ -19,14 +20,20 @@ public class UserPasswordServiceTests
private readonly IPasswordVerifier _passwordVerifier;
private readonly IPasswordHasher _passwordHasher;
private readonly IUserPasswordService _sut;

private readonly IServiceProvider _serviceProvider;
public UserPasswordServiceTests()
{
_context = new ApplicationDbContext(new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options);
_passwordVerifier = Substitute.For<IPasswordVerifier>();
_passwordHasher = Substitute.For<IPasswordHasher>();
_sut = new UserPasswordService(_context, _passwordVerifier, _passwordHasher);

var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped(_ => _context);

_serviceProvider = serviceCollection.BuildServiceProvider();

_sut = new UserPasswordService(_serviceProvider, _passwordVerifier, _passwordHasher);
SeedDatabase();
}

Expand Down
13 changes: 10 additions & 3 deletions RelationshipAnalysis.Test/Services/UserReceiverTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Security.Claims;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Models;
using RelationshipAnalysis.Models.Auth;
using RelationshipAnalysis.Services;
using RelationshipAnalysis.Services.UserPanelServices;

namespace RelationshipAnalysis.Test.Services
Expand All @@ -12,12 +11,20 @@ public class UserReceiverTests
{
private readonly ApplicationDbContext _context;
private readonly UserReceiver _sut;
private readonly IServiceProvider _serviceProvider;

public UserReceiverTests()
{
_context = new ApplicationDbContext(new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options);
_sut = new UserReceiver(_context);

var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped(_ => _context);

_serviceProvider = serviceCollection.BuildServiceProvider();


_sut = new UserReceiver(_serviceProvider);
SeedDatabase();
}

Expand Down
10 changes: 8 additions & 2 deletions RelationshipAnalysis.Test/Services/UserRolesReceiverTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Models;
using RelationshipAnalysis.Models.Auth;
Expand All @@ -12,12 +13,17 @@ public class UserRolesReceiverTests
{
private readonly ApplicationDbContext _context;
private readonly RoleReceiver _sut;

private readonly IServiceProvider _serviceProvider;
public UserRolesReceiverTests()
{
_context = new ApplicationDbContext(new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options);
_sut = new RoleReceiver(_context);
var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped(_ => _context);

_serviceProvider = serviceCollection.BuildServiceProvider();

_sut = new RoleReceiver(_serviceProvider);
SeedDatabase();
}

Expand Down
Loading