-
-
Save pkoch/c2f36bc9f55f4ccd7e015effefd5873f to your computer and use it in GitHub Desktop.
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
diff --git a/test/controllers/admin/admin_paper_vote_controller_test.exs b/test/controllers/admin/admin_paper_vote_controller_test.exs | |
new file mode 100644 | |
index 0000000..f63d82a | |
--- /dev/null | |
+++ b/test/controllers/admin/admin_paper_vote_controller_test.exs | |
@@ -0,0 +1,61 @@ | |
+defmodule Api.Admin.PaperVoteControllerTest do | |
+ use Api.ConnCase | |
+ | |
+ alias Api.{CompetitionActions} | |
+ alias Guardian.{Permissions} | |
+ | |
+ setup %{conn: conn} do | |
+ admin = create_admin() | |
+ c = create_category() | |
+ pv = create_paper_vote(c, admin) | |
+ | |
+ {:ok, jwt, _} = | |
+ Guardian.encode_and_sign(admin, :token, perms: %{admin: Permissions.max}) | |
+ | |
+ {:ok, %{ | |
+ admin: admin, | |
+ jwt: jwt, | |
+ category: c, | |
+ paper_vote: pv, | |
+ conn: | |
+ conn | |
+ |> put_req_header("authorization", "Bearer #{jwt}") | |
+ |> put_req_header("content-type", "application/json") | |
+ }} | |
+ end | |
+ | |
+ test "create", %{conn: conn, jwt: jwt, category: c} do | |
+ conn = post(conn, admin_paper_vote_path(conn, :create), %{category_name: c.name}) | |
+ | |
+ assert json_response(conn, 200) == %{ | |
+ } | |
+ end | |
+ | |
+ test "show", %{conn: conn, jwt: jwt, paper_vote: pv} do | |
+ conn = get(conn, admin_paper_vote_path(conn, :show, id: pv.id) | |
+ | |
+ assert json_response(conn, 200) == %{ | |
+ } | |
+ end | |
+ | |
+ test "404", %{conn: conn, jwt: jwt} do | |
+ conn = get(conn, admin_paper_vote_path(conn, :show, id: pv.id) | |
+ | |
+ assert json_response(conn, 200) == %{ | |
+ } | |
+ end | |
+ | |
+ test "redeem", %{conn: conn, jwt: jwt, paper_vote: pv, team: t, member: m} do | |
+ conn = post(conn, admin_paper_vote_path(conn, :annul, id: pv.id)) | |
+ end | |
+ | |
+ test "redeem error", %{conn: conn, jwt: jwt, paper_vote: pv} do | |
+ end | |
+ | |
+ test "annul", %{conn: conn, jwt: jwt, paper_vote: pv} do | |
+ conn = post(conn, admin_paper_vote_path(conn, :annul, id: pv.id)) | |
+ | |
+ assert json_response(conn, 200) == %{ | |
+ } | |
+ end | |
+end | |
diff --git a/web/controllers/admin/admin_paper_vote_controller.ex b/web/controllers/admin/admin_paper_vote_controller.ex | |
new file mode 100644 | |
index 0000000..ccf2f35 | |
--- /dev/null | |
+++ b/web/controllers/admin/admin_paper_vote_controller.ex | |
@@ -0,0 +1,45 @@ | |
+defmodule Api.Admin.PaperVoteController do | |
+ use Api.Web, :controller | |
+ | |
+ alias Api.{PaperVoteActions, Controller.Errors} | |
+ alias Guardian.Plug.{EnsureAuthenticated, EnsurePermissions} | |
+ | |
+ plug EnsureAuthenticated, [handler: Errors] | |
+ plug EnsurePermissions, [handler: Errors, admin: ~w(full)] | |
+ | |
+ defp _respond(conn, {:ok, pv}) do | |
+ render(conn, "paper_vote.json", paper_vote: pv) | |
+ end | |
+ defp _respond(conn, {:error, cause}) do | |
+ Errors.build(conn, :unprocessable_entity, cause) | |
+ end | |
+ | |
+ def create(conn, %{category_name: c_name}) do | |
+ _respond(conn, PaperVoteActions.create( | |
+ Repo.get_by!(Category, name: c_name), | |
+ SessionActions.current_user(conn) | |
+ )) | |
+ end | |
+ | |
+ def show(conn, %{id: pv_id}) do | |
+ _respond(conn, {:ok, | |
+ Repo.get!(PaperVote, pv_id) | |
+ }) | |
+ end | |
+ | |
+ def redeem(conn, %{id: pv_id, team_id: t_id, member_id: m_id}) do | |
+ _respond(conn, PaperVoteActions.redeem( | |
+ Repo.get!(PaperVote, pv_id), | |
+ Repo.get!(Team, t_id), | |
+ Repo.get!(User, m_id), | |
+ SessionActions.current_user(conn) | |
+ )) | |
+ end | |
+ | |
+ def annul(conn, %{id: pv_id) do | |
+ _respond(conn, PaperVoteActions.annul( | |
+ Repo.get!(PaperVote, pv_id), | |
+ SessionActions.current_user(conn) | |
+ )) | |
+ end | |
+end | |
diff --git a/web/controllers/helpers/errors.ex b/web/controllers/helpers/errors.ex | |
index 39638d7..4d03408 100644 | |
--- a/web/controllers/helpers/errors.ex | |
+++ b/web/controllers/helpers/errors.ex | |
@@ -37,4 +37,6 @@ defmodule Api.Controller.Errors do | |
defp message(:already_started), do: "Competition already started" | |
defp message(:already_ended), do: "Competition already ended" | |
defp message(:not_started), do: "Competition hasn't started yet" | |
+ defp message(:team_disqualified), do: "Team has been disqualified" | |
+ defp message(:team_not_eligible), do: "Team is not eligible" | |
end | |
diff --git a/web/router.ex b/web/router.ex | |
index b204fa5..de5c7cf 100644 | |
--- a/web/router.ex | |
+++ b/web/router.ex | |
@@ -50,6 +50,11 @@ defmodule Api.Router do | |
post "/teams/:id/repo/add_users", Admin.TeamController, :add_users_to_repo | |
post "/competition/start_voting", Admin.CompetitionController, :start_voting | |
post "/competition/end_voting", Admin.CompetitionController, :end_voting | |
+ | |
+ post "/paper_vote", Admin.PaperVoteController, :create | |
+ get "/paper_vote/:id", Admin.PaperVoteController, :show | |
+ post "/paper_vote/:id/redeem", Admin.PaperVoteController, :redeem | |
+ post "/paper_vote/:id/annul", Admin.PaperVoteController, :annul | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment