From 98e196a0c35e249eca0efe9a9aeeb1f308e91329 Mon Sep 17 00:00:00 2001 From: Robert J Samson Date: Thu, 23 Jun 2016 15:14:50 -0400 Subject: [PATCH] Phoenix 1.2, tests passing --- config/test.exs | 2 - test/controllers/admin_controller_test.exs | 6 +- test/controllers/score_controller_test.exs | 12 ++-- test/controllers/venue_controller_test.exs | 78 +++++++++++----------- test/support/conn_case.ex | 2 +- test/support/model_case.ex | 6 +- test/support/test_helpers.ex | 11 +++ test/test_helper.exs | 4 +- 8 files changed, 65 insertions(+), 56 deletions(-) create mode 100644 test/support/test_helpers.ex diff --git a/config/test.exs b/config/test.exs index cee8c2b..bdd0bd2 100644 --- a/config/test.exs +++ b/config/test.exs @@ -12,8 +12,6 @@ config :logger, level: :warn # Configure your database config :stackfooter, Stackfooter.Repo, adapter: Ecto.Adapters.Postgres, - username: "postgres", - password: "postgres", database: "stackfooter_test", hostname: "localhost", pool: Ecto.Adapters.SQL.Sandbox diff --git a/test/controllers/admin_controller_test.exs b/test/controllers/admin_controller_test.exs index bfebd00..aa84e68 100644 --- a/test/controllers/admin_controller_test.exs +++ b/test/controllers/admin_controller_test.exs @@ -31,7 +31,7 @@ defmodule Stackfooter.AdminControllerTest do end test "resets a venue" do - conn = put_req_header(conn(), "x-starfighter-authorization", @admin_apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @admin_apikey) |> post("/ob/api/admin/reset/obex") resp = json_response(conn, 200) @@ -41,7 +41,7 @@ defmodule Stackfooter.AdminControllerTest do end test "only admin account can perform admin actions" do - conn = put_req_header(conn(), "x-starfighter-authorization", @non_admin_apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @non_admin_apikey) |> post("/ob/api/admin/reset/obex") resp = json_response(conn, 401) @@ -52,7 +52,7 @@ defmodule Stackfooter.AdminControllerTest do end test "errors properly when a venue does not exist" do - conn = put_req_header(conn(), "x-starfighter-authorization", @admin_apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @admin_apikey) |> post("/ob/api/admin/reset/notavenue") resp = json_response(conn, 404) diff --git a/test/controllers/score_controller_test.exs b/test/controllers/score_controller_test.exs index d5b2309..5c84deb 100644 --- a/test/controllers/score_controller_test.exs +++ b/test/controllers/score_controller_test.exs @@ -43,7 +43,7 @@ defmodule Stackfooter.ScoreControllerTest do scores = [%{"cash" => 1176, "name" => "A", "nav" => 1176, "positions" => [%{"price" => 4225, "qty" => 0, "stock" => "NYC"}]}, %{"cash" => -1176, "name" => "RJSA", "nav" => -1176, "positions" => [%{"price" => 4225, "qty" => 0, "stock" => "NYC"}]}] - conn = get(conn(), "/ob/api/scores") + conn = get(build_conn(), "/ob/api/scores") resp = json_response(conn, 200) assert resp @@ -52,7 +52,7 @@ defmodule Stackfooter.ScoreControllerTest do end test "returns the correct score for an individual user" do - conn = put_req_header(conn(), "x-starfighter-authorization", @admin_apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @admin_apikey) |> get("/ob/api/scores/admin") resp = json_response(conn, 200) @@ -64,7 +64,7 @@ defmodule Stackfooter.ScoreControllerTest do end test "all scores route is unauthenticated" do - conn = get(conn(), "/ob/api/scores") + conn = get(build_conn(), "/ob/api/scores") resp = json_response(conn, 200) @@ -73,7 +73,7 @@ defmodule Stackfooter.ScoreControllerTest do end test "individual scores route is authenticated" do - conn = put_req_header(conn(), "x-starfighter-authorization", @admin_apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @admin_apikey) |> get("/ob/api/scores/admin") resp = json_response(conn, 200) @@ -81,14 +81,14 @@ defmodule Stackfooter.ScoreControllerTest do assert resp assert resp["ok"] - conn = get(conn(), "/ob/api/scores/admin") + conn = get(build_conn(), "/ob/api/scores/admin") resp = json_response(conn, 401) assert resp refute resp["ok"] - conn = put_req_header(conn(), "x-starfighter-authorization", @non_admin_apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @non_admin_apikey) |> get("/ob/api/scores/admin") resp = json_response(conn, 401) diff --git a/test/controllers/venue_controller_test.exs b/test/controllers/venue_controller_test.exs index 255456d..6c07f12 100644 --- a/test/controllers/venue_controller_test.exs +++ b/test/controllers/venue_controller_test.exs @@ -5,7 +5,7 @@ defmodule Stackfooter.VenueControllerTest do @apikey "4cy7uf63Lw2Sx6652YmLwBKy662weU4q" test "returns all open venues" do - conn = get(conn(), "/ob/api/venues/") + conn = get(build_conn(), "/ob/api/venues/") resp = json_response(conn, 200) expected_venues = [%{"id" => 0, "name" => "", "state" => "open", "venue" => "TESTEX"}, @@ -22,14 +22,14 @@ defmodule Stackfooter.VenueControllerTest do Venue.place_order(venue, %{direction: "sell", symbol: "NYC", qty: 5, price: 0, account: "admin", orderType: "market"}) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> delete("/ob/api/venues/obex/stocks/nyc/orders/10") resp = json_response(conn, 401) assert resp refute resp["ok"] assert resp["error"] == "Highest order id is 1" - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> delete("/ob/api/venues/obex/stocks/nyc/orders/-1") resp = json_response(conn, 401) assert resp @@ -43,14 +43,14 @@ defmodule Stackfooter.VenueControllerTest do Venue.place_order(venue, %{direction: "sell", symbol: "NYC", qty: 5, price: 0, account: "admin", orderType: "market"}) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/stocks/nyc/orders/10") resp = json_response(conn, 401) assert resp refute resp["ok"] assert resp["error"] == "Highest order id is 1" - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/stocks/nyc/orders/-1") resp = json_response(conn, 401) assert resp @@ -64,7 +64,7 @@ defmodule Stackfooter.VenueControllerTest do Venue.place_order(venue, %{direction: "sell", symbol: "NYC", qty: 5, price: 0, account: "admin", orderType: "market"}) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/stocks/nyc/orders/a") resp = json_response(conn, 200) assert resp @@ -78,7 +78,7 @@ defmodule Stackfooter.VenueControllerTest do Venue.place_order(venue, %{direction: "sell", symbol: "NYC", qty: 5, price: 0, account: "admin", orderType: "market"}) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> delete("/ob/api/venues/obex/stocks/nyc/orders/a") resp = json_response(conn, 200) assert resp @@ -92,7 +92,7 @@ defmodule Stackfooter.VenueControllerTest do Venue.place_order(venue, %{direction: "sell", symbol: "NYC", qty: 5, price: 0, account: "rjsamson", orderType: "market"}) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/accounts/rjsamson/orders") resp = json_response(conn, 401) @@ -100,7 +100,7 @@ defmodule Stackfooter.VenueControllerTest do refute resp["ok"] assert resp["error"] == "Not authorized to access details about that account's orders." - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/accounts/rjsamson/stocks/nyc/orders") resp = json_response(conn, 401) @@ -115,7 +115,7 @@ defmodule Stackfooter.VenueControllerTest do Venue.place_order(venue, %{direction: "sell", symbol: "NYC", qty: 5, price: 0, account: "rjsamson", orderType: "market"}) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("ob/api/venues/obex/stocks/nyc/orders/0") resp = json_response(conn, 401) @@ -131,7 +131,7 @@ defmodule Stackfooter.VenueControllerTest do Venue.place_order(venue, %{direction: "sell", symbol: "NYC", qty: 5, price: 0, account: "rjsamson", orderType: "market"}) Venue.place_order(venue, %{direction: "sell", symbol: "NYC", qty: 5, price: 100, account: "rjsamson", orderType: "limit"}) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> delete("ob/api/venues/obex/stocks/nyc/orders/0") resp = json_response(conn, 401) @@ -139,7 +139,7 @@ defmodule Stackfooter.VenueControllerTest do refute resp["ok"] assert resp["error"] == "Not authorized to delete that order. You have to own account RJSAMSON." - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> delete("ob/api/venues/obex/stocks/nyc/orders/1") resp = json_response(conn, 401) @@ -158,7 +158,7 @@ defmodule Stackfooter.VenueControllerTest do "price" => 0, "qty" => 0, "symbol" => "NYC", "totalFilled" => 0, "venue" => "OBEX"} - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> post("ob/api/venues/obex/stocks/nyc/orders/0/cancel") resp = json_response(conn, 200) @@ -177,7 +177,7 @@ defmodule Stackfooter.VenueControllerTest do "price" => 0, "qty" => 0, "symbol" => "NYC", "totalFilled" => 0, "venue" => "OBEX"} - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> delete("ob/api/venues/obex/stocks/nyc/orders/0") resp = json_response(conn, 200) @@ -199,7 +199,7 @@ defmodule Stackfooter.VenueControllerTest do "originalQty" => 10, "price" => 0, "qty" => 0, "symbol" => "NYC", "totalFilled" => 10, "venue" => "OBEX"} - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("ob/api/venues/obex/stocks/nyc/orders/2") resp = json_response(conn, 200) @@ -216,7 +216,7 @@ defmodule Stackfooter.VenueControllerTest do Venue.place_order(venue, %{direction: "buy", symbol: "NYC", qty: 7, price: 500, account: "admin", orderType: "limit"}) Venue.place_order(venue, %{direction: "sell", symbol: "NYC", qty: 7, price: 550, account: "admin", orderType: "limit"}) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("ob/api/venues/obex/accounts/admin/orders") resp = json_response(conn, 200) @@ -240,7 +240,7 @@ defmodule Stackfooter.VenueControllerTest do Venue.place_order(venue, %{direction: "buy", symbol: "NYC", qty: 7, price: 500, account: "admin", orderType: "limit"}) Venue.place_order(venue, %{direction: "sell", symbol: "NYC", qty: 7, price: 550, account: "admin", orderType: "limit"}) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("ob/api/venues/obex/accounts/admin/stocks/nyc/orders") resp = json_response(conn, 200) @@ -260,7 +260,7 @@ defmodule Stackfooter.VenueControllerTest do {:ok, venue} = VenueRegistry.lookup(Stackfooter.VenueRegistry, "OBEX") Venue.reset(venue) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/heartbeat") resp = json_response(conn, 200) assert resp @@ -272,7 +272,7 @@ defmodule Stackfooter.VenueControllerTest do {:ok, venue} = VenueRegistry.lookup(Stackfooter.VenueRegistry, "OBEX") Venue.reset(venue) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("ob/api/venues/tryex/stocks/nyc/orders/2") resp = json_response(conn, 404) @@ -310,7 +310,7 @@ defmodule Stackfooter.VenueControllerTest do "qty" => -100, "price" => 5000} - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> put_req_header("content-type", "application/json") |> post("/ob/api/venues/obex/stocks/nyc/orders", Poison.encode!(order)) @@ -326,7 +326,7 @@ defmodule Stackfooter.VenueControllerTest do "qty" => -100, "price" => 5000} - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> put_req_header("content-type", "application/json") |> post("/ob/api/venues/obex/stocks/nyc/orders", Poison.encode!(order)) @@ -340,14 +340,14 @@ defmodule Stackfooter.VenueControllerTest do {:ok, venue} = VenueRegistry.lookup(Stackfooter.VenueRegistry, "OBEX") Venue.reset(venue) - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> post("/ob/api/venues/obex/stocks/nyc/orders", %{"A.B.C:1.:{:{" => "Nothing"}) resp = json_response(conn, 500) assert resp refute resp["ok"] assert resp["error"] == "Invalid JSON." - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> put_req_header("content-type", "") |> post("/ob/api/venues/obex/stocks/nyc/orders", "A.B.C:1.:{:{") resp = json_response(conn, 500) @@ -378,7 +378,7 @@ defmodule Stackfooter.VenueControllerTest do assert resp_fills == [] assert resp_qty == 100 - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> post("/ob/api/venues/obex/stocks/nyc/orders", %{Poison.encode!(order) => "Nothing"}) resp = json_response(conn, 200) assert resp @@ -388,7 +388,7 @@ defmodule Stackfooter.VenueControllerTest do assert resp_fills == [] assert resp_qty == 100 - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> put_req_header("content-type", "application/json") |> post("/ob/api/venues/obex/stocks/nyc/orders", Poison.encode!(order)) resp = json_response(conn, 200) @@ -399,7 +399,7 @@ defmodule Stackfooter.VenueControllerTest do assert resp_fills == [] assert resp_qty == 100 - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> put_req_header("content-type", "") |> post("/ob/api/venues/obex/stocks/nyc/orders", Poison.encode!(order)) resp = json_response(conn, 200) @@ -418,7 +418,7 @@ defmodule Stackfooter.VenueControllerTest do "qty" => 100, "price" => 5000} - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> put_req_header("content-type", "application/json") |> post("/ob/api/venues/obex/stocks/nyc/orders", Poison.encode!(order)) resp = json_response(conn, 200) @@ -464,7 +464,7 @@ defmodule Stackfooter.VenueControllerTest do "symbol" => "NYC", "venue" => "OBEX"} - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/stocks/nyc") resp = json_response(conn, 200) %{"ok" => resp_ok, "asks" => resp_asks, "bids" => resp_bids} = resp @@ -501,7 +501,7 @@ defmodule Stackfooter.VenueControllerTest do "symbol" => "NYC", "venue" => "OBEX"} - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/stocks/nyc/quote") resp = json_response(conn, 200) @@ -531,7 +531,7 @@ defmodule Stackfooter.VenueControllerTest do "totalFilled" => 5, "venue" => "OBEX"} - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> delete("/ob/api/venues/obex/stocks/nyc/orders/10") resp = json_response(conn, 200) %{"ok" => resp_ok, "id" => resp_id, "open" => resp_open, "qty" => resp_qty, "originalQty" => resp_original_qty} = resp @@ -555,7 +555,7 @@ defmodule Stackfooter.VenueControllerTest do "symbol" => "NYC", "venue" => "OBEX"} - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/stocks/nyc/quote") resp = json_response(conn, 200) %{"ok" => resp_ok, "ask" => ask, "askDepth" => ask_depth, "askSize" => ask_size, "bid" => bid, "bidDepth" => bid_depth, "bidSize" => bid_size, "last" => last, "lastSize" => last_size} = resp @@ -591,56 +591,56 @@ defmodule Stackfooter.VenueControllerTest do %{"ok" => resp_ok} = resp assert resp_ok - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/stocks/nyc") resp = json_response(conn, 200) assert resp %{"ok" => resp_ok} = resp assert resp_ok - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/stocks/nyc/quote") resp = json_response(conn, 200) assert resp %{"ok" => resp_ok} = resp assert resp_ok - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/stocks/nyc/orders/0") resp = json_response(conn, 200) assert resp %{"ok" => resp_ok} = resp assert resp_ok - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/heartbeat") resp = json_response(conn, 200) assert resp %{"ok" => resp_ok} = resp assert resp_ok - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/stocks") resp = json_response(conn, 200) assert resp %{"ok" => resp_ok} = resp assert resp_ok - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/accounts/admin/orders") resp = json_response(conn, 200) assert resp %{"ok" => resp_ok} = resp assert resp_ok - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> get("/ob/api/venues/obex/accounts/admin/stocks/nyc/orders") resp = json_response(conn, 200) assert resp %{"ok" => resp_ok} = resp assert resp_ok - conn = put_req_header(conn(), "x-starfighter-authorization", @apikey) + conn = put_req_header(build_conn(), "x-starfighter-authorization", @apikey) |> delete("/ob/api/venues/obex/stocks/nyc/orders/0") resp = json_response(conn, 200) assert resp diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index a9af51b..fb62130 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -37,6 +37,6 @@ defmodule Stackfooter.ConnCase do # Ecto.Adapters.SQL.restart_test_transaction(Stackfooter.Repo, []) # end - {:ok, conn: Phoenix.ConnTest.conn()} + {:ok, conn: Phoenix.ConnTest.build_conn()} end end diff --git a/test/support/model_case.ex b/test/support/model_case.ex index 77a9691..7a412ba 100644 --- a/test/support/model_case.ex +++ b/test/support/model_case.ex @@ -26,9 +26,9 @@ defmodule Stackfooter.ModelCase do end setup tags do - # unless tags[:async] do - # Ecto.Adapters.SQL.restart_test_transaction(Stackfooter.Repo, []) - # end + unless tags[:async] do + # Ecto.Adapters.SQL.restart_test_transaction(Stackfooter.Repo, []) + end :ok end diff --git a/test/support/test_helpers.ex b/test/support/test_helpers.ex new file mode 100644 index 0000000..458f139 --- /dev/null +++ b/test/support/test_helpers.ex @@ -0,0 +1,11 @@ +defmodule Stackfooter.TestHelpers do + def insert_user(attrs \\ %{}) do + params = Dict.merge(%{ + username: "user#{Base.encode16(:crypto.rand_bytes(8))}", + password: "securepassword" + }, attrs) + + changeset = Stackfooter.User.changeset(%Stackfooter.User{}, params) + Stackfooter.Repo.insert!(changeset) + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs index 1e33324..9f52d81 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,5 +1,5 @@ ExUnit.start -# Mix.Task.run "ecto.create", ~w(-r Stackfooter.Repo --quiet) -# Mix.Task.run "ecto.migrate", ~w(-r Stackfooter.Repo --quiet) +Mix.Task.run "ecto.create", ~w(-r Stackfooter.Repo --quiet) +Mix.Task.run "ecto.migrate", ~w(-r Stackfooter.Repo --quiet) # Ecto.Adapters.SQL.begin_test_transaction(Stackfooter.Repo)