aboutsummaryrefslogtreecommitdiff
path: root/test/recaptcha_test.exs
diff options
context:
space:
mode:
authorSam Seay <sam@manuka.co>2016-09-01 09:43:23 +1200
committerGitHub <noreply@github.com>2016-09-01 09:43:23 +1200
commit3b5120b087182cb96a1a2082a5fb149276e45304 (patch)
tree53f44594fbac8a78ddf7d1fec99779ba310f4e00 /test/recaptcha_test.exs
parent7ff101f0fa5aaf52df07d37584472521c4b3cc55 (diff)
downloadrecaptcha-3b5120b087182cb96a1a2082a5fb149276e45304.tar.gz
recaptcha-3b5120b087182cb96a1a2082a5fb149276e45304.tar.xz
Feature/v2 rewrite (#7)
Rewrite of the API. * Change the copyright holder since its a rewrite. * Bump dependencies to their latest versions. * Revision of the README to document the new API * Break config up into keyword list (no longer a map) * A rewrite of the recaptcha verify API and move of templating into Recaptcha.Template * Add tests and credo for code style * Remove exception raising method calls (`HTTPoison.post!` and `Poison.decode!`) * Change Elixir version to 1.2 for `with` support.
Diffstat (limited to 'test/recaptcha_test.exs')
-rw-r--r--test/recaptcha_test.exs49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/recaptcha_test.exs b/test/recaptcha_test.exs
new file mode 100644
index 0000000..ad57772
--- /dev/null
+++ b/test/recaptcha_test.exs
@@ -0,0 +1,49 @@
+defmodule RecaptchaTest do
+ use ExUnit.Case, async: true
+
+ # see https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha-v2-what-should-i-do
+ @google_test_secret "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"
+
+ test "When the supplied g-recaptcha-response is nil, :invalid_input_response is returned" do
+ assert {:error, messages} = Recaptcha.verify(nil)
+ assert messages == [:invalid_input_response]
+ end
+
+ test "When the supplied g-recaptcha-response is not nil, but invalid, multiple errors are returned" do
+ assert {:error, messages} = Recaptcha.verify("not_valid")
+ assert messages == [:invalid_input_response, :invalid_input_secret]
+ end
+
+ test "When the correct secret is supplied but g-recaptcha-response is invalid :invalid_input_response' is returned" do
+ assert {:error, messages} = Recaptcha.verify("not_valid", secret: @google_test_secret)
+ assert messages == [:invalid_input_response]
+ end
+
+ test "When a valid response is supplied, a success response is returned" do
+ assert {:ok, %{challenge_ts: _, hostname: _}} = Recaptcha.verify("valid_response", secret: "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe")
+ end
+
+ test "When secret is not overridden the configured secret is used" do
+ Recaptcha.verify("valid_response")
+
+ assert_received {:request_verification, "response=valid_response&secret=test_secret", _}
+ end
+
+ test "When the timeout is overridden that config is passed to verify/2 as an option" do
+ Recaptcha.verify("valid_response", timeout: 25000)
+
+ assert_received {:request_verification, _, [timeout: 25000]}
+ end
+
+ test "Remote IP is used in the request body when it is passed into verify/2 as an option" do
+ Recaptcha.verify("valid_response", remote_ip: "192.168.1.1")
+
+ assert_received {:request_verification, "response=valid_response&secret=test_secret&remote_ip=192.168.1.1", _}
+ end
+
+ test "Adding unsupported options does not append them to the request body" do
+ Recaptcha.verify("valid_response", unsupported_option: "not_valid")
+
+ assert_received {:request_verification, "response=valid_response&secret=test_secret", _}
+ end
+end