From 00d9a68cfcc2dc899b9b0014da1d5d9287301e57 Mon Sep 17 00:00:00 2001 From: Alberto Mori Date: Sun, 30 Sep 2018 21:11:19 +0200 Subject: [PATCH 1/2] upgrade nuget packages, update package version, remove old classes --- .../Fixtures/AuthContextFixture.cs | 18 +-- .../ReadModels/AuthDatabaseTest.cs | 21 +-- .../Repository/RepositoryTest.cs | 46 ------- .../Wilcommerce.Auth.Data.EFCore.Test.csproj | 13 +- .../AuthContext.cs | 30 ++--- .../Mapping/UserTokenMapping.cs | 24 ---- .../ReadModels/AuthDatabase.cs | 4 +- .../Repository/Repository.cs | 124 ------------------ .../Wilcommerce.Auth.Data.EFCore.csproj | 18 ++- 9 files changed, 38 insertions(+), 260 deletions(-) delete mode 100644 Wilcommerce.Auth.Data.EFCore.Test/Repository/RepositoryTest.cs delete mode 100644 src/Wilcommerce.Auth.Data.EFCore/Mapping/UserTokenMapping.cs delete mode 100644 src/Wilcommerce.Auth.Data.EFCore/Repository/Repository.cs diff --git a/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs b/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs index d5b1a47..21e84cc 100644 --- a/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs +++ b/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs @@ -4,7 +4,6 @@ using System; using Wilcommerce.Auth.Models; using Wilcommerce.Auth.Services; -using Wilcommerce.Core.Common.Domain.Models; namespace Wilcommerce.Auth.Data.EFCore.Test.Fixtures { @@ -12,10 +11,6 @@ public class AuthContextFixture : IDisposable { public AuthContext Context { get; protected set; } - public User TestAdmin { get; protected set; } - - public string Token { get; protected set; } - public AuthContextFixture() { BuildContext(); @@ -35,21 +30,12 @@ public void Dispose() protected virtual void CleanData() { - Context.UserTokens.RemoveRange(Context.UserTokens); + } 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)); - - Context.UserTokens.Add(userToken); - Context.SaveChanges(); + } protected virtual void BuildContext() diff --git a/Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs b/Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs index c5ae99b..5d9127c 100644 --- a/Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs +++ b/Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs @@ -18,25 +18,6 @@ public AuthDatabaseTest(AuthContextFixture fixture) _database = new AuthDatabase(fixture.Context); } - [Fact] - public void AuthDatabase_Should_Contain_A_PasswordRecovery_Token() - { - bool existsToken = _database.Tokens.Any(t => t.TokenType == TokenTypes.PasswordRecovery); - Assert.True(existsToken); - } - - [Fact] - public void AuthDatabase_Should_Contain_A_Token_With_Fixture_Value() - { - bool existsToken = _database.Tokens.Any(t => t.Token == _fixture.Token); - Assert.True(existsToken); - } - - [Fact] - public void AuthDatabase_Should_Contain_A_Token_With_TestAdmin_Id() - { - bool existsToken = _database.Tokens.Any(t => t.UserId == _fixture.TestAdmin.Id); - Assert.True(existsToken); - } + } } 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 6456c6f..bdadb1c 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..39ef6dc 100644 --- a/src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs +++ b/src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs @@ -1,5 +1,5 @@ -using Microsoft.EntityFrameworkCore; -using Wilcommerce.Auth.Data.EFCore.Mapping; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Wilcommerce.Auth.Models; namespace Wilcommerce.Auth.Data.EFCore @@ -7,13 +7,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 +16,22 @@ 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); + foreach (var entity in builder.Model.GetEntityTypes()) + { + entity + .Relational() + .TableName = $"Wilcommerce_{entity.ClrType.Name}"; + } } } } 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 59e3cb4..858ee72 100644 --- a/src/Wilcommerce.Auth.Data.EFCore/ReadModels/AuthDatabase.cs +++ b/src/Wilcommerce.Auth.Data.EFCore/ReadModels/AuthDatabase.cs @@ -24,8 +24,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 f71e5e4..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; - } - - /// - /// 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 f1dfaff..dcf7b50 100644 --- a/src/Wilcommerce.Auth.Data.EFCore/Wilcommerce.Auth.Data.EFCore.csproj +++ b/src/Wilcommerce.Auth.Data.EFCore/Wilcommerce.Auth.Data.EFCore.csproj @@ -1,11 +1,11 @@ - + 1.0.0 netstandard2.0 Wilcommerce.Auth.Data.EFCore Wilcommerce.Auth.Data.EFCore - 1.0.0-rc2 + 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 + + From 23e4a5630ea908705d299448aafbe448d9935b42 Mon Sep 17 00:00:00 2001 From: Alberto Mori Date: Sat, 6 Oct 2018 16:10:37 +0200 Subject: [PATCH 2/2] upgrade nuget packages, add unit test to AuthDatabase --- .../Fixtures/AuthContextFixture.cs | 19 ++++++++++-- .../ReadModels/AuthDatabaseTest.cs | 30 +++++++++++++++++- .../Wilcommerce.Auth.Data.EFCore.Test.csproj | 2 +- .../AuthContext.cs | 8 ++--- .../Mapping/IdentityMapping.cs | 31 +++++++++++++++++++ .../Wilcommerce.Auth.Data.EFCore.csproj | 10 +++--- 6 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 src/Wilcommerce.Auth.Data.EFCore/Mapping/IdentityMapping.cs diff --git a/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs b/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs index 21e84cc..29e629b 100644 --- a/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs +++ b/Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs @@ -2,8 +2,8 @@ using Microsoft.EntityFrameworkCore; using Moq; using System; +using System.Linq; using Wilcommerce.Auth.Models; -using Wilcommerce.Auth.Services; namespace Wilcommerce.Auth.Data.EFCore.Test.Fixtures { @@ -11,8 +11,12 @@ public class AuthContextFixture : IDisposable { public AuthContext Context { get; protected set; } + public IPasswordHasher PasswordHasher { get; protected set; } + public AuthContextFixture() { + PasswordHasher = new Mock>().Object; + BuildContext(); PrepareData(); } @@ -30,12 +34,21 @@ public void Dispose() protected virtual void CleanData() { - + if (Context.Users.Any()) + { + Context.RemoveRange(Context.Users); + } + + Context.SaveChanges(); } protected virtual void PrepareData() { - + var user = User.CreateAsAdministrator("Administrator", "admin@wilcommerce.com", true); + user.PasswordHash = PasswordHasher.HashPassword(user, "password"); + + Context.Add(user); + Context.SaveChanges(); } protected virtual void BuildContext() diff --git a/Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs b/Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs index 5d9127c..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 @@ -18,6 +19,33 @@ public AuthDatabaseTest(AuthContextFixture fixture) _database = new AuthDatabase(fixture.Context); } - + [Fact] + public void Users_Should_Contains_An_Administrator_User() + { + 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 Users_WithUsername_Should_Match_Given_Username() + { + bool exists = _database.Users + .WithUsername("admin@wilcommerce.com") + .Any(); + + Assert.True(exists); + } + + [Fact] + public void Users_Actives_Should_Return_Only_Users_With_IsActive_Equal_To_True() + { + bool exists = _database.Users + .Actives() + .Any(u => !u.IsActive); + + Assert.False(exists); + } } } 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 81c8038..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,7 +7,7 @@ - + diff --git a/src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs b/src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs index 39ef6dc..e2ecb4f 100644 --- a/src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs +++ b/src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; +using Wilcommerce.Auth.Data.EFCore.Mapping; using Wilcommerce.Auth.Models; namespace Wilcommerce.Auth.Data.EFCore @@ -26,12 +27,7 @@ public AuthContext(DbContextOptions options) protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); - foreach (var entity in builder.Model.GetEntityTypes()) - { - entity - .Relational() - .TableName = $"Wilcommerce_{entity.ClrType.Name}"; - } + 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/Wilcommerce.Auth.Data.EFCore.csproj b/src/Wilcommerce.Auth.Data.EFCore/Wilcommerce.Auth.Data.EFCore.csproj index dcf7b50..a194eaa 100644 --- a/src/Wilcommerce.Auth.Data.EFCore/Wilcommerce.Auth.Data.EFCore.csproj +++ b/src/Wilcommerce.Auth.Data.EFCore/Wilcommerce.Auth.Data.EFCore.csproj @@ -26,15 +26,15 @@ - - - + + + - + all runtime; build; native; contentfiles; analyzers - +