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.

117 lines
3.0KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.CaptchaTest do
  5. use Pleroma.DataCase
  6. import Tesla.Mock
  7. alias Pleroma.Captcha
  8. alias Pleroma.Captcha.Kocaptcha
  9. alias Pleroma.Captcha.Native
  10. @ets_options [:ordered_set, :private, :named_table, {:read_concurrency, true}]
  11. setup do: clear_config([Pleroma.Captcha, :enabled])
  12. describe "Kocaptcha" do
  13. setup do
  14. ets_name = Kocaptcha.Ets
  15. ^ets_name = :ets.new(ets_name, @ets_options)
  16. mock(fn
  17. %{method: :get, url: "https://captcha.kotobank.ch/new"} ->
  18. json(%{
  19. md5: "63615261b77f5354fb8c4e4986477555",
  20. token: "afa1815e14e29355e6c8f6b143a39fa2",
  21. url: "/captchas/afa1815e14e29355e6c8f6b143a39fa2.png"
  22. })
  23. end)
  24. :ok
  25. end
  26. test "new and validate" do
  27. new = Kocaptcha.new()
  28. token = "afa1815e14e29355e6c8f6b143a39fa2"
  29. url = "https://captcha.kotobank.ch/captchas/afa1815e14e29355e6c8f6b143a39fa2.png"
  30. assert %{
  31. answer_data: answer,
  32. token: ^token,
  33. url: ^url,
  34. type: :kocaptcha
  35. } = new
  36. assert Kocaptcha.validate(token, "7oEy8c", answer) == :ok
  37. end
  38. end
  39. describe "Native" do
  40. test "new and validate" do
  41. new = Native.new()
  42. assert %{
  43. answer_data: answer,
  44. token: token,
  45. type: :native,
  46. url: "data:image/png;base64," <> _
  47. } = new
  48. assert is_binary(answer)
  49. assert :ok = Native.validate(token, answer, answer)
  50. assert {:error, :invalid} == Native.validate(token, answer, answer <> "foobar")
  51. end
  52. end
  53. describe "Captcha Wrapper" do
  54. test "validate" do
  55. Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
  56. new = Captcha.new()
  57. assert %{
  58. answer_data: answer,
  59. token: token
  60. } = new
  61. assert is_binary(answer)
  62. assert :ok = Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer)
  63. Cachex.del(:used_captcha_cache, token)
  64. end
  65. test "doesn't validate invalid answer" do
  66. Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
  67. new = Captcha.new()
  68. assert %{
  69. answer_data: answer,
  70. token: token
  71. } = new
  72. assert is_binary(answer)
  73. assert {:error, :invalid_answer_data} =
  74. Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer <> "foobar")
  75. end
  76. test "nil answer_data" do
  77. Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
  78. new = Captcha.new()
  79. assert %{
  80. answer_data: answer,
  81. token: token
  82. } = new
  83. assert is_binary(answer)
  84. assert {:error, :invalid_answer_data} =
  85. Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", nil)
  86. end
  87. end
  88. end