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>();
var validator = new UserValidator();
var passValidator = new PasswordValidator();
var mgr = new Mock>(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();
mgr.Object.Options = AuthenticationRules.DefaultAuthenticationRules();
List users= new List();
mgr.Setup(x => x.DeleteAsync(It.IsAny())).ReturnsAsync(IdentityResult.Success);
mgr.Setup(x => x.CreateAsync(It.IsAny(), It.IsAny())).ReturnsAsync(IdentityResult.Success).Callback((x, y) => users.Add(x));
mgr.Setup(x => x.UpdateAsync(It.IsAny())).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?
What I have tried:
I have tried to create identity options and create a setup for it and then add it as parameter like so:
var options = new Mock>();
var idOptions = AuthenticationRules.DefaultAuthenticationRules();
options.Setup(o => o.Value).Returns(idOptions);
var mgr = new Mock>(store.Object, options.Object, null, null, null, null, null, null, null);
But passwordhash property is still null for the user.
Thanks in advance.
Thanks in advance