Skip to content

Commit e7e4605

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

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,19 @@ defmodule RestAPI.Controller.Admin.User do
2626
error
2727
end
2828
end
29+
30+
def show(conn, %{"username" => _username} = params) do
31+
params
32+
|> ResourceManager.create_identity()
33+
|> case do
34+
{:ok, identity} when is_struct(identity) ->
35+
conn
36+
|> put_status(:created)
37+
|> put_view(User)
38+
|> render("show.json", response: identity)
39+
40+
{:error, error_reason} ->
41+
error_reason
42+
end
43+
end
2944
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,13 @@ 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+
username: response.username,
20+
status: response.status,
21+
is_admin: response.is_admin,
22+
blocked_until: response.blocked_until
23+
}
24+
end
1625
end

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,79 @@ defmodule RestAPI.Controllers.Admin.User do
163163
end
164164
end
165165

166+
describe "GET #{@create_endpoint}" do
167+
setup do
168+
access_token = "my-access-token"
169+
claims = default_claims()
170+
171+
{:ok, access_token: access_token, claims: claims}
172+
end
173+
174+
test "should render user identity response", %{
175+
conn: conn,
176+
access_token: access_token,
177+
claims: claims
178+
} do
179+
password = "MyP@ssword1234"
180+
181+
params = %{
182+
"username" => "Shurato",
183+
"password" => password,
184+
"scopes" => [
185+
"6a3a3771-9f56-4254-9497-927e441dacfc",
186+
"8a235ba0-a827-4593-92c9-6248bef4fa06"
187+
]
188+
}
189+
190+
expect(AuthenticatorMock, :validate_access_token, fn token ->
191+
assert access_token == token
192+
{:ok, claims}
193+
end)
194+
195+
expect(AuthenticatorMock, :get_session, fn %{"jti" => jti} ->
196+
assert claims["jti"] == jti
197+
{:ok, success_session(claims)}
198+
end)
199+
200+
expect(ResourceManagerMock, :password_allowed?, fn _input ->
201+
true
202+
end)
203+
204+
expect(AuthenticatorMock, :generate_hash, fn password_to_hash, :argon2 ->
205+
assert password == password_to_hash
206+
"password_hashed"
207+
end)
208+
209+
expect(ResourceManagerMock, :create_identity, fn input ->
210+
assert is_map(input)
211+
212+
{:ok,
213+
%{
214+
id: Ecto.UUID.generate(),
215+
inserted_at: NaiveDateTime.utc_now(),
216+
is_admin: false,
217+
status: "active",
218+
updated_at: NaiveDateTime.utc_now(),
219+
username: "Shurato"
220+
}}
221+
end)
222+
223+
expect(AuthorizerMock, :authorize_admin, fn %Plug.Conn{} -> :ok end)
224+
225+
assert %{
226+
"id" => _id,
227+
"inserted_at" => _inserted_at,
228+
"is_admin" => false,
229+
"status" => "active",
230+
"username" => "Shurato"
231+
} =
232+
conn
233+
|> put_req_header("authorization", "Bearer #{access_token}")
234+
|> post(@create_endpoint, params)
235+
|> json_response(201)
236+
end
237+
end
238+
166239
defp default_claims do
167240
%{
168241
"jti" => "03eds74a-c291-4b5f",

0 commit comments

Comments
 (0)