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.

58 lines
1.4KB

  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.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#{
  26. inspect(value)
  27. }"
  28. )
  29. end
  30. end
  31. def resolve_schema(%Schema{} = schema), do: schema
  32. def resolve_schema(%Reference{} = ref) do
  33. schemas = Pleroma.Web.ApiSpec.spec().components.schemas
  34. Reference.resolve_schema(ref, schemas)
  35. end
  36. def api_operations do
  37. paths = Pleroma.Web.ApiSpec.spec().paths
  38. Enum.flat_map(paths, fn {_, path_item} ->
  39. path_item
  40. |> Map.take([:delete, :get, :head, :options, :patch, :post, :put, :trace])
  41. |> Map.values()
  42. |> Enum.reject(&is_nil/1)
  43. end)
  44. |> Enum.uniq()
  45. end
  46. end