Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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 json_response_and_validate_schema(conn, status \\ nil) do
55 content_type =
56 conn
57 |> Plug.Conn.get_resp_header("content-type")
58 |> List.first()
59 |> String.split(";")
60 |> List.first()
61
62 status = status || conn.status
63
64 %{private: %{open_api_spex: %{operation_id: op_id, operation_lookup: lookup, spec: spec}}} =
65 conn
66
67 schema = lookup[op_id].responses[status].content[content_type].schema
68 json = json_response(conn, status)
69
70 case OpenApiSpex.cast_value(json, schema, spec) do
71 {:ok, _data} ->
72 json
73
74 {:error, errors} ->
75 errors =
76 Enum.map(errors, fn error ->
77 message = OpenApiSpex.Cast.Error.message(error)
78 path = OpenApiSpex.Cast.Error.path_to_string(error)
79 "#{message} at #{path}"
80 end)
81
82 flunk(
83 "Response does not conform to schema of #{op_id} operation: #{
84 Enum.join(errors, "\n")
85 }\n#{inspect(json)}"
86 )
87 end
88 end
89
90 defp ensure_federating_or_authenticated(conn, url, user) do
91 initial_setting = Config.get([:instance, :federating])
92 on_exit(fn -> Config.put([:instance, :federating], initial_setting) end)
93
94 Config.put([:instance, :federating], false)
95
96 conn
97 |> get(url)
98 |> response(403)
99
100 conn
101 |> assign(:user, user)
102 |> get(url)
103 |> response(200)
104
105 Config.put([:instance, :federating], true)
106
107 conn
108 |> get(url)
109 |> response(200)
110 end
111 end
112 end
113
114 setup tags do
115 Cachex.clear(:user_cache)
116 Cachex.clear(:object_cache)
117 :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
118
119 unless tags[:async] do
120 Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
121 end
122
123 if tags[:needs_streamer] do
124 start_supervised(Pleroma.Web.Streamer.supervisor())
125 end
126
127 {:ok, conn: Phoenix.ConnTest.build_conn()}
128 end
129 end