2021-04-05 00:55:51 +08:00
|
|
|
defmodule LivebookWeb.SessionControllerTest do
|
|
|
|
use LivebookWeb.ConnCase, async: true
|
|
|
|
|
2021-08-14 03:17:43 +08:00
|
|
|
alias Livebook.{SessionSupervisor, Session, Notebook, FileSystem}
|
2021-04-05 00:55:51 +08:00
|
|
|
|
|
|
|
describe "show_image" do
|
|
|
|
test "returns not found when the given session does not exist", %{conn: conn} do
|
|
|
|
conn = get(conn, Routes.session_path(conn, :show_image, "nonexistent", "image.jpg"))
|
|
|
|
|
|
|
|
assert conn.status == 404
|
|
|
|
assert conn.resp_body == "Not found"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns not found when the given image does not exist", %{conn: conn} do
|
|
|
|
{:ok, session_id} = SessionSupervisor.create_session()
|
|
|
|
|
|
|
|
conn = get(conn, Routes.session_path(conn, :show_image, session_id, "nonexistent.jpg"))
|
|
|
|
|
|
|
|
assert conn.status == 404
|
2021-08-14 03:17:43 +08:00
|
|
|
assert conn.resp_body == "No such file or directory"
|
2021-04-05 00:55:51 +08:00
|
|
|
|
2021-04-13 05:24:26 +08:00
|
|
|
SessionSupervisor.close_session(session_id)
|
2021-04-05 00:55:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns the image when it does exist", %{conn: conn} do
|
|
|
|
{:ok, session_id} = SessionSupervisor.create_session()
|
|
|
|
%{images_dir: images_dir} = Session.get_summary(session_id)
|
2021-08-14 03:17:43 +08:00
|
|
|
:ok = FileSystem.File.resolve(images_dir, "test.jpg") |> FileSystem.File.write("")
|
2021-04-05 00:55:51 +08:00
|
|
|
|
|
|
|
conn = get(conn, Routes.session_path(conn, :show_image, session_id, "test.jpg"))
|
|
|
|
|
|
|
|
assert conn.status == 200
|
|
|
|
assert get_resp_header(conn, "content-type") == ["image/jpeg"]
|
|
|
|
|
2021-04-13 05:24:26 +08:00
|
|
|
SessionSupervisor.close_session(session_id)
|
2021-04-05 00:55:51 +08:00
|
|
|
end
|
|
|
|
end
|
2021-07-23 07:18:40 +08:00
|
|
|
|
|
|
|
describe "download_source" do
|
|
|
|
test "returns not found when the given session does not exist", %{conn: conn} do
|
2021-07-28 19:40:36 +08:00
|
|
|
conn = get(conn, Routes.session_path(conn, :download_source, "nonexistent", "livemd"))
|
2021-07-23 07:18:40 +08:00
|
|
|
|
|
|
|
assert conn.status == 404
|
|
|
|
assert conn.resp_body == "Not found"
|
|
|
|
end
|
|
|
|
|
2021-07-28 19:40:36 +08:00
|
|
|
test "returns bad request when given an invalid format", %{conn: conn} do
|
2021-07-23 07:18:40 +08:00
|
|
|
{:ok, session_id} = SessionSupervisor.create_session()
|
|
|
|
|
2021-07-28 19:40:36 +08:00
|
|
|
conn = get(conn, Routes.session_path(conn, :download_source, session_id, "invalid"))
|
|
|
|
|
|
|
|
assert conn.status == 400
|
|
|
|
assert conn.resp_body == "Invalid format, supported formats: livemd, exs"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "handles live markdown notebook source", %{conn: conn} do
|
|
|
|
{:ok, session_id} = SessionSupervisor.create_session()
|
|
|
|
|
|
|
|
conn = get(conn, Routes.session_path(conn, :download_source, session_id, "livemd"))
|
2021-07-23 07:18:40 +08:00
|
|
|
|
|
|
|
assert conn.status == 200
|
|
|
|
assert get_resp_header(conn, "content-type") == ["text/plain"]
|
|
|
|
|
|
|
|
assert conn.resp_body == """
|
|
|
|
# Untitled notebook
|
|
|
|
"""
|
|
|
|
|
|
|
|
SessionSupervisor.close_session(session_id)
|
|
|
|
end
|
2021-07-28 19:40:36 +08:00
|
|
|
|
2021-07-30 00:41:34 +08:00
|
|
|
test "includes output in markdown when include_outputs parameter is set", %{conn: conn} do
|
|
|
|
notebook = %{
|
|
|
|
Notebook.new()
|
|
|
|
| name: "My Notebook",
|
|
|
|
sections: [
|
|
|
|
%{
|
|
|
|
Notebook.Section.new()
|
|
|
|
| name: "Section 1",
|
|
|
|
cells: [
|
|
|
|
%{
|
|
|
|
Notebook.Cell.new(:elixir)
|
|
|
|
| source: """
|
|
|
|
IO.puts("hey")\
|
|
|
|
""",
|
|
|
|
outputs: ["hey"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
{:ok, session_id} = SessionSupervisor.create_session(notebook: notebook)
|
|
|
|
|
|
|
|
query = [include_outputs: "true"]
|
|
|
|
conn = get(conn, Routes.session_path(conn, :download_source, session_id, "livemd", query))
|
|
|
|
|
|
|
|
assert conn.status == 200
|
|
|
|
assert get_resp_header(conn, "content-type") == ["text/plain"]
|
|
|
|
|
|
|
|
assert conn.resp_body == """
|
|
|
|
# My Notebook
|
|
|
|
|
|
|
|
## Section 1
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
IO.puts("hey")
|
|
|
|
```
|
|
|
|
|
|
|
|
```output
|
|
|
|
hey
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
|
|
|
SessionSupervisor.close_session(session_id)
|
|
|
|
end
|
|
|
|
|
2021-07-28 19:40:36 +08:00
|
|
|
test "handles elixir notebook source", %{conn: conn} do
|
|
|
|
{:ok, session_id} = SessionSupervisor.create_session()
|
|
|
|
|
|
|
|
conn = get(conn, Routes.session_path(conn, :download_source, session_id, "exs"))
|
|
|
|
|
|
|
|
assert conn.status == 200
|
|
|
|
assert get_resp_header(conn, "content-type") == ["text/plain"]
|
|
|
|
|
|
|
|
assert conn.resp_body == """
|
|
|
|
# Title: Untitled notebook
|
|
|
|
"""
|
|
|
|
|
|
|
|
SessionSupervisor.close_session(session_id)
|
|
|
|
end
|
2021-07-23 07:18:40 +08:00
|
|
|
end
|
2021-04-05 00:55:51 +08:00
|
|
|
end
|