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.

152 lines
4.2KB

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