logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

captcha_test.exs (3032B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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. clear_config([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. end
  66. test "doesn't validate invalid answer" do
  67. clear_config([Pleroma.Captcha, :enabled], true)
  68. new = Captcha.new()
  69. assert %{
  70. answer_data: answer,
  71. token: token
  72. } = new
  73. assert is_binary(answer)
  74. assert {:error, :invalid_answer_data} =
  75. Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer <> "foobar")
  76. end
  77. test "nil answer_data" do
  78. clear_config([Pleroma.Captcha, :enabled], true)
  79. new = Captcha.new()
  80. assert %{
  81. answer_data: answer,
  82. token: token
  83. } = new
  84. assert is_binary(answer)
  85. assert {:error, :invalid_answer_data} =
  86. Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", nil)
  87. end
  88. end
  89. end