summaryrefslogtreecommitdiff
path: root/test/pleroma/web/plugs/idempotency_plug_test.exs
blob: 4a7835993324159dda0e11b170189f4abf200c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.Plugs.IdempotencyPlugTest do
  use ExUnit.Case, async: true
  use Plug.Test

  alias Pleroma.Web.Plugs.IdempotencyPlug
  alias Plug.Conn

  test "returns result from cache" do
    key = "test1"
    orig_request_id = "test1"
    second_request_id = "test2"
    body = "testing"
    status = 200

    :post
    |> conn("/cofe")
    |> put_req_header("idempotency-key", key)
    |> Conn.put_resp_header("x-request-id", orig_request_id)
    |> Conn.put_resp_content_type("application/json")
    |> IdempotencyPlug.call([])
    |> Conn.send_resp(status, body)

    conn =
      :post
      |> conn("/cofe")
      |> put_req_header("idempotency-key", key)
      |> Conn.put_resp_header("x-request-id", second_request_id)
      |> Conn.put_resp_content_type("application/json")
      |> IdempotencyPlug.call([])

    assert_raise Conn.AlreadySentError, fn ->
      Conn.send_resp(conn, :im_a_teapot, "no cofe")
    end

    assert conn.resp_body == body
    assert conn.status == status

    assert [^second_request_id] = Conn.get_resp_header(conn, "x-request-id")
    assert [^orig_request_id] = Conn.get_resp_header(conn, "x-original-request-id")
    assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
    assert ["true"] = Conn.get_resp_header(conn, "idempotent-replayed")
    assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn, "content-type")
  end

  test "pass conn downstream if the cache not found" do
    key = "test2"
    orig_request_id = "test3"
    body = "testing"
    status = 200

    conn =
      :post
      |> conn("/cofe")
      |> put_req_header("idempotency-key", key)
      |> Conn.put_resp_header("x-request-id", orig_request_id)
      |> Conn.put_resp_content_type("application/json")
      |> IdempotencyPlug.call([])
      |> Conn.send_resp(status, body)

    assert conn.resp_body == body
    assert conn.status == status

    assert [] = Conn.get_resp_header(conn, "idempotent-replayed")
    assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
  end

  test "passes conn downstream if idempotency is not present in headers" do
    orig_request_id = "test4"
    body = "testing"
    status = 200

    conn =
      :post
      |> conn("/cofe")
      |> Conn.put_resp_header("x-request-id", orig_request_id)
      |> Conn.put_resp_content_type("application/json")
      |> IdempotencyPlug.call([])
      |> Conn.send_resp(status, body)

    assert [] = Conn.get_resp_header(conn, "idempotency-key")
  end

  test "doesn't work with GET/DELETE" do
    key = "test3"
    body = "testing"
    status = 200

    conn =
      :get
      |> conn("/cofe")
      |> put_req_header("idempotency-key", key)
      |> IdempotencyPlug.call([])
      |> Conn.send_resp(status, body)

    assert [] = Conn.get_resp_header(conn, "idempotency-key")

    conn =
      :delete
      |> conn("/cofe")
      |> put_req_header("idempotency-key", key)
      |> IdempotencyPlug.call([])
      |> Conn.send_resp(status, body)

    assert [] = Conn.get_resp_header(conn, "idempotency-key")
  end
end