Browse Source

Merge pull request #989 from abpframework/maliming/identity

Add Volo.Abp.Identity unit tests.
pull/997/head
Halil İbrahim Kalkan 7 years ago
committed by GitHub
parent
commit
a810cd24f1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserStore.cs
  2. 56
      modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserLookupAppService_Tests.cs
  3. 73
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdenityClaimTypeManager_Tests.cs
  4. 60
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityRoleManager_Tests.cs
  5. 186
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityRoleStore_Tests.cs
  6. 87
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs
  7. 777
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserStore_Tests.cs
  8. 30
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/UserRoleFinder_Tests.cs

16
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserStore.cs

@ -981,7 +981,7 @@ namespace Volo.Abp.Identity
/// <param name="value">The value of the token.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
public virtual async Task SetTokenAsync([NotNull] IdentityUser user, string loginProvider, string name, string value, CancellationToken cancellationToken)
public virtual async Task SetTokenAsync([NotNull] IdentityUser user, string loginProvider, string name, string value, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
@ -1000,7 +1000,7 @@ namespace Volo.Abp.Identity
/// <param name="name">The name of the token.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
public async Task RemoveTokenAsync(IdentityUser user, string loginProvider, string name, CancellationToken cancellationToken)
public async Task RemoveTokenAsync(IdentityUser user, string loginProvider, string name, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
@ -1019,7 +1019,7 @@ namespace Volo.Abp.Identity
/// <param name="name">The name of the token.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
public async Task<string> GetTokenAsync(IdentityUser user, string loginProvider, string name, CancellationToken cancellationToken)
public async Task<string> GetTokenAsync(IdentityUser user, string loginProvider, string name, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
@ -1030,12 +1030,12 @@ namespace Volo.Abp.Identity
return user.FindToken(loginProvider, name)?.Value;
}
public Task SetAuthenticatorKeyAsync(IdentityUser user, string key, CancellationToken cancellationToken)
public Task SetAuthenticatorKeyAsync(IdentityUser user, string key, CancellationToken cancellationToken = default)
{
return SetTokenAsync(user, InternalLoginProvider, AuthenticatorKeyTokenName, key, cancellationToken);
}
public Task<string> GetAuthenticatorKeyAsync(IdentityUser user, CancellationToken cancellationToken)
public Task<string> GetAuthenticatorKeyAsync(IdentityUser user, CancellationToken cancellationToken = default)
{
return GetTokenAsync(user, InternalLoginProvider, AuthenticatorKeyTokenName, cancellationToken);
}
@ -1046,7 +1046,7 @@ namespace Volo.Abp.Identity
/// <param name="user">The user who owns the recovery code.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The number of valid recovery codes for the user..</returns>
public virtual async Task<int> CountCodesAsync(IdentityUser user, CancellationToken cancellationToken)
public virtual async Task<int> CountCodesAsync(IdentityUser user, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
@ -1068,7 +1068,7 @@ namespace Volo.Abp.Identity
/// <param name="recoveryCodes">The new recovery codes for the user.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The new recovery codes for the user.</returns>
public virtual Task ReplaceCodesAsync(IdentityUser user, IEnumerable<string> recoveryCodes, CancellationToken cancellationToken)
public virtual Task ReplaceCodesAsync(IdentityUser user, IEnumerable<string> recoveryCodes, CancellationToken cancellationToken = default)
{
var mergedCodes = string.Join(";", recoveryCodes);
return SetTokenAsync(user, InternalLoginProvider, RecoveryCodeTokenName, mergedCodes, cancellationToken);
@ -1082,7 +1082,7 @@ namespace Volo.Abp.Identity
/// <param name="code">The recovery code to use.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>True if the recovery code was found for the user.</returns>
public virtual async Task<bool> RedeemCodeAsync(IdentityUser user, string code, CancellationToken cancellationToken)
public virtual async Task<bool> RedeemCodeAsync(IdentityUser user, string code, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

56
modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserLookupAppService_Tests.cs

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Shouldly;
using Xunit;
namespace Volo.Abp.Identity
{
public class IdentityUserLookupAppService_Tests : AbpIdentityApplicationTestBase
{
private readonly IIdentityUserLookupAppService _identityUserLookupAppService;
private readonly IIdentityUserRepository _identityUserRepository;
private readonly ILookupNormalizer _lookupNormalizer;
public IdentityUserLookupAppService_Tests()
{
_identityUserLookupAppService = GetRequiredService<IIdentityUserLookupAppService>();
_identityUserRepository = GetRequiredService<IIdentityUserRepository>();
_lookupNormalizer = GetRequiredService<ILookupNormalizer>();
}
[Fact]
public async Task FindByIdAsync()
{
var user = await _identityUserRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserLookupAppService.FindByIdAsync(user.Id)).UserName.ShouldBe(user.UserName);
}
[Fact]
public async Task FindById_NotExist_Should_Return_Null()
{
var user = await _identityUserLookupAppService.FindByIdAsync(Guid.NewGuid());
user.ShouldBeNull();
}
[Fact]
public async Task FindByUserNameAsync()
{
var user = await _identityUserRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserLookupAppService.FindByUserNameAsync(user.UserName)).UserName.ShouldBe(user.UserName);
}
[Fact]
public async Task FindByUserName_NotExist_Should_Return_Null()
{
var user = await _identityUserLookupAppService.FindByUserNameAsync(Guid.NewGuid().ToString());
user.ShouldBeNull();
}
}
}

73
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdenityClaimTypeManager_Tests.cs

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
namespace Volo.Abp.Identity
{
public class IdenityClaimTypeManager_Tests : AbpIdentityDomainTestBase
{
private readonly IIdentityClaimTypeRepository _identityClaimTypeRepository;
private readonly IdenityClaimTypeManager _claimTypeManager;
private readonly IdentityTestData _testData;
public IdenityClaimTypeManager_Tests()
{
_identityClaimTypeRepository = GetRequiredService<IIdentityClaimTypeRepository>();
_claimTypeManager = GetRequiredService<IdenityClaimTypeManager>();
_testData = GetRequiredService<IdentityTestData>();
}
[Fact]
public async Task CreateAsync()
{
var claimType = await _claimTypeManager.CreateAsync(new IdentityClaimType(Guid.NewGuid(), "Phone", false,
false, null,
null, null, IdentityClaimValueType.String));
claimType.ShouldNotBeNull();
claimType.Name.ShouldBe("Phone");
}
[Fact]
public async Task Create_Name_Exist_Should_Exception()
{
await Assert.ThrowsAnyAsync<AbpException>(async () => await _claimTypeManager.CreateAsync(
new IdentityClaimType(
Guid.NewGuid(), "Age")));
}
[Fact]
public async Task UpdateAsync()
{
var ageClaim = _identityClaimTypeRepository.Find(_testData.AgeClaimId);
ageClaim.ShouldNotBeNull();
ageClaim.Description = "this is age";
var updatedAgeClaimType = await _claimTypeManager.UpdateAsync(ageClaim);
updatedAgeClaimType.ShouldNotBeNull();
updatedAgeClaimType.Description.ShouldBe("this is age");
}
[Fact]
public async Task Update_Name_Exist_Should_Exception()
{
await Assert.ThrowsAnyAsync<AbpException>(async () => await _claimTypeManager.UpdateAsync(
new IdentityClaimType(
Guid.NewGuid(), "Age")));
}
[Fact]
public async Task Static_IdentityClaimType_Cant_Not_Update()
{
var phoneClaim = new IdentityClaimType(Guid.NewGuid(), "Phone", true, true);
_identityClaimTypeRepository.Insert(phoneClaim);
await Assert.ThrowsAnyAsync<AbpException>(async () => await _claimTypeManager.UpdateAsync(phoneClaim));
}
}
}

60
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityRoleManager_Tests.cs

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Shouldly;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.Identity
{
public class IdentityRoleManager_Tests : AbpIdentityDomainTestBase
{
private readonly IdentityRoleManager _identityRoleManager;
private readonly IIdentityRoleRepository _identityRoleRepository;
private readonly ILookupNormalizer _lookupNormalizer;
private readonly IdentityTestData _testData;
public IdentityRoleManager_Tests()
{
_identityRoleManager = GetRequiredService<IdentityRoleManager>();
_identityRoleRepository = GetRequiredService<IIdentityRoleRepository>();
_lookupNormalizer = GetRequiredService<ILookupNormalizer>();
_testData = GetRequiredService<IdentityTestData>();
}
[Fact]
public async Task GetByIdAsync()
{
var role = await _identityRoleManager.FindByIdAsync(_testData.RoleModeratorId.ToString());
role.ShouldNotBeNull();
role.Name.ShouldBe("moderator");
}
[Fact]
public async Task SetRoleNameAsync()
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
(await _identityRoleManager.SetRoleNameAsync(role, "teacher")).Succeeded.ShouldBeTrue();
role.Name.ShouldBe("teacher");
}
[Fact]
public async Task DeleteAsync()
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
await _identityRoleManager.DeleteAsync(role);
(await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"))).ShouldBeNull();
}
}
}

186
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityRoleStore_Tests.cs

@ -0,0 +1,186 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Shouldly;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.Identity
{
public class IdentityRoleStore_Tests : AbpIdentityDomainTestBase
{
private readonly IdentityRoleStore _identityRoleStore;
private readonly ILookupNormalizer _lookupNormalizer;
private readonly IIdentityRoleRepository _identityRoleRepository;
private readonly IdentityTestData _testData;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public IdentityRoleStore_Tests()
{
_identityRoleStore = GetRequiredService<IdentityRoleStore>();
_lookupNormalizer = GetRequiredService<ILookupNormalizer>();
_identityRoleRepository = GetRequiredService<IIdentityRoleRepository>();
_testData = GetRequiredService<IdentityTestData>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
}
[Fact]
public async Task CreateAsync()
{
var roleId = Guid.NewGuid();
var role = new IdentityRole(roleId, "teacher");
(await _identityRoleStore.CreateAsync(role)).Succeeded.ShouldBeTrue();
var teacher = await _identityRoleStore.FindByIdAsync(roleId.ToString());
teacher.ShouldNotBeNull();
teacher.Name.ShouldBe("teacher");
}
[Fact]
public async Task UpdateAsync()
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
role.IsDefault = true;
await _identityRoleStore.UpdateAsync(role);
role.IsDefault.ShouldBeTrue();
}
[Fact]
public async Task DeleteAsync()
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
await _identityRoleStore.DeleteAsync(role);
(await _identityRoleStore.FindByIdAsync(_testData.RoleModeratorId.ToString())).ShouldBeNull();
}
[Fact]
public async Task GetRoleIdAsync()
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
(await _identityRoleStore.GetRoleIdAsync(role)).ShouldBe(_testData.RoleModeratorId.ToString());
}
[Fact]
public async Task GetRoleNameAsync()
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
(await _identityRoleStore.GetRoleNameAsync(role)).ShouldBe(role.Name);
}
[Fact]
public async Task SetRoleNameAsync()
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
await _identityRoleStore.SetRoleNameAsync(role, "teacher");
role.Name.ShouldBe("teacher");
}
[Fact]
public async Task FindByIdAsync()
{
var role = await _identityRoleStore.FindByIdAsync(_testData.RoleModeratorId.ToString());
role.ShouldNotBeNull();
role.Name.ShouldBe("moderator");
}
[Fact]
public async Task FindByNameAsync()
{
var role = await _identityRoleStore.FindByNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
role.Name.ShouldBe("moderator");
}
[Fact]
public async Task GetNormalizedRoleNameAsync()
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
(await _identityRoleStore.GetNormalizedRoleNameAsync(role)).ShouldBe(role.NormalizedName);
}
[Fact]
public async Task SetNormalizedRoleNameAsync()
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
await _identityRoleStore.SetNormalizedRoleNameAsync(role, _lookupNormalizer.Normalize("teacher"));
role.NormalizedName.ShouldBe(_lookupNormalizer.Normalize("teacher"));
}
[Fact]
public async Task GetClaimsAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
var claims = await _identityRoleStore.GetClaimsAsync(role);
claims.ShouldNotBeEmpty();
claims.ShouldContain(x => x.Type == "test-claim" && x.Value == "test-value");
await uow.CompleteAsync();
}
}
[Fact]
public async Task AddClaimAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
await _identityRoleStore.AddClaimAsync(role, new Claim("my-claim", "my-value"));
role.Claims.ShouldContain(x => x.ClaimType == "my-claim" && x.ClaimValue == "my-value");
await uow.CompleteAsync();
}
}
[Fact]
public async Task RemoveClaimAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var role = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("moderator"));
role.ShouldNotBeNull();
await _identityRoleStore.RemoveClaimAsync(role, new Claim("test-claim", "test-value"));
role.Claims.ShouldNotContain(x => x.ClaimType == "test-claim" && x.ClaimValue == "test-value");
await uow.CompleteAsync();
}
}
}
}

