Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.9KB

  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. defmodule Pleroma.Web.ConnCase do
  5. @moduledoc """
  6. This module defines the test case to be used by
  7. tests that require setting up a connection.
  8. Such tests rely on `Phoenix.ConnTest` and also
  9. import other functionality to make it easier
  10. to build common datastructures and query the data layer.
  11. Finally, if the test case interacts with the database,
  12. it cannot be async. For this reason, every test runs
  13. inside a transaction which is reset at the beginning
  14. of the test unless the test case is marked as async.
  15. """
  16. use ExUnit.CaseTemplate
  17. using do
  18. quote do
  19. # Import conveniences for testing with connections
  20. import Plug.Conn
  21. import Phoenix.ConnTest
  22. use Pleroma.Tests.Helpers
  23. import Pleroma.Web.Router.Helpers
  24. alias Pleroma.Config
  25. # The default endpoint for testing
  26. @endpoint Pleroma.Web.Endpoint
  27. # Sets up OAuth access with specified scopes
  28. defp oauth_access(scopes, opts \\ []) do
  29. user =
  30. Keyword.get_lazy(opts, :user, fn ->
  31. Pleroma.Factory.insert(:user)
  32. end)
  33. token =
  34. Keyword.get_lazy(opts, :oauth_token, fn ->
  35. Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
  36. end)
  37. conn =
  38. build_conn()
  39. |> assign(:user, user)
  40. |> assign(:token, token)
  41. %{user: user, token: token, conn: conn}
  42. end
  43. defp request_content_type(%{conn: conn}) do
  44. conn = put_req_header(conn, "content-type", "multipart/form-data")
  45. [conn: conn]
  46. end
  47. defp empty_json_response(conn) do
  48. body = response(conn, 204)
  49. response_content_type(conn, :json)
  50. body
  51. end
  52. defp json_response_and_validate_schema(
  53. %{
  54. private: %{
  55. open_api_spex: %{operation_id: op_id, operation_lookup: lookup, spec: spec}
  56. }
  57. } = conn,
  58. status
  59. ) do
  60. content_type =
  61. conn
  62. |> Plug.Conn.get_resp_header("content-type")
  63. |> List.first()
  64. |> String.split(";")
  65. |> List.first()
  66. status = Plug.Conn.Status.code(status)
  67. unless lookup[op_id].responses[status] do
  68. err = "Response schema not found for #{status} #{conn.method} #{conn.request_path}"
  69. flunk(err)
  70. end
  71. schema = lookup[op_id].responses[status].content[content_type].schema
  72. json = if status == 204, do: empty_json_response(conn), else: json_response(conn, status)
  73. case OpenApiSpex.cast_value(json, schema, spec) do
  74. {:ok, _data} ->
  75. json
  76. {:error, errors} ->
  77. errors =
  78. Enum.map(errors, fn error ->
  79. message = OpenApiSpex.Cast.Error.message(error)
  80. path = OpenApiSpex.Cast.Error.path_to_string(error)
  81. "#{message} at #{path}"
  82. end)
  83. flunk(
  84. "Response does not conform to schema of #{op_id} operation: #{
  85. Enum.join(errors, "\n")
  86. }\n#{inspect(json)}"
  87. )
  88. end
  89. end
  90. defp json_response_and_validate_schema(conn, _status) do
  91. flunk("Response schema not found for #{conn.method} #{conn.request_path} #{conn.status}")
  92. end
  93. end
  94. end
  95. setup tags do
  96. Cachex.clear(:user_cache)
  97. Cachex.clear(:object_cache)
  98. :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
  99. unless tags[:async] do
  100. Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
  101. end
  102. if tags[:needs_streamer] do
  103. start_supervised(%{
  104. id: Pleroma.Web.Streamer.registry(),
  105. start:
  106. {Registry, :start_link, [[keys: :duplicate, name: Pleroma.Web.Streamer.registry()]]}
  107. })
  108. end
  109. {:ok, conn: Phoenix.ConnTest.build_conn()}
  110. end
  111. end