From 8f3d55d5c10456ce6198e43636d1b47cc93cb789 Mon Sep 17 00:00:00 2001 From: Robert J Samson Date: Thu, 23 Jun 2016 15:06:43 -0400 Subject: [PATCH 1/6] Update to phoenix 1.2 --- mix.exs | 11 ++++++----- mix.lock | 21 +++++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/mix.exs b/mix.exs index 7f8198a..ed01b23 100644 --- a/mix.exs +++ b/mix.exs @@ -20,8 +20,8 @@ defmodule Stackfooter.Mixfile do # Type `mix help compile.app` for more information. def application do [mod: {Stackfooter, []}, - applications: [:phoenix, :phoenix_html, :cowboy, :logger, :gettext, - :phoenix_ecto, :postgrex, :tzdata, :beaker, :comeonin]] + applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, + :gettext, :phoenix_ecto, :postgrex, :tzdata, :beaker, :comeonin]] end # Specifies which paths to compile per environment. @@ -32,15 +32,16 @@ defmodule Stackfooter.Mixfile do # # Type `mix help deps` for examples and options. defp deps do - [{:phoenix, "~> 1.1.6"}, - {:phoenix_ecto, "~> 2.0"}, + [{:phoenix, "~> 1.2.0"}, + {:phoenix_pubsub, "~> 1.0"}, + {:phoenix_ecto, "~> 3.0-rc"}, {:postgrex, ">= 0.0.0"}, {:phoenix_html, "~> 2.3"}, {:phoenix_live_reload, "~> 1.0", only: :dev}, {:gettext, "~> 0.9"}, {:cowboy, "~> 1.0"}, {:timex, "~> 1.0.0"}, - {:beaker, ">= 1.2.0"}, + {:beaker, ">= 1.3.0", git: "https://github.com/rjsamson/beaker.git"}, {:excoveralls, "~> 0.4", only: :test}, {:comeonin, "~> 2.0"}] end diff --git a/mix.lock b/mix.lock index fb24b16..c7902c9 100644 --- a/mix.lock +++ b/mix.lock @@ -1,14 +1,14 @@ -%{"beaker": {:hex, :beaker, "1.2.0", "77b3432c0b7828d6278e052e6d28e02f77c065c7eb4c1abe7db2f9cdbe0a0414", [:mix], [{:phoenix, "~> 1.1", [hex: :phoenix, optional: true]}, {:ecto, "~> 1.1", [hex: :ecto, optional: true]}, {:bureaucrat, "~> 0.1.2", [hex: :bureaucrat, optional: false]}]}, - "bureaucrat": {:hex, :bureaucrat, "0.1.2", "43d2873e65369bea2ecb2477ea5b0b460df1f1a7c2e84e289cffeafaef1deac0", [:mix], [{:plug, "~> 0.14 or ~> 1.0", [hex: :plug, optional: false]}]}, +%{"beaker": {:git, "https://github.com/rjsamson/beaker.git", "af653bd21a072de2d9f670b590d8b6446487c31e", []}, + "bureaucrat": {:hex, :bureaucrat, "0.1.4", "63c955828ba6f47f8978d984031e7a208402f634db899999e12352bb72f21aaf", [:mix], [{:plug, "~> 0.14 or ~> 1.0", [hex: :plug, optional: false]}]}, "certifi": {:hex, :certifi, "0.3.0", "389d4b126a47895fe96d65fcf8681f4d09eca1153dc2243ed6babad0aac1e763", [:rebar3], []}, "combine": {:hex, :combine, "0.7.0", "2ac6ae852a9835fe8189af18121cddd5bed2677f5df706dc0d208af668ab845d", [:mix], []}, "comeonin": {:hex, :comeonin, "2.2.0", "3924d5a2a58fdba5d54a4e404e129da5fe1616241ed5fcfff884a8f98781112d", [:mix, :make, :make], []}, - "connection": {:hex, :connection, "1.0.2", "f4a06dd3ecae4141aa66f94ce92ea4c4b8753069472814932f1cadbc3078ab80", [:mix], []}, + "connection": {:hex, :connection, "1.0.3", "3145f7416be3df248a4935f24e3221dc467c1e3a158d62015b35bd54da365786", [:mix], []}, "cowboy": {:hex, :cowboy, "1.0.4", "a324a8df9f2316c833a470d918aaf73ae894278b8aa6226ce7a9bf699388f878", [:rebar, :make], [{:cowlib, "~> 1.0.0", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.0", [hex: :ranch, optional: false]}]}, "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []}, - "db_connection": {:hex, :db_connection, "0.2.1", "8543ec61432c1bca949dbca596271fb3e2029faf2dc580d281e4f404097f0f4c", [:mix], [{:sbroker, "~> 0.7", [hex: :sbroker, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:connection, "~> 1.0.2", [hex: :connection, optional: false]}]}, - "decimal": {:hex, :decimal, "1.1.1", "a8ff5b673105e6cdaca96f799aeefc6f07142881b616c65db16e14e556b16e76", [:mix], []}, - "ecto": {:hex, :ecto, "1.1.3", "859244ba9b7e10ba18889e48172d6455b552df3f9029bf312bcc7e360e3224de", [:mix], [{:sbroker, "~> 0.7", [hex: :sbroker, optional: true]}, {:postgrex, "~> 0.11.0", [hex: :postgrex, optional: true]}, {:poolboy, "~> 1.4", [hex: :poolboy, optional: false]}, {:poison, "~> 1.0", [hex: :poison, optional: true]}, {:mariaex, "~> 0.5.0", [hex: :mariaex, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}, + "db_connection": {:hex, :db_connection, "1.0.0-rc.3", "d9ceb670fe300271140af46d357b669983cd16bc0d01206d7d3222dde56cf038", [:mix], [{:sbroker, "~> 1.0.0-beta.3", [hex: :sbroker, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:connection, "~> 1.0.2", [hex: :connection, optional: false]}]}, + "decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [:mix], []}, + "ecto": {:hex, :ecto, "2.0.1", "cf97a4d353e14af3d3cc3b4452cfbd18b3aeee1fb4075475efeccec3853444a9", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:postgrex, "~> 0.11.2", [hex: :postgrex, optional: true]}, {:db_connection, "~> 1.0-rc.2", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}]}, "excoveralls": {:hex, :excoveralls, "0.4.6", "022db34cf2470efdbc386f442072e00394a1c21f5fadc6b9bd6084dfdaa55b82", [:mix], [{:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}, {:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}]}, "exjsx": {:hex, :exjsx, "3.2.0", "7136cc739ace295fc74c378f33699e5145bead4fdc1b4799822d0287489136fb", [:mix], [{:jsx, "~> 2.6.2", [hex: :jsx, optional: false]}]}, "fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []}, @@ -17,14 +17,15 @@ "idna": {:hex, :idna, "1.0.3", "d456a8761cad91c97e9788c27002eb3b773adaf5c893275fc35ba4e3434bbd9b", [:rebar3], []}, "jsx": {:hex, :jsx, "2.6.2", "213721e058da0587a4bce3cc8a00ff6684ced229c8f9223245c6ff2c88fbaa5a", [:mix, :rebar], []}, "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []}, - "phoenix": {:hex, :phoenix, "1.1.6", "7bf19002669c8f692f5a9c8d30dab7b49f3dc56228d5bde92a12fb426b4783c2", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}, {:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}]}, - "phoenix_ecto": {:hex, :phoenix_ecto, "2.0.1", "24532a443b1686150fd14819a4a366eccf41135da2a2ed6ae2f78597828ecb0a", [:mix], [{:poison, "~> 1.3", [hex: :poison, optional: true]}, {:phoenix_html, "~> 2.2", [hex: :phoenix_html, optional: true]}, {:ecto, "~> 1.1.2", [hex: :ecto, optional: false]}]}, + "phoenix": {:hex, :phoenix, "1.2.0", "1bdeb99c254f4c534cdf98fd201dede682297ccc62fcac5d57a2627c3b6681fb", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.1", [hex: :plug, optional: false]}, {:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}]}, + "phoenix_ecto": {:hex, :phoenix_ecto, "3.0.0", "b947aaf03d076f5b1448f87828f22fb7710478ee38455c67cc3fe8e9a4dfd015", [:mix], [{:ecto, "~> 2.0.0-rc", [hex: :ecto, optional: false]}, {:phoenix_html, "~> 2.6", [hex: :phoenix_html, optional: true]}]}, "phoenix_html": {:hex, :phoenix_html, "2.6.0", "b9f7e091eb3d908586d9634596478fb9e577ee033d76f4ff327a745569bdd2d8", [:mix], [{:plug, "~> 0.13 or ~> 1.0", [hex: :plug, optional: false]}]}, "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.5", "829218c4152ba1e9848e2bf8e161fcde6b4ec679a516259442561d21fde68d0b", [:mix], [{:phoenix, "~> 1.0 or ~> 1.2-rc", [hex: :phoenix, optional: false]}, {:fs, "~> 0.9.1", [hex: :fs, optional: false]}]}, + "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.0", "c31af4be22afeeebfaf246592778c8c840e5a1ddc7ca87610c41ccfb160c2c57", [:mix], []}, "plug": {:hex, :plug, "1.1.6", "8927e4028433fcb859e000b9389ee9c37c80eb28378eeeea31b0273350bf668b", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}]}, - "poison": {:hex, :poison, "1.5.2", "560bdfb7449e3ddd23a096929fb9fc2122f709bcc758b2d5d5a5c7d0ea848910", [:mix], []}, + "poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []}, "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []}, - "postgrex": {:hex, :postgrex, "0.11.0", "ffc900db99742a57f26da3e56c15a6f38bb419f0d627452da82036b6809b6042", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, optional: false]}, {:db_connection, "~> 0.2", [hex: :db_connection, optional: false]}, {:connection, "~> 1.0", [hex: :connection, optional: false]}]}, + "postgrex": {:hex, :postgrex, "0.11.2", "139755c1359d3c5c6d6e8b1ea72556d39e2746f61c6ddfb442813c91f53487e8", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.0-rc", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}, "ranch": {:hex, :ranch, "1.2.1", "a6fb992c10f2187b46ffd17ce398ddf8a54f691b81768f9ef5f461ea7e28c762", [:make], []}, "ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5", "2e73e068cd6393526f9fa6d399353d7c9477d6886ba005f323b592d389fb47be", [:make], []}, "timex": {:hex, :timex, "1.0.0", "c2f4c86c6ec1af5b6614c85b0afa6bd2422f6d72fa3e8ab42baacb801e3235b5", [:mix], [{:tzdata, "== 0.1.8 or ~> 0.5", [hex: :tzdata, optional: false]}, {:combine, "~> 0.7", [hex: :combine, optional: false]}]}, From 98e196a0c35e249eca0efe9a9aeeb1f308e91329 Mon Sep 17 00:00:00 2001 From: Robert J Samson Date: Thu, 23 Jun 2016 15:14:50 -0400 Subject: [PATCH 2/6] 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) From 9da0af9adb2276f452ca24a189829a183a2a7d83 Mon Sep 17 00:00:00 2001 From: Robert J Samson Date: Thu, 23 Jun 2016 15:20:21 -0400 Subject: [PATCH 3/6] Update travis --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 078fc8b..971af8f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,8 @@ before_script: - MIX_ENV=test mix do ecto.create, ecto.migrate - MIX_ENV=test mix local.hex --force - MIX_ENV=test mix deps.get + - MIX_ENV=test mix ecto.create + - MIX_ENV=test mix ecto.migrate script: - mix test after_script: From 6602809f9b804503225d643cc6d43acfe0f1bb78 Mon Sep 17 00:00:00 2001 From: Robert J Samson Date: Thu, 23 Jun 2016 15:25:43 -0400 Subject: [PATCH 4/6] Fix travis DB --- .travis.yml | 1 + config/travis.exs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 config/travis.exs diff --git a/.travis.yml b/.travis.yml index 971af8f..1eeeb11 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ addons: services: - postgresql before_script: + - cp config/travis.exs config/test.exs - MIX_ENV=test mix do ecto.create, ecto.migrate - MIX_ENV=test mix local.hex --force - MIX_ENV=test mix deps.get diff --git a/config/travis.exs b/config/travis.exs new file mode 100644 index 0000000..5de7098 --- /dev/null +++ b/config/travis.exs @@ -0,0 +1,23 @@ +use Mix.Config + +# We don't run a server during test. If one is required, +# you can enable the server option below. +config :stackfooter, Stackfooter.Endpoint, + http: [port: 4001], + server: false + +# Print only warnings and errors during test +config :logger, level: :warn + +# Configure your database +config :stackfooter, Stackfooter.Repo, + adapter: Ecto.Adapters.Postgres, + username: "postgres", + password: "", + database: "stackfooter_test", + hostname: "localhost", + pool: Ecto.Adapters.SQL.Sandbox + +config :stackfooter, :bootstrap, + default_api_key: "4cy7uf63Lw2Sx6652YmLwBKy662weU4q", + default_account: "admin" From cd0d031d7dc2f1b75d358dc6f3eaeca3a2ef6d5a Mon Sep 17 00:00:00 2001 From: Robert J Samson Date: Thu, 23 Jun 2016 15:32:46 -0400 Subject: [PATCH 5/6] Ecto 2 fix --- config/config.exs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/config.exs b/config/config.exs index 5301395..257b11e 100644 --- a/config/config.exs +++ b/config/config.exs @@ -22,6 +22,8 @@ config :stackfooter, Stackfooter.Endpoint, {:_, Plug.Adapters.Cowboy.Handler, {Stackfooter.Endpoint, []}} ]}]] +config :stackfooter, ecto_repos: [Stackfooter.Repo] + # Configures Elixir's Logger config :logger, :console, format: "$time $metadata[$level] $message\n", From 1c547a0ab912f73d98f8ea2bef868e3d29894ee4 Mon Sep 17 00:00:00 2001 From: Robert J Samson Date: Thu, 23 Jun 2016 20:23:02 -0400 Subject: [PATCH 6/6] Finish Ecto 2 upgrade --- mix.exs | 3 ++- test/support/conn_case.ex | 5 +++++ test/support/model_case.ex | 8 +++++++- test/test_helper.exs | 6 ++++-- web/models/user.ex | 2 +- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/mix.exs b/mix.exs index ed01b23..61437c8 100644 --- a/mix.exs +++ b/mix.exs @@ -54,6 +54,7 @@ defmodule Stackfooter.Mixfile do # See the documentation for `Mix` for more info on aliases. defp aliases do ["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], - "ecto.reset": ["ecto.drop", "ecto.setup"]] + "ecto.reset": ["ecto.drop", "ecto.setup"], + "test": ["ecto.create --quite", "ecto.migrate", "test"]] end end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index fb62130..47f5688 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -36,6 +36,11 @@ defmodule Stackfooter.ConnCase do # unless tags[:async] do # Ecto.Adapters.SQL.restart_test_transaction(Stackfooter.Repo, []) # end + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Stackfooter.Repo) + + unless tags[:async] do + Ecto.Adapters.SQL.Sandbox.mode(Stackfooter.Repo, {:shared, self()}) + end {:ok, conn: Phoenix.ConnTest.build_conn()} end diff --git a/test/support/model_case.ex b/test/support/model_case.ex index 7a412ba..e1bf8c2 100644 --- a/test/support/model_case.ex +++ b/test/support/model_case.ex @@ -26,8 +26,14 @@ defmodule Stackfooter.ModelCase do end setup tags do - unless tags[:async] do + # unless tags[:async] do # Ecto.Adapters.SQL.restart_test_transaction(Stackfooter.Repo, []) + # end + + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Stackfooter.Repo) + + unless tags[:async] do + Ecto.Adapters.SQL.Sandbox.mode(Stackfooter.Repo, {:shared, self()}) end :ok diff --git a/test/test_helper.exs b/test/test_helper.exs index 9f52d81..8e0d4a9 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,5 +1,7 @@ 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) + +Ecto.Adapters.SQL.Sandbox.mode(Stackfooter.Repo, :manual) diff --git a/web/models/user.ex b/web/models/user.ex index 84c03f8..9a69160 100644 --- a/web/models/user.ex +++ b/web/models/user.ex @@ -19,7 +19,7 @@ defmodule Stackfooter.User do If no params are provided, an invalid changeset is returned with no validation performed. """ - def changeset(model, params \\ :empty) do + def changeset(model, params \\ %{}) do model |> cast(params, @required_fields, @optional_fields) |> put_pass_hash()