From 5b8d32e4c3807a78b0498959312c7047c27498e2 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Sat, 12 Sep 2020 19:47:28 +0100 Subject: [PATCH] creating plumbing for /approles/:client_id request https://github.com/dwyl/auth/issues/110#issuecomment-691529753 --- lib/auth/role.ex | 2 +- lib/auth_web/controllers/app_controller.ex | 15 ++++++++++----- lib/auth_web/router.ex | 17 +++++++++-------- .../controllers/app_controller_test.exs | 8 ++++++++ 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/auth/role.ex b/lib/auth/role.ex index 29f92e9a..5bae5490 100644 --- a/lib/auth/role.ex +++ b/lib/auth/role.ex @@ -109,7 +109,7 @@ defmodule Auth.Role do end end - defp strip_meta(struct) do + def strip_meta(struct) do struct |> Map.delete(:__meta__) |> Map.delete(:__struct__) diff --git a/lib/auth_web/controllers/app_controller.ex b/lib/auth_web/controllers/app_controller.ex index 140c9dd2..4ec22c99 100644 --- a/lib/auth_web/controllers/app_controller.ex +++ b/lib/auth_web/controllers/app_controller.ex @@ -119,10 +119,15 @@ defmodule AuthWeb.AppController do end end - # @doc """ - # approles/2 Return the (JSON) List of Roles for a given App based on apikey.client_id - # """ - # def approles(conn, %{"client_id" => client_id}) do + @doc """ + approles/2 Return the (JSON) List of Roles for a given App based on apikey.client_id + """ + def approles(conn, %{"client_id" => client_id}) do + IO.inspect(client_id) + # return empty JSON list with 401 status if client_id is invalid - # end + roles = Auth.Role.list_roles() + roles = Enum.map(roles, fn role -> Auth.Role.strip_meta(role) end) + json(conn, roles) + end end diff --git a/lib/auth_web/router.ex b/lib/auth_web/router.ex index 35d7896d..16b97a79 100644 --- a/lib/auth_web/router.ex +++ b/lib/auth_web/router.ex @@ -12,10 +12,6 @@ defmodule AuthWeb.Router do plug :put_secure_browser_headers end - # pipeline :api do - # plug :accepts, ["json"] - # end - scope "/", AuthWeb do pipe_through :browser @@ -27,7 +23,6 @@ defmodule AuthWeb.Router do # get "/auth/password/new", AuthController, :password_input post "/auth/password/create", AuthController, :password_create post "/auth/password/verify", AuthController, :password_prompt - # get "/approles/:client_id", AppController, :approles # https://github.com/dwyl/ping get "/ping", PingController, :ping end @@ -55,8 +50,14 @@ defmodule AuthWeb.Router do # resources "/settings/apikeys", ApikeyController end + pipeline :api do + plug :accepts, ["json"] + end + # Other scopes may use custom stacks. - # scope "/api", AuthWeb do - # pipe_through :api - # end + scope "/", AuthWeb do + pipe_through :api + + get "/approles/:client_id", AppController, :approles + end end diff --git a/test/auth_web/controllers/app_controller_test.exs b/test/auth_web/controllers/app_controller_test.exs index 74462b10..468be80f 100644 --- a/test/auth_web/controllers/app_controller_test.exs +++ b/test/auth_web/controllers/app_controller_test.exs @@ -160,4 +160,12 @@ defmodule AuthWeb.AppControllerTest do assert html_response(conn, 404) =~ "can't touch this." end end + + describe "GET /approles/:client_id" do + test "returns (JSON) list of roles", %{conn: conn, app: app} do + conn = admin_login(conn) + conn = get(conn, Routes.app_path(conn, :approles, app.apikey)) + assert html_response(conn, 200) =~ "successfully reset" + end + end end