akkoma/test/pleroma/web/plugs/idempotency_plug_test.exs

112 lines
3.2 KiB
Elixir
Raw Normal View History

2019-06-26 05:36:42 -06:00
# Pleroma: A lightweight social networking server
2022-02-25 23:11:42 -07:00
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
2019-06-26 05:36:42 -06:00
# SPDX-License-Identifier: AGPL-3.0-only
2020-06-23 09:16:47 -06:00
defmodule Pleroma.Web.Plugs.IdempotencyPlugTest do
# Relies on Cachex, has to stay synchronous
use Pleroma.DataCase
2019-06-26 05:36:42 -06:00
use Plug.Test
2020-06-24 01:09:39 -06:00
alias Pleroma.Web.Plugs.IdempotencyPlug
2019-06-26 05:36:42 -06:00
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)
2019-06-26 12:53:36 -06:00
conn =
2019-06-26 05:36:42 -06:00
: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 ->
2019-06-26 12:53:36 -06:00
Conn.send_resp(conn, :im_a_teapot, "no cofe")
2019-06-26 05:36:42 -06:00
end
2019-06-26 12:53:36 -06:00
assert conn.resp_body == body
assert conn.status == status
2019-06-26 05:36:42 -06:00
2019-06-26 12:53:36 -06:00
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")
2019-06-26 05:36:42 -06:00
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