1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Tests.ApiSpecHelpers do
7 OpenAPI spec test helpers
10 import ExUnit.Assertions
12 alias OpenApiSpex.Cast.Error
13 alias OpenApiSpex.Reference
14 alias OpenApiSpex.Schema
16 def assert_schema(value, schema) do
17 api_spec = Pleroma.Web.ApiSpec.spec()
19 case OpenApiSpex.cast_value(value, schema, api_spec) do
25 Enum.map(errors, fn error ->
26 message = Error.message(error)
27 path = Error.path_to_string(error)
28 "#{message} at #{path}"
32 "Value does not conform to schema #{schema.title}: #{Enum.join(errors, "\n")}\n#{inspect(value)}"
37 def resolve_schema(%Schema{} = schema), do: schema
39 def resolve_schema(%Reference{} = ref) do
40 schemas = Pleroma.Web.ApiSpec.spec().components.schemas
41 Reference.resolve_schema(ref, schemas)
45 paths = Pleroma.Web.ApiSpec.spec().paths
47 Enum.flat_map(paths, fn {_, path_item} ->
49 |> Map.take([:delete, :get, :head, :options, :patch, :post, :put, :trace])
51 |> Enum.reject(&is_nil/1)