Skip to content

Commit

Permalink
Merge tag '1.0.0-rc4' into develop
Browse files Browse the repository at this point in the history
Upgrade Wilcommerce.Auth package 1.0.0-rc4
  • Loading branch information
albx committed Oct 6, 2018
2 parents 298b361 + 4b3af79 commit bc1ba26
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 245 deletions.
27 changes: 13 additions & 14 deletions Wilcommerce.Auth.Data.EFCore.Test/Fixtures/AuthContextFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
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
{
public class AuthContextFixture : IDisposable
{
public AuthContext Context { get; protected set; }

public User TestAdmin { get; protected set; }

public string Token { get; protected set; }
public IPasswordHasher<User> PasswordHasher { get; protected set; }

public AuthContextFixture()
{
PasswordHasher = new Mock<IPasswordHasher<User>>().Object;

BuildContext();
PrepareData();
}
Expand All @@ -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<IPasswordHasher<User>>().Object;

TestAdmin = User.CreateAsAdministrator("Admin", "[email protected]", "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", "[email protected]", true);
user.PasswordHash = PasswordHasher.HashPassword(user, "password");

Context.UserTokens.Add(userToken);
Context.Add(user);
Context.SaveChanges();
}

Expand Down
27 changes: 18 additions & 9 deletions Wilcommerce.Auth.Data.EFCore.Test/ReadModels/AuthDatabaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 == "[email protected]" && u.Email == "[email protected]" && 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("[email protected]")
.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);
}
}
}
46 changes: 0 additions & 46 deletions Wilcommerce.Auth.Data.EFCore.Test/Repository/RepositoryTest.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="Moq" Version="4.8.3" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.1.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Moq" Version="4.10.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

Expand Down
24 changes: 9 additions & 15 deletions src/Wilcommerce.Auth.Data.EFCore/AuthContext.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -7,33 +8,26 @@ namespace Wilcommerce.Auth.Data.EFCore
/// <summary>
/// Defines the Entity Framework context for the auth package
/// </summary>
public class AuthContext : DbContext
public class AuthContext : IdentityDbContext<User>
{
/// <summary>
/// Get or set the list of user tokens
/// </summary>
public virtual DbSet<UserToken> UserTokens { get; set; }

/// <summary>
/// Construct the auth context
/// </summary>
/// <param name="options">The context options</param>
public AuthContext(DbContextOptions<AuthContext> options)
: base(options)
{

}

/// <summary>
/// Override the <see cref="DbContext.OnModelCreating(ModelBuilder)"/>
/// Override the OnModelCreating method
/// </summary>
/// <param name="modelBuilder">The model builder instance</param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
/// <param name="builder">The model builder instance</param>
protected override void OnModelCreating(ModelBuilder builder)
{
modelBuilder
.MapUserToken();

base.OnModelCreating(modelBuilder);
base.OnModelCreating(builder);
builder.MapIdentity();
}
}
}
31 changes: 31 additions & 0 deletions src/Wilcommerce.Auth.Data.EFCore/Mapping/IdentityMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Wilcommerce.Auth.Models;

namespace Wilcommerce.Auth.Data.EFCore.Mapping
{
/// <summary>
/// Defines all the extension methods which maps the identity classes
/// </summary>
public static class IdentityMapping
{
/// <summary>
/// Maps all the Identity classes
/// </summary>
/// <param name="modelBuilder">The modelBuilder instance</param>
/// <returns>The modelBuilder instance</returns>
public static ModelBuilder MapIdentity(this ModelBuilder modelBuilder)
{
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
entity
.Relational()
.TableName = $"Wilcommerce_{entity.ClrType.Name}";
}

modelBuilder.Entity<User>()
.OwnsOne(u => u.ProfileImage);

return modelBuilder;
}
}
}
24 changes: 0 additions & 24 deletions src/Wilcommerce.Auth.Data.EFCore/Mapping/UserTokenMapping.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Wilcommerce.Auth.Data.EFCore/ReadModels/AuthDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public AuthDatabase(AuthContext context)
}

/// <summary>
/// Get the tokens created by the platform
/// Get the list of users
/// </summary>
public IQueryable<UserToken> Tokens => _context.UserTokens;
public IQueryable<User> Users => _context.Users;
}
}
Loading

0 comments on commit bc1ba26

Please sign in to comment.