Browse Source

Fixed unit test: CreateAsync

pull/179/head
Halil İbrahim Kalkan 9 years ago
parent
commit
7b2ce256d5
  1. BIN
      src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll
  2. 4
      src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountControllerBase.cs
  3. 21
      src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityAppServiceBase.cs
  4. 24
      src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs
  5. 17
      src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs
  6. 2
      test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserAppService_Tests.cs

BIN
src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll

Binary file not shown.

4
src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountControllerBase.cs

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;
@ -17,7 +19,7 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers
{ {
if (!identityResult.Succeeded) if (!identityResult.Succeeded)
{ {
throw new UserFriendlyException("Operation failed!"); throw new UserFriendlyException("Operation failed: " + identityResult.Errors.Select(e => $"[{e.Code}] {e.Description}").JoinAsString(", "));
} }
//identityResult.CheckErrors(LocalizationManager); //TODO: Get from old Abp //identityResult.CheckErrors(LocalizationManager); //TODO: Get from old Abp

21
src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityAppServiceBase.cs

@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Identity;
using Volo.Abp.Application.Services;
using Volo.Abp.Ui;
namespace Volo.Abp.Identity
{
public abstract class IdentityAppServiceBase : ApplicationService
{
protected void CheckIdentityErrors(IdentityResult identityResult)
{
if (!identityResult.Succeeded)
{
throw new UserFriendlyException("Operation failed: " + identityResult.Errors.Select(e => $"[{e.Code}] {e.Description}").JoinAsString(", "));
}
//identityResult.CheckErrors(LocalizationManager); //TODO: Get from old Abp
}
}
}

24
src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs

@ -2,13 +2,12 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
namespace Volo.Abp.Identity namespace Volo.Abp.Identity
{ {
//TODO: Consider a way of passing cancellation token to all async application service methods! //TODO: Consider a way of passing cancellation token to all async application service methods!
public class IdentityUserAppService : ApplicationService, IIdentityUserAppService public class IdentityUserAppService : IdentityAppServiceBase, IIdentityUserAppService
{ {
private readonly IdentityUserManager _userManager; private readonly IdentityUserManager _userManager;
private readonly IIdentityUserRepository _userRepository; private readonly IIdentityUserRepository _userRepository;
@ -41,8 +40,9 @@ namespace Volo.Abp.Identity
{ {
var user = new IdentityUser(GuidGenerator.Create(), input.UserName); var user = new IdentityUser(GuidGenerator.Create(), input.UserName);
CheckIdentityErrors(await _userManager.CreateAsync(user, input.Password));
await UpdateUserByInput(user, input); await UpdateUserByInput(user, input);
await _userManager.CreateAsync(user, input.Password);
await CurrentUnitOfWork.SaveChangesAsync(); await CurrentUnitOfWork.SaveChangesAsync();
return ObjectMapper.Map<IdentityUser, IdentityUserDto>(user); return ObjectMapper.Map<IdentityUser, IdentityUserDto>(user);
@ -52,9 +52,9 @@ namespace Volo.Abp.Identity
{ {
var user = await _userManager.GetByIdAsync(id); var user = await _userManager.GetByIdAsync(id);
await _userManager.SetUserNameAsync(user, input.UserName); CheckIdentityErrors(await _userManager.SetUserNameAsync(user, input.UserName));
await UpdateUserByInput(user, input); await UpdateUserByInput(user, input);
await _userManager.UpdateAsync(user); CheckIdentityErrors(await _userManager.UpdateAsync(user));
await CurrentUnitOfWork.SaveChangesAsync(); await CurrentUnitOfWork.SaveChangesAsync();
return ObjectMapper.Map<IdentityUser, IdentityUserDto>(user); return ObjectMapper.Map<IdentityUser, IdentityUserDto>(user);
@ -63,7 +63,7 @@ namespace Volo.Abp.Identity
public async Task DeleteAsync(Guid id) public async Task DeleteAsync(Guid id)
{ {
var user = await _userManager.GetByIdAsync(id); var user = await _userManager.GetByIdAsync(id);
await _userManager.DeleteAsync(user); CheckIdentityErrors(await _userManager.DeleteAsync(user));
} }
public async Task<ListResultDto<IdentityRoleDto>> GetRolesAsync(Guid id) public async Task<ListResultDto<IdentityRoleDto>> GetRolesAsync(Guid id)
@ -77,19 +77,19 @@ namespace Volo.Abp.Identity
public async Task UpdateRolesAsync(Guid id, IdentityUserUpdateRolesDto input) public async Task UpdateRolesAsync(Guid id, IdentityUserUpdateRolesDto input)
{ {
var user = await _userManager.GetByIdAsync(id); var user = await _userManager.GetByIdAsync(id);
await _userManager.SetRolesAsync(user, input.RoleNames); CheckIdentityErrors(await _userManager.SetRolesAsync(user, input.RoleNames));
} }
private async Task UpdateUserByInput(IdentityUser user, IdentityUserCreateOrUpdateDtoBase input) private async Task UpdateUserByInput(IdentityUser user, IdentityUserCreateOrUpdateDtoBase input)
{ {
await _userManager.SetEmailAsync(user, input.Email); CheckIdentityErrors(await _userManager.SetEmailAsync(user, input.Email));
await _userManager.SetPhoneNumberAsync(user, input.PhoneNumber); CheckIdentityErrors(await _userManager.SetPhoneNumberAsync(user, input.PhoneNumber));
await _userManager.SetTwoFactorEnabledAsync(user, input.TwoFactorEnabled); CheckIdentityErrors(await _userManager.SetTwoFactorEnabledAsync(user, input.TwoFactorEnabled));
await _userManager.SetLockoutEnabledAsync(user, input.LockoutEnabled); CheckIdentityErrors(await _userManager.SetLockoutEnabledAsync(user, input.LockoutEnabled));
if (input.RoleNames != null) if (input.RoleNames != null)
{ {
await _userManager.SetRolesAsync(user, input.RoleNames); CheckIdentityErrors(await _userManager.SetRolesAsync(user, input.RoleNames));
} }
} }
} }

17
src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

@ -47,15 +47,26 @@ namespace Volo.Abp.Identity
return user; return user;
} }
public async Task SetRolesAsync([NotNull] IdentityUser user, [NotNull] IEnumerable<string> roleNames) public async Task<IdentityResult> SetRolesAsync([NotNull] IdentityUser user, [NotNull] IEnumerable<string> roleNames)
{ {
Check.NotNull(user, nameof(user)); Check.NotNull(user, nameof(user));
Check.NotNull(roleNames, nameof(roleNames)); Check.NotNull(roleNames, nameof(roleNames));
var currentRoleNames = await GetRolesAsync(user); var currentRoleNames = await GetRolesAsync(user);
await RemoveFromRolesAsync(user, currentRoleNames.Except(roleNames).Distinct()); var result = await RemoveFromRolesAsync(user, currentRoleNames.Except(roleNames).Distinct());
await AddToRolesAsync(user, roleNames.Except(currentRoleNames).Distinct()); if (!result.Succeeded)
{
return result;
}
result = await AddToRolesAsync(user, roleNames.Except(currentRoleNames).Distinct());
if (!result.Succeeded)
{
return result;
}
return IdentityResult.Success;
} }
} }
} }

2
test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserAppService_Tests.cs

@ -63,7 +63,7 @@ namespace Volo.Abp.Identity
Email = CreateRandomEmail(), Email = CreateRandomEmail(),
LockoutEnabled = true, LockoutEnabled = true,
PhoneNumber = CreateRandomPhoneNumber(), PhoneNumber = CreateRandomPhoneNumber(),
Password = "123qwe", Password = "123qwE4r*",
RoleNames = new[] { "moderator" } RoleNames = new[] { "moderator" }
}; };

Loading…
Cancel
Save