lecutre 07
This commit is contained in:
122 files changed
+9723
No files matched your search
@@ -0,0 +1,14 @@
|
||||
defmodule BcitWeb.ErrorHTMLTest do
|
||||
use BcitWeb.ConnCase, async: true
|
||||
|
||||
# Bring render_to_string/4 for testing custom views
|
||||
import Phoenix.Template, only: [render_to_string: 4]
|
||||
|
||||
test "renders 404.html" do
|
||||
assert render_to_string(BcitWeb.ErrorHTML, "404", "html", []) == "Not Found"
|
||||
end
|
||||
|
||||
test "renders 500.html" do
|
||||
assert render_to_string(BcitWeb.ErrorHTML, "500", "html", []) == "Internal Server Error"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
defmodule BcitWeb.ErrorJSONTest do
|
||||
use BcitWeb.ConnCase, async: true
|
||||
|
||||
test "renders 404" do
|
||||
assert BcitWeb.ErrorJSON.render("404.json", %{}) == %{errors: %{detail: "Not Found"}}
|
||||
end
|
||||
|
||||
test "renders 500" do
|
||||
assert BcitWeb.ErrorJSON.render("500.json", %{}) ==
|
||||
%{errors: %{detail: "Internal Server Error"}}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
defmodule BcitWeb.PageControllerTest do
|
||||
use BcitWeb.ConnCase
|
||||
|
||||
test "GET /", %{conn: conn} do
|
||||
conn = get(conn, ~p"/")
|
||||
assert html_response(conn, 200) =~ "Peace of mind from prototype to production"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,95 @@
|
||||
defmodule BcitWeb.StudentControllerTest do
|
||||
use BcitWeb.ConnCase
|
||||
|
||||
import Bcit.SchoolsFixtures
|
||||
|
||||
@create_attrs %{
|
||||
sid: "A00000001",
|
||||
firstname: "some firstname",
|
||||
lastname: "some lastname",
|
||||
gpa: 85.5
|
||||
}
|
||||
@update_attrs %{
|
||||
sid: "A00000002",
|
||||
firstname: "some updated firstname",
|
||||
lastname: "some updated lastname",
|
||||
gpa: 92.3
|
||||
}
|
||||
@invalid_attrs %{sid: nil, firstname: nil, lastname: nil, gpa: nil}
|
||||
|
||||
describe "index" do
|
||||
test "lists all students", %{conn: conn} do
|
||||
conn = get(conn, ~p"/students")
|
||||
assert html_response(conn, 200) =~ "Listing Students"
|
||||
end
|
||||
end
|
||||
|
||||
describe "new student" do
|
||||
test "renders form", %{conn: conn} do
|
||||
conn = get(conn, ~p"/students/new")
|
||||
assert html_response(conn, 200) =~ "New Student"
|
||||
end
|
||||
end
|
||||
|
||||
describe "create student" do
|
||||
test "redirects to show when data is valid", %{conn: conn} do
|
||||
conn = post(conn, ~p"/students", student: @create_attrs)
|
||||
|
||||
assert %{id: id} = redirected_params(conn)
|
||||
assert redirected_to(conn) == ~p"/students/#{id}"
|
||||
|
||||
conn = get(conn, ~p"/students/#{id}")
|
||||
assert html_response(conn, 200) =~ "Student #{id}"
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn} do
|
||||
conn = post(conn, ~p"/students", student: @invalid_attrs)
|
||||
assert html_response(conn, 200) =~ "New Student"
|
||||
end
|
||||
end
|
||||
|
||||
describe "edit student" do
|
||||
setup [:create_student]
|
||||
|
||||
test "renders form for editing chosen student", %{conn: conn, student: student} do
|
||||
conn = get(conn, ~p"/students/#{student}/edit")
|
||||
assert html_response(conn, 200) =~ "Edit Student"
|
||||
end
|
||||
end
|
||||
|
||||
describe "update student" do
|
||||
setup [:create_student]
|
||||
|
||||
test "redirects when data is valid", %{conn: conn, student: student} do
|
||||
conn = put(conn, ~p"/students/#{student}", student: @update_attrs)
|
||||
assert redirected_to(conn) == ~p"/students/#{student}"
|
||||
|
||||
conn = get(conn, ~p"/students/#{student}")
|
||||
assert html_response(conn, 200) =~ "A00000002"
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn, student: student} do
|
||||
conn = put(conn, ~p"/students/#{student}", student: @invalid_attrs)
|
||||
assert html_response(conn, 200) =~ "Edit Student"
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete student" do
|
||||
setup [:create_student]
|
||||
|
||||
test "deletes chosen student", %{conn: conn, student: student} do
|
||||
conn = delete(conn, ~p"/students/#{student}")
|
||||
assert redirected_to(conn) == ~p"/students"
|
||||
|
||||
assert_error_sent 404, fn ->
|
||||
get(conn, ~p"/students/#{student}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp create_student(_) do
|
||||
student = student_fixture()
|
||||
|
||||
%{student: student}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,147 @@
|
||||
defmodule BcitWeb.UserSessionControllerTest do
|
||||
use BcitWeb.ConnCase, async: true
|
||||
|
||||
import Bcit.AccountsFixtures
|
||||
alias Bcit.Accounts
|
||||
|
||||
setup do
|
||||
%{unconfirmed_user: unconfirmed_user_fixture(), user: user_fixture()}
|
||||
end
|
||||
|
||||
describe "POST /users/log-in - email and password" do
|
||||
test "logs the user in", %{conn: conn, user: user} do
|
||||
user = set_password(user)
|
||||
|
||||
conn =
|
||||
post(conn, ~p"/users/log-in", %{
|
||||
"user" => %{"email" => user.email, "password" => valid_user_password()}
|
||||
})
|
||||
|
||||
assert get_session(conn, :user_token)
|
||||
assert redirected_to(conn) == ~p"/"
|
||||
|
||||
# Now do a logged in request and assert on the menu
|
||||
conn = get(conn, ~p"/")
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ user.email
|
||||
assert response =~ ~p"/users/settings"
|
||||
assert response =~ ~p"/users/log-out"
|
||||
end
|
||||
|
||||
test "logs the user in with remember me", %{conn: conn, user: user} do
|
||||
user = set_password(user)
|
||||
|
||||
conn =
|
||||
post(conn, ~p"/users/log-in", %{
|
||||
"user" => %{
|
||||
"email" => user.email,
|
||||
"password" => valid_user_password(),
|
||||
"remember_me" => "true"
|
||||
}
|
||||
})
|
||||
|
||||
assert conn.resp_cookies["_bcit_web_user_remember_me"]
|
||||
assert redirected_to(conn) == ~p"/"
|
||||
end
|
||||
|
||||
test "logs the user in with return to", %{conn: conn, user: user} do
|
||||
user = set_password(user)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> init_test_session(user_return_to: "/foo/bar")
|
||||
|> post(~p"/users/log-in", %{
|
||||
"user" => %{
|
||||
"email" => user.email,
|
||||
"password" => valid_user_password()
|
||||
}
|
||||
})
|
||||
|
||||
assert redirected_to(conn) == "/foo/bar"
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Welcome back!"
|
||||
end
|
||||
|
||||
test "redirects to login page with invalid credentials", %{conn: conn, user: user} do
|
||||
conn =
|
||||
post(conn, ~p"/users/log-in?mode=password", %{
|
||||
"user" => %{"email" => user.email, "password" => "invalid_password"}
|
||||
})
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) == "Invalid email or password"
|
||||
assert redirected_to(conn) == ~p"/users/log-in"
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /users/log-in - magic link" do
|
||||
test "logs the user in", %{conn: conn, user: user} do
|
||||
{token, _hashed_token} = generate_user_magic_link_token(user)
|
||||
|
||||
conn =
|
||||
post(conn, ~p"/users/log-in", %{
|
||||
"user" => %{"token" => token}
|
||||
})
|
||||
|
||||
assert get_session(conn, :user_token)
|
||||
assert redirected_to(conn) == ~p"/"
|
||||
|
||||
# Now do a logged in request and assert on the menu
|
||||
conn = get(conn, ~p"/")
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ user.email
|
||||
assert response =~ ~p"/users/settings"
|
||||
assert response =~ ~p"/users/log-out"
|
||||
end
|
||||
|
||||
test "confirms unconfirmed user", %{conn: conn, unconfirmed_user: user} do
|
||||
{token, _hashed_token} = generate_user_magic_link_token(user)
|
||||
refute user.confirmed_at
|
||||
|
||||
conn =
|
||||
post(conn, ~p"/users/log-in", %{
|
||||
"user" => %{"token" => token},
|
||||
"_action" => "confirmed"
|
||||
})
|
||||
|
||||
assert get_session(conn, :user_token)
|
||||
assert redirected_to(conn) == ~p"/"
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "User confirmed successfully."
|
||||
|
||||
assert Accounts.get_user!(user.id).confirmed_at
|
||||
|
||||
# Now do a logged in request and assert on the menu
|
||||
conn = get(conn, ~p"/")
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ user.email
|
||||
assert response =~ ~p"/users/settings"
|
||||
assert response =~ ~p"/users/log-out"
|
||||
end
|
||||
|
||||
test "redirects to login page when magic link is invalid", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, ~p"/users/log-in", %{
|
||||
"user" => %{"token" => "invalid"}
|
||||
})
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) ==
|
||||
"The link is invalid or it has expired."
|
||||
|
||||
assert redirected_to(conn) == ~p"/users/log-in"
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE /users/log-out" do
|
||||
test "logs the user out", %{conn: conn, user: user} do
|
||||
conn = conn |> log_in_user(user) |> delete(~p"/users/log-out")
|
||||
assert redirected_to(conn) == ~p"/"
|
||||
refute get_session(conn, :user_token)
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Logged out successfully"
|
||||
end
|
||||
|
||||
test "succeeds even if the user is not logged in", %{conn: conn} do
|
||||
conn = delete(conn, ~p"/users/log-out")
|
||||
assert redirected_to(conn) == ~p"/"
|
||||
refute get_session(conn, :user_token)
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Logged out successfully"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user