Skip to content

Commit da3d5a0

Browse files
Merge pull request #2 from elixirschool/user-auth-tests
add factory and tests
2 parents ba35aac + 46f7015 commit da3d5a0

File tree

8 files changed

+70
-5
lines changed

8 files changed

+70
-5
lines changed

lib/pointing_party/card.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule PointingParty.Card do
22
use Ecto.Schema
33
import Ecto.Changeset
4+
import Ecto.Query
45
alias PointingParty.Repo
56
alias PointingParty.Card
67

@@ -21,4 +22,10 @@ defmodule PointingParty.Card do
2122
def get!(id) do
2223
Repo.get!(Card, id)
2324
end
25+
26+
def first do
27+
Card
28+
|> first
29+
|> Repo.one()
30+
end
2431
end

lib/pointing_party_web/controllers/card_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule PointingPartyWeb.CardController do
44

55
def index(conn, _params) do
66
# temporary, just to get something on the page for now
7-
card = Card.get!(1)
7+
card = Card.first()
88
render(conn, "index.html", card: card)
99
end
1010
end

mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ defmodule PointingParty.MixProject do
4242
{:phoenix_live_reload, "~> 1.2", only: :dev},
4343
{:gettext, "~> 0.11"},
4444
{:jason, "~> 1.0"},
45-
{:plug_cowboy, "~> 2.0"}
45+
{:plug_cowboy, "~> 2.0"},
46+
{:ex_machina, "~> 2.3", only: :test}
4647
]
4748
end
4849

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"decimal": {:hex, :decimal, "1.8.0", "ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e", [:mix], [], "hexpm"},
77
"ecto": {:hex, :ecto, "3.1.7", "fa21d06ef56cdc2fdaa62574e8c3ba34a2751d44ea34c30bc65f0728421043e5", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
88
"ecto_sql": {:hex, :ecto_sql, "3.1.6", "1e80e30d16138a729c717f73dcb938590bcdb3a4502f3012414d0cbb261045d8", [:mix], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.1.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.1", [hex: :mariaex, repo: "hexpm", optional: true]}, {:myxql, "~> 0.2.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0 or ~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
9+
"ex_machina": {:hex, :ex_machina, "2.3.0", "92a5ad0a8b10ea6314b876a99c8c9e3f25f4dde71a2a835845b136b9adaf199a", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm"},
910
"file_system": {:hex, :file_system, "0.2.7", "e6f7f155970975789f26e77b8b8d8ab084c59844d8ecfaf58cbda31c494d14aa", [:mix], [], "hexpm"},
1011
"gettext": {:hex, :gettext, "0.17.0", "abe21542c831887a2b16f4c94556db9c421ab301aee417b7c4fbde7fbdbe01ec", [:mix], [], "hexpm"},
1112
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule PointingPartyWeb.CardControllerTest do
2+
use PointingPartyWeb.ConnCase
3+
import PointingParty.Factory
4+
5+
@username "test_user"
6+
7+
describe "authenticated user" do
8+
setup %{conn: conn} do
9+
conn = conn
10+
|> Plug.Test.init_test_session(username: @username)
11+
{:ok, %{conn: conn}}
12+
end
13+
14+
test "GET /cards", %{conn: conn} do
15+
insert(:card)
16+
conn = get(conn, "/cards")
17+
assert html_response(conn, 200) =~ "Ticket Title0"
18+
end
19+
end
20+
21+
describe "unauthenticated user" do
22+
test "GET / cards redirects to the log in page", %{conn: conn} do
23+
conn = get(conn, "/cards")
24+
assert redirected_to(conn) =~ "/login"
25+
end
26+
end
27+
end
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
defmodule PointingPartyWeb.PageControllerTest do
22
use PointingPartyWeb.ConnCase
33

4-
test "GET /", %{conn: conn} do
5-
conn = get(conn, "/")
6-
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
4+
@username "test_user"
5+
6+
describe "authenticated user" do
7+
setup %{conn: conn} do
8+
conn = conn
9+
|> Plug.Conn.assign(:username, @username)
10+
{:ok, %{conn: conn}}
11+
end
12+
13+
test "GET / renders the page with log out link", %{conn: conn} do
14+
conn = get(conn, "/")
15+
assert html_response(conn, 200) =~ "Log Out"
16+
end
17+
end
18+
19+
describe "unauthenticated user" do
20+
test "GET / renders the page with log in link", %{conn: conn} do
21+
conn = get(conn, "/")
22+
assert html_response(conn, 200) =~ "Log In"
23+
end
724
end
825
end

test/support/factory.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule PointingParty.Factory do
2+
use ExMachina.Ecto, repo: PointingParty.Repo
3+
alias PointingParty.Card
4+
5+
def card_factory do
6+
%Card{
7+
title: sequence("Ticket Title"),
8+
description: sequence("Ticket description")
9+
}
10+
end
11+
end

test/test_helper.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
{:ok, _} = Application.ensure_all_started(:ex_machina)
12
ExUnit.start()
23
Ecto.Adapters.SQL.Sandbox.mode(PointingParty.Repo, :manual)

0 commit comments

Comments
 (0)