Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Commit

Permalink
Removed redudant userID calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Suhas Joshi committed Mar 5, 2015
1 parent 08d0416 commit 5daaa5b
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions test/Shared/UserManagerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ public async Task CanDeleteUser()
var manager = CreateManager();
var user = CreateTestUser();
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "CreateAsync", await manager.GetUserIdAsync(user));
var userId = await manager.GetUserIdAsync(user);
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "CreateAsync", userId);

IdentityResultAssert.IsSuccess(await manager.DeleteAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "DeleteAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "DeleteAsync", userId);

Assert.Null(await manager.FindByIdAsync(await manager.GetUserIdAsync(user)));
Assert.Null(await manager.FindByIdAsync(userId));
}

[Fact]
Expand Down Expand Up @@ -139,13 +140,14 @@ public async Task CanUpdatePasswordUsingHasher()
var user = CreateTestUser("UpdatePassword");
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user, "password"));
Assert.True(await manager.CheckPasswordAsync(user, "password"));
string expectedLog = string.Format("{0} for user: {1} : {2}", "CheckPasswordAsync", await manager.GetUserIdAsync(user), true.ToString());
var userId = await manager.GetUserIdAsync(user);
string expectedLog = string.Format("{0} for user: {1} : {2}", "CheckPasswordAsync", userId, true.ToString());
IdentityResultAssert.VerifyLogMessage(manager.Logger, expectedLog);

SetUserPasswordHash(user, manager.PasswordHasher.HashPassword(user, "New"));
IdentityResultAssert.IsSuccess(await manager.UpdateAsync(user));
Assert.False(await manager.CheckPasswordAsync(user, "password"));
expectedLog = string.Format("{0} for user: {1} : {2}", "CheckPasswordAsync", await manager.GetUserIdAsync(user), false.ToString());
expectedLog = string.Format("{0} for user: {1} : {2}", "CheckPasswordAsync", userId, false.ToString());
IdentityResultAssert.VerifyLogMessage(manager.Logger, expectedLog);
Assert.True(await manager.CheckPasswordAsync(user, "New"));
}
Expand Down Expand Up @@ -306,11 +308,12 @@ public async Task CanCreateUserLoginAndAddPassword()
var manager = CreateManager();
var user = CreateTestUser();
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
var login = new UserLoginInfo("Provider", await manager.GetUserIdAsync(user), "display");
var userId = await manager.GetUserIdAsync(user);
var login = new UserLoginInfo("Provider", userId, "display");
IdentityResultAssert.IsSuccess(await manager.AddLoginAsync(user, login));
Assert.False(await manager.HasPasswordAsync(user));
IdentityResultAssert.IsSuccess(await manager.AddPasswordAsync(user, "password"));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "AddPasswordAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "AddPasswordAsync", userId);
Assert.True(await manager.HasPasswordAsync(user));
var logins = await manager.GetLoginsAsync(user);
Assert.NotNull(logins);
Expand Down Expand Up @@ -338,7 +341,8 @@ public async Task CanCreateUserAddRemoveLogin()
var user = CreateTestUser();
var result = await manager.CreateAsync(user);
Assert.NotNull(user);
var login = new UserLoginInfo("Provider", await manager.GetUserIdAsync(user), "display");
var userId = await manager.GetUserIdAsync(user);
var login = new UserLoginInfo("Provider", userId, "display");
IdentityResultAssert.IsSuccess(result);
IdentityResultAssert.IsSuccess(await manager.AddLoginAsync(user, login));
Assert.Equal(user, await manager.FindByLoginAsync(login.LoginProvider, login.ProviderKey));
Expand All @@ -350,7 +354,7 @@ public async Task CanCreateUserAddRemoveLogin()
Assert.Equal(login.ProviderDisplayName, logins.Last().ProviderDisplayName);
var stamp = await manager.GetSecurityStampAsync(user);
IdentityResultAssert.IsSuccess(await manager.RemoveLoginAsync(user, login.LoginProvider, login.ProviderKey));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "RemoveLoginAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "RemoveLoginAsync", userId);
Assert.Null(await manager.FindByLoginAsync(login.LoginProvider, login.ProviderKey));
logins = await manager.GetLoginsAsync(user);
Assert.NotNull(logins);
Expand Down Expand Up @@ -403,11 +407,12 @@ public async Task CanAddRemoveUserClaim()
{
IdentityResultAssert.IsSuccess(await manager.AddClaimAsync(user, c));
}
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "AddClaimsAsync", await manager.GetUserIdAsync(user));
var userId = await manager.GetUserIdAsync(user);
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "AddClaimsAsync", userId);
var userClaims = await manager.GetClaimsAsync(user);
Assert.Equal(3, userClaims.Count);
IdentityResultAssert.IsSuccess(await manager.RemoveClaimAsync(user, claims[0]));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "RemoveClaimsAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "RemoveClaimsAsync", userId);
userClaims = await manager.GetClaimsAsync(user);
Assert.Equal(2, userClaims.Count);
IdentityResultAssert.IsSuccess(await manager.RemoveClaimAsync(user, claims[1]));
Expand Down Expand Up @@ -645,9 +650,10 @@ public async Task CanResetPasswordWithStaticTokenProvider()
Assert.NotNull(stamp);
var token = await manager.GeneratePasswordResetTokenAsync(user);
Assert.NotNull(token);
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "GeneratePasswordResetTokenAsync", await manager.GetUserIdAsync(user));
var userId = await manager.GetUserIdAsync(user);
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "GeneratePasswordResetTokenAsync", userId);
IdentityResultAssert.IsSuccess(await manager.ResetPasswordAsync(user, token, newPassword));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "ResetPasswordAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "ResetPasswordAsync", userId);
Assert.False(await manager.CheckPasswordAsync(user, password));
Assert.True(await manager.CheckPasswordAsync(user, newPassword));
Assert.NotEqual(stamp, await manager.GetSecurityStampAsync(user));
Expand Down Expand Up @@ -702,14 +708,15 @@ public async Task CanGenerateAndVerifyUserTokenWithStaticTokenProvider()
var user2 = CreateTestUser();
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user2));
var userId = await manager.GetUserIdAsync(user);
var token = await manager.GenerateUserTokenAsync(user, "Static", "test");
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "GenerateUserTokenAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "GenerateUserTokenAsync", userId);

