Skip to content

Commit

Permalink
Supersede GET /api/user/stream with POST /api/streams
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Jul 2, 2024
1 parent 8d07285 commit fbbb2ce
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/asciinema_web/controllers/api/live_stream_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}"),
Expand Down
1 change: 1 addition & 0 deletions lib/asciinema_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions test/controllers/api/live_stream_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit fbbb2ce

Please sign in to comment.