analytics/test/plausible_web/captcha_test.exs
Adam Rutkowski 0fa6b688af
Google APIs integration improvements (#2358)
* Make TestUtils module available in all tests

* Add macros patching the application env in tests

Unfortunately a lot of existing functionality relies on
certain application env setup. This isn't ideal because
the app config is a shared state that prevents us from
running the tests in parallel.

Those macros encapsulate setting up new env for test purposes
and make sure the changes are reverted when the test finishes.

* Allow passing request opts to HTTPClient.post/4

We need this to swap custom request building in
Google Analytics import.

* Unify errors when listing sites

* React: propagate backend error messages if available

* React: catch API errors in Search Terms component

* Propagate google API errors on referrer drilldown

* Handle verified properties errors in SC settings

* Add missing tests for SC settings controller

* Unify errors for fetching search analytics queries (list stats)

* Unify errors refreshing Google Auth Token

* Test fetch_stats/3 errors and replace Double with Mox

* Fixup makrup

* s/class/className

* Simplify Search Terms display in case of errors

* Fix warnings
2022-10-24 09:34:02 +02:00

60 lines
1.5 KiB
Elixir

defmodule PlausibleWeb.CaptchaTest do
use Plausible.DataCase
import Mox
setup :verify_on_exit!
alias PlausibleWeb.Captcha
describe "mocked payloads" do
@failure Jason.decode!(~s/{"success":false,"error-codes":["invalid-input-response"]}/)
@success Jason.decode!(~s/{"success":true}/)
test "returns false for non-success response" do
expect(
Plausible.HTTPClient.Mock,
:post,
fn "https://hcaptcha.com/siteverify",
[{"content-type", "application/x-www-form-urlencoded"}],
%{response: "bad", secret: "scottiger"} ->
{:ok,
%Finch.Response{
status: 200,
headers: [{"content-type", "application/json"}],
body: @failure
}}
end
)
refute Captcha.verify("bad")
end
test "returns true for successful response" do
expect(
Plausible.HTTPClient.Mock,
:post,
fn "https://hcaptcha.com/siteverify",
[{"content-type", "application/x-www-form-urlencoded"}],
%{response: "good", secret: "scottiger"} ->
{:ok,
%Finch.Response{
status: 200,
headers: [{"content-type", "application/json"}],
body: @success
}}
end
)
assert Captcha.verify("good")
end
end
describe "with patched application env" do
setup_patch_env(:hcaptcha, sitekey: nil)
test "returns true when disabled" do
assert Captcha.verify("disabled")
end
end
end