2017-12-08 09:58:14 +01:00
|
|
|
defmodule EventosWeb.AccountControllerTest do
|
|
|
|
use EventosWeb.ConnCase
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
import Eventos.Factory
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
alias Eventos.Accounts
|
|
|
|
|
|
|
|
@create_attrs %{description: "some description", display_name: "some display_name", domain: "some domain", private_key: "some private_key", public_key: "some public_key", suspended: true, uri: "some uri", url: "some url", username: "some username"}
|
|
|
|
|
|
|
|
def fixture(:account) do
|
|
|
|
{:ok, account} = Accounts.create_account(@create_attrs)
|
|
|
|
account
|
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
setup %{conn: conn} do
|
|
|
|
account = insert(:account)
|
|
|
|
user = insert(:user, account: account)
|
|
|
|
{: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 accounts", %{conn: conn, user: user} do
|
|
|
|
conn = get conn, account_path(conn, :index)
|
|
|
|
assert hd(json_response(conn, 200)["data"])["username"] == user.account.username
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "delete account" do
|
|
|
|
setup [:create_account]
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
test "deletes own account", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
|
|
|
conn = delete conn, account_path(conn, :delete, user.account)
|
|
|
|
assert response(conn, 204)
|
2017-12-08 09:58:14 +01:00
|
|
|
assert_error_sent 404, fn ->
|
2018-01-13 23:33:03 +01:00
|
|
|
get conn, account_path(conn, :show, user.account)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
2018-01-13 23:33:03 +01:00
|
|
|
|
|
|
|
test "deletes other account", %{conn: conn, account: account, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
|
|
|
conn = delete conn, account_path(conn, :delete, account)
|
|
|
|
assert response(conn, 401)
|
|
|
|
conn = get conn, account_path(conn, :show, account)
|
|
|
|
assert response(conn, 200)
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp create_account(_) do
|
|
|
|
account = fixture(:account)
|
|
|
|
{:ok, account: account}
|
|
|
|
end
|
2018-01-13 23:33:03 +01:00
|
|
|
|
|
|
|
defp auth_conn(conn, %Eventos.Accounts.User{} = user) do
|
|
|
|
{:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user)
|
|
|
|
conn
|
|
|
|
|> put_req_header("authorization", "Bearer #{token}")
|
|
|
|
|> put_req_header("accept", "application/json")
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|