mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Change username to email
This commit is contained in:
parent
4bf4c4cb45
commit
155284139e
@ -40,9 +40,9 @@ defmodule Chess.Auth do
|
|||||||
end
|
end
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
def authenticate_user(username, password) do
|
def authenticate_user(email, password) do
|
||||||
query = from u in User,
|
query = from u in User,
|
||||||
where: u.username == ^username
|
where: u.email == ^email
|
||||||
|
|
||||||
query
|
query
|
||||||
|> Repo.one
|
|> Repo.one
|
||||||
|
|||||||
@ -7,7 +7,7 @@ defmodule Chess.Auth.User do
|
|||||||
|
|
||||||
schema "users" do
|
schema "users" do
|
||||||
field :name, :string
|
field :name, :string
|
||||||
field :username, :string
|
field :email, :string
|
||||||
field :password, :string, virtual: true
|
field :password, :string, virtual: true
|
||||||
field :password_hash, :string
|
field :password_hash, :string
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ defmodule Chess.Auth.User do
|
|||||||
struct
|
struct
|
||||||
|> cast(params, required_attrs())
|
|> cast(params, required_attrs())
|
||||||
|> validate_required(required_attrs())
|
|> validate_required(required_attrs())
|
||||||
|> unique_constraint(:username)
|
|> unique_constraint(:email)
|
||||||
|> hash_password()
|
|> hash_password()
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -36,5 +36,5 @@ defmodule Chess.Auth.User do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp required_attrs, do: ~w[name username password]a
|
defp required_attrs, do: ~w[name email password]a
|
||||||
end
|
end
|
||||||
|
|||||||
@ -81,7 +81,7 @@ defmodule ChessWeb.GameController do
|
|||||||
defp get_opponents(user) do
|
defp get_opponents(user) do
|
||||||
query = from user in "users",
|
query = from user in "users",
|
||||||
where: user.id != ^user.id,
|
where: user.id != ^user.id,
|
||||||
select: {user.username, user.id}
|
select: {user.name, user.id}
|
||||||
|
|
||||||
query
|
query
|
||||||
|> Repo.all
|
|> Repo.all
|
||||||
|
|||||||
@ -12,9 +12,9 @@ defmodule ChessWeb.SessionController do
|
|||||||
|
|
||||||
def create(
|
def create(
|
||||||
conn,
|
conn,
|
||||||
%{"user" => %{"username" => username, "password" => password}}
|
%{"user" => %{"email" => email, "password" => password}}
|
||||||
) do
|
) do
|
||||||
case Auth.authenticate_user(username, password) do
|
case Auth.authenticate_user(email, password) do
|
||||||
{:ok, user} ->
|
{:ok, user} ->
|
||||||
conn
|
conn
|
||||||
|> Guardian.Plug.sign_in(user)
|
|> Guardian.Plug.sign_in(user)
|
||||||
@ -23,7 +23,7 @@ defmodule ChessWeb.SessionController do
|
|||||||
{:error, _error} ->
|
{:error, _error} ->
|
||||||
changeset = User.changeset(%User{})
|
changeset = User.changeset(%User{})
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Bad username or password")
|
|> put_flash(:error, "Bad email or password")
|
||||||
|> render("new.html", changeset: changeset)
|
|> render("new.html", changeset: changeset)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
<%= for game <- @games do %>
|
<%= for game <- @games do %>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<%= link "Game with #{opponent(@conn, game).username}", to: game_path(@conn, :show, game), class: "btn btn-default btn-xs" %>
|
<%= link "Game with #{opponent(@conn, game).name}", to: game_path(@conn, :show, game), class: "btn btn-default btn-xs" %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
Started on <%= Timex.format!(game.inserted_at, "%b %e at %I:%M %P", :strftime) %>
|
Started on <%= Timex.format!(game.inserted_at, "%b %e at %I:%M %P", :strftime) %>
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
<h2>Game with <%= opponent(@conn, @game).username %></h2>
|
<h2>Game with <%= opponent(@conn, @game).name %></h2>
|
||||||
|
|
||||||
<p><%= link "Back to games", to: game_path(@conn, :index) %></p>
|
<p><%= link "Back to games", to: game_path(@conn, :index) %></p>
|
||||||
|
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
<header role="banner">
|
<header role="banner">
|
||||||
<nav role="user">
|
<nav role="user">
|
||||||
<%= if current_user(@conn) do %>
|
<%= if current_user(@conn) do %>
|
||||||
<%= current_user(@conn).name %> (<%= current_user(@conn).username %>)
|
<%= current_user(@conn).name %> (<%= current_user(@conn).email %>)
|
||||||
|
|
|
|
||||||
<%= link("Log out", to: session_path(@conn, :delete), method: :delete) %>
|
<%= link("Log out", to: session_path(@conn, :delete), method: :delete) %>
|
||||||
<% else %>
|
<% else %>
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
<%= input f, :name %>
|
<%= input f, :name %>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-field">
|
<div class="form-field">
|
||||||
<%= input f, :username %>
|
<%= input f, :email %>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-field">
|
<div class="form-field">
|
||||||
<%= input f, :password, as: :password %>
|
<%= input f, :password, as: :password %>
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="form-field">
|
<div class="form-field">
|
||||||
<%= input f, :username %>
|
<%= input f, :email %>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-field">
|
<div class="form-field">
|
||||||
<%= input f, :password, as: :password %>
|
<%= input f, :password, as: :password %>
|
||||||
|
|||||||
@ -0,0 +1,9 @@
|
|||||||
|
defmodule Chess.Repo.Migrations.ChangeUsernameToEmail do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
drop index(:users, [:username])
|
||||||
|
rename(table("users"), :username, to: :email)
|
||||||
|
create unique_index(:users, [:email])
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -7,7 +7,11 @@ defmodule Chess.UserTest do
|
|||||||
alias Chess.Auth.User
|
alias Chess.Auth.User
|
||||||
alias Chess.Repo
|
alias Chess.Repo
|
||||||
|
|
||||||
@valid_attrs %{name: "Zelda", username: "zelda", password: "password"}
|
@valid_attrs %{
|
||||||
|
name: "Zelda",
|
||||||
|
email: "zelda@hyrule.com",
|
||||||
|
password: "password"
|
||||||
|
}
|
||||||
@invalid_attrs %{}
|
@invalid_attrs %{}
|
||||||
|
|
||||||
test "changeset with valid attributes" do
|
test "changeset with valid attributes" do
|
||||||
@ -20,8 +24,8 @@ defmodule Chess.UserTest do
|
|||||||
refute changeset.valid?
|
refute changeset.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "username must be unique" do
|
test "email must be unique" do
|
||||||
insert(:user, %{username: "zelda"})
|
insert(:user, %{email: "zelda@hyrule.com"})
|
||||||
|
|
||||||
changeset = User.changeset(%User{}, @valid_attrs)
|
changeset = User.changeset(%User{}, @valid_attrs)
|
||||||
{:error, changeset} = Repo.insert(changeset)
|
{:error, changeset} = Repo.insert(changeset)
|
||||||
|
|||||||
@ -6,9 +6,21 @@ defmodule Chess.AuthTest do
|
|||||||
describe "users" do
|
describe "users" do
|
||||||
alias Chess.Auth.User
|
alias Chess.Auth.User
|
||||||
|
|
||||||
@valid_attrs %{name: "some name", password: "some password", username: "some username"}
|
@valid_attrs %{
|
||||||
@update_attrs %{name: "some name", password: "some updated password", username: "some updated username"}
|
name: "some name",
|
||||||
@invalid_attrs %{name: nil, password: nil, username: nil}
|
email: "some email",
|
||||||
|
password: "some password"
|
||||||
|
}
|
||||||
|
@update_attrs %{
|
||||||
|
name: "some name",
|
||||||
|
email: "some updated email",
|
||||||
|
password: "some updated password"
|
||||||
|
}
|
||||||
|
@invalid_attrs %{
|
||||||
|
name: nil,
|
||||||
|
email: nil,
|
||||||
|
password: nil
|
||||||
|
}
|
||||||
|
|
||||||
def user_fixture(attrs \\ %{}) do
|
def user_fixture(attrs \\ %{}) do
|
||||||
{:ok, user} =
|
{:ok, user} =
|
||||||
@ -31,7 +43,7 @@ defmodule Chess.AuthTest do
|
|||||||
|
|
||||||
test "create_user/1 with valid data creates a user" do
|
test "create_user/1 with valid data creates a user" do
|
||||||
assert {:ok, %User{} = user} = Auth.create_user(@valid_attrs)
|
assert {:ok, %User{} = user} = Auth.create_user(@valid_attrs)
|
||||||
assert user.username == "some username"
|
assert user.email == "some email"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create_user/1 with invalid data returns error changeset" do
|
test "create_user/1 with invalid data returns error changeset" do
|
||||||
@ -42,7 +54,7 @@ defmodule Chess.AuthTest do
|
|||||||
user = user_fixture()
|
user = user_fixture()
|
||||||
assert {:ok, user} = Auth.update_user(user, @update_attrs)
|
assert {:ok, user} = Auth.update_user(user, @update_attrs)
|
||||||
assert %User{} = user
|
assert %User{} = user
|
||||||
assert user.username == "some updated username"
|
assert user.email == "some updated email"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "update_user/2 with invalid data returns error changeset" do
|
test "update_user/2 with invalid data returns error changeset" do
|
||||||
@ -63,14 +75,16 @@ defmodule Chess.AuthTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "authenticate_user/1 returns false on incorrect password " do
|
test "authenticate_user/1 returns false on incorrect password " do
|
||||||
user_fixture(username: "link", password: "eyeofsheikah")
|
user_fixture(email: "link@hyrule.com", password: "eyeofsheikah")
|
||||||
assert {:error, message} = Auth.authenticate_user("link", "shadowtemple")
|
assert {:error, message} =
|
||||||
|
Auth.authenticate_user("link@hyrule.com", "shadowtemple")
|
||||||
assert message == "invalid password"
|
assert message == "invalid password"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "authenticate_user/1 returns true on correct password " do
|
test "authenticate_user/1 returns true on correct password " do
|
||||||
user = user_fixture(username: "link", password: "eyeofsheikah")
|
user = user_fixture(email: "link@hyrule.com", password: "eyeofsheikah")
|
||||||
assert {:ok, ^user} = Auth.authenticate_user("link", "eyeofsheikah")
|
assert {:ok, ^user} =
|
||||||
|
Auth.authenticate_user("link@hyrule.com", "eyeofsheikah")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -8,8 +8,8 @@ defmodule Chess.GameTest do
|
|||||||
import Chess.Factory
|
import Chess.Factory
|
||||||
|
|
||||||
test "game is valid with a board and user" do
|
test "game is valid with a board and user" do
|
||||||
user = insert(:user, %{username: "link"})
|
user = insert(:user, %{email: "link@hyrule.com"})
|
||||||
opponent = insert(:user, %{username: "zelda"})
|
opponent = insert(:user, %{email: "zelda@hyrule.com"})
|
||||||
|
|
||||||
attrs = %{
|
attrs = %{
|
||||||
board: %{},
|
board: %{},
|
||||||
@ -37,8 +37,8 @@ defmodule Chess.GameTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "game is invalid without a board" do
|
test "game is invalid without a board" do
|
||||||
user = insert(:user, %{username: "link"})
|
user = insert(:user, %{email: "link@hyrule.com"})
|
||||||
opponent = insert(:user, %{username: "zelda"})
|
opponent = insert(:user, %{email: "zelda@hyrule.com"})
|
||||||
|
|
||||||
attrs = %{board: nil, user_id: user.id, opponent_id: opponent.id}
|
attrs = %{board: nil, user_id: user.id, opponent_id: opponent.id}
|
||||||
changeset = Game.changeset(%Game{}, attrs)
|
changeset = Game.changeset(%Game{}, attrs)
|
||||||
@ -48,7 +48,7 @@ defmodule Chess.GameTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "game is invalid without a user" do
|
test "game is invalid without a user" do
|
||||||
opponent = insert(:user, %{username: "zelda"})
|
opponent = insert(:user, %{email: "zelda@hyrule.com"})
|
||||||
|
|
||||||
attrs = %{board: %{}, opponent_id: opponent.id}
|
attrs = %{board: %{}, opponent_id: opponent.id}
|
||||||
changeset = Game.changeset(%Game{}, attrs)
|
changeset = Game.changeset(%Game{}, attrs)
|
||||||
@ -58,7 +58,7 @@ defmodule Chess.GameTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "game is invalid without an opponent" do
|
test "game is invalid without an opponent" do
|
||||||
user = insert(:user, %{username: "link"})
|
user = insert(:user, %{email: "link@hyrule.com"})
|
||||||
|
|
||||||
attrs = %{board: %{}, user_id: user.id}
|
attrs = %{board: %{}, user_id: user.id}
|
||||||
changeset = Game.changeset(%Game{}, attrs)
|
changeset = Game.changeset(%Game{}, attrs)
|
||||||
|
|||||||
@ -7,7 +7,7 @@ defmodule Chess.ApiGameControllerTest do
|
|||||||
|
|
||||||
test "shows chosen game", %{conn: conn} do
|
test "shows chosen game", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
opponent = insert(:user, %{username: "revali"})
|
opponent = insert(:user, %{email: "revali@rito.village"})
|
||||||
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
||||||
|
|
||||||
conn =
|
conn =
|
||||||
@ -20,10 +20,10 @@ defmodule Chess.ApiGameControllerTest do
|
|||||||
|
|
||||||
test "does not show a game if the user is not a player", %{conn: conn} do
|
test "does not show a game if the user is not a player", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
opponent = insert(:user, %{username: "revali"})
|
opponent = insert(:user, %{email: "revali@rito.village"})
|
||||||
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
||||||
|
|
||||||
other_user = insert(:user, %{username: "mipha"})
|
other_user = insert(:user, %{email: "mipha@zora.domain"})
|
||||||
|
|
||||||
conn =
|
conn =
|
||||||
conn
|
conn
|
||||||
@ -36,7 +36,7 @@ defmodule Chess.ApiGameControllerTest do
|
|||||||
|
|
||||||
test "responds with 403 if user is not logged in", %{conn: conn} do
|
test "responds with 403 if user is not logged in", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
opponent = insert(:user, %{username: "revali"})
|
opponent = insert(:user, %{email: "revali@rito.village"})
|
||||||
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
||||||
|
|
||||||
conn =
|
conn =
|
||||||
@ -48,10 +48,10 @@ defmodule Chess.ApiGameControllerTest do
|
|||||||
|
|
||||||
test "does not update a game if the user is not a player", %{conn: conn} do
|
test "does not update a game if the user is not a player", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
opponent = insert(:user, %{username: "revali"})
|
opponent = insert(:user, %{email: "revali@rito.village"})
|
||||||
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
||||||
|
|
||||||
other_user = insert(:user, %{username: "mipha"})
|
other_user = insert(:user, %{email: "mipha@zora.domain"})
|
||||||
|
|
||||||
conn =
|
conn =
|
||||||
conn
|
conn
|
||||||
|
|||||||
@ -18,7 +18,7 @@ defmodule Chess.GameControllerTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "creates resource and redirects when data is valid", %{conn: conn} do
|
test "creates resource and redirects when data is valid", %{conn: conn} do
|
||||||
opponent = insert(:user, %{username: "daruk"})
|
opponent = insert(:user, %{email: "daruk@goron.city"})
|
||||||
attrs = %{"opponent_id" => opponent.id}
|
attrs = %{"opponent_id" => opponent.id}
|
||||||
|
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
@ -35,7 +35,7 @@ defmodule Chess.GameControllerTest do
|
|||||||
|
|
||||||
test "shows chosen game", %{conn: conn} do
|
test "shows chosen game", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
opponent = insert(:user, %{username: "revali"})
|
opponent = insert(:user, %{email: "revali@rito.village"})
|
||||||
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
||||||
|
|
||||||
conn =
|
conn =
|
||||||
@ -48,10 +48,10 @@ defmodule Chess.GameControllerTest do
|
|||||||
|
|
||||||
test "does not show a game if the user is not a player", %{conn: conn} do
|
test "does not show a game if the user is not a player", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
opponent = insert(:user, %{username: "revali"})
|
opponent = insert(:user, %{email: "revali@rito.village"})
|
||||||
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
||||||
|
|
||||||
other_user = insert(:user, %{username: "mipha"})
|
other_user = insert(:user, %{email: "mipha@zora.domain"})
|
||||||
|
|
||||||
conn =
|
conn =
|
||||||
conn
|
conn
|
||||||
|
|||||||
@ -12,67 +12,89 @@ defmodule Chess.GamesTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "can create a new game", %{session: session} do
|
test "can create a new game", %{session: session} do
|
||||||
insert(:user, %{username: "zelda", password: "ganonsucks"})
|
insert(:user, %{
|
||||||
|
name: "Zelda",
|
||||||
|
email: "zelda@hyrule.com",
|
||||||
|
password: "ganonsucks"
|
||||||
|
})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> create_user_and_login()
|
|> create_user_and_login()
|
||||||
|> visit("/games")
|
|> visit("/games")
|
||||||
|> click(link("New game"))
|
|> click(link("New game"))
|
||||||
|> select("game[opponent_id]", option: "zelda")
|
|> select("game[opponent_id]", option: "Zelda")
|
||||||
|> click(button("Create game"))
|
|> click(button("Create game"))
|
||||||
|
|
||||||
session
|
session
|
||||||
|> assert_has(css("h2", text: "Game with zelda"))
|
|> assert_has(css("h2", text: "Game with Zelda"))
|
||||||
|> assert_has(css(".board"))
|
|> assert_has(css(".board"))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "can only see own games", %{session: session} do
|
test "can only see own games", %{session: session} do
|
||||||
opponent = insert(:user, %{username: "urbosa", password: "gerudoqueen"})
|
opponent = insert(:user, %{
|
||||||
|
name: "Urbosa",
|
||||||
user = insert(:user, %{username: "zelda", password: "ganonsucks"})
|
email: "urbosa@gerudo.town",
|
||||||
|
password: "gerudoqueen"
|
||||||
|
})
|
||||||
|
user = insert(:user, %{
|
||||||
|
name: "Zelda",
|
||||||
|
email: "zelda@hyrule.com",
|
||||||
|
password: "ganonsucks"
|
||||||
|
})
|
||||||
insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> create_user_and_login()
|
|> create_user_and_login()
|
||||||
|> visit("/games")
|
|> visit("/games")
|
||||||
|> click(link("New game"))
|
|> click(link("New game"))
|
||||||
|> select("game[opponent_id]", option: "urbosa")
|
|> select("game[opponent_id]", option: "Urbosa")
|
||||||
|> click(button("Create game"))
|
|> click(button("Create game"))
|
||||||
|> click(link("Back to games"))
|
|> click(link("Back to games"))
|
||||||
|
|
||||||
session
|
session
|
||||||
|> assert_has(css(".table tr", count: 1))
|
|> assert_has(css(".table tr", count: 1))
|
||||||
|> assert_has(link("Game with urbosa"))
|
|> assert_has(link("Game with Urbosa"))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "can see games as an opponent", %{session: session} do
|
test "can see games as an opponent", %{session: session} do
|
||||||
opponent = insert(:user, %{username: "urbosa", password: "gerudoqueen"})
|
opponent = insert(:user, %{
|
||||||
|
name: "Urbosa",
|
||||||
user = insert(:user, %{username: "zelda", password: "ganonsucks"})
|
email: "urbosa@gerudo.town",
|
||||||
|
password: "gerudoqueen"
|
||||||
|
})
|
||||||
|
user = insert(:user, %{
|
||||||
|
name: "Zelda",
|
||||||
|
email: "zelda@hyrule.com",
|
||||||
|
password: "ganonsucks"
|
||||||
|
})
|
||||||
insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> login("urbosa", "gerudoqueen")
|
|> login("urbosa@gerudo.town", "gerudoqueen")
|
||||||
|
|
||||||
session
|
session
|
||||||
|> assert_has(css(".table tr", count: 1))
|
|> assert_has(css(".table tr", count: 1))
|
||||||
|> assert_has(link("Game with zelda"))
|
|> assert_has(link("Game with Zelda"))
|
||||||
|
|
||||||
session
|
session
|
||||||
|> click(link("Game with zelda"))
|
|> click(link("Game with Zelda"))
|
||||||
|
|
||||||
session
|
session
|
||||||
|> assert_has(css("h2", text: "Game with zelda"))
|
|> assert_has(css("h2", text: "Game with Zelda"))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "can move a piece", %{session: session} do
|
test "can move a piece", %{session: session} do
|
||||||
insert(:user, %{username: "zelda", password: "ganonsucks"})
|
insert(:user, %{
|
||||||
|
name: "Zelda",
|
||||||
|
email: "zelda@hyrule.com",
|
||||||
|
password: "ganonsucks"
|
||||||
|
})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> create_user_and_login()
|
|> create_user_and_login()
|
||||||
|> visit("/games")
|
|> visit("/games")
|
||||||
|> click(link("New game"))
|
|> click(link("New game"))
|
||||||
|> select("game[opponent_id]", option: "zelda")
|
|> select("game[opponent_id]", option: "Zelda")
|
||||||
|> click(button("Create game"))
|
|> click(button("Create game"))
|
||||||
|
|
||||||
session
|
session
|
||||||
@ -87,13 +109,17 @@ defmodule Chess.GamesTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "cannot move the opponents pieces", %{session: session} do
|
test "cannot move the opponents pieces", %{session: session} do
|
||||||
insert(:user, %{username: "zelda", password: "ganonsucks"})
|
insert(:user, %{
|
||||||
|
name: "Zelda",
|
||||||
|
email: "zelda@hyrule.com",
|
||||||
|
password: "ganonsucks"
|
||||||
|
})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> create_user_and_login()
|
|> create_user_and_login()
|
||||||
|> visit("/games")
|
|> visit("/games")
|
||||||
|> click(link("New game"))
|
|> click(link("New game"))
|
||||||
|> select("game[opponent_id]", option: "zelda")
|
|> select("game[opponent_id]", option: "Zelda")
|
||||||
|> click(button("Create game"))
|
|> click(button("Create game"))
|
||||||
|
|
||||||
session
|
session
|
||||||
@ -102,20 +128,28 @@ defmodule Chess.GamesTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "cannot move pieces when it's the opponents turn", %{session: session} do
|
test "cannot move pieces when it's the opponents turn", %{session: session} do
|
||||||
insert(:user, %{username: "link", password: "ilovezelda"})
|
insert(:user, %{
|
||||||
insert(:user, %{username: "zelda", password: "ganonsucks"})
|
name: "Link",
|
||||||
|
email: "link@hyrule.com",
|
||||||
|
password: "ilovezelda"
|
||||||
|
})
|
||||||
|
insert(:user, %{
|
||||||
|
name: "Zelda",
|
||||||
|
email: "zelda@hyrule.com",
|
||||||
|
password: "ganonsucks"
|
||||||
|
})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> login("link", "ilovezelda")
|
|> login("link@hyrule.com", "ilovezelda")
|
||||||
|> visit("/games")
|
|> visit("/games")
|
||||||
|> click(link("New game"))
|
|> click(link("New game"))
|
||||||
|> select("game[opponent_id]", option: "zelda")
|
|> select("game[opponent_id]", option: "Zelda")
|
||||||
|> click(button("Create game"))
|
|> click(button("Create game"))
|
||||||
|
|
||||||
{:ok, session2} = Wallaby.start_session
|
{:ok, session2} = Wallaby.start_session
|
||||||
session2
|
session2
|
||||||
|> login("zelda", "ganonsucks")
|
|> login("zelda@hyrule.com", "ganonsucks")
|
||||||
|> click(link("Game with link"))
|
|> click(link("Game with Link"))
|
||||||
|
|
||||||
session2
|
session2
|
||||||
|> click(css("#f4-r6"))
|
|> click(css("#f4-r6"))
|
||||||
@ -123,20 +157,28 @@ defmodule Chess.GamesTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "move is reflected on opponents screen", %{session: session} do
|
test "move is reflected on opponents screen", %{session: session} do
|
||||||
insert(:user, %{username: "link", password: "ilovezelda"})
|
insert(:user, %{
|
||||||
insert(:user, %{username: "zelda", password: "ganonsucks"})
|
name: "Link",
|
||||||
|
email: "link@hyrule.com",
|
||||||
|
password: "ilovezelda"
|
||||||
|
})
|
||||||
|
insert(:user, %{
|
||||||
|
name: "Zelda",
|
||||||
|
email: "zelda@hyrule.com",
|
||||||
|
password: "ganonsucks"
|
||||||
|
})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> login("link", "ilovezelda")
|
|> login("link@hyrule.com", "ilovezelda")
|
||||||
|> visit("/games")
|
|> visit("/games")
|
||||||
|> click(link("New game"))
|
|> click(link("New game"))
|
||||||
|> select("game[opponent_id]", option: "zelda")
|
|> select("game[opponent_id]", option: "Zelda")
|
||||||
|> click(button("Create game"))
|
|> click(button("Create game"))
|
||||||
|
|
||||||
{:ok, session2} = Wallaby.start_session
|
{:ok, session2} = Wallaby.start_session
|
||||||
session2
|
session2
|
||||||
|> login("zelda", "ganonsucks")
|
|> login("zelda@hyrule.com", "ganonsucks")
|
||||||
|> click(link("Game with link"))
|
|> click(link("Game with Link"))
|
||||||
|
|
||||||
session
|
session
|
||||||
|> click(css("#f4-r1"))
|
|> click(css("#f4-r1"))
|
||||||
@ -149,16 +191,20 @@ defmodule Chess.GamesTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp create_user_and_login(session) do
|
defp create_user_and_login(session) do
|
||||||
insert(:user, %{username: "link", password: "ilovezelda"})
|
insert(:user, %{
|
||||||
|
name: "Link",
|
||||||
|
email: "link@hyrule.com",
|
||||||
|
password: "ilovezelda"
|
||||||
|
})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> login("link", "ilovezelda")
|
|> login("link@hyrule.com", "ilovezelda")
|
||||||
end
|
end
|
||||||
|
|
||||||
defp login(session, username, password) do
|
defp login(session, email, password) do
|
||||||
session
|
session
|
||||||
|> visit("/session/new")
|
|> visit("/session/new")
|
||||||
|> fill_in(text_field("Username"), with: username)
|
|> fill_in(text_field("Email"), with: email)
|
||||||
|> fill_in(text_field("Password"), with: password)
|
|> fill_in(text_field("Password"), with: password)
|
||||||
|> click(button("Log in"))
|
|> click(button("Log in"))
|
||||||
end
|
end
|
||||||
|
|||||||
@ -8,7 +8,7 @@ defmodule Chess.RegistrationTest do
|
|||||||
|> visit("/")
|
|> visit("/")
|
||||||
|> click(link("Register"))
|
|> click(link("Register"))
|
||||||
|> fill_in(text_field("Name"), with: "Link")
|
|> fill_in(text_field("Name"), with: "Link")
|
||||||
|> fill_in(text_field("Username"), with: "link@example.com")
|
|> fill_in(text_field("Email"), with: "link@example.com")
|
||||||
|> fill_in(text_field("Password"), with: "ilovezelda")
|
|> fill_in(text_field("Password"), with: "ilovezelda")
|
||||||
|> click(button("Register"))
|
|> click(button("Register"))
|
||||||
|
|
||||||
|
|||||||
@ -4,39 +4,39 @@ defmodule Chess.SessionTest do
|
|||||||
import Wallaby.Query, only: [text_field: 1, link: 1, button: 1]
|
import Wallaby.Query, only: [text_field: 1, link: 1, button: 1]
|
||||||
import Chess.Factory
|
import Chess.Factory
|
||||||
|
|
||||||
test "user cannot log in with incorrect username", %{session: session} do
|
test "user cannot log in with incorrect email", %{session: session} do
|
||||||
insert(:user, %{username: "link@hyrule.kingdom", password: "ilovezelda"})
|
insert(:user, %{email: "link@hyrule.kingdom", password: "ilovezelda"})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> visit("/")
|
|> visit("/")
|
||||||
|> click(link("Log in"))
|
|> click(link("Log in"))
|
||||||
|> fill_in(text_field("Username"), with: "link@example.com")
|
|> fill_in(text_field("Email"), with: "link@example.com")
|
||||||
|> fill_in(text_field("Password"), with: "ilovezelda")
|
|> fill_in(text_field("Password"), with: "ilovezelda")
|
||||||
|> click(button("Log in"))
|
|> click(button("Log in"))
|
||||||
|
|
||||||
assert session |> has_text?("Bad username or password")
|
assert session |> has_text?("Bad email or password")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "user cannot log in with incorrect password", %{session: session} do
|
test "user cannot log in with incorrect password", %{session: session} do
|
||||||
insert(:user, %{username: "link@hyrule.kingdom", password: "ilovezelda"})
|
insert(:user, %{email: "link@hyrule.kingdom", password: "ilovezelda"})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> visit("/")
|
|> visit("/")
|
||||||
|> click(link("Log in"))
|
|> click(link("Log in"))
|
||||||
|> fill_in(text_field("Username"), with: "link@hyrule.kingdom")
|
|> fill_in(text_field("Email"), with: "link@hyrule.kingdom")
|
||||||
|> fill_in(text_field("Password"), with: "calamityganon")
|
|> fill_in(text_field("Password"), with: "calamityganon")
|
||||||
|> click(button("Log in"))
|
|> click(button("Log in"))
|
||||||
|
|
||||||
assert session |> has_text?("Bad username or password")
|
assert session |> has_text?("Bad email or password")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "user can log in with correct details", %{session: session} do
|
test "user can log in with correct details", %{session: session} do
|
||||||
insert(:user, %{username: "link@hyrule.kingdom", password: "ilovezelda"})
|
insert(:user, %{email: "link@hyrule.kingdom", password: "ilovezelda"})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> visit("/")
|
|> visit("/")
|
||||||
|> click(link("Log in"))
|
|> click(link("Log in"))
|
||||||
|> fill_in(text_field("Username"), with: "link@hyrule.kingdom")
|
|> fill_in(text_field("Email"), with: "link@hyrule.kingdom")
|
||||||
|> fill_in(text_field("Password"), with: "ilovezelda")
|
|> fill_in(text_field("Password"), with: "ilovezelda")
|
||||||
|> click(button("Log in"))
|
|> click(button("Log in"))
|
||||||
|
|
||||||
@ -46,12 +46,12 @@ defmodule Chess.SessionTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "user can log out", %{session: session} do
|
test "user can log out", %{session: session} do
|
||||||
insert(:user, %{username: "link@hyrule.kingdom", password: "ilovezelda"})
|
insert(:user, %{email: "link@hyrule.kingdom", password: "ilovezelda"})
|
||||||
|
|
||||||
session
|
session
|
||||||
|> visit("/")
|
|> visit("/")
|
||||||
|> click(link("Log in"))
|
|> click(link("Log in"))
|
||||||
|> fill_in(text_field("Username"), with: "link@hyrule.kingdom")
|
|> fill_in(text_field("Email"), with: "link@hyrule.kingdom")
|
||||||
|> fill_in(text_field("Password"), with: "ilovezelda")
|
|> fill_in(text_field("Password"), with: "ilovezelda")
|
||||||
|> click(button("Log in"))
|
|> click(button("Log in"))
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@ defmodule Chess.Factory do
|
|||||||
def insert(:user, params) do
|
def insert(:user, params) do
|
||||||
attrs = %{
|
attrs = %{
|
||||||
name: "Zelda",
|
name: "Zelda",
|
||||||
username: "zelda",
|
email: "zelda@hyrule.com",
|
||||||
password: "ilovelink"
|
password: "ilovelink"
|
||||||
}
|
}
|
||||||
|> Map.merge(params)
|
|> Map.merge(params)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user