87
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Shouldly;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.Identity
{
public class IdentityUserManager_Tests : AbpIdentityDomainTestBase
{
private readonly IdentityUserManager _identityUserManager;
private readonly IIdentityUserRepository _identityUserRepository;
private readonly IIdentityRoleRepository _identityRoleRepository;
private readonly ILookupNormalizer _lookupNormalizer;
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IdentityTestData _testData;
public IdentityUserManager_Tests()
{
_identityUserManager = GetRequiredService<IdentityUserManager>();
_identityUserRepository = GetRequiredService<IIdentityUserRepository>();
_identityRoleRepository = GetRequiredService<IIdentityRoleRepository>();
_lookupNormalizer = GetRequiredService<ILookupNormalizer>();
_testData = GetRequiredService<IdentityTestData>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
}
[Fact]
public async Task GetByIdAsync()
{
var user = await _identityUserManager.GetByIdAsync(_testData.UserJohnId);
user.ShouldNotBeNull();
user.UserName.ShouldBe("john.nash");
}
[Fact]
public async Task SetRolesAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _identityUserRepository.FindByNormalizedUserNameAsync(
_lookupNormalizer.Normalize("david"));
user.ShouldNotBeNull();
var identityResult = await _identityUserManager.SetRolesAsync(user, new List<string>()
{
"moderator",
});
identityResult.Succeeded.ShouldBeTrue();
user.Roles.ShouldContain(x => x.RoleId == _testData.RoleModeratorId);
await uow.CompleteAsync();
}
}
[Fact]
public async Task SetRoles_Should_Remove_Other_Roles()
{
using (var uow = _unitOfWorkManager.Begin())
{
var roleSupporter =
await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize("supporter"));
roleSupporter.ShouldNotBeNull();
var user = await _identityUserRepository.FindByNormalizedUserNameAsync(
_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
var identityResult = await _identityUserManager.SetRolesAsync(user, new List<string>()
{
"admin",
});
identityResult.Succeeded.ShouldBeTrue();
user.Roles.ShouldNotContain(x => x.RoleId == _testData.RoleModeratorId);
user.Roles.ShouldNotContain(x => x.RoleId == roleSupporter.Id);
await uow.CompleteAsync();
}
}
}
}

