@@ -7,36 +7,76 @@ defmodule Guard.ServiceAccount.ActionsTest do
7
7
alias Guard.Store.ServiceAccount
8
8
alias Support.Factories.ServiceAccountFactory
9
9
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
+
10
77
describe "create/1" do
11
78
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
40
80
params = ServiceAccountFactory . build_params ( )
41
81
42
82
{ :ok , % { service_account: service_account , api_token: api_token } } = Actions . create ( params )
@@ -115,33 +155,7 @@ defmodule Guard.ServiceAccount.ActionsTest do
115
155
end
116
156
117
157
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
145
159
params = ServiceAccountFactory . build_params ( )
146
160
147
161
{ :error , :rbac_user_creation_failed } = Actions . create ( params )
@@ -152,69 +166,15 @@ defmodule Guard.ServiceAccount.ActionsTest do
152
166
end
153
167
154
168
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
182
170
params = ServiceAccountFactory . build_params ( )
183
171
184
172
{ :error , :rbac_user_not_found } = Actions . create ( params )
185
173
end
186
174
end
187
175
188
176
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
218
178
params = ServiceAccountFactory . build_params ( )
219
179
220
180
{ :error , :assignment_failed } = Actions . create ( params )
@@ -380,8 +340,8 @@ defmodule Guard.ServiceAccount.ActionsTest do
380
340
end
381
341
382
342
describe "integration tests" do
383
- test "full create flow with database" do
384
- with_mocks ( [
343
+ defp setup_integration_mocks do
344
+ [
385
345
{ Guard.Api.Organization , [ :passthrough ] , [ fetch: fn _ -> % { username: "test-org" } end ] } ,
386
346
{ Guard.FrontRepo.User , [ :passthrough ] ,
387
347
[ reset_auth_token: fn _ -> { :ok , "test-token" } end ] } ,
@@ -392,7 +352,11 @@ defmodule Guard.ServiceAccount.ActionsTest do
392
352
] } ,
393
353
{ Guard.Api.Rbac , [ :passthrough ] , [ assign_role: fn _ , _ , _ -> :ok end ] } ,
394
354
{ 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
396
360
params =
397
361
ServiceAccountFactory . build_params_with_creator (
398
362
name: "Integration Test SA" ,
@@ -419,18 +383,7 @@ defmodule Guard.ServiceAccount.ActionsTest do
419
383
end
420
384
421
385
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
434
387
# Create service account first
435
388
params = ServiceAccountFactory . build_params_with_creator ( name: "Original Name" )
436
389
{ :ok , % { service_account: service_account , api_token: _ } } = Actions . create ( params )
0 commit comments