This is the test for this case
defmodule WorkersWeb.Api.V1.CountyControllerTest do
use WorkersWeb.ConnCase
alias Workers.Mongo, as: Wmongo
@token "authtoken123"
setup do
Wmongo.delete_many!("counties", %{})
conn = build_conn()
|> put_req_header("accept", "application/json")
|> put_req_header("content-type", "text/json")
jwt = sign_and_store_user(@token)
{:ok, conn: conn, jwt: jwt}
end
describe "index/2" do
test "OK", %{conn: conn, jwt: jwt} do
create_counties()
conn = conn
|> put_req_header("authorization", jwt)
|> get("/api/v1/counties?order_by=name&order_dir=desc")
json = Poison.decode!(conn.resp_body)
assert Enum.count(json["counties"]) == 3
assert Enum.map(json["counties"], &(&1["name"])) == ["Sheridan", "SABINE PASS", "SABINE OTHER"]
co = List.first(json["counties"])
assert Map.keys(co) == ["api_id", "name", "state_abbr"]
end
test "Unathorized", %{conn: conn} do
conn = get(conn, "/api/v1/counties")
assert conn.status == 401
json = Poison.decode!(conn.resp_body)
assert json["msg"] == "Please login."
end
end
describe "show/2" do
test "ok", %{conn: conn, jwt: jwt} do
api_id = "49-033"
Wmongo.insert_one!("counties", %{name: "Sheridan", county_api: "033", api_id: api_id, state_abbr: "WY"})
conn = conn
|> put_req_header("authorization", jwt)
|> get("/api/v1/counties/#{api_id}")
json = Poison.decode!(conn.resp_body)
assert json["county"] == %{"api_id" => "49-033", "name" => "Sheridan", "state_abbr" => "WY"}
end
test "Not found", %{conn: conn, jwt: jwt} do
conn = conn
|> put_req_header("authorization", jwt)
|> get("/api/v1/counties/49-333")
json = Poison.decode!(conn.resp_body)
assert json["error"] == "County not found"
assert conn.status == 404
end
end
end
Method to create counties
def create_counties do
Mongo.delete_many(:mongo, "counties", %{}, [pool: DBConnection.Poolboy])
Mongo.insert_many(:mongo, "counties", load_fixture("counties")["counties"], [pool: DBConnection.Poolboy])
end