I am working with Microsoft.AspNetCore.Identity.UserManager and I'm trying to mock the creation of a new user. In fact, it does create a new user with username, email etc. but the password hash property is still null.
This is how I set up mock usermanager with some additional setup:
var store = new Mock<IUserPasswordStore<User>>();
var validator = new UserValidator<User>();
var passValidator = new PasswordValidator<User>();
var mgr = new Mock<UserManager<User>>(store.Object, null, null, null, null, null, null, null, null);
mgr.Object.UserValidators.Add(validator);
mgr.Object.PasswordValidators.Add(passValidator);
mgr.Object.PasswordHasher = new PasswordHasher<User>();
mgr.Object.Options = AuthenticationRules.DefaultAuthenticationRules();
List<User> users= new List<User>();
mgr.Setup(x => x.DeleteAsync(It.IsAny<User>())).ReturnsAsync(IdentityResult.Success);
mgr.Setup(x => x.CreateAsync(It.IsAny<User>(), It.IsAny<string>())).ReturnsAsync(IdentityResult.Success).Callback<User, string>((x, y) => users.Add(x));
mgr.Setup(x => x.UpdateAsync(It.IsAny<User>())).ReturnsAsync(IdentityResult.Success);
The 'DefaultAuthenticationRules' returns this:
public static IdentityOptions DefaultAuthenticationRules(IdentityOptions identityOptions = null)
{
if(identityOptions == null)
identityOptions = new IdentityOptions();
identityOptions.User.RequireUniqueEmail = true;
identityOptions.Password.RequireNonAlphanumeric = false;
identityOptions.Password.RequiredUniqueChars = 0;
return identityOptions;
}
I am then passing the mgr.Object to a method that handles the creation of the new user where 'Object' is _userManager
var creationResult = await _userManager.CreateAsync(_user, _registrationModel.Password);
if (!creationResult.Succeeded)
return false;
return true;
_registrationModel.Password IS populated.
So now when the setup adding a new user in the callback to the list of users the user is populated without password hash. I am not exactly sure what I'm missing here. I'm missing something in mgr.Setup?
Thanks in advance