Merge branch 'ostatus-controller-no-auth-check-on-non-federating-instances' into...
[akkoma] / test / support / conn_case.ex
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.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 using do
23 quote do
24 # Import conveniences for testing with connections
25 import Plug.Conn
26 import Phoenix.ConnTest
27 use Pleroma.Tests.Helpers
28 import Pleroma.Web.Router.Helpers
29
30 alias Pleroma.Config
31
32 # The default endpoint for testing
33 @endpoint Pleroma.Web.Endpoint
34
35 # Sets up OAuth access with specified scopes
36 defp oauth_access(scopes, opts \\ []) do
37 user =
38 Keyword.get_lazy(opts, :user, fn ->
39 Pleroma.Factory.insert(:user)
40 end)
41
42 token =
43 Keyword.get_lazy(opts, :oauth_token, fn ->
44 Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
45 end)
46
47 conn =
48 build_conn()
49 |> assign(:user, user)
50 |> assign(:token, token)
51
52 %{user: user, token: token, conn: conn}
53 end
54
55 defp request_content_type(%{conn: conn}) do
56 conn = put_req_header(conn, "content-type", "multipart/form-data")
57 [conn: conn]
58 end
59
60 defp empty_json_response(conn) do
61 body = response(conn, 204)
62 response_content_type(conn, :json)
63
64 body
65 end
66
67 defp json_response_and_validate_schema(
68 %{
69 private: %{
70 open_api_spex: %{operation_id: op_id, operation_lookup: lookup, spec: spec}
71 }
72 } = conn,
73 status
74 ) do
75 content_type =
76 conn
77 |> Plug.Conn.get_resp_header("content-type")
78 |> List.first()
79 |> String.split(";")
80 |> List.first()
81
82 status = Plug.Conn.Status.code(status)
83
84 unless lookup[op_id].responses[status] do
85 err = "Response schema not found for #{status} #{conn.method} #{conn.request_path}"
86 flunk(err)
87 end
88
89 schema = lookup[op_id].responses[status].content[content_type].schema
90 json = if status == 204, do: empty_json_response(conn), else: json_response(conn, status)
91
92 case OpenApiSpex.cast_value(json, schema, spec) do
93 {:ok, _data} ->
94 json
95
96 {:error, errors} ->
97 errors =
98 Enum.map(errors, fn error ->
99 message = OpenApiSpex.Cast.Error.message(error)
100 path = OpenApiSpex.Cast.Error.path_to_string(error)
101 "#{message} at #{path}"
102 end)
103
104 flunk(
105 "Response does not conform to schema of #{op_id} operation: #{
106 Enum.join(errors, "\n")
107 }\n#{inspect(json)}"
108 )
109 end
110 end
111
112 defp json_response_and_validate_schema(conn, _status) do
113 flunk("Response schema not found for #{conn.method} #{conn.request_path} #{conn.status}")
114 end
115 end
116 end
117
118 setup tags do
119 Cachex.clear(:user_cache)
120 Cachex.clear(:object_cache)
121 :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
122
123 unless tags[:async] do
124 Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
125 end
126
127 if tags[:needs_streamer] do
128 start_supervised(%{
129 id: Pleroma.Web.Streamer.registry(),
130 start:
131 {Registry, :start_link, [[keys: :duplicate, name: Pleroma.Web.Streamer.registry()]]}
132 })
133 end
134
135 {:ok, conn: Phoenix.ConnTest.build_conn()}
136 end
137 end