1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.CaptchaTest do
11 alias Pleroma.Captcha.Kocaptcha
12 alias Pleroma.Captcha.Native
14 @ets_options [:ordered_set, :private, :named_table, {:read_concurrency, true}]
15 setup do: clear_config([Pleroma.Captcha, :enabled])
17 describe "Kocaptcha" do
19 ets_name = Kocaptcha.Ets
20 ^ets_name = :ets.new(ets_name, @ets_options)
23 %{method: :get, url: "https://captcha.kotobank.ch/new"} ->
25 md5: "63615261b77f5354fb8c4e4986477555",
26 token: "afa1815e14e29355e6c8f6b143a39fa2",
27 url: "/captchas/afa1815e14e29355e6c8f6b143a39fa2.png"
34 test "new and validate" do
37 token = "afa1815e14e29355e6c8f6b143a39fa2"
38 url = "https://captcha.kotobank.ch/captchas/afa1815e14e29355e6c8f6b143a39fa2.png"
48 assert Kocaptcha.validate(token, "7oEy8c", answer) == :ok
53 test "new and validate" do
60 url: "data:image/png;base64," <> _,
64 assert is_binary(answer)
65 assert :ok = Native.validate(token, answer, answer)
66 assert {:error, :invalid} == Native.validate(token, answer, answer <> "foobar")
70 describe "Captcha Wrapper" do
72 Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
81 assert is_binary(answer)
82 assert :ok = Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer)
83 Cachex.del(:used_captcha_cache, token)
86 test "doesn't validate invalid answer" do
87 Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
96 assert is_binary(answer)
98 assert {:error, :invalid_answer_data} =
99 Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer <> "foobar")
102 test "nil answer_data" do
103 Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
112 assert is_binary(answer)
114 assert {:error, :invalid_answer_data} =
115 Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", nil)