diff options
author | Egor Kislitsyn <egor@kislitsyn.com> | 2020-06-01 17:38:57 +0400 |
---|---|---|
committer | Egor Kislitsyn <egor@kislitsyn.com> | 2020-06-01 17:38:57 +0400 |
commit | 6b84c62d4a51cb17192945d1b67999b2c56a23d2 (patch) | |
tree | 53744600bd910bb0a62be6ab5884c6972da2ffce /test/support/api_spec_helpers.ex | |
parent | cb8236cda62cddb72f4320af6347defae44b81ca (diff) | |
parent | e96765df6b04fe5e9766271a9c62e559392758b2 (diff) | |
download | pleroma-6b84c62d4a51cb17192945d1b67999b2c56a23d2.tar.gz |
Merge remote-tracking branch 'origin/develop' into feature/embeddable-posts
Diffstat (limited to 'test/support/api_spec_helpers.ex')
-rw-r--r-- | test/support/api_spec_helpers.ex | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/test/support/api_spec_helpers.ex b/test/support/api_spec_helpers.ex new file mode 100644 index 000000000..46388f92c --- /dev/null +++ b/test/support/api_spec_helpers.ex @@ -0,0 +1,57 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Tests.ApiSpecHelpers do + @moduledoc """ + OpenAPI spec test helpers + """ + + import ExUnit.Assertions + + alias OpenApiSpex.Cast.Error + alias OpenApiSpex.Reference + alias OpenApiSpex.Schema + + def assert_schema(value, schema) do + api_spec = Pleroma.Web.ApiSpec.spec() + + case OpenApiSpex.cast_value(value, schema, api_spec) do + {:ok, data} -> + data + + {:error, errors} -> + errors = + Enum.map(errors, fn error -> + message = Error.message(error) + path = Error.path_to_string(error) + "#{message} at #{path}" + end) + + flunk( + "Value does not conform to schema #{schema.title}: #{Enum.join(errors, "\n")}\n#{ + inspect(value) + }" + ) + end + end + + def resolve_schema(%Schema{} = schema), do: schema + + def resolve_schema(%Reference{} = ref) do + schemas = Pleroma.Web.ApiSpec.spec().components.schemas + Reference.resolve_schema(ref, schemas) + end + + def api_operations do + paths = Pleroma.Web.ApiSpec.spec().paths + + Enum.flat_map(paths, fn {_, path_item} -> + path_item + |> Map.take([:delete, :get, :head, :options, :patch, :post, :put, :trace]) + |> Map.values() + |> Enum.reject(&is_nil/1) + end) + |> Enum.uniq() + end +end |