Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.7KB

  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. defmodule Pleroma.CaptchaTest do
  5. use ExUnit.Case
  6. import Tesla.Mock
  7. alias Pleroma.Captcha.Kocaptcha
  8. alias Pleroma.Captcha.Native
  9. @ets_options [:ordered_set, :private, :named_table, {:read_concurrency, true}]
  10. describe "Kocaptcha" do
  11. setup do
  12. ets_name = Kocaptcha.Ets
  13. ^ets_name = :ets.new(ets_name, @ets_options)
  14. mock(fn
  15. %{method: :get, url: "https://captcha.kotobank.ch/new"} ->
  16. json(%{
  17. md5: "63615261b77f5354fb8c4e4986477555",
  18. token: "afa1815e14e29355e6c8f6b143a39fa2",
  19. url: "/captchas/afa1815e14e29355e6c8f6b143a39fa2.png"
  20. })
  21. end)
  22. :ok
  23. end
  24. test "new and validate" do
  25. new = Kocaptcha.new()
  26. assert new[:type] == :kocaptcha
  27. assert new[:token] == "afa1815e14e29355e6c8f6b143a39fa2"
  28. assert new[:url] ==
  29. "https://captcha.kotobank.ch/captchas/afa1815e14e29355e6c8f6b143a39fa2.png"
  30. assert Kocaptcha.validate(
  31. new[:token],
  32. "7oEy8c",
  33. new[:answer_data]
  34. ) == :ok
  35. end
  36. end
  37. describe "Native" do
  38. test "new and validate" do
  39. new = Native.new()
  40. assert %{
  41. answer_data: answer,
  42. token: token,
  43. type: :native,
  44. url: "data:image/png;base64," <> _
  45. } = new
  46. assert is_binary(answer)
  47. assert :ok = Native.validate(token, answer, answer)
  48. assert {:error, "Invalid CAPTCHA"} == Native.validate(token, answer, answer <> "foobar")
  49. end
  50. end
  51. end