Use json_response_and_validate_schema/2 in tests to validate OpenAPI schema

This commit is contained in:
Egor Kislitsyn 2020-04-27 20:46:52 +04:00
commit 2efc00b3cf
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
7 changed files with 330 additions and 358 deletions

View file

@ -56,7 +56,14 @@ defmodule Pleroma.Web.ConnCase do
[conn: conn]
end
defp json_response_and_validate_schema(conn, status \\ nil) do
defp json_response_and_validate_schema(
%{
private: %{
open_api_spex: %{operation_id: op_id, operation_lookup: lookup, spec: spec}
}
} = conn,
status
) do
content_type =
conn
|> Plug.Conn.get_resp_header("content-type")
@ -64,10 +71,12 @@ defmodule Pleroma.Web.ConnCase do
|> String.split(";")
|> List.first()
status = status || conn.status
status = Plug.Conn.Status.code(status)
%{private: %{open_api_spex: %{operation_id: op_id, operation_lookup: lookup, spec: spec}}} =
conn
unless lookup[op_id].responses[status] do
err = "Response schema not found for #{conn.status} #{conn.method} #{conn.request_path}"
flunk(err)
end
schema = lookup[op_id].responses[status].content[content_type].schema
json = json_response(conn, status)
@ -92,6 +101,10 @@ defmodule Pleroma.Web.ConnCase do
end
end
defp json_response_and_validate_schema(conn, _status) do
flunk("Response schema not found for #{conn.method} #{conn.request_path} #{conn.status}")
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)