Assert.True(await manager.VerifyUserTokenAsync(user, "Static", "test", token));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "VerifyUserTokenAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "VerifyUserTokenAsync", userId);

Assert.False(await manager.VerifyUserTokenAsync(user, "Static", "test2", token));
IdentityResultAssert.VerifyUserManagerFailureLog(manager.Logger, "VerifyUserTokenAsync", await manager.GetUserIdAsync(user), IdentityErrorDescriber.Default.InvalidToken());
IdentityResultAssert.VerifyUserManagerFailureLog(manager.Logger, "VerifyUserTokenAsync", userId, IdentityErrorDescriber.Default.InvalidToken());

Assert.False(await manager.VerifyUserTokenAsync(user, "Static", "test", token + "a"));
Assert.False(await manager.VerifyUserTokenAsync(user2, "Static", "test", token));
Expand All @@ -726,9 +733,10 @@ public async Task CanConfirmEmailWithStaticToken()
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
var token = await manager.GenerateEmailConfirmationTokenAsync(user);
Assert.NotNull(token);
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "GenerateEmailConfirmationTokenAsync", await manager.GetUserIdAsync(user));
var userId = await manager.GetUserIdAsync(user);
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "GenerateEmailConfirmationTokenAsync", userId);
IdentityResultAssert.IsSuccess(await manager.ConfirmEmailAsync(user, token));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "ConfirmEmailAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "ConfirmEmailAsync", userId);
Assert.True(await manager.IsEmailConfirmedAsync(user));
IdentityResultAssert.IsSuccess(await manager.SetEmailAsync(user, null));
Assert.False(await manager.IsEmailConfirmedAsync(user));
Expand Down Expand Up @@ -1371,17 +1379,18 @@ public async Task CanVerifyPhoneNumber()
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
const string num1 = "111-123-4567";
const string num2 = "111-111-1111";
var userId = await manager.GetUserIdAsync(user);
var token1 = await manager.GenerateChangePhoneNumberTokenAsync(user, num1);
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "GenerateChangePhoneNumberTokenAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "GenerateChangePhoneNumberTokenAsync", userId);

