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