Prefer naming this function build_image_url/2
[akkoma] / test / support / conn_case.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ConnCase do
6 @moduledoc """
7 This module defines the test case to be used by
8 tests that require setting up a connection.
9
10 Such tests rely on `Phoenix.ConnTest` and also
11 import other functionality to make it easier
12 to build common datastructures and query the data layer.
13
14 Finally, if the test case interacts with the database,
15 it cannot be async. For this reason, every test runs
16 inside a transaction which is reset at the beginning
17 of the test unless the test case is marked as async.
18 """
19
20 use ExUnit.CaseTemplate
21
22 alias Pleroma.DataCase
23
24 using do
25 quote do
26 # Import conveniences for testing with connections
27 import Plug.Conn
28 import Phoenix.ConnTest
29 use Pleroma.Tests.Helpers
30 import Pleroma.Web.Router.Helpers
31
32 alias Pleroma.Config
33
34 # The default endpoint for testing
35 @endpoint Pleroma.Web.Endpoint
36
37 # Sets up OAuth access with specified scopes
38 defp oauth_access(scopes, opts \\ []) do
39 user =
40 Keyword.get_lazy(opts, :user, fn ->
41 Pleroma.Factory.insert(:user)
42 end)
43
44 token =
45 Keyword.get_lazy(opts, :oauth_token, fn ->
46 Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
47 end)
48
49 conn =
50 build_conn()
51 |> assign(:user, user)
52 |> assign(:token, token)
53
54 %{user: user, token: token, conn: conn}
55 end
56
57 defp request_content_type(%{conn: conn}) do
58 conn = put_req_header(conn, "content-type", "multipart/form-data")
59 [conn: conn]
60 end
61
62 defp empty_json_response(conn) do
63 body = response(conn, 204)
64 response_content_type(conn, :json)
65
66 body
67 end
68
69 defp json_response_and_validate_schema(
70 %{
71 private: %{
72 open_api_spex: %{operation_id: op_id, operation_lookup: lookup, spec: spec}
73 }
74 } = conn,
75 status
76 ) do
77 content_type =
78 conn
79 |> Plug.Conn.get_resp_header("content-type")
80 |> List.first()
81 |> String.split(";")
82 |> List.first()
83
84 status = Plug.Conn.Status.code(status)
85
86 unless lookup[op_id].responses[status] do
87 err = "Response schema not found for #{status} #{conn.method} #{conn.request_path}"
88 flunk(err)
89 end
90
91 schema = lookup[op_id].responses[status].content[content_type].schema
92 json = if status == 204, do: empty_json_response(conn), else: json_response(conn, status)
93
94 case OpenApiSpex.cast_value(json, schema, spec) do
95 {:ok, _data} ->
96 json
97
98 {:error, errors} ->
99 errors =
100 Enum.map(errors, fn error ->
101 message = OpenApiSpex.Cast.Error.message(error)
102 path = OpenApiSpex.Cast.Error.path_to_string(error)
103 "#{message} at #{path}"
104 end)
105
106 flunk(
107 "Response does not conform to schema of #{op_id} operation: #{
108 Enum.join(errors, "\n")
109 }\n#{inspect(json)}"
110 )
111 end
112 end
113
114 defp json_response_and_validate_schema(conn, _status) do
115 flunk("Response schema not found for #{conn.method} #{conn.request_path} #{conn.status}")
116 end
117 end
118 end
119
120 setup tags do
121 DataCase.setup_multi_process_mode(tags)
122 DataCase.setup_streamer(tags)
123 DataCase.stub_pipeline()
124
125 Mox.verify_on_exit!()
126
127 {:ok, conn: Phoenix.ConnTest.build_conn()}
128 end
129 end