var token2 = await manager.GenerateChangePhoneNumberTokenAsync(user, num2);
Assert.NotEqual(token1, token2);
Assert.True(await manager.VerifyChangePhoneNumberTokenAsync(user, token1, num1));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "VerifyChangePhoneNumberTokenAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "VerifyChangePhoneNumberTokenAsync", userId);
Assert.True(await manager.VerifyChangePhoneNumberTokenAsync(user, token2, num2));
Assert.False(await manager.VerifyChangePhoneNumberTokenAsync(user, token2, num1));
Assert.False(await manager.VerifyChangePhoneNumberTokenAsync(user, token1, num2));
IdentityResultAssert.VerifyUserManagerFailureLog(manager.Logger, "VerifyChangePhoneNumberTokenAsync", await manager.GetUserIdAsync(user), IdentityErrorDescriber.Default.InvalidToken());
IdentityResultAssert.VerifyUserManagerFailureLog(manager.Logger, "VerifyChangePhoneNumberTokenAsync", userId, IdentityErrorDescriber.Default.InvalidToken());
}

[Fact]
Expand Down Expand Up @@ -1499,19 +1508,20 @@ public async Task EmailTokenFactorWithFormatTest()
var user = CreateTestUser("foouser");
const string password = "password";
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user, password));
var userId = await manager.GetUserIdAsync(user);
var email = await manager.GetUserNameAsync(user) + "@diddly.bop";
IdentityResultAssert.IsSuccess(await manager.SetEmailAsync(user, email));
var stamp = await manager.GetSecurityStampAsync(user);
Assert.NotNull(stamp);
var token = await manager.GenerateTwoFactorTokenAsync(user, factorId);
Assert.NotNull(token);
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "GenerateTwoFactorTokenAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "GenerateTwoFactorTokenAsync", userId);
Assert.Null(messageService.Message);
IdentityResultAssert.IsSuccess(await manager.NotifyTwoFactorTokenAsync(user, factorId, token));
Assert.NotNull(messageService.Message);
Assert.Equal(subject, messageService.Message.Subject);
Assert.Equal(string.Format(body, token), messageService.Message.Body);
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "NotifyTwoFactorTokenAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "NotifyTwoFactorTokenAsync", userId);
Assert.True(await manager.VerifyTwoFactorTokenAsync(user, factorId, token));
}

Expand Down Expand Up @@ -1641,11 +1651,12 @@ public async Task CanGetValidTwoFactor()
var manager = CreateManager();
var user = CreateTestUser();
IdentityResultAssert.IsSuccess(await manager.CreateAsync(user));
var userId = await manager.GetUserIdAsync(user);
var factors = await manager.GetValidTwoFactorProvidersAsync(user);
Assert.NotNull(factors);
Assert.False(factors.Any());
IdentityResultAssert.IsSuccess(await manager.SetPhoneNumberAsync(user, "111-111-1111"));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "SetPhoneNumberAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "SetPhoneNumberAsync", userId);
var token = await manager.GenerateChangePhoneNumberTokenAsync(user, "111-111-1111");
IdentityResultAssert.IsSuccess(await manager.ChangePhoneNumberAsync(user, "111-111-1111", token));
await manager.UpdateAsync(user);
Expand All @@ -1654,7 +1665,7 @@ public async Task CanGetValidTwoFactor()
Assert.Equal(1, factors.Count());
Assert.Equal("Phone", factors[0]);
IdentityResultAssert.IsSuccess(await manager.SetEmailAsync(user, "test@test.com"));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "SetEmailAsync", await manager.GetUserIdAsync(user));
IdentityResultAssert.VerifyUserManagerSuccessLog(manager.Logger, "SetEmailAsync", userId);
token = await manager.GenerateEmailConfirmationTokenAsync(user);
await manager.ConfirmEmailAsync(user, token);
factors = await manager.GetValidTwoFactorProvidersAsync(user);
Expand Down

0 comments on commit 5daaa5b

Please sign in to comment.