- token: "afa1815e14e29355e6c8f6b143a39fa2",
- url: "http://localhost:9093/captchas/afa1815e14e29355e6c8f6b143a39fa2.png"
- }
-
- assert Pleroma.Captcha.Kocaptcha.validate(
- "afa1815e14e29355e6c8f6b143a39fa2",
- "7oEy8c"
- )
+ seconds_valid: 300
+ } = new
+
+ assert Kocaptcha.validate(token, "7oEy8c", answer) == :ok
+ end
+ end
+
+ describe "Native" do
+ test "new and validate" do
+ new = Native.new()
+
+ assert %{
+ answer_data: answer,
+ token: token,
+ type: :native,
+ url: "data:image/png;base64," <> _,
+ seconds_valid: 300
+ } = new
+
+ assert is_binary(answer)
+ assert :ok = Native.validate(token, answer, answer)
+ assert {:error, :invalid} == Native.validate(token, answer, answer <> "foobar")
+ end
+ end
+
+ describe "Captcha Wrapper" do
+ test "validate" do
+ Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
+
+ new = Captcha.new()
+
+ assert %{
+ answer_data: answer,
+ token: token
+ } = new
+
+ assert is_binary(answer)
+ assert :ok = Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer)
+ Cachex.del(:used_captcha_cache, token)
+ end
+
+ test "doesn't validate invalid answer" do
+ Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
+
+ new = Captcha.new()
+
+ assert %{
+ answer_data: answer,
+ token: token
+ } = new
+
+ assert is_binary(answer)
+
+ assert {:error, :invalid_answer_data} =
+ Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer <> "foobar")
+ end
+
+ test "nil answer_data" do
+ Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
+
+ new = Captcha.new()
+
+ assert %{
+ answer_data: answer,
+ token: token
+ } = new
+
+ assert is_binary(answer)
+
+ assert {:error, :invalid_answer_data} =
+ Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", nil)