diff options
| author | lain <lain@soykaf.club> | 2020-04-27 12:07:08 +0200 | 
|---|---|---|
| committer | lain <lain@soykaf.club> | 2020-04-27 12:07:08 +0200 | 
| commit | c86143ed73ba5b2d8d373607ca706f1a428f3fe4 (patch) | |
| tree | e86641043e676803ffc27a57ce23c29a71347063 /test/support | |
| parent | a51cdafc0192b66ce75659b424a690f52c9b2a49 (diff) | |
| parent | 01cc93b6873b5c50c0fc54774a3b004bf660e46b (diff) | |
| download | pleroma-c86143ed73ba5b2d8d373607ca706f1a428f3fe4.tar.gz pleroma-c86143ed73ba5b2d8d373607ca706f1a428f3fe4.zip | |
Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
Diffstat (limited to 'test/support')
| -rw-r--r-- | test/support/api_spec_helpers.ex | 57 | ||||
| -rw-r--r-- | test/support/conn_case.ex | 36 | 
2 files changed, 93 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..80c69c788 --- /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) +      |> Enum.uniq() +    end) +  end +end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 064874201..781622476 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -51,6 +51,42 @@ defmodule Pleroma.Web.ConnCase do          %{user: user, token: token, conn: conn}        end +      defp json_response_and_validate_schema(conn, status \\ nil) do +        content_type = +          conn +          |> Plug.Conn.get_resp_header("content-type") +          |> List.first() +          |> String.split(";") +          |> List.first() + +        status = status || conn.status + +        %{private: %{open_api_spex: %{operation_id: op_id, operation_lookup: lookup, spec: spec}}} = +          conn + +        schema = lookup[op_id].responses[status].content[content_type].schema +        json = json_response(conn, status) + +        case OpenApiSpex.cast_value(json, schema, spec) do +          {:ok, _data} -> +            json + +          {:error, errors} -> +            errors = +              Enum.map(errors, fn error -> +                message = OpenApiSpex.Cast.Error.message(error) +                path = OpenApiSpex.Cast.Error.path_to_string(error) +                "#{message} at #{path}" +              end) + +            flunk( +              "Response does not conform to schema of #{op_id} operation: #{ +                Enum.join(errors, "\n") +              }\n#{inspect(json)}" +            ) +        end +      end +        defp ensure_federating_or_authenticated(conn, url, user) do          initial_setting = Config.get([:instance, :federating])          on_exit(fn -> Config.put([:instance, :federating], initial_setting) end) | 
