logo

pleroma

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

idempotency_plug_test.exs (3254B)


  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.Web.Plugs.IdempotencyPlugTest do
  5. # Relies on Cachex, has to stay synchronous
  6. use Pleroma.DataCase
  7. use Plug.Test
  8. alias Pleroma.Web.Plugs.IdempotencyPlug
  9. alias Plug.Conn
  10. test "returns result from cache" do
  11. key = "test1"
  12. orig_request_id = "test1"
  13. second_request_id = "test2"
  14. body = "testing"
  15. status = 200
  16. :post
  17. |> conn("/cofe")
  18. |> put_req_header("idempotency-key", key)
  19. |> Conn.put_resp_header("x-request-id", orig_request_id)
  20. |> Conn.put_resp_content_type("application/json")
  21. |> IdempotencyPlug.call([])
  22. |> Conn.send_resp(status, body)
  23. conn =
  24. :post
  25. |> conn("/cofe")
  26. |> put_req_header("idempotency-key", key)
  27. |> Conn.put_resp_header("x-request-id", second_request_id)
  28. |> Conn.put_resp_content_type("application/json")
  29. |> IdempotencyPlug.call([])
  30. assert_raise Conn.AlreadySentError, fn ->
  31. Conn.send_resp(conn, :im_a_teapot, "no cofe")
  32. end
  33. assert conn.resp_body == body
  34. assert conn.status == status
  35. assert [^second_request_id] = Conn.get_resp_header(conn, "x-request-id")
  36. assert [^orig_request_id] = Conn.get_resp_header(conn, "x-original-request-id")
  37. assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
  38. assert ["true"] = Conn.get_resp_header(conn, "idempotent-replayed")
  39. assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn, "content-type")
  40. end
  41. test "pass conn downstream if the cache not found" do
  42. key = "test2"
  43. orig_request_id = "test3"
  44. body = "testing"
  45. status = 200
  46. conn =
  47. :post
  48. |> conn("/cofe")
  49. |> put_req_header("idempotency-key", key)
  50. |> Conn.put_resp_header("x-request-id", orig_request_id)
  51. |> Conn.put_resp_content_type("application/json")
  52. |> IdempotencyPlug.call([])
  53. |> Conn.send_resp(status, body)
  54. assert conn.resp_body == body
  55. assert conn.status == status
  56. assert [] = Conn.get_resp_header(conn, "idempotent-replayed")
  57. assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
  58. end
  59. test "passes conn downstream if idempotency is not present in headers" do
  60. orig_request_id = "test4"
  61. body = "testing"
  62. status = 200
  63. conn =
  64. :post
  65. |> conn("/cofe")
  66. |> Conn.put_resp_header("x-request-id", orig_request_id)
  67. |> Conn.put_resp_content_type("application/json")
  68. |> IdempotencyPlug.call([])
  69. |> Conn.send_resp(status, body)
  70. assert [] = Conn.get_resp_header(conn, "idempotency-key")
  71. end
  72. test "doesn't work with GET/DELETE" do
  73. key = "test3"
  74. body = "testing"
  75. status = 200
  76. conn =
  77. :get
  78. |> conn("/cofe")
  79. |> put_req_header("idempotency-key", key)
  80. |> IdempotencyPlug.call([])
  81. |> Conn.send_resp(status, body)
  82. assert [] = Conn.get_resp_header(conn, "idempotency-key")
  83. conn =
  84. :delete
  85. |> conn("/cofe")
  86. |> put_req_header("idempotency-key", key)
  87. |> IdempotencyPlug.call([])
  88. |> Conn.send_resp(status, body)
  89. assert [] = Conn.get_resp_header(conn, "idempotency-key")
  90. end
  91. end