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