Skip to content

Commit adaa2ea

Browse files
author
Yashin Santos
committed
wip
1 parent e7e4605 commit adaa2ea

File tree

4 files changed

+24
-35
lines changed

4 files changed

+24
-35
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ defmodule RestAPI.Controller.Admin.User do
2727
end
2828
end
2929

30-
def show(conn, %{"username" => _username} = params) do
30+
def show(conn, %{"id" => username} = params) do
3131
params
32-
|> ResourceManager.create_identity()
32+
|> Map.put(:username, username)
33+
|> ResourceManager.get_identity()
3334
|> case do
34-
{:ok, identity} when is_struct(identity) ->
35+
{:ok, identity} when is_map(identity) ->
3536
conn
3637
|> put_status(:created)
3738
|> put_view(User)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ defmodule RestAPI.Views.Admin.User do
1616

1717
def render("show.json", %{response: response}) do
1818
%{
19+
id: response.id,
1920
username: response.username,
2021
status: response.status,
2122
is_admin: response.is_admin,
22-
blocked_until: response.blocked_until
23+
inserted_at: response.inserted_at,
24+
updated_at: response.updated_at
2325
}
2426
end
2527
end

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

Lines changed: 16 additions & 31 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,29 +164,21 @@ defmodule RestAPI.Controllers.Admin.User do
163164
end
164165
end
165166

166-
describe "GET #{@create_endpoint}" do
167+
describe "GET #{@show_endpoint}" do
167168
setup do
168169
access_token = "my-access-token"
169170
claims = default_claims()
170171

171-
{:ok, access_token: access_token, claims: claims}
172+
{:ok, access_token: access_token, claims: claims, user: insert!(:user)}
172173
end
173174

174-
test "should render user identity response", %{
175+
test "should render user identity", %{
175176
conn: conn,
176177
access_token: access_token,
177-
claims: claims
178+
claims: claims,
179+
user: user
178180
} 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-
}
181+
username = user.username
189182

190183
expect(AuthenticatorMock, :validate_access_token, fn token ->
191184
assert access_token == token
@@ -197,41 +190,33 @@ defmodule RestAPI.Controllers.Admin.User do
197190
{:ok, success_session(claims)}
198191
end)
199192

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)
193+
expect(AuthorizerMock, :authorize_admin, fn %Plug.Conn{} -> :ok end)
208194

209-
expect(ResourceManagerMock, :create_identity, fn input ->
195+
expect(ResourceManagerMock, :get_identity, fn input ->
210196
assert is_map(input)
211197

212198
{:ok,
213199
%{
214-
id: Ecto.UUID.generate(),
200+
id: user.id,
215201
inserted_at: NaiveDateTime.utc_now(),
216-
is_admin: false,
217-
status: "active",
202+
is_admin: user.is_admin,
203+
status: user.status,
218204
updated_at: NaiveDateTime.utc_now(),
219-
username: "Shurato"
205+
username: username
220206
}}
221207
end)
222208

223-
expect(AuthorizerMock, :authorize_admin, fn %Plug.Conn{} -> :ok end)
224-
225209
assert %{
226210
"id" => _id,
227211
"inserted_at" => _inserted_at,
212+
"updated_at" => _updated_at,
228213
"is_admin" => false,
229214
"status" => "active",
230-
"username" => "Shurato"
215+
"username" => ^username
231216
} =
232217
conn
233218
|> put_req_header("authorization", "Bearer #{access_token}")
234-
|> post(@create_endpoint, params)
219+
|> get(@show_endpoint <> "username")
235220
|> json_response(201)
236221
end
237222
end

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)