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.

128 lines
3.6KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 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. alias Pleroma.DataCase
  18. using do
  19. quote do
  20. # Import conveniences for testing with connections
  21. import Plug.Conn
  22. import Phoenix.ConnTest
  23. use Pleroma.Tests.Helpers
  24. import Pleroma.Web.Router.Helpers
  25. alias Pleroma.Config
  26. # The default endpoint for testing
  27. @endpoint Pleroma.Web.Endpoint
  28. # Sets up OAuth access with specified scopes
  29. defp oauth_access(scopes, opts \\ []) do
  30. user =
  31. Keyword.get_lazy(opts, :user, fn ->
  32. Pleroma.Factory.insert(:user)
  33. end)
  34. token =
  35. Keyword.get_lazy(opts, :oauth_token, fn ->
  36. Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
  37. end)
  38. conn =
  39. build_conn()
  40. |> assign(:user, user)
  41. |> assign(:token, token)
  42. %{user: user, token: token, conn: conn}
  43. end
  44. defp request_content_type(%{conn: conn}) do
  45. conn = put_req_header(conn, "content-type", "multipart/form-data")
  46. [conn: conn]
  47. end
  48. defp empty_json_response(conn) do
  49. body = response(conn, 204)
  50. response_content_type(conn, :json)
  51. body
  52. end
  53. defp json_response_and_validate_schema(
  54. %{private: %{operation_id: op_id}} = conn,
  55. status
  56. ) do
  57. {spec, lookup} = OpenApiSpex.Plug.PutApiSpec.get_spec_and_operation_lookup(conn)
  58. content_type =
  59. conn
  60. |> Plug.Conn.get_resp_header("content-type")
  61. |> List.first()
  62. |> String.split(";")
  63. |> List.first()
  64. status = Plug.Conn.Status.code(status)
  65. unless lookup[op_id].responses[status] do
  66. err = "Response schema not found for #{status} #{conn.method} #{conn.request_path}"
  67. flunk(err)
  68. end
  69. schema = lookup[op_id].responses[status].content[content_type].schema
  70. json = if status == 204, do: empty_json_response(conn), else: json_response(conn, status)
  71. case OpenApiSpex.cast_value(json, schema, spec) do
  72. {:ok, _data} ->
  73. json
  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. flunk(
  82. "Response does not conform to schema of #{op_id} operation: #{
  83. Enum.join(errors, "\n")
  84. }\n#{inspect(json)}"
  85. )
  86. end
  87. end
  88. defp json_response_and_validate_schema(conn, _status) do
  89. flunk("Response schema not found for #{conn.method} #{conn.request_path} #{conn.status}")
  90. end
  91. end
  92. end
  93. setup tags do
  94. DataCase.setup_multi_process_mode(tags)
  95. DataCase.setup_streamer(tags)
  96. DataCase.stub_pipeline()
  97. Mox.verify_on_exit!()
  98. {:ok, conn: Phoenix.ConnTest.build_conn()}
  99. end
  100. end