Skip to content

Commit

Permalink
Properly check validation response
Browse files Browse the repository at this point in the history
Restore correct checking of JSON response from ReCaptcha server. Plugin will now work again. Refs #26
  • Loading branch information
iamthechad committed Feb 19, 2015
1 parent 3533315 commit 896d94a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/groovy/com/megatome/grails/recaptcha/ReCaptcha.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ReCaptcha {
*/
public String createRecaptchaHtml(Map options) {
def includeScriptForInstance = includeScript
if (options.containsKey('includeScript')) {
if (options?.containsKey('includeScript')) {
includeScriptForInstance = Boolean.valueOf(options.includeScript)
}

Expand Down Expand Up @@ -105,7 +105,7 @@ public class ReCaptcha {
* @param options Options for creating the tag. Only <code>lang</code> is supported.
* @return
*/
public String createScriptTag(Map options) {
public static String createScriptTag(Map options) {
def qs = new QueryString()
if (options?.lang) {
qs.add("hl", URLEncoder.encode(options.remove("lang")))
Expand Down Expand Up @@ -154,6 +154,6 @@ public class ReCaptcha {
if (!responseObject) {
return false
}
responseObject.success?.trim()?.toBoolean() == true
return responseObject.success
}
}
47 changes: 19 additions & 28 deletions test/unit/com/megatome/grails/recaptcha/ReCaptchaTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class ReCaptchaTests extends GroovyTestCase {
assertTrue r.createRecaptchaHtml(null).contains("<script")

r.includeNoScript = true

assertTrue r.createRecaptchaHtml(null).contains("<noscript>")

def options = new Properties()
Expand All @@ -45,54 +44,46 @@ public class ReCaptchaTests extends GroovyTestCase {
assertTrue html.contains("data-theme=\"mytheme\"")
}


public void testCreateCaptchaHtmlWithLangInURL() {
def recap = new ReCaptcha(privateKey: "testing", publicKey: "testing", includeNoScript: false, includeScript: true)

public void testCreateCaptchaHtmlWithLangInOptions() {
def options = [:]
options.lang = "fr"
def html = recap.createRecaptchaHtml(options)
def html = r.createRecaptchaHtml(options)
assertTrue html.contains("<script")
assertTrue html.contains("hl=fr")

options.lang = null
html = recap.createRecaptchaHtml(options)
html = r.createRecaptchaHtml(options)
assertTrue html.contains("<script")
assertFalse html.contains("hl=fr")
}

public void testCheckAnswer() {
buildAndCheckAnswer("true\nnone", false)
}

public void testCheckAnswer_02() {
buildAndCheckAnswer("true\n", true)
}

public void testCheckAnswer_03() {
buildAndCheckAnswer("true", true)
}

public void testCheckAnswer_04() {
buildAndCheckAnswer("false", false)
public void testCheckAnswerSuccess() {
def answer = """{ "success": true }"""
buildAndCheckAnswer(answer, true)
}

public void testCheckAnswer_05() {
buildAndCheckAnswer("nottrue", false)
public void testCheckAnswerFail() {
def answer = """{ "success": false }"""
buildAndCheckAnswer(answer, false)
}

public void testCheckAnswer_06() {
buildAndCheckAnswer("false\nblabla", false)
public void testCheckAnswerInvalidResponse() {
def answer = """{ "foo": "bar" }"""
buildAndCheckAnswer(answer, false)
}

public void testCheckAnswer_07() {
buildAndCheckAnswer("false\nblabla\n\n", false)
public void testCheckAnswerWithErrors() {
def answer = """{
"success": false,
"error-codes": ["missing-input-response"]
}"""
buildAndCheckAnswer(answer, false)
}

private void buildAndCheckAnswer(String postText, boolean expectedValid) {
def mocker = new MockFor(Post.class)
mocker.demand.getQueryString(3..3) { new QueryString() }
mocker.demand.getResponse { new JsonSlurper().parseText("{\"success\":\"${postText}\"}") }
mocker.demand.getResponse { new JsonSlurper().parseText(postText) }
mocker.use {
def response = r.checkAnswer("123.123.123.123", "response")

Expand Down

0 comments on commit 896d94a

Please sign in to comment.