2018-01-17 11:39:01 +01:00
|
|
|
defmodule EventosWeb.AddressControllerTest do
|
|
|
|
use EventosWeb.ConnCase
|
|
|
|
|
|
|
|
import Eventos.Factory
|
|
|
|
|
|
|
|
alias Eventos.Addresses
|
|
|
|
alias Eventos.Addresses.Address
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
@create_attrs %{
|
|
|
|
addressCountry: "some addressCountry",
|
|
|
|
addressLocality: "some addressLocality",
|
|
|
|
addressRegion: "some addressRegion",
|
|
|
|
description: "some description",
|
|
|
|
floor: "some floor",
|
|
|
|
postalCode: "some postalCode",
|
|
|
|
streetAddress: "some streetAddress",
|
|
|
|
geom: %{type: :point, data: %{latitude: -20, longitude: 30}}
|
|
|
|
}
|
|
|
|
@update_attrs %{
|
|
|
|
addressCountry: "some updated addressCountry",
|
|
|
|
addressLocality: "some updated addressLocality",
|
|
|
|
addressRegion: "some updated addressRegion",
|
|
|
|
description: "some updated description",
|
|
|
|
floor: "some updated floor",
|
|
|
|
postalCode: "some updated postalCode",
|
|
|
|
streetAddress: "some updated streetAddress",
|
|
|
|
geom: %{type: :point, data: %{latitude: -40, longitude: 40}}
|
|
|
|
}
|
|
|
|
@invalid_attrs %{
|
|
|
|
addressCountry: nil,
|
|
|
|
addressLocality: nil,
|
|
|
|
addressRegion: nil,
|
|
|
|
description: nil,
|
|
|
|
floor: nil,
|
|
|
|
postalCode: nil,
|
|
|
|
streetAddress: nil,
|
2018-08-24 11:34:00 +02:00
|
|
|
geom: %{type: "oh no", data: %{latitude: nil, longitude: nil}}
|
2018-07-27 10:45:35 +02:00
|
|
|
}
|
2018-01-17 11:39:01 +01:00
|
|
|
|
|
|
|
def fixture(:address) do
|
|
|
|
{:ok, address} = Addresses.create_address(@create_attrs)
|
|
|
|
address
|
|
|
|
end
|
|
|
|
|
|
|
|
setup %{conn: conn} do
|
2018-07-10 10:00:07 +02:00
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
2018-01-17 11:39:01 +01:00
|
|
|
{:ok, conn: conn, user: user}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "index" do
|
|
|
|
test "lists all addresses", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = get(conn, address_path(conn, :index))
|
2018-01-17 11:39:01 +01:00
|
|
|
assert json_response(conn, 200)["data"] == []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "create address" do
|
|
|
|
test "renders address when data is valid", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = post(conn, address_path(conn, :create), address: @create_attrs)
|
2018-01-17 11:39:01 +01:00
|
|
|
assert %{"id" => id} = json_response(conn, 201)["data"]
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = get(conn, address_path(conn, :show, id))
|
|
|
|
|
2018-01-17 11:39:01 +01:00
|
|
|
assert json_response(conn, 200)["data"] == %{
|
2018-07-27 10:45:35 +02:00
|
|
|
"id" => id,
|
|
|
|
"addressCountry" => "some addressCountry",
|
|
|
|
"addressLocality" => "some addressLocality",
|
|
|
|
"addressRegion" => "some addressRegion",
|
|
|
|
"description" => "some description",
|
|
|
|
"floor" => "some floor",
|
|
|
|
"postalCode" => "some postalCode",
|
|
|
|
"streetAddress" => "some streetAddress",
|
|
|
|
"geom" => %{
|
|
|
|
"data" => %{"latitude" => -20.0, "longitude" => 30.0},
|
|
|
|
"type" => "point"
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "renders errors when data is invalid", %{conn: conn, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = post(conn, address_path(conn, :create), address: @invalid_attrs)
|
2018-01-17 11:39:01 +01:00
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "update address" do
|
|
|
|
setup [:create_address]
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
test "renders address when data is valid", %{
|
|
|
|
conn: conn,
|
|
|
|
address: %Address{id: id} = address,
|
|
|
|
user: user
|
|
|
|
} do
|
2018-01-17 11:39:01 +01:00
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = put(conn, address_path(conn, :update, address), address: @update_attrs)
|
2018-01-17 11:39:01 +01:00
|
|
|
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = get(conn, address_path(conn, :show, id))
|
|
|
|
|
2018-01-17 11:39:01 +01:00
|
|
|
assert json_response(conn, 200)["data"] == %{
|
2018-07-27 10:45:35 +02:00
|
|
|
"id" => id,
|
|
|
|
"addressCountry" => "some updated addressCountry",
|
|
|
|
"addressLocality" => "some updated addressLocality",
|
|
|
|
"addressRegion" => "some updated addressRegion",
|
|
|
|
"description" => "some updated description",
|
|
|
|
"floor" => "some updated floor",
|
|
|
|
"postalCode" => "some updated postalCode",
|
|
|
|
"streetAddress" => "some updated streetAddress",
|
|
|
|
"geom" => %{
|
|
|
|
"data" => %{"latitude" => -40.0, "longitude" => 40.0},
|
|
|
|
"type" => "point"
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "renders errors when data is invalid", %{conn: conn, address: address, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = put(conn, address_path(conn, :update, address), address: @invalid_attrs)
|
2018-01-17 11:39:01 +01:00
|
|
|
assert json_response(conn, 422)["errors"] != %{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "delete address" do
|
|
|
|
setup [:create_address]
|
|
|
|
|
|
|
|
test "deletes chosen address", %{conn: conn, address: address, user: user} do
|
|
|
|
conn = auth_conn(conn, user)
|
2018-07-27 10:45:35 +02:00
|
|
|
conn = delete(conn, address_path(conn, :delete, address))
|
2018-01-17 11:39:01 +01:00
|
|
|
assert response(conn, 204)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
|
|
|
assert_error_sent(404, fn ->
|
|
|
|
get(conn, address_path(conn, :show, address))
|
|
|
|
end)
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_address(_) do
|
|
|
|
{:ok, address: insert(:address)}
|
|
|
|
end
|
|
|
|
end
|