diff options
author | Sam Seay <sam@manuka.co> | 2016-09-01 09:43:23 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-01 09:43:23 +1200 |
commit | 3b5120b087182cb96a1a2082a5fb149276e45304 (patch) | |
tree | 53f44594fbac8a78ddf7d1fec99779ba310f4e00 /test | |
parent | 7ff101f0fa5aaf52df07d37584472521c4b3cc55 (diff) | |
download | recaptcha-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')
-rw-r--r-- | test/recaptcha/template_test.exs | 25 | ||||
-rw-r--r-- | test/recaptcha_test.exs | 49 | ||||
-rw-r--r-- | test/test_helper.exs | 1 |
3 files changed, 75 insertions, 0 deletions
diff --git a/test/recaptcha/template_test.exs b/test/recaptcha/template_test.exs new file mode 100644 index 0000000..335eb5f --- /dev/null +++ b/test/recaptcha/template_test.exs @@ -0,0 +1,25 @@ +defmodule RecaptchaTemplateTest do + use ExUnit.Case, async: true + + test "supplying options to display/1 renders them in the g-recaptcha div" do + template_string = Recaptcha.Template.display(theme: "dark", type: "audio", tabindex: 1, size: "compact") + + assert template_string =~ "data-theme=\"dark\"" + assert template_string =~ "data-type=\"audio\"" + assert template_string =~ "data-tabindex=\"1\"" + assert template_string =~ "data-size=\"compact\"" + end + + test "supplying a public key in options to display/1 overrides it in the g-recaptcha-div" do + template_string = Recaptcha.Template.display(public_key: "override_test_public_key") + + assert template_string =~ "data-sitekey=\"override_test_public_key\"" + end + + test "supplying noscript option displays the noscript fallback" do + template_string = Recaptcha.Template.display(noscript: true) + + assert template_string =~ "<noscript>" + assert template_string =~ "https://www.google.com/recaptcha/api/fallback?k=test_public_key" + end +end 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 diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() |