2017-12-08 09:58:14 +01:00
|
|
|
defmodule EventosWeb.UserControllerTest do
|
|
|
|
use EventosWeb.ConnCase
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
import Eventos.Factory
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
alias Eventos.Actors
|
|
|
|
alias Eventos.Actors.User
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
@create_attrs %{email: "foo@bar.tld", password: "some password_hash", username: "some username"}
|
2018-01-14 17:56:50 +01:00
|
|
|
# @update_attrs %{email: "foo@fighters.tld", password: "some updated password_hash", username: "some updated username"}
|
2018-01-13 23:33:03 +01:00
|
|
|
@invalid_attrs %{email: "not an email", password: nil, username: nil}
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
def fixture(:user) do
|
2018-06-14 17:25:55 +02:00
|
|
|
{:ok, user} = Actors.create_user(@create_attrs)
|
2017-12-08 09:58:14 +01:00
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
setup %{conn: conn} do
|
2018-06-14 17:25:55 +02:00
|
|
|
actor = insert(:actor)
|
|
|
|
user = insert(:user, actor: actor)
|
2018-01-13 23:33:03 +01:00
|
|
|
{:ok, conn: conn, user: user}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
describe "index" do
|
|
|
|
test "lists all users", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
|
|
|
conn = get conn, user_path(conn, :index)
|
|
|
|
assert hd(json_response(conn, 200)["data"])["id"] == user.id
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "create user" do
|
2018-01-13 23:33:03 +01:00
|
|
|
test "renders user when data is valid", %{conn: conn} do
|
|
|
|
conn = post conn, user_path(conn, :create), @create_attrs
|
2018-07-04 16:23:52 +02:00
|
|
|
assert %{"email" => "foo@bar.tld"} = json_response(conn, 201)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "renders errors when data is invalid", %{conn: conn} do
|
2018-01-13 23:33:03 +01:00
|
|
|
conn = post conn, user_path(conn, :create), @invalid_attrs
|
2018-06-14 17:25:55 +02:00
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
2018-01-26 11:02:11 +01:00
|
|
|
|
|
|
|
test "renders user with avatar when email is valid", %{conn: conn} do
|
|
|
|
attrs = %{email: "contact@framasoft.org", password: "some password_hash", username: "framasoft"}
|
|
|
|
conn = post conn, user_path(conn, :create), attrs
|
2018-07-04 16:23:52 +02:00
|
|
|
assert %{"email" => "contact@framasoft.org"} = json_response(conn, 201)
|
2018-01-26 11:02:11 +01:00
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
# describe "update user" do
|
|
|
|
# setup [:create_user]
|
|
|
|
#
|
|
|
|
# test "renders user when data is valid", %{conn: conn, user: %User{id: id} = user} do
|
|
|
|
# conn = auth_conn(conn, user)
|
|
|
|
# conn = put conn, user_path(conn, :update, user), user: @update_attrs
|
|
|
|
# assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
|
|
|
#
|
|
|
|
# conn = get conn, user_path(conn, :show, id)
|
|
|
|
# assert json_response(conn, 200)["data"] == %{
|
|
|
|
# "id" => id,
|
|
|
|
# "email" => "some updated email",
|
|
|
|
# "password_hash" => "some updated password_hash",
|
|
|
|
# "role" => 43}
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# test "renders errors when data is invalid", %{conn: conn, user: user} do
|
|
|
|
# conn = auth_conn(conn, user)
|
|
|
|
# conn = put conn, user_path(conn, :update, user), user: @invalid_attrs
|
|
|
|
# assert json_response(conn, 422)["errors"] != %{}
|
|
|
|
# end
|
|
|
|
# end
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
describe "delete user" do
|
|
|
|
setup [:create_user]
|
|
|
|
|
|
|
|
test "deletes chosen user", %{conn: conn, user: user} do
|
2018-01-13 23:33:03 +01:00
|
|
|
conn = auth_conn(conn, user)
|
2017-12-08 09:58:14 +01:00
|
|
|
conn = delete conn, user_path(conn, :delete, user)
|
2018-01-13 23:33:03 +01:00
|
|
|
assert response(conn, 204)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_user(_) do
|
2018-01-13 23:33:03 +01:00
|
|
|
user = insert(:user)
|
2017-12-08 09:58:14 +01:00
|
|
|
{:ok, user: user}
|
|
|
|
end
|
|
|
|
end
|