Added UpdateAsync test for IIdentityUserAppService.

pull/112/head
Halil İbrahim Kalkan 8 years ago
parent e2d438b286
commit aedf5d1087

@ -4,7 +4,7 @@ using Volo.Abp.Application.Services;
namespace Volo.Abp.Identity
{
public interface IIdentityUserAppService : IAsyncCrudAppService<IdentityUserDto, Guid, PagedAndSortedResultRequestDto, IdentityUserCreateOrUpdateDto>
public interface IIdentityUserAppService : IAsyncCrudAppService<IdentityUserDto, Guid, PagedAndSortedResultRequestDto, IdentityUserCreateDto, IdentityUserUpdateDto>
{
}

@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;
namespace Volo.Abp.Identity
{
public class IdentityUserCreateDto: IdentityUserCreateOrUpdateDtoBase
{
[Required]
[MaxLength(16)] //TODO: Create a shared dll of Identity and move consts to there for sharing!
public string Password { get; set; }
}
}

@ -1,9 +1,6 @@
using System.ComponentModel.DataAnnotations;
namespace Volo.Abp.Identity
namespace Volo.Abp.Identity
{
//TODO: Use different Dtos for Create & Update even if they are derived from a base Dto. Thus, clients will be backward compatible in a future change.
public class IdentityUserCreateOrUpdateDto
public abstract class IdentityUserCreateOrUpdateDtoBase
{
public string UserName { get; set; }
@ -14,9 +11,5 @@ namespace Volo.Abp.Identity
public bool TwoFactorEnabled { get; set; } //TODO: Optional?
public bool LockoutEnabled { get; set; } //TODO: Optional?
[Required]
[MaxLength(16)] //TODO: Create a shared dll of Identity and move consts to there for sharing!
public string Password { get; set; }
}
}

@ -0,0 +1,7 @@
namespace Volo.Abp.Identity
{
public class IdentityUserUpdateDto : IdentityUserCreateOrUpdateDtoBase
{
}
}

@ -36,7 +36,7 @@ namespace Volo.Abp.Identity
return new PagedResultDto<IdentityUserDto>(userCount, userDtos);
}
public async Task<IdentityUserDto> CreateAsync(IdentityUserCreateOrUpdateDto input)
public async Task<IdentityUserDto> CreateAsync(IdentityUserCreateDto input)
{
var user = new IdentityUser(GuidGenerator.Create(), input.UserName);
@ -48,7 +48,7 @@ namespace Volo.Abp.Identity
return ObjectMapper.Map<IdentityUser, IdentityUserDto>(user);
}
public async Task<IdentityUserDto> UpdateAsync(Guid id, IdentityUserCreateOrUpdateDto input)
public async Task<IdentityUserDto> UpdateAsync(Guid id, IdentityUserUpdateDto input)
{
var user = await _userManager.GetByIdAsync(id);
@ -66,7 +66,7 @@ namespace Volo.Abp.Identity
await _userManager.DeleteAsync(user);
}
private async Task UpdateUserProperties(IdentityUserCreateOrUpdateDto input, IdentityUser user)
private async Task UpdateUserProperties(IdentityUserCreateOrUpdateDtoBase input, IdentityUser user)
{
await _userManager.SetEmailAsync(user, input.Email);
await _userManager.SetPhoneNumberAsync(user, input.PhoneNumber);

@ -1,8 +1,10 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Xunit;
namespace Volo.Abp.Identity
@ -10,10 +12,12 @@ namespace Volo.Abp.Identity
public class IdentityUserAppService_Tests : AbpIdentityApplicationTestBase
{
private readonly IIdentityUserAppService _identityUserAppService;
private readonly IRepository<IdentityUser> _userRepository;
public IdentityUserAppService_Tests()
{
_identityUserAppService = ServiceProvider.GetRequiredService<IIdentityUserAppService>();
_userRepository = ServiceProvider.GetRequiredService<IRepository<IdentityUser>>();
}
[Fact]
@ -27,24 +31,87 @@ namespace Volo.Abp.Identity
[Fact]
public async Task CreateAsync()
{
var input = new IdentityUserCreateOrUpdateDto
//Arrange
var input = new IdentityUserCreateDto
{
UserName = Guid.NewGuid().ToString(),
Email = Guid.NewGuid().ToString("N").Left(16) + "@abp.io",
Email = CreateRandomEmail(),
LockoutEnabled = true,
PhoneNumber = RandomHelper.GetRandom(10000000,100000000).ToString(),
PhoneNumber = CreateRandomPhoneNumber(),
Password = "123qwe"
};
//Act
var result = await _identityUserAppService.CreateAsync(input);
//Assert
result.Id.ShouldNotBe(Guid.Empty);
result.UserName.ShouldBe(input.UserName);
result.Email.ShouldBe(input.Email);
result.LockoutEnabled.ShouldBe(input.LockoutEnabled);
result.PhoneNumber.ShouldBe(input.PhoneNumber);
//TODO: Also check repository
var user = await _userRepository.GetAsync(result.Id);
user.Id.ShouldBe(result.Id);
user.UserName.ShouldBe(input.UserName);
user.Email.ShouldBe(input.Email);
user.LockoutEnabled.ShouldBe(input.LockoutEnabled);
user.PhoneNumber.ShouldBe(input.PhoneNumber);
}
[Fact]
public async Task UpdateAsync()
{
//Arrange
var johnNash = await GetUserAsync("john.nash");
var input = new IdentityUserUpdateDto
{
UserName = johnNash.UserName,
LockoutEnabled = true,
TwoFactorEnabled = true,
PhoneNumber = CreateRandomPhoneNumber(),
Email = CreateRandomEmail()
};
//Act
var result = await _identityUserAppService.UpdateAsync(johnNash.Id, input);
//Assert
result.Id.ShouldBe(johnNash.Id);
result.UserName.ShouldBe(input.UserName);
result.Email.ShouldBe(input.Email);
result.LockoutEnabled.ShouldBe(input.LockoutEnabled);
result.PhoneNumber.ShouldBe(input.PhoneNumber);
var user = await _userRepository.GetAsync(result.Id);
user.Id.ShouldBe(result.Id);
user.UserName.ShouldBe(input.UserName);
user.Email.ShouldBe(input.Email);
user.LockoutEnabled.ShouldBe(input.LockoutEnabled);
user.PhoneNumber.ShouldBe(input.PhoneNumber);
}
private async Task<IdentityUser> GetUserAsync(string userName)
{
return (await _userRepository.GetListAsync()).First(u => u.UserName == userName);
}
private static string CreateRandomEmail()
{
return Guid.NewGuid().ToString("N").Left(16) + "@abp.io";
}
private static string CreateRandomPhoneNumber()
{
return RandomHelper.GetRandom(10000000, 100000000).ToString();
}
}
}

Loading…
Cancel
Save