8099461cc3cdb6dc7f5096da614707fcc3b5a922
[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 use Phoenix.ConnTest
26 use Pleroma.Tests.Helpers
27 import Pleroma.Web.Router.Helpers
28
29 alias Pleroma.Config
30
31 # The default endpoint for testing
32 @endpoint Pleroma.Web.Endpoint
33
34 # Sets up OAuth access with specified scopes
35 defp oauth_access(scopes, opts \\ []) do
36 user =
37 Keyword.get_lazy(opts, :user, fn ->
38 Pleroma.Factory.insert(:user)
39 end)
40
41 token =
42 Keyword.get_lazy(opts, :oauth_token, fn ->
43 Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
44 end)
45
46 conn =
47 build_conn()
48 |> assign(:user, user)
49 |> assign(:token, token)
50
51 %{user: user, token: token, conn: conn}
52 end
53
54 defp request_content_type(%{conn: conn}) do
55 conn = put_req_header(conn, "content-type", "multipart/form-data")
56 [conn: conn]
57 end
58
59 defp json_response_and_validate_schema(conn, status \\ nil) do
60 content_type =
61 conn
62 |> Plug.Conn.get_resp_header("content-type")
63 |> List.first()
64 |> String.split(";")
65 |> List.first()
66
67 status = status || conn.status
68
69 %{private: %{open_api_spex: %{operation_id: op_id, operation_lookup: lookup, spec: spec}}} =
70 conn
71
72 schema = lookup[op_id].responses[status].content[content_type].schema
73 json = json_response(conn, status)
74
75 case OpenApiSpex.cast_value(json, schema, spec) do
76 {:ok, _data} ->
77 json
78
79 {:error, errors} ->
80 errors =
81 Enum.map(errors, fn error ->
82 message = OpenApiSpex.Cast.Error.message(error)
83 path = OpenApiSpex.Cast.Error.path_to_string(error)
84 "#{message} at #{path}"
85 end)
86
87 flunk(
88 "Response does not conform to schema of #{op_id} operation: #{
89 Enum.join(errors, "\n")
90 }\n#{inspect(json)}"
91 )
92 end
93 end
94
95 defp ensure_federating_or_authenticated(conn, url, user) do
96 initial_setting = Config.get([:instance, :federating])
97 on_exit(fn -> Config.put([:instance, :federating], initial_setting) end)
98
99 Config.put([:instance, :federating], false)
100
101 conn
102 |> get(url)
103 |> response(403)
104
105 conn
106 |> assign(:user, user)
107 |> get(url)
108 |> response(200)
109
110 Config.put([:instance, :federating], true)
111
112 conn
113 |> get(url)
114 |> response(200)
115 end
116 end
117 end
118
119 setup tags do
120 Cachex.clear(:user_cache)
121 Cachex.clear(:object_cache)
122 :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
123
124 unless tags[:async] do
125 Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
126 end
127
128 if tags[:needs_streamer] do
129 start_supervised(Pleroma.Web.Streamer.supervisor())
130 end
131
132 {:ok, conn: Phoenix.ConnTest.build_conn()}
133 end
134 end