mercredi 27 juillet 2016

Mocking TokenProviders ValidateAsync in identity framework

I have a call in my code that calls the VerifyTwoFactorTokenAsync from the UserManager:

    public async Task<IdentityResult> ConfirmEmailconfirmationAsync(string token, Account user)
    {
        RegisterTwoFactorProvider(TokenType.Confirmation.ToString(), new TotpSecurityStampBasedTokenProvider<Account, int>());

        if (!await VerifyTwoFactorTokenAsync(user.Id, TokenType.Confirmation.ToString(), token))
        {
            return IdentityResult.Failed("Invalid code.");
        }

        return await ConfirmEmailAsync(user.Id, await GenerateEmailConfirmationTokenAsync(user.Id));
    }

I'm trying to mock what this returns and since I can't mock the UserManager class easily I'm trying to mock anything used inside this call.

So far this is the portion of my unit test that is relevent:

    [Fact]
    public async Task ItShouldReturnOk()
    {
        Account = new Account { Id = 1, UserName = "username@email.com", Email = "username@email.com" };

        UserStore.Setup(x => x.FindByIdAsync(1)).ReturnsAsync(Account);
        TokenProvider.Setup(x => x.ValidateAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<UserManager<Account, int>>(), It.IsAny<CmsAccount>())).ReturnsAsync(true);
        IHttpActionResult result = await AccountController.ConfirmEmail(Model);

        Assert.IsType<OkResult>(result);
    }

UserStore and TokenProvider are both mocked. I would have thought these would have covered me but looking at the source code in the identity framework I see where my problem is occurring. Inside the VerifyTwoFactorTokenAsync I see this call being made:

var result = await _tokenProviders[tokenProvider].ValidateAsync("TwoFactor", token, this, user);

It doesn't seem to use a tokenProvider I can mock, unless I am missing out on something. Anyone have any ideas on how to Mock this?

Aucun commentaire:

Enregistrer un commentaire