Skip to content

Commit 7559f3e

Browse files
committed
fix: add role_id to pattern matching in create request
1 parent b6767c9 commit 7559f3e

File tree

2 files changed

+91
-129
lines changed

2 files changed

+91
-129
lines changed

guard/lib/guard/grpc_servers/service_account_server.ex

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,25 @@ defmodule Guard.GrpcServers.ServiceAccountServer do
1818
org_id: org_id,
1919
name: name,
2020
description: description,
21-
creator_id: creator_id
21+
creator_id: creator_id,
22+
role_id: role_id
2223
},
2324
_stream
2425
) do
2526
observe_and_log(
2627
"grpc.service_account.create",
27-
%{org_id: org_id, name: name, description: description, creator_id: creator_id},
28+
%{
29+
org_id: org_id,
30+
name: name,
31+
description: description,
32+
creator_id: creator_id,
33+
role_id: role_id
34+
},
2835
fn ->
2936
# Validate input parameters
3037
validate_uuid!(org_id)
3138
validate_uuid!(creator_id)
39+
validate_uuid!(role_id)
3240

3341
if String.trim(name) == "" do
3442
grpc_error!(:invalid_argument, "Service account name cannot be empty")
@@ -45,7 +53,8 @@ defmodule Guard.GrpcServers.ServiceAccountServer do
4553
org_id: org_id,
4654
name: String.trim(name),
4755
description: String.trim(description || ""),
48-
creator_id: creator_id
56+
creator_id: creator_id,
57+
role_id: role_id
4958
}
5059

5160
case Guard.ServiceAccount.Actions.create(params) do

guard/test/guard/service_account/actions_test.exs

Lines changed: 79 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,76 @@ defmodule Guard.ServiceAccount.ActionsTest do
77
alias Guard.Store.ServiceAccount
88
alias Support.Factories.ServiceAccountFactory
99

10+
# Common mock helpers
11+
defp setup_common_mocks do
12+
[
13+
{Guard.Store.RbacUser, [:passthrough],
14+
[
15+
create: fn _, _, _, _ -> :ok end,
16+
fetch: fn _ -> %{id: "rbac-user-id", user_id: "user-id"} end
17+
]},
18+
{Guard.Api.Rbac, [:passthrough], [assign_role: fn _, _, _ -> :ok end]},
19+
{Guard.Events.UserCreated, [:passthrough], [publish: fn _, _ -> :ok end]}
20+
]
21+
end
22+
23+
defp successful_service_account_mock(email \\ "[email protected]") do
24+
{ServiceAccount, [:passthrough],
25+
[
26+
create: fn _ ->
27+
{:ok,
28+
%{
29+
service_account: %{
30+
id: "user-id",
31+
user_id: "user-id",
32+
name: "Test SA",
33+
description: "Test Description",
34+
org_id: "org-id",
35+
creator_id: "creator-id",
36+
deactivated: false,
37+
email: email
38+
},
39+
api_token: "test-token"
40+
}}
41+
end
42+
]}
43+
end
44+
45+
defp rbac_failure_mocks do
46+
[
47+
{Guard.Store.RbacUser, [:passthrough], [create: fn _, _, _, _ -> :error end]},
48+
{Guard.Api.Rbac, [:passthrough], [assign_role: fn _, _, _ -> :ok end]},
49+
{Guard.Events.UserCreated, [:passthrough], [publish: fn _, _ -> :ok end]}
50+
]
51+
end
52+
53+
defp rbac_user_not_found_mocks do
54+
[
55+
{Guard.Store.RbacUser, [:passthrough],
56+
[
57+
create: fn _, _, _, _ -> :ok end,
58+
fetch: fn _ -> nil end
59+
]},
60+
{Guard.Api.Rbac, [:passthrough], [assign_role: fn _, _, _ -> :ok end]}
61+
]
62+
end
63+
64+
defp role_assignment_failure_mocks do
65+
[
66+
{Guard.Store.RbacUser, [:passthrough],
67+
[
68+
create: fn _, _, _, _ -> :ok end,
69+
fetch: fn _ -> %{id: "rbac-user-id", user_id: "user-id"} end
70+
]},
71+
{Guard.Api.Rbac, [:passthrough],
72+
[assign_role: fn _, _, _ -> {:error, :assignment_failed} end]},
73+
{Guard.Events.UserCreated, [:passthrough], [publish: fn _, _ -> :ok end]}
74+
]
75+
end
76+
1077
describe "create/1" do
1178
test "creates service account successfully and publishes event" do
12-
with_mocks([
13-
{ServiceAccount, [:passthrough],
14-
[
15-
create: fn _ ->
16-
{:ok,
17-
%{
18-
service_account: %{
19-
id: "user-id",
20-
user_id: "user-id",
21-
name: "Test SA",
22-
description: "Test Description",
23-
org_id: "org-id",
24-
creator_id: "creator-id",
25-
deactivated: false,
26-
27-
},
28-
api_token: "test-token"
29-
}}
30-
end
31-
]},
32-
{Guard.Store.RbacUser, [:passthrough],
33-
[
34-
create: fn _, _, _, _ -> :ok end,
35-
fetch: fn _ -> %{id: "rbac-user-id", user_id: "user-id"} end
36-
]},
37-
{Guard.Api.Rbac, [:passthrough], [assign_role: fn _, _, _ -> :ok end]},
38-
{Guard.Events.UserCreated, [:passthrough], [publish: fn _, _ -> :ok end]}
39-
]) do
79+
with_mocks([successful_service_account_mock() | setup_common_mocks()]) do
4080
params = ServiceAccountFactory.build_params()
4181

