Skip to content

Commit

Permalink
Refactoring user controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mackenziehicks committed Nov 11, 2016
1 parent 1928206 commit 72a5974
Showing 1 changed file with 32 additions and 57 deletions.
89 changes: 32 additions & 57 deletions test/controllers/user_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule CodeCorps.UserControllerTest do
use CodeCorps.ApiCase
use CodeCorps.ApiCase, resource_name: :user

alias CodeCorps.User
alias CodeCorps.Repo
Expand Down Expand Up @@ -27,68 +27,56 @@ defmodule CodeCorps.UserControllerTest 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"] == []
[user_1, user_2] = insert_pair(:user)

conn
|> request_index
|> json_response(200)
|> assert_ids_from_response([user_1.id, user_2.id])
end

test "filters resources on index", %{conn: conn} do
[user_1, user_2] = insert_pair(:user)
insert(:user, username: "user_3", email: "user_3@mail.com")
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}"
[user_1, user_2 | _] = insert_list(3, :user)

path = "users/?filter[id]=#{user_1.id},#{user_2.id}"

conn
|> get(path)
|> json_response(200)
|> assert_ids_from_response([user_1.id, user_2.id])
end
end

describe "#show" do

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
conn
|> request_show(user)
|> json_response(200)
|> Map.get("data")
|> assert_result_id(user.id)
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
@tag :authenticated
test "renders email when authenticated", %{conn: conn, current_user: current_user} do
assert conn |> request_show(current_user) |> json_response(200)
end

test "renders 404 when id is nonexistent", %{conn: conn} do
path = conn |> user_path(:show, -1)
assert conn |> get(path) |> json_response(404)
assert conn |> request_show(:not_found) |> 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
"attributes" => attrs
}
}

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
assert conn |> json_response(201)
end

test "calls segment tracking after user is created", %{conn: conn} do
Expand All @@ -105,20 +93,19 @@ defmodule CodeCorps.UserControllerTest do
end

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

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

describe "update" do
@tag :authenticated
test "updates and renders chosen resource when data is valid", %{conn: conn} do
user = insert(:user)
attrs = Map.put(@valid_attrs, :password, "password")
Expand All @@ -135,19 +122,7 @@ defmodule CodeCorps.UserControllerTest do

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 == "test@user.com"
assert user.first_name == "Test"
assert user.last_name == "User"
assert user.website == "http://www.example.com"
assert user.biography == "Just a test user"
assert conn |> authenticate(user) |> put(path, params) |> json_response(200)
end

test "tracks authentication & update profile events in Segment", %{conn: conn} do
Expand Down

0 comments on commit 72a5974

Please sign in to comment.