diff --git a/lib/asciinema_web/controllers/api/live_stream_controller.ex b/lib/asciinema_web/controllers/api/live_stream_controller.ex index 8b18654a0..5c0a949ea 100644 --- a/lib/asciinema_web/controllers/api/live_stream_controller.ex +++ b/lib/asciinema_web/controllers/api/live_stream_controller.ex @@ -7,8 +7,15 @@ defmodule AsciinemaWeb.Api.LiveStreamController do plug :authenticate def show(conn, params) do - id = params["id"] + get_stream(conn, params["id"]) + end + + def create(conn, _params) do + # TODO add mode (config option) where new streams are actually created here + get_stream(conn, nil) + end + defp get_stream(conn, id) do if stream = Streaming.get_live_stream(conn.assigns.current_user, id) do json(conn, %{ url: url(~p"/s/#{stream}"), diff --git a/lib/asciinema_web/router.ex b/lib/asciinema_web/router.ex index d1217ebe2..826fcda76 100644 --- a/lib/asciinema_web/router.ex +++ b/lib/asciinema_web/router.ex @@ -83,6 +83,7 @@ defmodule AsciinemaWeb.Router do scope "/api", AsciinemaWeb.Api, as: :api do post "/asciicasts", RecordingController, :create + post "/streams", LiveStreamController, :create scope "/user" do get "/stream", LiveStreamController, :show diff --git a/test/controllers/api/live_stream_controller_test.exs b/test/controllers/api/live_stream_controller_test.exs index 0aa8c186b..e5fe35afa 100644 --- a/test/controllers/api/live_stream_controller_test.exs +++ b/test/controllers/api/live_stream_controller_test.exs @@ -4,6 +4,59 @@ defmodule Asciinema.Api.LiveStreamControllerTest do @default_install_id "9da34ff4-9bf7-45d4-aa88-98c933b15a3f" + describe "create stream" do + test "responds with 401 when auth missing", %{conn: conn} do + conn = post(conn, ~p"/api/streams") + + assert response(conn, 401) + end + + test "responds with 401 when the install ID is unknown", %{conn: conn} do + conn = add_auth_header(conn, @default_install_id) + + conn = post(conn, ~p"/api/streams") + + assert response(conn, 401) + end + + test "responds with 401 when the install ID has been revoked", %{conn: conn} do + conn = add_auth_header(conn, insert(:revoked_api_token)) + + conn = post(conn, ~p"/api/streams") + + assert response(conn, 401) + end + + test "responds with 401 when the user has not been verified", %{conn: conn} do + conn = add_auth_header(conn, insert(:api_token, user: build(:temporary_user))) + + conn = post(conn, ~p"/api/streams") + + assert json_response(conn, 401) + end + + test "responds with 404 when no stream is available", %{conn: conn} do + conn = add_auth_header(conn, insert(:api_token)) + + conn = post(conn, ~p"/api/streams") + + assert %{} = json_response(conn, 404) + end + + test "responds with stream info when a stream is available", %{conn: conn} do + user = insert(:user) + insert(:live_stream, user: user, public_token: "foobar", producer_token: "bazqux") + conn = add_auth_header(conn, insert(:api_token, user: user)) + + conn = post(conn, ~p"/api/streams") + + assert %{ + "url" => "http://localhost:4001/s/foobar", + "ws_producer_url" => "ws://localhost:4001/ws/S/bazqux" + } = json_response(conn, 200) + end + end + describe "get default stream" do test "responds with 401 when auth missing", %{conn: conn} do conn = get(conn, ~p"/api/user/stream")