logo

pleroma

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

api_spec_helpers.ex (1431B)


  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.Tests.ApiSpecHelpers do
  5. @moduledoc """
  6. OpenAPI spec test helpers
  7. """
  8. import ExUnit.Assertions
  9. alias OpenApiSpex.Cast.Error
  10. alias OpenApiSpex.Reference
  11. alias OpenApiSpex.Schema
  12. def assert_schema(value, schema) do
  13. api_spec = Pleroma.Web.ApiSpec.spec()
  14. case OpenApiSpex.cast_value(value, schema, api_spec) do
  15. {:ok, data} ->
  16. data
  17. {:error, errors} ->
  18. errors =
  19. Enum.map(errors, fn error ->
  20. message = Error.message(error)
  21. path = Error.path_to_string(error)
  22. "#{message} at #{path}"
  23. end)
  24. flunk(
  25. "Value does not conform to schema #{schema.title}: #{Enum.join(errors, "\n")}\n#{inspect(value)}"
  26. )
  27. end
  28. end
  29. def resolve_schema(%Schema{} = schema), do: schema
  30. def resolve_schema(%Reference{} = ref) do
  31. schemas = Pleroma.Web.ApiSpec.spec().components.schemas
  32. Reference.resolve_schema(ref, schemas)
  33. end
  34. def api_operations do
  35. paths = Pleroma.Web.ApiSpec.spec().paths
  36. Enum.flat_map(paths, fn {_, path_item} ->
  37. path_item
  38. |> Map.take([:delete, :get, :head, :options, :patch, :post, :put, :trace])
  39. |> Map.values()
  40. |> Enum.reject(&is_nil/1)
  41. end)
  42. |> Enum.uniq()
  43. end
  44. end