-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: upgrade guardian controller to 1.7
- Loading branch information
1 parent
4c82d3e
commit b31e714
Showing
7 changed files
with
272 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
defmodule BokkenWeb.GuardianControllerTest do | ||
use BokkenWeb.ConnCase | ||
|
||
alias Bokken.Accounts | ||
alias Bokken.Accounts.Guardian | ||
|
||
import Bokken.Factory | ||
|
||
setup %{conn: conn} do | ||
password = "password1234!" | ||
guardian_user = insert(:user, role: "guardian", password: password) | ||
|
||
{:ok, user} = Accounts.authenticate_user(guardian_user.email, password) | ||
|
||
{:ok, conn: log_in_user(conn, user)} | ||
end | ||
|
||
describe "index" do | ||
test "lists all guardians", %{conn: conn} do | ||
conn = get(conn, ~p"/api/guardians/") | ||
assert json_response(conn, 200)["data"] == [] | ||
end | ||
end | ||
|
||
describe "create guardian" do | ||
test "renders guardian when data is valid", %{conn: conn} do | ||
new_user = insert(:user, role: "guardian") | ||
|
||
guardian_attrs = %{ | ||
city: "Guimarães", | ||
mobile: "+351915196743", | ||
first_name: "Carla Maria", | ||
last_name: "Silva Costa", | ||
user_id: new_user.id | ||
} | ||
|
||
guardian = params_for(:guardian, guardian_attrs) | ||
|
||
conn = post(conn, ~p"/api/guardians/guardian", guardian: guardian) | ||
assert %{"id" => id} = json_response(conn, 201)["data"] | ||
|
||
conn = get(conn, Routes.guardian_path(conn, :show, id)) | ||
|
||
assert %{ | ||
"id" => _id, | ||
"first_name" => "Carla Maria", | ||
"last_name" => "Silva Costa" | ||
} = json_response(conn, 200)["data"] | ||
|
||
assert not Map.has_key?(json_response(conn, 200)["data"], "city") | ||
assert not Map.has_key?(json_response(conn, 200)["data"], "mobile") | ||
end | ||
|
||
test "renders errors when data is invalid", %{conn: conn} do | ||
guardian = params_for(:guardian, mobile: nil) | ||
user_id = get_req_header(conn, "user_id") | ||
user_id = Enum.at(user_id, 0) | ||
guardian = Map.put(guardian, :user_id, user_id) | ||
conn = post(conn, Routes.guardian_path(conn, :create), guardian: guardian) | ||
assert json_response(conn, 422)["errors"] != %{} | ||
end | ||
end | ||
|
||
describe "update guardian" do | ||
setup [:new_guardian_update] | ||
|
||
test "renders guardian when data is valid", %{ | ||
conn: conn, | ||
guardian: %Guardian{id: _id} = guardian | ||
} do | ||
update_attrs = %{ | ||
mobile: "+351915096743", | ||
first_name: "Ana Maria", | ||
last_name: "Silva Costa", | ||
city: "Guimarães" | ||
} | ||
|
||
conn = put(conn, Routes.guardian_path(conn, :update, guardian), guardian: update_attrs) | ||
assert %{"id" => id} = json_response(conn, 200)["data"] | ||
|
||
conn = get(conn, Routes.guardian_path(conn, :show, id)) | ||
|
||
assert %{ | ||
"first_name" => "Ana Maria", | ||
"last_name" => "Silva Costa" | ||
} = json_response(conn, 200)["data"] | ||
|
||
assert not Map.has_key?(json_response(conn, 200)["data"], "mobile") | ||
assert not Map.has_key?(json_response(conn, 200)["data"], "city") | ||
end | ||
|
||
test "renders errors when data is invalid", %{conn: conn, guardian: guardian} do | ||
invalid_attrs = %{mobile: nil} | ||
conn = put(conn, Routes.guardian_path(conn, :update, guardian), guardian: invalid_attrs) | ||
assert json_response(conn, 422)["errors"] != %{} | ||
end | ||
end | ||
|
||
describe "delete guardian" do | ||
setup [:new_guardian] | ||
|
||
test "deletes chosen guardian", %{conn: conn, guardian: guardian} do | ||
conn = delete(conn, Routes.guardian_path(conn, :delete, guardian)) | ||
assert response(conn, 204) | ||
|
||
assert_error_sent 404, fn -> | ||
get(conn, Routes.guardian_path(conn, :show, guardian)) | ||
end | ||
end | ||
end | ||
|
||
defp new_guardian(_) do | ||
guardian = insert(:guardian) | ||
%{guardian: guardian} | ||
end | ||
|
||
defp new_guardian_update(_) do | ||
guardian = insert(:guardian) | ||
%{guardian: guardian} | ||
end | ||
end |
10 changes: 4 additions & 6 deletions
10
lib/bokken_web/views/admin/guardian_view.ex → ...en_web/controllers/admin/guardian_json.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 8 additions & 11 deletions
19
lib/bokken_web/views/guardian_view.ex → lib/bokken_web/controllers/guardian_json.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
test/bokken_web/controllers/admin/guardian_controller_test.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# defmodule BokkenWeb.Admin.GuardianControllerTest do | ||
# use BokkenWeb.ConnCase | ||
|
||
# alias Bokken.Accounts | ||
# alias Bokken.Accounts.Guardian | ||
|
||
# import Bokken.Factory | ||
|
||
# setup %{conn: conn} do | ||
# password = "password1234!" | ||
# guardian_user = insert(:user, role: "organizer", password: password) | ||
|
||
# {:ok, user} = Accounts.authenticate_user(guardian_user.email, password) | ||
|
||
# {:ok, conn: log_in_user(conn, user)} | ||
# end | ||
|
||
# describe "index" do | ||
# test "lists all guardians", %{conn: conn} do | ||
# conn = get(conn, Routes.guardian_path(conn, :index)) | ||
# assert json_response(conn, 200)["data"] == [] | ||
# end | ||
# end | ||
|
||
# describe "create guardian" do | ||
# test "renders guardian when data is valid", %{conn: conn} do | ||
# new_user = insert(:user, role: "guardian") | ||
|
||
# guardian_attrs = %{ | ||
# city: "Guimarães", | ||
# mobile: "+351915196743", | ||
# first_name: "Carla Maria", | ||
# last_name: "Silva Costa", | ||
# user_id: new_user.id | ||
# } | ||
|
||
# guardian = params_for(:guardian, guardian_attrs) | ||
|
||
# conn = post(conn, Routes.guardian_path(conn, :create), guardian: guardian) | ||
# assert %{"id" => id} = json_response(conn, 201)["data"] | ||
|
||
# conn = get(conn, Routes.guardian_path(conn, :show, id)) | ||
|
||
# assert %{ | ||
# "id" => _id, | ||
# "first_name" => "Carla Maria", | ||
# "last_name" => "Silva Costa" | ||
# } = json_response(conn, 200)["data"] | ||
|
||
# assert not Map.has_key?(json_response(conn, 200)["data"], "city") | ||
# assert not Map.has_key?(json_response(conn, 200)["data"], "mobile") | ||
# end | ||
|
||
# test "renders errors when data is invalid", %{conn: conn} do | ||
# guardian = params_for(:guardian, mobile: nil) | ||
# user_id = get_req_header(conn, "user_id") | ||
# user_id = Enum.at(user_id, 0) | ||
# guardian = Map.put(guardian, :user_id, user_id) | ||
# conn = post(conn, Routes.guardian_path(conn, :create), guardian: guardian) | ||
# assert json_response(conn, 422)["errors"] != %{} | ||
# end | ||
# end | ||
|
||
# describe "update guardian" do | ||
# setup [:new_guardian_update] | ||
|
||
# test "renders guardian when data is valid", %{ | ||
# conn: conn, | ||
# guardian: %Guardian{id: _id} = guardian | ||
# } do | ||
# update_attrs = %{ | ||
# mobile: "+351915096743", | ||
# first_name: "Ana Maria", | ||
# last_name: "Silva Costa", | ||
# city: "Guimarães" | ||
# } | ||
|
||
# conn = put(conn, Routes.guardian_path(conn, :update, guardian), guardian: update_attrs) | ||
# assert %{"id" => id} = json_response(conn, 200)["data"] | ||
|
||
# conn = get(conn, Routes.guardian_path(conn, :show, id)) | ||
|
||
# assert %{ | ||
# "first_name" => "Ana Maria", | ||
# "last_name" => "Silva Costa" | ||
# } = json_response(conn, 200)["data"] | ||
|
||
# assert not Map.has_key?(json_response(conn, 200)["data"], "mobile") | ||
# assert not Map.has_key?(json_response(conn, 200)["data"], "city") | ||
# end | ||
|
||
# test "renders errors when data is invalid", %{conn: conn, guardian: guardian} do | ||
# invalid_attrs = %{mobile: nil} | ||
# conn = put(conn, Routes.guardian_path(conn, :update, guardian), guardian: invalid_attrs) | ||
# assert json_response(conn, 422)["errors"] != %{} | ||
# end | ||
# end | ||
|
||
# describe "delete guardian" do | ||
# setup [:new_guardian] | ||
|
||
# test "deletes chosen guardian", %{conn: conn, guardian: guardian} do | ||
# conn = delete(conn, Routes.guardian_path(conn, :delete, guardian)) | ||
# assert response(conn, 204) | ||
|
||
# assert_error_sent 404, fn -> | ||
# get(conn, Routes.guardian_path(conn, :show, guardian)) | ||
# end | ||
# end | ||
# end | ||
|
||
# defp new_guardian(_) do | ||
# guardian = insert(:guardian) | ||
# %{guardian: guardian} | ||
# end | ||
|
||
# defp new_guardian_update(_) do | ||
# guardian = insert(:guardian) | ||
# %{guardian: guardian} | ||
# end | ||
# end |
Oops, something went wrong.