private Mock <IAntiforgeryTokenStore> GetTokenStore( HttpContext context, TestTokenSet testTokenSet, bool saveNewCookie = true) { var oldCookieToken = testTokenSet.OldCookieToken; var formToken = testTokenSet.FormToken; var mockTokenStore = new Mock <IAntiforgeryTokenStore>(MockBehavior.Strict); mockTokenStore.Setup(o => o.GetCookieToken(context)) .Returns(oldCookieToken); mockTokenStore.Setup(o => o.GetRequestTokensAsync(context)) .Returns(() => Task.FromResult(new AntiforgeryTokenSet( testTokenSet.FormTokenString, testTokenSet.OldCookieTokenString))); if (saveNewCookie) { var newCookieToken = testTokenSet.NewCookieToken; mockTokenStore.Setup(o => o.SaveCookieToken(context, newCookieToken)) .Verifiable(); } return(mockTokenStore); }
private Mock <IAntiForgeryTokenProvider> GetTokenProvider( HttpContext context, TestTokenSet testTokenSet, bool useOldCookie, bool isOldCookieValid = true, bool isNewCookieValid = true) { var oldCookieToken = testTokenSet.OldCookieToken; var newCookieToken = testTokenSet.NewCookieToken; var formToken = testTokenSet.FormToken; var mockValidator = new Mock <IAntiForgeryTokenProvider>(MockBehavior.Strict); mockValidator.Setup(o => o.GenerateFormToken( context, context.User.Identity as ClaimsIdentity, useOldCookie ? oldCookieToken : newCookieToken)) .Returns(formToken); mockValidator.Setup(o => o.IsCookieTokenValid(oldCookieToken)) .Returns(isOldCookieValid); mockValidator.Setup(o => o.IsCookieTokenValid(newCookieToken)) .Returns(isNewCookieValid); mockValidator.Setup(o => o.GenerateCookieToken()) .Returns(useOldCookie ? oldCookieToken : newCookieToken); return(mockValidator); }
public void SetCookieTokenAndHeader_NullCookieToken() { // Arrange var antiforgeryFeature = new AntiforgeryFeature { HaveDeserializedCookieToken = false, HaveGeneratedNewCookieToken = false, HaveStoredNewCookieToken = true, NewCookieToken = new AntiforgeryToken(), NewCookieTokenString = "serialized-cookie-token-from-context", NewRequestToken = new AntiforgeryToken(), NewRequestTokenString = "serialized-form-token-from-context", }; var context = CreateMockContext( new AntiforgeryOptions(), useOldCookie: false, isOldCookieValid: false, antiforgeryFeature: antiforgeryFeature); var testTokenSet = new TestTokenSet { OldCookieTokenString = null }; var nullTokenStore = GetTokenStore(context.HttpContext, testTokenSet, false); var antiforgery = GetAntiforgery( context.HttpContext, tokenGenerator: context.TokenGenerator.Object, tokenStore: nullTokenStore.Object); // Act antiforgery.SetCookieTokenAndHeader(context.HttpContext); // Assert context.TokenSerializer.Verify(s => s.Deserialize(null), Times.Never); }
private Mock <IAntiForgeryTokenSerializer> GetTokenSerializer(TestTokenSet testTokenSet) { var oldCookieToken = testTokenSet.OldCookieToken; var newCookieToken = testTokenSet.NewCookieToken; var formToken = testTokenSet.FormToken; var mockSerializer = new Mock <IAntiForgeryTokenSerializer>(MockBehavior.Strict); mockSerializer.Setup(o => o.Serialize(formToken)) .Returns("serialized-form-token"); mockSerializer.Setup(o => o.Deserialize("serialized-old-cookie-token")) .Returns(oldCookieToken); mockSerializer.Setup(o => o.Serialize(newCookieToken)) .Returns("serialized-new-cookie-token"); return(mockSerializer); }
private Mock <IAntiforgeryTokenSerializer> GetTokenSerializer(TestTokenSet testTokenSet) { var oldCookieToken = testTokenSet.OldCookieToken; var newCookieToken = testTokenSet.NewCookieToken; var formToken = testTokenSet.RequestToken; var mockSerializer = new Mock <IAntiforgeryTokenSerializer>(MockBehavior.Strict); mockSerializer.Setup(o => o.Serialize(formToken)) .Returns(testTokenSet.FormTokenString); mockSerializer.Setup(o => o.Deserialize(testTokenSet.FormTokenString)) .Returns(formToken); mockSerializer.Setup(o => o.Deserialize(testTokenSet.OldCookieTokenString)) .Returns(oldCookieToken); mockSerializer.Setup(o => o.Serialize(oldCookieToken)) .Returns(testTokenSet.OldCookieTokenString); mockSerializer.Setup(o => o.Serialize(newCookieToken)) .Returns(testTokenSet.NewCookieTokenString); return(mockSerializer); }
private Mock<IAntiForgeryTokenSerializer> GetTokenSerializer(TestTokenSet testTokenSet) { var oldCookieToken = testTokenSet.OldCookieToken; var newCookieToken = testTokenSet.NewCookieToken; var formToken = testTokenSet.FormToken; var mockSerializer = new Mock<IAntiForgeryTokenSerializer>(MockBehavior.Strict); mockSerializer.Setup(o => o.Serialize(formToken)) .Returns("serialized-form-token"); mockSerializer.Setup(o => o.Deserialize("serialized-old-cookie-token")) .Returns(oldCookieToken); mockSerializer.Setup(o => o.Serialize(newCookieToken)) .Returns("serialized-new-cookie-token"); return mockSerializer; }
private Mock<ITokenStore> GetTokenStore(HttpContext context, TestTokenSet testTokenSet, bool saveNewCookie = true) { var oldCookieToken = testTokenSet.OldCookieToken; var formToken = testTokenSet.FormToken; var mockTokenStore = new Mock<ITokenStore>(MockBehavior.Strict); mockTokenStore.Setup(o => o.GetCookieToken(context)) .Returns(oldCookieToken); mockTokenStore.Setup(o => o.GetFormTokenAsync(context)) .Returns(Task.FromResult(formToken)); if (saveNewCookie) { var newCookieToken = testTokenSet.NewCookieToken; mockTokenStore.Setup(o => o.SaveCookieToken(context, newCookieToken)) .Verifiable(); } return mockTokenStore; }
private Mock<ITokenProvider> GetTokenProvider(HttpContext context, TestTokenSet testTokenSet, bool useOldCookie, bool isOldCookieValid = true, bool isNewCookieValid = true) { var oldCookieToken = testTokenSet.OldCookieToken; var newCookieToken = testTokenSet.NewCookieToken; var formToken = testTokenSet.FormToken; var mockValidator = new Mock<ITokenProvider>(MockBehavior.Strict); mockValidator.Setup(o => o.GenerateFormToken(context, context.User.Identity as ClaimsIdentity, useOldCookie ? oldCookieToken : newCookieToken)) .Returns(formToken); mockValidator.Setup(o => o.IsCookieTokenValid(oldCookieToken)) .Returns(isOldCookieValid); mockValidator.Setup(o => o.IsCookieTokenValid(newCookieToken)) .Returns(isNewCookieValid); mockValidator.Setup(o => o.GenerateCookieToken()) .Returns(useOldCookie ? oldCookieToken : newCookieToken); return mockValidator; }