First
[anni] / test / pleroma / web / plugs / digest_plug_test.exs
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
5 defmodule Pleroma.Web.Plugs.DigestPlugTest do
6   use ExUnit.Case, async: true
7   use Plug.Test
8
9   test "digest algorithm is taken from digest header" do
10     body = "{\"hello\": \"world\"}"
11     digest = "X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE="
12
13     {:ok, ^body, conn} =
14       :get
15       |> conn("/", body)
16       |> put_req_header("content-type", "application/json")
17       |> put_req_header("digest", "sha-256=" <> digest)
18       |> Pleroma.Web.Plugs.DigestPlug.read_body([])
19
20     assert conn.assigns[:digest] == "sha-256=" <> digest
21
22     {:ok, ^body, conn} =
23       :get
24       |> conn("/", body)
25       |> put_req_header("content-type", "application/json")
26       |> put_req_header("digest", "SHA-256=" <> digest)
27       |> Pleroma.Web.Plugs.DigestPlug.read_body([])
28
29     assert conn.assigns[:digest] == "SHA-256=" <> digest
30   end
31
32   test "error if digest algorithm is invalid" do
33     body = "{\"hello\": \"world\"}"
34     digest = "X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE="
35
36     assert_raise ArgumentError, "invalid value for digest algorithm, got: MD5", fn ->
37       :get
38       |> conn("/", body)
39       |> put_req_header("content-type", "application/json")
40       |> put_req_header("digest", "MD5=" <> digest)
41       |> Pleroma.Web.Plugs.DigestPlug.read_body([])
42     end
43
44     assert_raise ArgumentError, "invalid value for digest algorithm, got: md5", fn ->
45       :get
46       |> conn("/", body)
47       |> put_req_header("content-type", "application/json")
48       |> put_req_header("digest", "md5=" <> digest)
49       |> Pleroma.Web.Plugs.DigestPlug.read_body([])
50     end
51   end
52 end