Skip to content

Commit b424007

Browse files
author
Yashin Santos
committed
feat: add show admin endpoint
wip
1 parent cb23442 commit b424007

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

apps/rest_api/lib/controllers/admin/user.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,20 @@ defmodule RestAPI.Controller.Admin.User do
2626
error
2727
end
2828
end
29+
30+
def show(conn, %{"id" => username} = params) do
31+
params
32+
|> Map.put(:username, username)
33+
|> ResourceManager.get_identity()
34+
|> case do
35+
{:ok, identity} when is_map(identity) ->
36+
conn
37+
|> put_status(:created)
38+
|> put_view(User)
39+
|> render("show.json", response: identity)
40+
41+
{:error, error_reason} ->
42+
error_reason
43+
end
44+
end
2945
end

apps/rest_api/lib/ports/resource_manager.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@ defmodule RestAPI.Ports.ResourceManager do
77
@type possible_create_identity_response ::
88
{:ok, struct()} | {:error, Ecto.Changeset.t() | :invalid_params}
99

10+
@type possible_get_identity_responses ::
11+
{:ok, struct()} | {:error, :not_found | :invalid_params}
12+
1013
@doc "Delegates to ResourceManager.create_identity/1"
1114
@callback create_identity(input :: map()) :: possible_create_identity_response()
1215

1316
@doc "Delegates to ResourceManager.password_allowed?/1"
1417
@callback password_allowed?(password :: String.t()) :: boolean()
1518

19+
@callback get_identity(input :: String.t()) :: possible_get_identity_responses()
20+
1621
@doc "Create a new identity with it's credentials"
1722
@spec create_identity(input :: map()) :: possible_create_identity_response()
1823
def create_identity(input), do: implementation().create_identity(input)
1924

25+
@doc "Returns an user or application identity seaching by the given input"
26+
@spec get_identity(input :: String.t()) :: possible_get_identity_responses()
27+
def get_identity(input), do: implementation().get_identity(input)
28+
2029
@doc "Checks if the given password is strong enough to be used"
2130
@spec password_allowed?(password :: String.t()) :: boolean()
2231
def password_allowed?(password), do: implementation().password_allowed?(password)

apps/rest_api/lib/views/admin/user.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@ defmodule RestAPI.Views.Admin.User do
1313
update_at: response.updated_at
1414
}
1515
end
16+
17+
def render("show.json", %{response: response}) do
18+
%{
19+
id: response.id,
20+
username: response.username,
21+
status: response.status,
22+
is_admin: response.is_admin,
23+
inserted_at: response.inserted_at,
24+
updated_at: response.updated_at
25+
}
26+
end
1627
end

apps/rest_api/test/controllers/admin/user_test.exs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule RestAPI.Controllers.Admin.User do
55
alias RestAPI.Ports.{AuthenticatorMock, AuthorizerMock, ResourceManagerMock}
66

77
@create_endpoint "/admin/v1/users"
8+
@show_endpoint "/admin/v1/users/"
89

910
describe "POST #{@create_endpoint}" do
1011
setup do
@@ -163,6 +164,63 @@ defmodule RestAPI.Controllers.Admin.User do
163164
end
164165
end
165166

167+
describe "GET #{@show_endpoint}" do
168+
setup do
169+
access_token = "my-access-token"
170+
claims = default_claims()
171+
172+
{:ok, access_token: access_token, claims: claims, user: insert!(:user)}
173+
end
174+
175+
test "should render user identity", %{
176+
conn: conn,
177+
access_token: access_token,
178+
claims: claims,
179+
user: user
180+
} do
181+
username = user.username
182+
183+
expect(AuthenticatorMock, :validate_access_token, fn token ->
184+
assert access_token == token
185+
{:ok, claims}
186+
end)
187+
188+
expect(AuthenticatorMock, :get_session, fn %{"jti" => jti} ->
189+
assert claims["jti"] == jti
190+
{:ok, success_session(claims)}
191+
end)
192+
193+
expect(AuthorizerMock, :authorize_admin, fn %Plug.Conn{} -> :ok end)
194+
195+
expect(ResourceManagerMock, :get_identity, fn input ->
196+
assert is_map(input)
197+
198+
{:ok,
199+
%{
200+
id: user.id,
201+
inserted_at: NaiveDateTime.utc_now(),
202+
is_admin: user.is_admin,
203+
status: user.status,
204+
updated_at: NaiveDateTime.utc_now(),
205+
username: username
206+
}}
207+
end)
208+
209+
assert %{
210+
"id" => _id,
211+
"inserted_at" => _inserted_at,
212+
"updated_at" => _updated_at,
213+
"is_admin" => false,
214+
"status" => "active",
215+
"username" => ^username
216+
} =
217+
conn
218+
|> put_req_header("authorization", "Bearer #{access_token}")
219+
|> get(@show_endpoint <> "username")
220+
|> json_response(201)
221+
end
222+
end
223+
166224
defp default_claims do
167225
%{
168226
"jti" => "03eds74a-c291-4b5f",

apps/rest_api/test/support/conn_case.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ defmodule RestAPI.ConnCase do
2424
import Phoenix.ConnTest
2525
import RestAPI.ConnCase
2626
import Mox
27+
import ResourceManager.Factory
2728

2829
alias RestAPI.Router.Helpers, as: Routes
2930

0 commit comments

Comments
 (0)