Merge branch 'fix_for_migrate_to_db_test' into 'develop'
[akkoma] / test / captcha_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.CaptchaTest do
6 use ExUnit.Case
7
8 import Tesla.Mock
9
10 alias Pleroma.Captcha.Kocaptcha
11 alias Pleroma.Captcha.Native
12
13 @ets_options [:ordered_set, :private, :named_table, {:read_concurrency, true}]
14
15 describe "Kocaptcha" do
16 setup do
17 ets_name = Kocaptcha.Ets
18 ^ets_name = :ets.new(ets_name, @ets_options)
19
20 mock(fn
21 %{method: :get, url: "https://captcha.kotobank.ch/new"} ->
22 json(%{
23 md5: "63615261b77f5354fb8c4e4986477555",
24 token: "afa1815e14e29355e6c8f6b143a39fa2",
25 url: "/captchas/afa1815e14e29355e6c8f6b143a39fa2.png"
26 })
27 end)
28
29 :ok
30 end
31
32 test "new and validate" do
33 new = Kocaptcha.new()
34 assert new[:type] == :kocaptcha
35 assert new[:token] == "afa1815e14e29355e6c8f6b143a39fa2"
36
37 assert new[:url] ==
38 "https://captcha.kotobank.ch/captchas/afa1815e14e29355e6c8f6b143a39fa2.png"
39
40 assert Kocaptcha.validate(
41 new[:token],
42 "7oEy8c",
43 new[:answer_data]
44 ) == :ok
45 end
46 end
47
48 describe "Native" do
49 test "new and validate" do
50 new = Native.new()
51
52 assert %{
53 answer_data: answer,
54 token: token,
55 type: :native,
56 url: "data:image/png;base64," <> _
57 } = new
58
59 assert is_binary(answer)
60 assert :ok = Native.validate(token, answer, answer)
61 assert {:error, "Invalid CAPTCHA"} == Native.validate(token, answer, answer <> "foobar")
62 end
63 end
64 end