36d6a8b81c246973b3cb62b012acc163b29bbfd8
[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#{
33 inspect(value)
34 }"
35 )
36 end
37 end
38
39 def resolve_schema(%Schema{} = schema), do: schema
40
41 def resolve_schema(%Reference{} = ref) do
42 schemas = Pleroma.Web.ApiSpec.spec().components.schemas
43 Reference.resolve_schema(ref, schemas)
44 end
45
46 def api_operations do
47 paths = Pleroma.Web.ApiSpec.spec().paths
48
49 Enum.flat_map(paths, fn {_, path_item} ->
50 path_item
51 |> Map.take([:delete, :get, :head, :options, :patch, :post, :put, :trace])
52 |> Map.values()
53 |> Enum.reject(&is_nil/1)
54 end)
55 |> Enum.uniq()
56 end
57 end