logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

captcha_test.exs (3095B)


  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. seconds_valid: 300
  36. } = new
  37. assert Kocaptcha.validate(token, "7oEy8c", answer) == :ok
  38. end
  39. end
  40. describe "Native" do
  41. test "new and validate" do
  42. new = Native.new()
  43. assert %{
  44. answer_data: answer,
  45. token: token,
  46. type: :native,
  47. url: "data:image/png;base64," <> _,
  48. seconds_valid: 300
  49. } = new
  50. assert is_binary(answer)
  51. assert :ok = Native.validate(token, answer, answer)
  52. assert {:error, :invalid} == Native.validate(token, answer, answer <> "foobar")
  53. end
  54. end
  55. describe "Captcha Wrapper" do
  56. test "validate" do
  57. Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
  58. new = Captcha.new()
  59. assert %{
  60. answer_data: answer,
  61. token: token
  62. } = new
  63. assert is_binary(answer)
  64. assert :ok = Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer)
  65. Cachex.del(:used_captcha_cache, token)
  66. end
  67. test "doesn't validate invalid answer" do
  68. Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
  69. new = Captcha.new()
  70. assert %{
  71. answer_data: answer,
  72. token: token
  73. } = new
  74. assert is_binary(answer)
  75. assert {:error, :invalid_answer_data} =
  76. Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer <> "foobar")
  77. end
  78. test "nil answer_data" do
  79. Pleroma.Config.put([Pleroma.Captcha, :enabled], true)
  80. new = Captcha.new()
  81. assert %{
  82. answer_data: answer,
  83. token: token
  84. } = new
  85. assert is_binary(answer)
  86. assert {:error, :invalid_answer_data} =
  87. Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", nil)
  88. end
  89. end
  90. end