Add test for AsciicastAnimationController

ex-upload
Marcin Kulik 7 years ago
parent 66ac200733
commit fbd8acf326

@ -0,0 +1,9 @@
defmodule Asciinema.AsciicastAnimationControllerTest do
use Asciinema.ConnCase
test "shows GIF generation instructions", %{conn: conn} do
asciicast = fixture(:asciicast)
conn = get conn, asciicast_animation_download_path(conn, asciicast)
assert html_response(conn, 200) =~ "GIF"
end
end

@ -26,6 +26,8 @@ defmodule Asciinema.ConnCase do
import Ecto.Query
import Asciinema.Router.Helpers
import Asciinema.Router.Helpers.Extra
import Asciinema.Fixtures
# The default endpoint for testing
@endpoint Asciinema.Endpoint

@ -0,0 +1,28 @@
defmodule Asciinema.Fixtures do
alias Asciinema.{Repo, Asciicast, User}
def fixture(:upload) do
%Plug.Upload{path: "resources/welcome.json",
filename: "welcome.json",
content_type: "application/json"}
end
def fixture(:user) do
attrs = %{username: "test",
auth_token: "authy-auth-auth"}
Repo.insert!(User.changeset(%User{}, attrs))
end
def fixture(:asciicast) do
user = fixture(:user)
upload = fixture(:upload)
attrs = %{version: 1,
duration: 123,
terminal_columns: 80,
terminal_lines: 24,
file: upload.filename,
secret_token: "v3ry-sekr1t",
user_id: user.id}
Repo.insert!(Asciicast.changeset(%Asciicast{}, attrs))
end
end

@ -7,6 +7,6 @@ defmodule Asciinema.AsciicastAnimationController do
conn
|> put_layout("simple.html")
|> render("show.html", asciicast: asciicast)
|> render("show.html", file_url: asciicast_file_download_url(conn, asciicast))
end
end

@ -9,6 +9,8 @@ defmodule Asciinema.Asciicast do
schema "asciicasts" do
field :version, :integer
field :file, :string
field :terminal_columns, :integer
field :terminal_lines, :integer
field :stdout_data, :string
field :stdout_timing, :string
field :stdout_frames, :string
@ -18,9 +20,17 @@ defmodule Asciinema.Asciicast do
field :theme_name, :string
field :snapshot_at, :float
timestamps(inserted_at: :created_at)
belongs_to :user, User
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:user_id, :version, :file, :duration, :terminal_columns, :terminal_lines, :secret_token])
|> validate_required([:user_id, :version, :duration, :terminal_columns, :terminal_lines, :secret_token])
end
def by_id_or_secret_token(thing) do
if String.length(thing) == 25 do
from a in __MODULE__, where: a.secret_token == ^thing

@ -10,7 +10,9 @@ defmodule Asciinema.User do
field :theme_name, :string
field :asciicasts_private_by_default, :boolean, default: true
timestamps()
timestamps(inserted_at: :created_at)
has_many :asciicasts, Asciinema.Asciicast
end
@doc """
@ -19,6 +21,6 @@ defmodule Asciinema.User do
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:email, :name, :username, :temporary_username, :auth_token, :theme_name, :asciicasts_private_by_default])
|> validate_required([:email, :name, :username, :temporary_username, :auth_token, :theme_name, :asciicasts_private_by_default])
|> validate_required([:auth_token])
end
end

@ -57,3 +57,17 @@ defmodule Asciinema.Router do
# pipe_through :api
# end
end
defmodule Asciinema.Router.Helpers.Extra do
alias Asciinema.Router.Helpers, as: H
def asciicast_file_download_url(conn, asciicast) do
H.asciicast_file_url(conn, :show, asciicast)
|> String.replace_suffix("/json", ".json")
end
def asciicast_animation_download_path(conn, asciicast) do
H.asciicast_animation_path(conn, :show, asciicast)
|> String.replace_suffix("/gif", ".gif")
end
end

@ -6,9 +6,9 @@
yourself. Once you have it installed run the following command in your
terminal: </p>
<pre><code>asciicast2gif <%= asciicast_file_url(@conn, @asciicast) %> demo.gif</code> </pre>
<pre><code>asciicast2gif <%= @file_url %> demo.gif</code> </pre>
<p> If you have <a href="https://www.docker.com/">Docker</a> installed then just
run this command: </p>
<pre><code>docker run --rm -v $PWD:/data asciinema/asciicast2gif <%= asciicast_file_url(@conn, @asciicast) %> demo.gif</code></pre>
<pre><code>docker run --rm -v $PWD:/data asciinema/asciicast2gif <%= @file_url %> demo.gif</code></pre>

@ -1,7 +1,3 @@
defmodule Asciinema.AsciicastAnimationView do
use Asciinema.Web, :view
def asciicast_file_url(conn, asciicast) do
"#{asciicast_url(conn, :show, asciicast)}.json" # TODO: use route helper
end
end

@ -36,6 +36,7 @@ defmodule Asciinema.Web do
import Ecto.Query
import Asciinema.Router.Helpers
import Asciinema.Router.Helpers.Extra
import Asciinema.Gettext
end
end
@ -51,6 +52,7 @@ defmodule Asciinema.Web do
use Phoenix.HTML
import Asciinema.Router.Helpers
import Asciinema.Router.Helpers.Extra
import Asciinema.ErrorHelpers
import Asciinema.Gettext
import Asciinema.UserView

Loading…
Cancel
Save