4282
{:ok, %{service_account: service_account, api_token: api_token}} = Actions.create(params)
@@ -115,33 +155,7 @@ defmodule Guard.ServiceAccount.ActionsTest do
115155
end
116156

117157
test "handles RBAC user creation failure" do
118-
with_mocks([
119-
{ServiceAccount, [:passthrough],
120-
[
121-
create: fn _ ->
122-
{:ok,
123-
%{
124-
service_account: %{
125-
id: "user-id",
126-
user_id: "user-id",
127-
name: "Test SA",
128-
description: "Test Description",
129-
org_id: "org-id",
130-
creator_id: "creator-id",
131-
deactivated: false,
132-
133-
},
134-
api_token: "test-token"
135-
}}
136-
end
137-
]},
138-
{Guard.Store.RbacUser, [:passthrough],
139-
[
140-
create: fn _, _, _, _ -> :error end
141-
]},
142-
{Guard.Api.Rbac, [:passthrough], [assign_role: fn _, _, _ -> :ok end]},
143-
{Guard.Events.UserCreated, [:passthrough], [publish: fn _, _ -> :ok end]}
144-
]) do
158+
with_mocks([successful_service_account_mock() | rbac_failure_mocks()]) do
145159
params = ServiceAccountFactory.build_params()
146160

147161
{:error, :rbac_user_creation_failed} = Actions.create(params)
@@ -152,69 +166,15 @@ defmodule Guard.ServiceAccount.ActionsTest do
152166
end
153167

154168
test "handles RBAC user fetch failure after creation" do
155-
with_mocks([
156-
{ServiceAccount, [:passthrough],
157-
[
158-
create: fn _ ->
159-
{:ok,
160-
%{
161-
service_account: %{
162-
id: "user-id",
163-
user_id: "user-id",
164-
name: "Test SA",
165-
description: "Test Description",
166-
org_id: "org-id",
167-
creator_id: "creator-id",
168-
deactivated: false,
169-
170-
},
171-
api_token: "test-token"
172-
}}
173-
end
174-
]},
175-
{Guard.Store.RbacUser, [:passthrough],
176-
[
177-
create: fn _, _, _, _ -> :ok end,
178-
fetch: fn _ -> nil end
179-
]},
180-
{Guard.Api.Rbac, [:passthrough], [assign_role: fn _, _, _ -> :ok end]}
181-
]) do
169+
with_mocks([successful_service_account_mock() | rbac_user_not_found_mocks()]) do
182170
params = ServiceAccountFactory.build_params()
183171

