logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

conn_case.ex (3671B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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: #{Enum.join(errors, "\n")}\n#{inspect(json)}"
  83. )
  84. end
  85. end
  86. defp json_response_and_validate_schema(conn, _status) do
  87. flunk("Response schema not found for #{conn.method} #{conn.request_path} #{conn.status}")
  88. end
  89. end
  90. end
  91. setup tags do
  92. DataCase.setup_multi_process_mode(tags)
  93. DataCase.setup_streamer(tags)
  94. DataCase.stub_pipeline()
  95. Mox.verify_on_exit!()
  96. {:ok, conn: Phoenix.ConnTest.build_conn()}
  97. end
  98. end