From 8f19a086dfaeb54e3f64b7c8ba6f3b1d4bee104f Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sun, 18 Feb 2018 16:05:35 -0500 Subject: [PATCH] Game controller accepts opponent id --- lib/chess_web/controllers/game_controller.ex | 7 +++++-- .../controllers/game_controller_test.exs | 17 ++++++----------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/chess_web/controllers/game_controller.ex b/lib/chess_web/controllers/game_controller.ex index ae3a9ae..f2d8cbc 100644 --- a/lib/chess_web/controllers/game_controller.ex +++ b/lib/chess_web/controllers/game_controller.ex @@ -21,10 +21,13 @@ defmodule ChessWeb.GameController do render(conn, "new.html", changeset: changeset, opponents: opponents) end - def create(conn, _params) do + def create(conn, %{"game" => %{"opponent_id" => opponent_id}}) do changeset = Game.create_changeset( %Game{}, - %{user_id: current_user(conn).id} + %{ + user_id: current_user(conn).id, + opponent_id: opponent_id + } ) case Repo.insert(changeset) do diff --git a/test/chess_web/controllers/game_controller_test.exs b/test/chess_web/controllers/game_controller_test.exs index f23b6fc..4da8f4c 100644 --- a/test/chess_web/controllers/game_controller_test.exs +++ b/test/chess_web/controllers/game_controller_test.exs @@ -2,9 +2,9 @@ defmodule Chess.GameControllerTest do use ChessWeb.ConnCase alias Chess.Store.Game - alias Chess.Auth.User alias Chess.Auth.Guardian - @valid_attrs %{} + + import Chess.Factory, only: [create_user: 0, create_user: 2] test "lists all entries on index", %{conn: conn} do conn = login(conn) @@ -13,8 +13,11 @@ defmodule Chess.GameControllerTest do end test "creates resource and redirects when data is valid", %{conn: conn} do + opponent = create_user("daruk", "deathmountain") + attrs = %{"opponent_id" => opponent.id} + conn = login(conn) - conn = post conn, game_path(conn, :create), game: @valid_attrs + conn = post conn, game_path(conn, :create), game: attrs game = Repo.one(Game) assert redirected_to(conn) == game_path(conn, :show, game) end @@ -45,12 +48,4 @@ defmodule Chess.GameControllerTest do user = create_user() conn |> Guardian.Plug.sign_in(user) end - - defp create_user() do - changeset = User.changeset( - %User{}, - %{username: "link@hyrule.kingdom", password: "ilovezelda"} - ) - Repo.insert!(changeset) - end end