184172
{:error, :rbac_user_not_found} = Actions.create(params)
185173
end
186174
end
187175

188176
test "handles role assignment failure" do
189-
with_mocks([
190-
{ServiceAccount, [:passthrough],
191-
[
192-
create: fn _ ->
193-
{:ok,
194-
%{
195-
service_account: %{
196-
id: "user-id",
197-
user_id: "user-id",
198-
name: "Test SA",
199-
description: "Test Description",
200-
org_id: "org-id",
201-
creator_id: "creator-id",
202-
deactivated: false,
203-
204-
},
205-
api_token: "test-token"
206-
}}
207-
end
208-
]},
209-
{Guard.Store.RbacUser, [:passthrough],
210-
[
211-
create: fn _, _, _, _ -> :ok end,
212-
fetch: fn _ -> %{id: "rbac-user-id", user_id: "user-id"} end
213-
]},
214-
{Guard.Api.Rbac, [:passthrough],
215-
[assign_role: fn _, _, _ -> {:error, :assignment_failed} end]},
216-
{Guard.Events.UserCreated, [:passthrough], [publish: fn _, _ -> :ok end]}
217-
]) do
177+
with_mocks([successful_service_account_mock() | role_assignment_failure_mocks()]) do
218178
params = ServiceAccountFactory.build_params()
219179

220180
{:error, :assignment_failed} = Actions.create(params)
@@ -380,8 +340,8 @@ defmodule Guard.ServiceAccount.ActionsTest do
380340
end
381341

382342
describe "integration tests" do
383-
test "full create flow with database" do
384-
with_mocks([
343+
defp setup_integration_mocks do
344+
[
385345
{Guard.Api.Organization, [:passthrough], [fetch: fn _ -> %{username: "test-org"} end]},
386346
{Guard.FrontRepo.User, [:passthrough],
387347
[reset_auth_token: fn _ -> {:ok, "test-token"} end]},
@@ -392,7 +352,11 @@ defmodule Guard.ServiceAccount.ActionsTest do
392352
]},
393353
{Guard.Api.Rbac, [:passthrough], [assign_role: fn _, _, _ -> :ok end]},
394354
{Guard.Events.UserCreated, [:passthrough], [publish: fn _, _ -> :ok end]}
395-
]) do
355+
]
356+
end
357+
358+
test "full create flow with database" do
359+
with_mocks(setup_integration_mocks()) do
396360
params =
397361
ServiceAccountFactory.build_params_with_creator(
398362
name: "Integration Test SA",
@@ -419,18 +383,7 @@ defmodule Guard.ServiceAccount.ActionsTest do
419383
end
420384

421385
test "full update flow with database" do
422-
with_mocks([
423-
{Guard.Api.Organization, [:passthrough], [fetch: fn _ -> %{username: "test-org"} end]},
424-
{Guard.FrontRepo.User, [:passthrough],
425-
[reset_auth_token: fn _ -> {:ok, "test-token"} end]},
426-
{Guard.Store.RbacUser, [:passthrough],
427-
[
428-
create: fn _, _, _, _ -> :ok end,
429-
fetch: fn _ -> %{id: "rbac-user-id"} end
430-
]},
431-
{Guard.Api.Rbac, [:passthrough], [assign_role: fn _, _, _ -> :ok end]},
432-
{Guard.Events.UserCreated, [:passthrough], [publish: fn _, _ -> :ok end]}
433-
]) do
386+
with_mocks(setup_integration_mocks()) do
434387
# Create service account first
435388
params = ServiceAccountFactory.build_params_with_creator(name: "Original Name")
436389
{:ok, %{service_account: service_account, api_token: _}} = Actions.create(params)

0 commit comments

Comments
 (0)