<commit_before>defmodule CodeCorps.UserControllerTest do
  use CodeCorps.ApiCase

  alias CodeCorps.User
  alias CodeCorps.Repo
  alias CodeCorps.SluggedRoute

  @valid_attrs %{
    email: "<EMAIL>",
    username: "testuser",
    first_name: "Test",
    last_name: "User",
    website: "http://www.example.com",
    twitter: "testuser",
    biography: "Just a test user"
  }

  @invalid_attrs %{
    email: "",
    username: "",
    website: "---_<>-blank.com",
    twitter: " @ testuser"
  }

  defp relationships, do: %{}

  describe "index" do

    test "lists all entries on index", %{conn: conn} do
      conn = get conn, user_path(conn, :index)
      assert json_response(conn, 200)["data"] == []
    end

    test "filters resources on index", %{conn: conn} do
      [user_1, user_2] = insert_pair(:user)
      insert(:user, username: "user_3", email: "<EMAIL>")
      conn = get conn, "users/?filter[id]=#{user_1.id},#{user_2.id}"
      data = json_response(conn, 200)["data"]
      [first_result, second_result | _] = data
      assert length(data) == 2
      assert first_result["id"] == "#{user_1.id}"
      assert second_result["id"] == "#{user_2.id}"
    end
  end

  describe "#show" do

    test "shows chosen resource", %{conn: conn} do
      user = insert(:user)
      conn = get conn, user_path(conn, :show, user)
      data = json_response(conn, 200)["data"]
      assert data["id"] == "#{user.id}"
      assert data["type"] == "user"
      assert data["attributes"]["username"] == user.username
      assert data["attributes"]["email"] == ""
      assert data["attributes"]["password"] == nil
    end

    test "renders email when authenticated", %{conn: conn} do
      user = insert(:user)
      path = conn |> user_path(:show, user)
      json = conn |> authenticate(user) |> get(path) |> json_response(200)

      assert json["data"]["attributes"]["email"] == user.email
    end

    test "renders 404 when id is nonexistent", %{conn: conn} do
      path = conn |> user_path(:show, -1)
      assert conn |> get(path) |> json_response(404)
    end
  end

  describe "create" do
    test "creates and renders resource when data is valid", %{conn: conn} do
      attrs = Map.put(@valid_attrs, :password, "password")
      conn = post conn, user_path(conn, :create), %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "attributes" => attrs,
          "relationships" => relationships
        }
      }

      id = json_response(conn, 201)["data"]["id"]
      assert id
      user = Repo.get(User, id)
      assert user
      slugged_route = Repo.get_by(SluggedRoute, slug: "testuser")
      assert slugged_route
      assert user.id == slugged_route.user_id
    end

    test "calls segment tracking after user is created", %{conn: conn} do
      conn = post conn, user_path(conn, :create), %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "attributes" => Map.put(@valid_attrs, :password, "password"),
          "relationships" => relationships
        }
      }
      id = json_response(conn, 201)["data"]["id"]

      assert_received {:track, id, "Signed Up", %{}}
    end

    test "does not create resource and renders errors when data is invalid", %{conn: conn} do
      conn = post conn, user_path(conn, :create), %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "attributes" => @invalid_attrs,
          "relationships" => relationships
        }
      }

      assert json_response(conn, 422)["errors"] != %{}
    end
  end

  describe "update" do
    test "updates and renders chosen resource when data is valid", %{conn: conn} do
      user = insert(:user)
      attrs = Map.put(@valid_attrs, :password, "password")

      params = %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "id" => user.id,
          "attributes" => attrs,
          "relationships" => relationships
        }
      }

      path = user_path(conn, :update, user)

      conn =
        conn
        |> authenticate(user)
        |> put(path, params)

      id = json_response(conn, 200)["data"]["id"]
      assert id
      user =  Repo.get(User, id)
      assert user.email == "<EMAIL>"
      assert user.first_name == "Test"
      assert user.last_name == "User"
      assert user.website == "http://www.example.com"
      assert user.biography == "Just a test user"
    end

    test "tracks authentication & update profile events in Segment", %{conn: conn} do
      user = insert(:user)
      attrs = Map.put(@valid_attrs, :password, "password")

      params = %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "id" => user.id,
          "attributes" => attrs,
          "relationships" => relationships
        }
      }

      path = user_path(conn, :update, user)

      conn =
        conn
        |> authenticate(user)
        |> put(path, params)

      id = json_response(conn, 200)["data"]["id"]
      assert_received {:identify, id, attrs}
      assert_received {:track, id, "Updated Profile", %{}}
    end

    test "does not update when authorized as different user", %{conn: conn} do
      [user, another_user] = insert_pair(:user)

      attrs = Map.put(@valid_attrs, :password, "password")

      path = user_path(conn, :update, user)

      params = %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "id" => user.id,<commit_msg>Fix incorrect segment specs<commit_after>defmodule CodeCorps.UserControllerTest do
  use CodeCorps.ApiCase

  alias CodeCorps.User
  alias CodeCorps.Repo
  alias CodeCorps.SluggedRoute

  @valid_attrs %{
    email: "<EMAIL>",
    username: "testuser",
    first_name: "Test",
    last_name: "User",
    website: "http://www.example.com",
    twitter: "testuser",
    biography: "<PASSWORD>"
  }

  @invalid_attrs %{
    email: "",
    username: "",
    website: "---_<>-blank.com",
    twitter: " @ testuser"
  }

  defp relationships, do: %{}

  describe "index" do

    test "lists all entries on index", %{conn: conn} do
      conn = get conn, user_path(conn, :index)
      assert json_response(conn, 200)["data"] == []
    end

    test "filters resources on index", %{conn: conn} do
      [user_1, user_2] = insert_pair(:user)
      insert(:user, username: "user_3", email: "<EMAIL>")
      conn = get conn, "users/?filter[id]=#{user_1.id},#{user_2.id}"
      data = json_response(conn, 200)["data"]
      [first_result, second_result | _] = data
      assert length(data) == 2
      assert first_result["id"] == "#{user_1.id}"
      assert second_result["id"] == "#{user_2.id}"
    end
  end

  describe "#show" do

    test "shows chosen resource", %{conn: conn} do
      user = insert(:user)
      conn = get conn, user_path(conn, :show, user)
      data = json_response(conn, 200)["data"]
      assert data["id"] == "#{user.id}"
      assert data["type"] == "user"
      assert data["attributes"]["username"] == user.username
      assert data["attributes"]["email"] == ""
      assert data["attributes"]["password"] == nil
    end

    test "renders email when authenticated", %{conn: conn} do
      user = insert(:user)
      path = conn |> user_path(:show, user)
      json = conn |> authenticate(user) |> get(path) |> json_response(200)

      assert json["data"]["attributes"]["email"] == user.email
    end

    test "renders 404 when id is nonexistent", %{conn: conn} do
      path = conn |> user_path(:show, -1)
      assert conn |> get(path) |> json_response(404)
    end
  end

  describe "create" do
    test "creates and renders resource when data is valid", %{conn: conn} do
      attrs = Map.put(@valid_attrs, :password, "password")
      conn = post conn, user_path(conn, :create), %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "attributes" => attrs,
          "relationships" => relationships
        }
      }

      id = json_response(conn, 201)["data"]["id"]
      assert id
      user = Repo.get(User, id)
      assert user
      slugged_route = Repo.get_by(SluggedRoute, slug: "testuser")
      assert slugged_route
      assert user.id == slugged_route.user_id
    end

    test "calls segment tracking after user is created", %{conn: conn} do
      conn = post conn, user_path(conn, :create), %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "attributes" => Map.put(@valid_attrs, :password, "password"),
          "relationships" => relationships
        }
      }
      id = json_response(conn, 201)["data"]["id"] |> String.to_integer
      assert_received {:track, ^id, "Signed Up", %{}}
    end

    test "does not create resource and renders errors when data is invalid", %{conn: conn} do
      conn = post conn, user_path(conn, :create), %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "attributes" => @invalid_attrs,
          "relationships" => relationships
        }
      }

      assert json_response(conn, 422)["errors"] != %{}
    end
  end

  describe "update" do
    test "updates and renders chosen resource when data is valid", %{conn: conn} do
      user = insert(:user)
      attrs = Map.put(@valid_attrs, :password, "password")

      params = %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "id" => user.id,
          "attributes" => attrs,
          "relationships" => relationships
        }
      }

      path = user_path(conn, :update, user)

      conn =
        conn
        |> authenticate(user)
        |> put(path, params)

      id = json_response(conn, 200)["data"]["id"]
      assert id
      user =  Repo.get(User, id)
      assert user.email == "<EMAIL>"
      assert user.first_name == "Test"
      assert user.last_name == "User"
      assert user.website == "http://www.example.com"
      assert user.biography == "Just a test user"
    end

    test "tracks authentication & update profile events in Segment", %{conn: conn} do
      user = insert(:user, email: "<EMAIL>")
      attrs = Map.put(@valid_attrs, :password, "password")

      params = %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "id" => user.id,
          "attributes" => attrs,
          "relationships" => relationships
        }
      }

      path = user_path(conn, :update, user)

      conn =
        conn
        |> authenticate(user)
        |> put(path, params)

      id = json_response(conn, 200)["data"]["id"] |> String.to_integer
      assert_received {:identify, ^id, %{email: "<EMAIL>"}}
      assert_received {:track, ^id, "Updated Profile", %{}}
    end

    test "does not update when authorized as different user", %{conn: conn} do
      [user, another_user] = insert_pair(:user)

      attrs = Map.put(@valid_attrs, :password, "password")

      path = user_path(conn, :update, user)

      params = %{
        "meta" => %{},
        "data" => %{
          "type" => "user",
          "id" => user.id,
{'fraction_non_alphanumeric': 0.15982191531891696, 'fraction_numerical': 0.007632200617844812, 'mean_word_length': 2.419384902143523, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6803696', 'n_tokens_mistral': 3610, 'n_tokens_neox': 3418, 'n_words': 894}