<commit_before>defmodule Asciinema.Api.AsciicastControllerTest do
  use AsciinemaWeb.ConnCase
  alias Asciinema.Accounts

  setup %{conn: conn} = context do
    token = Map.get(context, :token, "<PASSWORD>")

    conn = if token do
      put_req_header(conn, "authorization", "Basic " <> Base.encode64("test:" <> token))
    else
      conn
    end

    {:ok, conn: conn, token: token}
  end

  @asciicast_url ~r|^http://localhost:4001/a/[a-zA-Z0-9]{25}|

  describe ".create" do
    @tag token: nil
    test "separate files (pre-v1 params), v0.9.7 client", %{conn: conn} do
      asciicast = %{"meta" => fixture(:upload, %{path: "0.9.7/meta.json",
                                                 content_type: "application/json"}),
                    "stdout" => fixture(:upload, %{path: "0.9.7/stdout",
                                                   content_type: "application/octet-stream"}),
                    "stdout_timing" => fixture(:upload, %{path: "0.9.7/stdout.time",
                                                          content_type: "application/octet-stream"})}

      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => asciicast}
      assert text_response(conn, 201) =~ @asciicast_url
      assert List.first(get_resp_header(conn, "location")) =~ @asciicast_url
    end

    @tag token: nil
    test "separate files (pre-v1 params), v0.9.8 client", %{conn: conn} do
      asciicast = %{"meta" => fixture(:upload, %{path: "0.9.8/meta.json",
                                                 content_type: "application/json"}),
                    "stdout" => fixture(:upload, %{path: "0.9.8/stdout",
                                                   content_type: "application/octet-stream"}),
                    "stdout_timing" => fixture(:upload, %{path: "0.9.8/stdout.time",
                                                          content_type: "application/octet-stream"})}

      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => asciicast}
      assert text_response(conn, 201) =~ @asciicast_url
      assert List.first(get_resp_header(conn, "location")) =~ @asciicast_url
    end

    test "separate files (pre-v1 params), v0.9.9 client", %{conn: conn} do
      asciicast = %{"meta" => fixture(:upload, %{path: "0.9.9/meta.json",
                                                 content_type: "application/json"}),
                    "stdout" => fixture(:upload, %{path: "0.9.9/stdout",
                                                   content_type: "application/octet-stream"}),
                    "stdout_timing" => fixture(:upload, %{path: "0.9.9/stdout.time",
                                                          content_type: "application/octet-stream"})}

      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => asciicast}
      assert text_response(conn, 201) =~ @asciicast_url
      assert List.first(get_resp_header(conn, "location")) =~ @asciicast_url
    end

    test "json file, v1 format", %{conn: conn} do
      upload = fixture(:upload, %{path: "1/asciicast.json"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}
      assert text_response(conn, 201) =~ @asciicast_url
      assert List.first(get_resp_header(conn, "location")) =~ @asciicast_url
    end

    test "json file, v1 format (missing required data)", %{conn: conn} do
      upload = fixture(:upload, %{path: "1/invalid.json"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}
      assert %{"errors" => _} = json_response(conn, 422)
    end

    test "json file, unsupported version number", %{conn: conn} do
      upload = fixture(:upload, %{path: "5/asciicast.json"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}
      assert text_response(conn, 422) =~ ~r|not supported|
    end

    test "non-json file", %{conn: conn} do
      upload = fixture(:upload, %{path: "new-logo-bars.png"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}
      assert text_response(conn, 400) =~ ~r|valid asciicast|
    end

    test "existing user (API token)", %{conn: conn, token: token} do
      {:ok, _} = Accounts.create_user_with_api_token(token, "test")
      upload = fixture(:upload, %{path: "1/asciicast.json"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}<commit_msg>Add controller test for uploading v2 cast<commit_after>defmodule Asciinema.Api.AsciicastControllerTest do
  use AsciinemaWeb.ConnCase
  alias Asciinema.Accounts

  setup %{conn: conn} = context do
    token = Map.get(context, :token, "9<PASSWORD>")

    conn = if token do
      put_req_header(conn, "authorization", "Basic " <> Base.encode64("test:" <> token))
    else
      conn
    end

    {:ok, conn: conn, token: token}
  end

  @asciicast_url ~r|^http://localhost:4001/a/[a-zA-Z0-9]{25}|

  describe ".create" do
    @tag token: nil
    test "separate files (pre-v1 params), v0.9.7 client", %{conn: conn} do
      asciicast = %{"meta" => fixture(:upload, %{path: "0.9.7/meta.json",
                                                 content_type: "application/json"}),
                    "stdout" => fixture(:upload, %{path: "0.9.7/stdout",
                                                   content_type: "application/octet-stream"}),
                    "stdout_timing" => fixture(:upload, %{path: "0.9.7/stdout.time",
                                                          content_type: "application/octet-stream"})}

      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => asciicast}
      assert text_response(conn, 201) =~ @asciicast_url
      assert List.first(get_resp_header(conn, "location")) =~ @asciicast_url
    end

    @tag token: nil
    test "separate files (pre-v1 params), v0.9.8 client", %{conn: conn} do
      asciicast = %{"meta" => fixture(:upload, %{path: "0.9.8/meta.json",
                                                 content_type: "application/json"}),
                    "stdout" => fixture(:upload, %{path: "0.9.8/stdout",
                                                   content_type: "application/octet-stream"}),
                    "stdout_timing" => fixture(:upload, %{path: "0.9.8/stdout.time",
                                                          content_type: "application/octet-stream"})}

      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => asciicast}
      assert text_response(conn, 201) =~ @asciicast_url
      assert List.first(get_resp_header(conn, "location")) =~ @asciicast_url
    end

    test "separate files (pre-v1 params), v0.9.9 client", %{conn: conn} do
      asciicast = %{"meta" => fixture(:upload, %{path: "0.9.9/meta.json",
                                                 content_type: "application/json"}),
                    "stdout" => fixture(:upload, %{path: "0.9.9/stdout",
                                                   content_type: "application/octet-stream"}),
                    "stdout_timing" => fixture(:upload, %{path: "0.9.9/stdout.time",
                                                          content_type: "application/octet-stream"})}

      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => asciicast}
      assert text_response(conn, 201) =~ @asciicast_url
      assert List.first(get_resp_header(conn, "location")) =~ @asciicast_url
    end

    test "json file, v1 format", %{conn: conn} do
      upload = fixture(:upload, %{path: "1/asciicast.json"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}
      assert text_response(conn, 201) =~ @asciicast_url
      assert List.first(get_resp_header(conn, "location")) =~ @asciicast_url
    end

    test "json file, v2 format", %{conn: conn} do
      upload = fixture(:upload, %{path: "2/minimal.cast"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}
      assert text_response(conn, 201) =~ @asciicast_url
      assert List.first(get_resp_header(conn, "location")) =~ @asciicast_url
    end

    test "json file, v1 format (missing required data)", %{conn: conn} do
      upload = fixture(:upload, %{path: "1/invalid.json"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}
      assert %{"errors" => _} = json_response(conn, 422)
    end

    test "json file, unsupported version number", %{conn: conn} do
      upload = fixture(:upload, %{path: "5/asciicast.json"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}
      assert text_response(conn, 422) =~ ~r|not supported|
    end

    test "non-json file", %{conn: conn} do
      upload = fixture(:upload, %{path: "new-logo-bars.png"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}
      assert text_response(conn, 400) =~ ~r|valid asciicast|
    end

    test "existing user (API token)", %{conn: conn, token: token} do
      {:ok, _} = Accounts.create_user_with_api_token(token, "<PASSWORD>")
      upload = fixture(:upload, %{path: "1/asciicast.json"})
      conn = post conn, api_asciicast_path(conn, :create), %{"asciicast" => upload}
{'fraction_non_alphanumeric': 0.15106198526224535, 'fraction_numerical': 0.017230169050715213, 'mean_word_length': 2.353561046511628, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5649619', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2854, 'n_words': 649}