1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.Helpers.Test;
using System.Web.TestUtil;
using System.Web.WebPages.Scope;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace Microsoft.Web.Helpers.Test
{
public class ReCaptchaTest
{
[Fact]
public void ReCaptchaOptionsMissingWhenNoOptionsAndDefaultRendering()
{
var html = ReCaptcha.GetHtml(GetContext(), "PUBLIC_KEY");
UnitTestHelper.AssertEqualsIgnoreWhitespace(
@"<script src=""http://www.google.com/recaptcha/api/challenge?k=PUBLIC_KEY"" type=""text/javascript""></script>" +
@"<noscript>" +
@"<iframe frameborder=""0"" height=""300px"" src=""http://www.google.com/recaptcha/api/noscript?k=PUBLIC_KEY"" width=""500px""></iframe><br/><br/>" +
@"<textarea cols=""40"" name=""recaptcha_challenge_field"" rows=""3""></textarea>" +
@"<input name=""recaptcha_response_field"" type=""hidden"" value=""manual_challenge""/>" +
@"</noscript>",
html.ToString());
XhtmlAssert.Validate1_0(html, addRoot: true);
}
[Fact]
public void ReCaptchaOptionsWhenOneOptionAndDefaultRendering()
{
var html = ReCaptcha.GetHtml(GetContext(), "PUBLIC_KEY", options: new { theme = "white" });
UnitTestHelper.AssertEqualsIgnoreWhitespace(
@"<script type=""text/javascript"">var RecaptchaOptions={""theme"":""white""};</script>" +
@"<script src=""http://www.google.com/recaptcha/api/challenge?k=PUBLIC_KEY"" type=""text/javascript""></script>" +
@"<noscript>" +
@"<iframe frameborder=""0"" height=""300px"" src=""http://www.google.com/recaptcha/api/noscript?k=PUBLIC_KEY"" width=""500px""></iframe><br/><br/>" +
@"<textarea cols=""40"" name=""recaptcha_challenge_field"" rows=""3""></textarea>" +
@"<input name=""recaptcha_response_field"" type=""hidden"" value=""manual_challenge""/>" +
@"</noscript>",
html.ToString());
XhtmlAssert.Validate1_0(html, addRoot: true);
}
[Fact]
public void ReCaptchaOptionsWhenMultipleOptionsAndDefaultRendering()
{
var html = ReCaptcha.GetHtml(GetContext(), "PUBLIC_KEY", options: new { theme = "white", tabindex = 5 });
UnitTestHelper.AssertEqualsIgnoreWhitespace(
@"<script type=""text/javascript"">var RecaptchaOptions={""theme"":""white"",""tabindex"":5};</script>" +
@"<script src=""http://www.google.com/recaptcha/api/challenge?k=PUBLIC_KEY"" type=""text/javascript""></script>" +
@"<noscript>" +
@"<iframe frameborder=""0"" height=""300px"" src=""http://www.google.com/recaptcha/api/noscript?k=PUBLIC_KEY"" width=""500px""></iframe><br/><br/>" +
@"<textarea cols=""40"" name=""recaptcha_challenge_field"" rows=""3""></textarea>" +
@"<input name=""recaptcha_response_field"" type=""hidden"" value=""manual_challenge""/>" +
@"</noscript>",
html.ToString());
XhtmlAssert.Validate1_0(html, addRoot: true);
}
[Fact]
public void ReCaptchaOptionsWhenMultipleOptionsFromDictionaryAndDefaultRendering()
{
// verifies that a dictionary will serialize the same as a projection
var options = new Dictionary<string, object> { { "theme", "white" }, { "tabindex", 5 } };
var html = ReCaptcha.GetHtml(GetContext(), "PUBLIC_KEY", options: options);
UnitTestHelper.AssertEqualsIgnoreWhitespace(
@"<script type=""text/javascript"">var RecaptchaOptions={""theme"":""white"",""tabindex"":5};</script>" +
@"<script src=""http://www.google.com/recaptcha/api/challenge?k=PUBLIC_KEY"" type=""text/javascript""></script>" +
@"<noscript>" +
@"<iframe frameborder=""0"" height=""300px"" src=""http://www.google.com/recaptcha/api/noscript?k=PUBLIC_KEY"" width=""500px""></iframe><br/><br/>" +
@"<textarea cols=""40"" name=""recaptcha_challenge_field"" rows=""3""></textarea>" +
@"<input name=""recaptcha_response_field"" type=""hidden"" value=""manual_challenge""/>" +
@"</noscript>",
html.ToString());
XhtmlAssert.Validate1_0(html, addRoot: true);
}
[Fact]
public void RenderUsesLastError()
{
HttpContextBase context = GetContext();
ReCaptcha.HandleValidateResponse(context, "false\nincorrect-captcha-sol");
var html = ReCaptcha.GetHtml(context, "PUBLIC_KEY");
UnitTestHelper.AssertEqualsIgnoreWhitespace(
@"<script src=""http://www.google.com/recaptcha/api/challenge?k=PUBLIC_KEY&error=incorrect-captcha-sol"" type=""text/javascript""></script>" +
@"<noscript>" +
@"<iframe frameborder=""0"" height=""300px"" src=""http://www.google.com/recaptcha/api/noscript?k=PUBLIC_KEY"" width=""500px""></iframe><br/><br/>" +
@"<textarea cols=""40"" name=""recaptcha_challenge_field"" rows=""3""></textarea>" +
@"<input name=""recaptcha_response_field"" type=""hidden"" value=""manual_challenge""/>" +
@"</noscript>",
html.ToString());
XhtmlAssert.Validate1_0(html, addRoot: true);
}
[Fact]
public void RenderWhenConnectionIsSecure()
{
var html = ReCaptcha.GetHtml(GetContext(isSecure: true), "PUBLIC_KEY");
UnitTestHelper.AssertEqualsIgnoreWhitespace(
@"<script src=""https://www.google.com/recaptcha/api/challenge?k=PUBLIC_KEY"" type=""text/javascript""></script>" +
@"<noscript>" +
@"<iframe frameborder=""0"" height=""300px"" src=""https://www.google.com/recaptcha/api/noscript?k=PUBLIC_KEY"" width=""500px""></iframe><br/><br/>" +
@"<textarea cols=""40"" name=""recaptcha_challenge_field"" rows=""3""></textarea>" +
@"<input name=""recaptcha_response_field"" type=""hidden"" value=""manual_challenge""/>" +
@"</noscript>",
html.ToString());
XhtmlAssert.Validate1_0(html, addRoot: true);
}
[Fact]
public void ValidateThrowsWhenRemoteAddressNotAvailable()
{
HttpContextBase context = GetContext();
VirtualPathUtilityBase virtualPathUtility = GetVirtualPathUtility();
context.Request.Form["recaptcha_challenge_field"] = "CHALLENGE";
context.Request.Form["recaptcha_response_field"] = "RESPONSE";
Assert.Throws<InvalidOperationException>(() => { ReCaptcha.Validate(context, privateKey: "PRIVATE_KEY", virtualPathUtility: virtualPathUtility).ToString(); }, "The captcha cannot be validated because the remote address was not found in the request.");
}
[Fact]
public void ValidateReturnsFalseWhenChallengeNotPosted()
{
HttpContextBase context = GetContext();
VirtualPathUtilityBase virtualPathUtility = GetVirtualPathUtility();
context.Request.ServerVariables["REMOTE_ADDR"] = "127.0.0.1";
Assert.False(ReCaptcha.Validate(context, privateKey: "PRIVATE_KEY", virtualPathUtility: virtualPathUtility));
}
[Fact]
public void ValidatePostData()
{
HttpContextBase context = GetContext();
VirtualPathUtilityBase virtualPathUtility = GetVirtualPathUtility();
context.Request.ServerVariables["REMOTE_ADDR"] = "127.0.0.1";
context.Request.Form["recaptcha_challenge_field"] = "CHALLENGE";
context.Request.Form["recaptcha_response_field"] = "RESPONSE";
Assert.Equal("privatekey=PRIVATE_KEY&remoteip=127.0.0.1&challenge=CHALLENGE&response=RESPONSE",
ReCaptcha.GetValidatePostData(context, "PRIVATE_KEY", virtualPathUtility));
}
[Fact]
public void ValidatePostDataWhenNoResponse()
{
// Arrange
HttpContextBase context = GetContext();
VirtualPathUtilityBase virtualPathUtility = GetVirtualPathUtility();
context.Request.ServerVariables["REMOTE_ADDR"] = "127.0.0.1";
context.Request.Form["recaptcha_challenge_field"] = "CHALLENGE";
// Act
var validatePostData = ReCaptcha.GetValidatePostData(context, "PRIVATE_KEY", virtualPathUtility);
// Assert
Assert.Equal("privatekey=PRIVATE_KEY&remoteip=127.0.0.1&challenge=CHALLENGE&response=", validatePostData);
}
[Fact]
public void ValidateResponseReturnsFalseOnEmptyReCaptchaResponse()
{
HttpContextBase context = GetContext();
Assert.False(ReCaptcha.HandleValidateResponse(context, ""));
Assert.Equal(String.Empty, ReCaptcha.GetLastError(context));
}
[Fact]
public void ValidateResponseReturnsTrueOnSuccess()
{
HttpContextBase context = GetContext();
Assert.True(ReCaptcha.HandleValidateResponse(context, "true\nsuccess"));
Assert.Equal(String.Empty, ReCaptcha.GetLastError(context));
}
[Fact]
public void ValidateResponseReturnsFalseOnError()
{
HttpContextBase context = GetContext();
Assert.False(ReCaptcha.HandleValidateResponse(context, "false\nincorrect-captcha-sol"));
Assert.Equal("incorrect-captcha-sol", ReCaptcha.GetLastError(context));
}
[Fact]
public void ReCaptchaPrivateKeyThowsWhenSetToNull()
{
Assert.ThrowsArgumentNull(() => ReCaptcha.PrivateKey = null, "value");
}
[Fact]
public void ReCaptchaPrivateKeyUsesScopeStorage()
{
// Arrange
var value = "value";
// Act
ReCaptcha.PrivateKey = value;
// Assert
Assert.Equal(ReCaptcha.PrivateKey, value);
Assert.Equal(ScopeStorage.CurrentScope[ReCaptcha._privateKey], value);
}
[Fact]
public void PublicKeyThowsWhenSetToNull()
{
Assert.ThrowsArgumentNull(() => ReCaptcha.PublicKey = null, "value");
}
[Fact]
public void ReCaptchaPublicKeyUsesScopeStorage()
{
// Arrange
var value = "value";
// Act
ReCaptcha.PublicKey = value;
// Assert
Assert.Equal(ReCaptcha.PublicKey, value);
Assert.Equal(ScopeStorage.CurrentScope[ReCaptcha._publicKey], value);
}
private HttpContextBase GetContext(bool isSecure = false)
{
// mock HttpRequest
Mock<HttpRequestBase> requestMock = new Mock<HttpRequestBase>();
requestMock.Setup(request => request.IsSecureConnection).Returns(isSecure);
requestMock.Setup(request => request.Form).Returns(new NameValueCollection());
requestMock.Setup(request => request.ServerVariables).Returns(new NameValueCollection());
// mock HttpContext
Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
contextMock.Setup(context => context.Items).Returns(new Hashtable());
contextMock.Setup(context => context.Request).Returns(requestMock.Object);
return contextMock.Object;
}
private static VirtualPathUtilityBase GetVirtualPathUtility()
{
var virtualPathUtility = new Mock<VirtualPathUtilityBase>();
virtualPathUtility.Setup(c => c.ToAbsolute(It.IsAny<string>())).Returns<string>(_ => _);
return virtualPathUtility.Object;
}
}
}
|