777
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserStore_Tests.cs

@ -0,0 +1,777 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Shouldly;
using Volo.Abp.Guids;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.Identity
{
public class IdentityUserStore_Tests : AbpIdentityDomainTestBase
{
private readonly IdentityUserStore _identityUserStore;
private readonly ILookupNormalizer _lookupNormalizer;
private readonly IIdentityUserRepository _userRepository;
private readonly IdentityTestData _testData;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public IdentityUserStore_Tests()
{
_identityUserStore = GetRequiredService<IdentityUserStore>();
_lookupNormalizer = GetRequiredService<ILookupNormalizer>();
_userRepository = GetRequiredService<IIdentityUserRepository>();
_testData = GetRequiredService<IdentityTestData>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
}
[Fact]
public async Task GetUserIdAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetUserIdAsync(user)).ShouldBe(user.Id.ToString());
}
[Fact]
public async Task GetUserNameAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetUserNameAsync(user)).ShouldBe(user.UserName);
}
[Fact]
public async Task SetUserNameAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetUserNameAsync(user, "bob.lee");
user.UserName.ShouldBe("bob.lee");
//user.NormalizedUserName.ShouldBe(_lookupNormalizer.Normalize("bob.lee"));
}
[Fact]
public async Task GetNormalizedUserNameAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetNormalizedUserNameAsync(user)).ShouldBe(user.NormalizedUserName);
}
[Fact]
public async Task SetNormalizedUserNameAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetNormalizedUserNameAsync(user, _lookupNormalizer.Normalize("bob.lee"));
user.NormalizedUserName.ShouldBe(_lookupNormalizer.Normalize("bob.lee"));
}
[Fact]
public async Task CreateAsync()
{
var userId = Guid.NewGuid();
var user = new IdentityUser(userId, "bob.lee", "bob.lee@abp.io");
await _identityUserStore.CreateAsync(user);
var bobLee = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("bob.lee"));
bobLee.ShouldNotBeNull();
bobLee.UserName.ShouldBe("bob.lee");
bobLee.Email.ShouldBe("bob.lee@abp.io");
}
[Fact]
public async Task UpdateAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
user.Name = "lee";
(await _identityUserStore.UpdateAsync(user)).Succeeded.ShouldBeTrue();
(await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"))).Name
.ShouldBe("lee");
}
[Fact]
public async Task Update_Concurrency()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.UpdateAsync(user)).Succeeded.ShouldBeTrue();
user.ConcurrencyStamp = Guid.NewGuid().ToString();
var identityResult = await _identityUserStore.UpdateAsync(user);
identityResult.Succeeded.ShouldBeFalse();
identityResult.Errors.ShouldContain(x =>
x.Code == nameof(IdentityErrorDescriber.ConcurrencyFailure));
// && x.Description == Resources.PasswordMismatch);
}
[Fact]
public async Task DeleteAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.DeleteAsync(user)).Succeeded.ShouldBeTrue();
(await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"))).ShouldBeNull();
}
[Fact]
public async Task FindByIdAsync()
{
var user = await _identityUserStore.FindByIdAsync(_testData.UserJohnId.ToString());
user.ShouldNotBeNull();
user.UserName.ShouldBe("john.nash");
}
[Fact]
public async Task FindByNameAsync()
{
var user = await _identityUserStore.FindByNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
user.UserName.ShouldBe("john.nash");
}
[Fact]
public async Task SetPasswordHashAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
user.PasswordHash.ShouldBeNull();
await _identityUserStore.SetPasswordHashAsync(user, "P@ssw0rd");
user.PasswordHash.ShouldBe("P@ssw0rd");
}
[Fact]
public async Task GetPasswordHashAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetPasswordHashAsync(user, "P@ssw0rd");
(await _identityUserStore.GetPasswordHashAsync(user)).ShouldBe("P@ssw0rd");
}
[Fact]
public async Task HasPasswordAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.HasPasswordAsync(user)).ShouldBeFalse();
await _identityUserStore.SetPasswordHashAsync(user, "P@ssw0rd");
(await _identityUserStore.HasPasswordAsync(user)).ShouldBeTrue();
}
[Fact]
public async Task AddToRoleAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("david"));
user.ShouldNotBeNull();
user.Roles.ShouldBeEmpty();
await _identityUserStore.AddToRoleAsync(user, _lookupNormalizer.Normalize("moderator"));
user.Roles.ShouldContain(x => x.RoleId == _testData.RoleModeratorId);
await uow.CompleteAsync();
}
}
[Fact]
public async Task RemoveFromRoleAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
user.Roles.ShouldContain(x => x.RoleId == _testData.RoleModeratorId);
await _identityUserStore.RemoveFromRoleAsync(user, _lookupNormalizer.Normalize("moderator"));
user.Roles.ShouldNotContain(x => x.RoleId == _testData.RoleModeratorId);
await uow.CompleteAsync();
}
}
[Fact]
public async Task GetRolesAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
var roleNames = await _identityUserStore.GetRolesAsync(user);
roleNames.ShouldNotBeEmpty();
roleNames.ShouldContain(x => x == "moderator");
roleNames.ShouldContain(x => x == "supporter");
}
[Fact]
public async Task IsInRoleAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.IsInRoleAsync(user, _lookupNormalizer.Normalize("moderator"))).ShouldBeTrue();
(await _identityUserStore.IsInRoleAsync(user, _lookupNormalizer.Normalize("moderatorNotExist"))).ShouldBeFalse();
await uow.CompleteAsync();
}
}
[Fact]
public async Task GetClaimsAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
var claims = await _identityUserStore.GetClaimsAsync(user);
claims.ShouldNotBeEmpty();
claims.ShouldContain(x => x.Type == "TestClaimType" && x.Value == "42");
await uow.CompleteAsync();
}
}
[Fact]
public async Task AddClaimsAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.AddClaimsAsync(user, new List<Claim>()
{
new Claim("MyClaimType", "MyClaimValue")
});
user.Claims.ShouldContain(x => x.ClaimType == "MyClaimType" && x.ClaimValue == "MyClaimValue");
await uow.CompleteAsync();
}
}
[Fact]
public async Task ReplaceClaimAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.ReplaceClaimAsync(user, new Claim("TestClaimType", "42"), new Claim("MyClaimType", "MyClaimValue"));
user.Claims.ShouldNotContain(x => x.ClaimType == "TestClaimType" && x.ClaimValue == "42");
user.Claims.ShouldContain(x => x.ClaimType == "MyClaimType" && x.ClaimValue == "MyClaimValue");
await uow.CompleteAsync();
}
}
[Fact]
public async Task RemoveClaimsAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.RemoveClaimsAsync(user, new List<Claim>()
{
new Claim("TestClaimType", "42")
});
user.Claims.ShouldNotContain(x => x.ClaimType == "TestClaimType" && x.ClaimValue == "42");
await uow.CompleteAsync();
}
}
[Fact]
public async Task AddLoginAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
user.Logins.ShouldNotContain(x => x.LoginProvider == "facebook" && x.ProviderKey == "john");
await _identityUserStore.AddLoginAsync(user, new UserLoginInfo("facebook", "john", "John Nash"));
user.Logins.ShouldContain(x => x.LoginProvider == "facebook" && x.ProviderKey == "john");
await uow.CompleteAsync();
}
}
[Fact]
public async Task RemoveLoginAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
user.Logins.ShouldContain(x => x.LoginProvider == "github" && x.ProviderKey == "john");
await _identityUserStore.RemoveLoginAsync(user, "github", "john");
user.Logins.ShouldNotContain(x => x.LoginProvider == "github" && x.ProviderKey == "john");
await uow.CompleteAsync();
}
}
[Fact]
public async Task GetLoginsAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
var logins = await _identityUserStore.GetLoginsAsync(user);
logins.ShouldNotBeNull();
logins.ShouldContain(x => x.LoginProvider == "github" && x.ProviderKey == "john");
logins.ShouldContain(x => x.LoginProvider == "twitter" && x.ProviderKey == "johnx");
await uow.CompleteAsync();
}
}
[Fact]
public async Task FindByLoginAsync()
{
var user = await _identityUserStore.FindByLoginAsync("github", "john");
user.ShouldNotBeNull();
user.UserName.ShouldBe("john.nash");
}
[Fact]
public async Task GetEmailConfirmedAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetEmailConfirmedAsync(user)).ShouldBe(user.EmailConfirmed);
}
[Fact]
public async Task SetEmailConfirmedAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
var emailConfirmed = user.EmailConfirmed;
await _identityUserStore.SetEmailConfirmedAsync(user, !emailConfirmed);
user.EmailConfirmed.ShouldBe(!emailConfirmed);
}
[Fact]
public async Task SetEmailAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetEmailAsync(user, "john.nash.kobe@abp.io");
user.Email.ShouldBe("john.nash.kobe@abp.io");
//user.NormalizedEmail.ShouldBe(_lookupNormalizer.Normalize("john.nash.kobe@abp.io"));
}
[Fact]
public async Task GetEmailAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetEmailAsync(user)).ShouldBe(user.Email);
}
[Fact]
public async Task GetNormalizedEmailAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetNormalizedEmailAsync(user)).ShouldBe(user.NormalizedEmail);
}
[Fact]
public async Task SetNormalizedEmailAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetNormalizedEmailAsync(user, _lookupNormalizer.Normalize("john.nash.kobe@abp.io"));
user.NormalizedEmail.ShouldBe(_lookupNormalizer.Normalize("john.nash.kobe@abp.io"));
}
[Fact]
public async Task FindByEmailAsync()
{
var user = await _identityUserStore.FindByEmailAsync(_lookupNormalizer.Normalize("john.nash@abp.io"));
user.ShouldNotBeNull();
user.Email.ShouldBe("john.nash@abp.io");
}
[Fact]
public async Task GetLockoutEndDateAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetLockoutEndDateAsync(user)).ShouldBe(user.LockoutEnd);
}
[Fact]
public async Task SetLockoutEndDateAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetLockoutEndDateAsync(user, DateTimeOffset.Parse("01/01/2019"));
user.LockoutEnd.ShouldBe(DateTimeOffset.Parse("01/01/2019"));
}
[Fact]
public async Task IncrementAccessFailedCountAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
var count = user.AccessFailedCount;
await _identityUserStore.IncrementAccessFailedCountAsync(user);
user.AccessFailedCount.ShouldBe(count + 1);
}
[Fact]
public async Task ResetAccessFailedCountAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.ResetAccessFailedCountAsync(user);
user.AccessFailedCount.ShouldBe(0);
}
[Fact]
public async Task GetAccessFailedCountAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetAccessFailedCountAsync(user)).ShouldBe(user.AccessFailedCount);
}
[Fact]
public async Task GetLockoutEnabledAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetLockoutEnabledAsync(user)).ShouldBe(user.LockoutEnabled);
}
[Fact]
public async Task SetLockoutEnabledAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
var lockoutEnabled = user.LockoutEnabled;
await _identityUserStore.SetLockoutEnabledAsync(user, !lockoutEnabled);
user.LockoutEnabled.ShouldBe(!lockoutEnabled);
}
[Fact]
public async Task SetPhoneNumberAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetPhoneNumberAsync(user, "13800138000");
user.PhoneNumber.ShouldBe("13800138000");
}
[Fact]
public async Task GetPhoneNumberAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetPhoneNumberAsync(user)).ShouldBe(user.PhoneNumber);
}
[Fact]
public async Task GetPhoneNumberConfirmedAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetPhoneNumberConfirmedAsync(user)).ShouldBe(user.PhoneNumberConfirmed);
}
[Fact]
public async Task SetPhoneNumberConfirmedAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
var phoneNumberConfirmed = user.PhoneNumberConfirmed;
await _identityUserStore.SetPhoneNumberConfirmedAsync(user, !phoneNumberConfirmed);
user.PhoneNumberConfirmed.ShouldBe(!phoneNumberConfirmed);
}
[Fact]
public async Task SetSecurityStampAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
var securityStamp = Guid.NewGuid().ToString();
await _identityUserStore.SetSecurityStampAsync(user, securityStamp);
user.SecurityStamp.ShouldBe(securityStamp);
}
[Fact]
public async Task GetSecurityStampAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetSecurityStampAsync(user)).ShouldBe(user.SecurityStamp);
}
[Fact]
public async Task SetTwoFactorEnabledAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
var twoFactorEnabled = user.TwoFactorEnabled;
await _identityUserStore.SetTwoFactorEnabledAsync(user, !twoFactorEnabled);
user.TwoFactorEnabled.ShouldBe(!twoFactorEnabled);
}
[Fact]
public async Task GetTwoFactorEnabledAsync()
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetTwoFactorEnabledAsync(user)).ShouldBe(user.TwoFactorEnabled);
}
[Fact]
public async Task GetUsersForClaimAsync()
{
var user = await _identityUserStore.GetUsersForClaimAsync(new Claim("TestClaimType", "42"));
user.ShouldNotBeNull();
user.ShouldNotBeEmpty();
user.ShouldContain(x => x.UserName == "john.nash");
}
[Fact]
public async Task GetUsersInRoleAsync()
{
var user = await _identityUserStore.GetUsersInRoleAsync(_lookupNormalizer.Normalize("moderator"));
user.ShouldNotBeNull();
user.ShouldContain(x => x.UserName == "john.nash");
}
[Fact]
public async Task SetTokenAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetTokenAsync(user, "test-provider", "test-name", "123123");
user.Tokens.ShouldNotBeEmpty();
user.Tokens.ShouldContain(x => x.LoginProvider == "test-provider" && x.Name == "test-name" && x.Value == "123123");
await uow.CompleteAsync();
}
}
[Fact]
public async Task RemoveTokenAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.RemoveTokenAsync(user, "test-provider", "test-name");
user.Tokens.ShouldNotContain(x => x.LoginProvider == "test-provider" && x.Name == "test-name" && x.Value == "123123");
await uow.CompleteAsync();
}
}
[Fact]
public async Task GetTokenAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
(await _identityUserStore.GetTokenAsync(user, "test-provider", "test-name")).ShouldBe("test-value");
await uow.CompleteAsync();
}
}
[Fact]
public async Task SetAuthenticatorKeyAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetAuthenticatorKeyAsync(user, "testKey");
user.Tokens.ShouldContain(x => x.LoginProvider == "[AspNetUserStore]" && x.Name == "AuthenticatorKey" && x.Value == "testKey");
await uow.CompleteAsync();
}
}
[Fact]
public async Task GetAuthenticatorKeyAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetAuthenticatorKeyAsync(user, "testKey");
(await _identityUserStore.GetAuthenticatorKeyAsync(user)).ShouldBe("testKey");
await uow.CompleteAsync();
}
}
[Fact]
public async Task CountCodesAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetTokenAsync(user, "[AspNetUserStore]", "RecoveryCodes", "testKey;testKey2");
(await _identityUserStore.CountCodesAsync(user)).ShouldBe(2);
await uow.CompleteAsync();
}
}
[Fact]
public async Task ReplaceCodesAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.ReplaceCodesAsync(user, new List<string>()
{
"testKey",
"testKey2"
});
user.Tokens.ShouldContain(x => x.LoginProvider == "[AspNetUserStore]" && x.Name == "RecoveryCodes" && x.Value == "testKey;testKey2");
await uow.CompleteAsync();
}
}
[Fact]
public async Task RedeemCodeAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash"));
user.ShouldNotBeNull();
await _identityUserStore.SetTokenAsync(user, "[AspNetUserStore]", "RecoveryCodes", "testKey;testKey2");
(await _identityUserStore.RedeemCodeAsync(user, "testKey")).ShouldBeTrue();
await uow.CompleteAsync();
}
}
}
}

30
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/UserRoleFinder_Tests.cs

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
namespace Volo.Abp.Identity
{
public class UserRoleFinder_Tests : AbpIdentityDomainTestBase
{
private readonly IUserRoleFinder _userRoleFinder;
private readonly IdentityTestData _testData;
public UserRoleFinder_Tests()
{
_userRoleFinder = GetRequiredService<IUserRoleFinder>();
_testData = GetRequiredService<IdentityTestData>();
}
[Fact]
public async Task GetRolesAsync()
{
var roleNames = await _userRoleFinder.GetRolesAsync(_testData.UserJohnId);
roleNames.ShouldNotBeEmpty();
roleNames.ShouldContain(x => x == "moderator");
roleNames.ShouldContain(x => x == "supporter");
}
}
}
Loading…
Cancel
Save