Skip to content

Commit

Permalink
[RTC-419] Standardise Jellyfish API test (#134)
Browse files Browse the repository at this point in the history
* New component tests draft

* Add custom functions for assertions

* Better errors WIP

* Check for missing parameters
  • Loading branch information
roznawsk authored Dec 20, 2023
1 parent d7c1ab1 commit 7dac6c5
Show file tree
Hide file tree
Showing 12 changed files with 482 additions and 442 deletions.
6 changes: 5 additions & 1 deletion lib/jellyfish/component/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ defmodule Jellyfish.Component.File do

{:ok, %{endpoint: endpoint_spec, properties: %{}}}
else
{:error, _reason} = error -> error
{:error, [%OpenApiSpex.Cast.Error{reason: :missing_field, name: name}]} ->
{:error, {:missing_parameter, name}}

{:error, _reason} = error ->
error
end
end

Expand Down
6 changes: 5 additions & 1 deletion lib/jellyfish/component/rtsp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ defmodule Jellyfish.Component.RTSP do

{:ok, %{endpoint: endpoint_spec, properties: %{}}}
else
{:error, _reason} = error -> error
{:error, [%OpenApiSpex.Cast.Error{reason: :missing_field, name: name}]} ->
{:error, {:missing_parameter, name}}

{:error, _reason} = error ->
error
end
end
end
24 changes: 20 additions & 4 deletions lib/jellyfish/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ defmodule Jellyfish.Room do
@spec add_component(id(), Component.component(), map()) ::
{:ok, Component.t()}
| :error
| {:error, :incompatible_codec | :reached_components_limit}
| {:error, :incompatible_codec | :reached_components_limit_hls}
def add_component(room_id, component_type, options \\ %{}) do
GenServer.call(registry_id(room_id), {:add_component, component_type, options})
end
Expand Down Expand Up @@ -270,9 +270,25 @@ defmodule Jellyfish.Room do
Logger.warning("Unable to add component: incompatible codec")
{:reply, {:error, :incompatible_codec}, state}

{:error, :reached_components_limit} ->
{:error, :reached_components_limit_hls} ->
Logger.warning("Unable to add component: reached components limit")
{:reply, {:error, :reached_components_limit}, state}
{:reply, {:error, :reached_components_limit_hls}, state}

{:error, :file_does_not_exist} ->
Logger.warning("Unable to add component: file does not exist")
{:reply, {:error, :file_does_not_exist}, state}

{:error, :invalid_file_path} ->
Logger.warning("Unable to add component: invalid file path")
{:reply, {:error, :invalid_file_path}, state}

{:error, :unsupported_file_type} ->
Logger.warning("Unable to add component: unsupported file path")
{:reply, {:error, :unsupported_file_type}, state}

{:error, {:missing_parameter, name}} ->
Logger.warning("Unable to add component: missing parameter #{inspect(name)}")
{:reply, {:error, {:missing_parameter, name}}, state}

{:error, reason} ->
Logger.warning("Unable to add component: #{inspect(reason)}")
Expand Down Expand Up @@ -529,7 +545,7 @@ defmodule Jellyfish.Room do
{:error, :incompatible_codec}

hls_component_already_present?(components) ->
{:error, :reached_components_limit}
{:error, :reached_components_limit_hls}

true ->
:ok
Expand Down
7 changes: 5 additions & 2 deletions lib/jellyfish_web/controllers/component_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ defmodule JellyfishWeb.ComponentController do
:error ->
{:error, :bad_request, "Invalid request body structure"}

{:error, {:missing_parameter, name}} ->
{:error, :bad_request, "Required field \"#{Atom.to_string(name)}\" missing"}

{:error, :invalid_type} ->
{:error, :bad_request, "Invalid component type"}

Expand All @@ -92,8 +95,8 @@ defmodule JellyfishWeb.ComponentController do
{:error, :unsupported_file_type} ->
{:error, :bad_request, "Unsupported file type"}

{:error, :reached_components_limit} ->
{:error, :bad_request, "Reached components limit in room #{room_id}"}
{:error, :reached_components_limit_hls} ->
{:error, :bad_request, "Reached components limit for component HLS in room #{room_id}"}
end
end

Expand Down
15 changes: 1 addition & 14 deletions test/jellyfish/component/file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,7 @@ defmodule Jellyfish.Component.FileTest do
end

test "missing filePath" do
reason = [
%OpenApiSpex.Cast.Error{
reason: :missing_field,
value: %{},
format: nil,
type: nil,
name: :filePath,
path: [:filePath],
length: 0,
meta: %{}
}
]

{:error, ^reason} = Component.File.config(@jellyfish_opts)
{:error, {:missing_parameter, :filePath}} = Component.File.config(@jellyfish_opts)
end

defp get_audio_endpoint(audio_path) do
Expand Down
15 changes: 1 addition & 14 deletions test/jellyfish/component/rtsp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,6 @@ defmodule Jellyfish.Component.RTSPTest do
end

test "missing required sourceUri" do
expected_reason = [
%OpenApiSpex.Cast.Error{
reason: :missing_field,
value: %{},
format: nil,
type: nil,
name: :sourceUri,
path: [:sourceUri],
length: 0,
meta: %{}
}
]

{:error, ^expected_reason} = Component.RTSP.config(@jellyfish_opts)
{:error, {:missing_parameter, :sourceUri}} = Component.RTSP.config(@jellyfish_opts)
end
end
67 changes: 67 additions & 0 deletions test/jellyfish_web/controllers/component/file_component_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
defmodule JellyfishWeb.Component.FileComponentTest do
use JellyfishWeb.ConnCase
use JellyfishWeb.ComponentCase

@file_component_directory "file_component_sources"
@file_component_source "video.h264"

setup _tags do
media_sources_directory =
Application.fetch_env!(:jellyfish, :media_files_path)
|> Path.join(@file_component_directory)
|> Path.expand()

File.mkdir_p!(media_sources_directory)

media_sources_directory
|> Path.join(@file_component_source)
|> File.touch!()

{:ok, %{}}
end

describe "Create File Component" do
test "renders component with required options", %{conn: conn, room_id: room_id} do
conn =
post(conn, ~p"/room/#{room_id}/component",
type: "file",
options: %{filePath: @file_component_source}
)

assert %{
"data" => %{
"id" => id,
"type" => "file",
"properties" => %{}
}
} =
model_response(conn, :created, "ComponentDetailsResponse")

assert_component_created(conn, room_id, id, "file")
end

test "renders error when required options are missing", %{
conn: conn,
room_id: room_id
} do
conn = post(conn, ~p"/room/#{room_id}/component", type: "file")

assert model_response(conn, :bad_request, "Error")["errors"] ==
"Required field \"filePath\" missing"
end

test "renders error when filePath is invalid", %{
conn: conn,
room_id: room_id
} do
conn =
post(conn, ~p"/room/#{room_id}/component",
type: "file",
options: %{filePath: "some/fake/path.h264"}
)

assert model_response(conn, :not_found, "Error")["errors"] ==
"File not found"
end
end
end
Loading

0 comments on commit 7dac6c5

Please sign in to comment.