diff --git a/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs b/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs index d5b1a47..29e629b 100644 --- a/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs +++ b/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs @@ -2,9 +2,8 @@ using Microsoft.EntityFrameworkCore; using Moq; using System; +using System.Linq; using Wilcommerce.Auth.Models; -using Wilcommerce.Auth.Services; -using Wilcommerce.Core.Common.Domain.Models; namespace Wilcommerce.Auth.Data.EFCore.Test.Fixtures { @@ -12,12 +11,12 @@ public class AuthContextFixture : IDisposable { public AuthContext Context { get; protected set; } - public User TestAdmin { get; protected set; } - - public string Token { get; protected set; } + public IPasswordHasher PasswordHasher { get; protected set; } public AuthContextFixture() { + PasswordHasher = new Mock>().Object; + BuildContext(); PrepareData(); } @@ -35,20 +34,20 @@ public void Dispose() protected virtual void CleanData() { - Context.UserTokens.RemoveRange(Context.UserTokens); + if (Context.Users.Any()) + { + Context.RemoveRange(Context.Users); + } + + Context.SaveChanges(); } protected virtual void PrepareData() { - var passwordHasher = new Mock>().Object; - - TestAdmin = User.CreateAsAdministrator("Admin", "admin@admin.com", "password", passwordHasher); - var tokenGenerator = new TokenGenerator(); - Token = tokenGenerator.GenerateForUser(TestAdmin); - - var userToken = UserToken.PasswordRecovery(TestAdmin, Token, DateTime.Now.AddDays(10)); + var user = User.CreateAsAdministrator("Administrator", "admin@wilcommerce.com", true); + user.PasswordHash = PasswordHasher.HashPassword(user, "password"); - Context.UserTokens.Add(userToken); + Context.Add(user); Context.SaveChanges(); } diff --git a/Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs b/Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs index c5ae99b..bcca913 100644 --- a/Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs +++ b/Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs @@ -2,6 +2,7 @@ using Wilcommerce.Auth.Data.EFCore.ReadModels; using Wilcommerce.Auth.Data.EFCore.Test.Fixtures; using Wilcommerce.Auth.Models; +using Wilcommerce.Auth.ReadModels; using Xunit; namespace Wilcommerce.Auth.Data.EFCore.Test.ReadModels @@ -19,24 +20,32 @@ public AuthDatabaseTest(AuthContextFixture fixture) } [Fact] - public void AuthDatabase_Should_Contain_A_PasswordRecovery_Token() + public void Users_Should_Contains_An_Administrator_User() { - bool existsToken = _database.Tokens.Any(t => t.TokenType == TokenTypes.PasswordRecovery); - Assert.True(existsToken); + bool existAdministrator = _database.Users + .Any(u => u.UserName == "admin@wilcommerce.com" && u.Email == "admin@wilcommerce.com" && u.Name == "Administrator"); + + Assert.True(existAdministrator); } [Fact] - public void AuthDatabase_Should_Contain_A_Token_With_Fixture_Value() + public void Users_WithUsername_Should_Match_Given_Username() { - bool existsToken = _database.Tokens.Any(t => t.Token == _fixture.Token); - Assert.True(existsToken); + bool exists = _database.Users + .WithUsername("admin@wilcommerce.com") + .Any(); + + Assert.True(exists); } [Fact] - public void AuthDatabase_Should_Contain_A_Token_With_TestAdmin_Id() + public void Users_Actives_Should_Return_Only_Users_With_IsActive_Equal_To_True() { - bool existsToken = _database.Tokens.Any(t => t.UserId == _fixture.TestAdmin.Id); - Assert.True(existsToken); + bool exists = _database.Users + .Actives() + .Any(u => !u.IsActive); + + Assert.False(exists); } } } diff --git a/Wilcommerce.Auth.Data.EFCore.Test/Repository/RepositoryTest.cs b/Wilcommerce.Auth.Data.EFCore.Test/Repository/RepositoryTest.cs deleted file mode 100644 index ce3f239..0000000 --- a/Wilcommerce.Auth.Data.EFCore.Test/Repository/RepositoryTest.cs +++ /dev/null @@ -1,46 +0,0 @@ -using Microsoft.AspNetCore.Identity; -using Moq; -using System; -using System.Linq; -using Wilcommerce.Auth.Data.EFCore.ReadModels; -using Wilcommerce.Auth.Data.EFCore.Test.Fixtures; -using Wilcommerce.Auth.Models; -using Wilcommerce.Auth.Services; -using Wilcommerce.Core.Common.Domain.Models; -using Xunit; - -namespace Wilcommerce.Auth.Data.EFCore.Test.Repository -{ - public class RepositoryTest : IClassFixture - { - private AuthContextFixture _fixture; - - private AuthDatabase _database; - - private EFCore.Repository.Repository _repository; - - public RepositoryTest(AuthContextFixture fixture) - { - _fixture = fixture; - _database = new AuthDatabase(fixture.Context); - _repository = new EFCore.Repository.Repository(fixture.Context); - } - - [Fact] - public void AddToken_Should_Increment_Tokens_Number() - { - var tokensCount = _database.Tokens.Count(); - - var passwordHasher = new Mock>().Object; - var admin = User.CreateAsAdministrator("Admin2", "admin2@admin.com", "password", passwordHasher); - var tokenGenerator = new TokenGenerator(); - string token = tokenGenerator.GenerateForUser(admin); - - var userToken = UserToken.Registration(admin, token, DateTime.Now.AddDays(1)); - _repository.Add(userToken); - _repository.SaveChanges(); - - Assert.Equal(tokensCount + 1, _database.Tokens.Count()); - } - } -} diff --git a/Wilcommerce.Auth.Data.EFCore.Test/Wilcommerce.Auth.Data.EFCore.Test.csproj b/Wilcommerce.Auth.Data.EFCore.Test/Wilcommerce.Auth.Data.EFCore.Test.csproj index 24d8f5b..3b0caa0 100644 --- a/Wilcommerce.Auth.Data.EFCore.Test/Wilcommerce.Auth.Data.EFCore.Test.csproj +++ b/Wilcommerce.Auth.Data.EFCore.Test/Wilcommerce.Auth.Data.EFCore.Test.csproj @@ -7,11 +7,14 @@ - - - - - + + + + + + all + runtime; build; native; contentfiles; analyzers + diff --git a/src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs b/src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs index dadf324..e2ecb4f 100644 --- a/src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs +++ b/src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Wilcommerce.Auth.Data.EFCore.Mapping; using Wilcommerce.Auth.Models; @@ -7,13 +8,8 @@ namespace Wilcommerce.Auth.Data.EFCore /// /// Defines the Entity Framework context for the auth package /// - public class AuthContext : DbContext + public class AuthContext : IdentityDbContext { - /// - /// Get or set the list of user tokens - /// - public virtual DbSet UserTokens { get; set; } - /// /// Construct the auth context /// @@ -21,19 +17,17 @@ public class AuthContext : DbContext public AuthContext(DbContextOptions options) : base(options) { - + } /// - /// Override the + /// Override the OnModelCreating method /// - /// The model builder instance - protected override void OnModelCreating(ModelBuilder modelBuilder) + /// The model builder instance + protected override void OnModelCreating(ModelBuilder builder) { - modelBuilder - .MapUserToken(); - - base.OnModelCreating(modelBuilder); + base.OnModelCreating(builder); + builder.MapIdentity(); } } } diff --git a/src/Wilcommerce.Auth.Data.EFCore/Mapping/IdentityMapping.cs b/src/Wilcommerce.Auth.Data.EFCore/Mapping/IdentityMapping.cs new file mode 100644 index 0000000..0b00156 --- /dev/null +++ b/src/Wilcommerce.Auth.Data.EFCore/Mapping/IdentityMapping.cs @@ -0,0 +1,31 @@ +using Microsoft.EntityFrameworkCore; +using Wilcommerce.Auth.Models; + +namespace Wilcommerce.Auth.Data.EFCore.Mapping +{ + /// + /// Defines all the extension methods which maps the identity classes + /// + public static class IdentityMapping + { + /// + /// Maps all the Identity classes + /// + /// The modelBuilder instance + /// The modelBuilder instance + public static ModelBuilder MapIdentity(this ModelBuilder modelBuilder) + { + foreach (var entity in modelBuilder.Model.GetEntityTypes()) + { + entity + .Relational() + .TableName = $"Wilcommerce_{entity.ClrType.Name}"; + } + + modelBuilder.Entity() + .OwnsOne(u => u.ProfileImage); + + return modelBuilder; + } + } +} diff --git a/src/Wilcommerce.Auth.Data.EFCore/Mapping/UserTokenMapping.cs b/src/Wilcommerce.Auth.Data.EFCore/Mapping/UserTokenMapping.cs deleted file mode 100644 index 52b948c..0000000 --- a/src/Wilcommerce.Auth.Data.EFCore/Mapping/UserTokenMapping.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Wilcommerce.Auth.Models; - -namespace Wilcommerce.Auth.Data.EFCore.Mapping -{ - /// - /// Defines the modelBuilder's extension methods to map the class - /// - public static class UserTokenMapping - { - /// - /// Extension method. Maps the user token class - /// - /// The modelBuilder instance - /// The modelBuilder instance - public static ModelBuilder MapUserToken(this ModelBuilder modelBuilder) - { - modelBuilder.Entity() - .ToTable("Wilcommerce_UserTokens"); - - return modelBuilder; - } - } -} diff --git a/src/Wilcommerce.Auth.Data.EFCore/ReadModels/AuthDatabase.cs b/src/Wilcommerce.Auth.Data.EFCore/ReadModels/AuthDatabase.cs index b38b7ee..1c18346 100644 --- a/src/Wilcommerce.Auth.Data.EFCore/ReadModels/AuthDatabase.cs +++ b/src/Wilcommerce.Auth.Data.EFCore/ReadModels/AuthDatabase.cs @@ -25,8 +25,8 @@ public AuthDatabase(AuthContext context) } /// - /// Get the tokens created by the platform + /// Get the list of users /// - public IQueryable Tokens => _context.UserTokens; + public IQueryable Users => _context.Users; } } diff --git a/src/Wilcommerce.Auth.Data.EFCore/Repository/Repository.cs b/src/Wilcommerce.Auth.Data.EFCore/Repository/Repository.cs deleted file mode 100644 index 8e02058..0000000 --- a/src/Wilcommerce.Auth.Data.EFCore/Repository/Repository.cs +++ /dev/null @@ -1,124 +0,0 @@ -using System; -using System.Threading.Tasks; -using Wilcommerce.Core.Infrastructure; - -namespace Wilcommerce.Auth.Data.EFCore.Repository -{ - /// - /// Implementation of - /// - public class Repository : Auth.Repository.IRepository - { - /// - /// The DbContext instance - /// - protected AuthContext _context; - - /// - /// Construct the repository - /// - /// The auth context instance - public Repository(AuthContext context) - { - _context = context ?? throw new ArgumentNullException(nameof(context)); - } - - /// - /// Dispose all the resource used - /// - public void Dispose() - { - if (_context != null) - { - _context.Dispose(); - } - - GC.SuppressFinalize(this); - } - - /// - /// Saves all the changes made on the aggregate - /// - public void SaveChanges() - { - try - { - _context.SaveChanges(); - } - catch - { - throw; - } - } - - /// - /// Async method. Saves all the changes made on the aggregate - /// - /// - public async Task SaveChangesAsync() - { - try - { - await _context.SaveChangesAsync(); - } - catch - { - throw; - } - } - - /// - /// Add an aggregate to the repository - /// - /// The aggregate's type - /// The aggregate to add - public void Add(TModel model) where TModel : class, IAggregateRoot - { - try - { - _context.Set().Add(model); - } - catch - { - throw; - } - } - - /// - /// Gets the aggregate based on the specified key - /// - /// The aggregate's type - /// The key of the aggregate to search - /// The aggregate found - public TModel GetByKey(Guid key) where TModel : class, IAggregateRoot - { - try - { - return _context.Find(key); - } - catch - { - throw; - } - } - - /// - /// Async method. Gets the aggregate based on the specified key - /// - /// The aggregate's type - /// The key of the aggregate to search - /// The aggregate found - public async Task GetByKeyAsync(Guid key) where TModel : class, IAggregateRoot - { - try - { - var model = await _context.FindAsync(key); - return model; - } - catch - { - throw; - } - } - } -} diff --git a/src/Wilcommerce.Auth.Data.EFCore/Wilcommerce.Auth.Data.EFCore.csproj b/src/Wilcommerce.Auth.Data.EFCore/Wilcommerce.Auth.Data.EFCore.csproj index cf39162..a194eaa 100644 --- a/src/Wilcommerce.Auth.Data.EFCore/Wilcommerce.Auth.Data.EFCore.csproj +++ b/src/Wilcommerce.Auth.Data.EFCore/Wilcommerce.Auth.Data.EFCore.csproj @@ -5,7 +5,7 @@ netstandard2.0 Wilcommerce.Auth.Data.EFCore Wilcommerce.Auth.Data.EFCore - 1.0.0-rc3 + 1.0.0-rc4 Alberto Mori Implementation of Authentication package for Entity Framework Core https://github.com/wilcommerce/Wilcommerce.Auth.Data.EFCore/blob/master/LICENSE @@ -25,12 +25,16 @@ - - - + + + + - - + + all + runtime; build; native; contentfiles; analyzers + +