-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathcredential_controller_test.exs
More file actions
852 lines (709 loc) · 25.4 KB
/
Copy pathcredential_controller_test.exs
File metadata and controls
852 lines (709 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
defmodule LightningWeb.API.CredentialControllerTest do
use LightningWeb.ConnCase, async: true
import Lightning.Factories
# Sample curl requests:
#
# # List your credentials
# curl http://localhost:4000/api/credentials \
# -H "Authorization: Bearer $TOKEN" -H "Accept: application/json"
#
# # List credentials for a project
# curl http://localhost:4000/api/projects/$PROJECT_ID/credentials \
# -H "Authorization: Bearer $TOKEN" -H "Accept: application/json"
#
# # Create a credential
# curl -X POST http://localhost:4000/api/credentials \
# -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
# -d '{"name":"My Cred","schema":"raw","credential_bodies":[{"name":"main","body":{"key":"val"}}]}'
#
# # Delete a credential
# curl -X DELETE http://localhost:4000/api/credentials/$CREDENTIAL_ID \
# -H "Authorization: Bearer $TOKEN"
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "without a token" do
test "index gets a 401", %{conn: conn} do
conn = get(conn, ~p"/api/credentials")
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
test "project credentials index gets a 401", %{conn: conn} do
conn = get(conn, ~p"/api/projects/#{Ecto.UUID.generate()}/credentials")
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
test "create gets a 401", %{conn: conn} do
conn = post(conn, ~p"/api/credentials", %{})
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
test "delete gets a 401", %{conn: conn} do
conn = delete(conn, ~p"/api/credentials/#{Ecto.UUID.generate()}")
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
end
describe "with invalid token" do
test "index gets a 401", %{conn: conn} do
token = "InvalidToken"
conn = conn |> Plug.Conn.put_req_header("authorization", "Bearer #{token}")
conn = get(conn, ~p"/api/credentials")
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
test "project credentials index gets a 401", %{conn: conn} do
token = "InvalidToken"
conn = conn |> Plug.Conn.put_req_header("authorization", "Bearer #{token}")
conn = get(conn, ~p"/api/projects/#{Ecto.UUID.generate()}/credentials")
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
test "create gets a 401", %{conn: conn} do
token = "InvalidToken"
conn = conn |> Plug.Conn.put_req_header("authorization", "Bearer #{token}")
conn = post(conn, ~p"/api/credentials", %{})
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
test "delete gets a 401", %{conn: conn} do
token = "InvalidToken"
conn = conn |> Plug.Conn.put_req_header("authorization", "Bearer #{token}")
conn = delete(conn, ~p"/api/credentials/#{Ecto.UUID.generate()}")
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
end
describe "index" do
setup [:assign_bearer_for_api]
test "lists all credentials owned by the user", %{conn: conn, user: user} do
# Create some credentials for the user
_credential1 =
insert(:credential, user: user, name: "First Credential", schema: "raw")
_credential2 =
insert(:credential, user: user, name: "Second Credential", schema: "raw")
# Create a credential for another user (should not appear)
other_user = insert(:user)
_other_credential =
insert(:credential,
user: other_user,
name: "Other User Credential",
schema: "raw"
)
conn = get(conn, ~p"/api/credentials")
response = json_response(conn, 200)
assert %{
"credentials" => credentials,
"errors" => %{}
} = response
assert length(credentials) == 2
# Check that credentials are returned without body field
returned_names = Enum.map(credentials, & &1["name"]) |> Enum.sort()
assert returned_names == ["First Credential", "Second Credential"]
# Verify body field is excluded for security
Enum.each(credentials, fn credential ->
refute Map.has_key?(credential, "body")
assert credential["user_id"] == user.id
end)
# Check specific credential structure
first_credential =
Enum.find(credentials, &(&1["name"] == "First Credential"))
assert %{
"id" => _,
"name" => "First Credential",
"schema" => _,
"external_id" => _,
"user_id" => user_id,
"project_credentials" => _,
"projects" => _,
"inserted_at" => _,
"updated_at" => _
} = first_credential
assert user_id == user.id
# production field no longer exists
refute Map.has_key?(first_credential, "production")
end
test "returns empty list when user has no credentials", %{
conn: conn,
user: _user
} do
conn = get(conn, ~p"/api/credentials")
response = json_response(conn, 200)
assert %{
"credentials" => [],
"errors" => %{}
} = response
end
test "includes project associations in the response", %{
conn: conn,
user: user
} do
project =
insert(:project, project_users: [%{user_id: user.id, role: :owner}])
_credential =
insert(:credential,
user: user,
name: "Project Credential",
schema: "raw",
project_credentials: [%{project_id: project.id}]
)
conn = get(conn, ~p"/api/credentials")
response = json_response(conn, 200)
assert %{"credentials" => [credential_data]} = response
assert credential_data["name"] == "Project Credential"
assert length(credential_data["project_credentials"]) == 1
assert length(credential_data["projects"]) == 1
project_data = List.first(credential_data["projects"])
assert project_data["id"] == project.id
assert project_data["name"] == project.name
end
end
describe "index for specific project" do
setup [:assign_bearer_for_api]
test "lists all credentials in a project when user has access", %{
conn: conn,
user: user
} do
project =
insert(:project, project_users: [%{user_id: user.id, role: :editor}])
# Create credentials for different users but in the same project
_user_credential =
insert(:credential,
user: user,
name: "User Credential",
schema: "raw",
project_credentials: [%{project_id: project.id}]
)
other_user = insert(:user)
_other_user_credential =
insert(:credential,
user: other_user,
name: "Other User Credential",
schema: "raw",
project_credentials: [%{project_id: project.id}]
)
# Create a credential not in this project (should not appear)
_unrelated_credential =
insert(:credential,
user: user,
name: "Unrelated Credential",
schema: "raw"
)
conn = get(conn, ~p"/api/projects/#{project.id}/credentials")
response = json_response(conn, 200)
assert %{
"credentials" => credentials,
"errors" => %{}
} = response
assert length(credentials) == 2
# Should include credentials from all users that have access to this project
returned_names = Enum.map(credentials, & &1["name"]) |> Enum.sort()
assert returned_names == ["Other User Credential", "User Credential"]
# Verify body field is excluded for security
Enum.each(credentials, fn credential ->
refute Map.has_key?(credential, "body")
end)
end
test "returns 403 when user lacks access to project", %{
conn: conn,
user: _user
} do
other_user = insert(:user)
project =
insert(:project,
project_users: [%{user_id: other_user.id, role: :owner}]
)
conn = get(conn, ~p"/api/projects/#{project.id}/credentials")
assert json_response(conn, 403) == %{"error" => "Forbidden"}
end
test "returns 404 when project does not exist", %{conn: conn, user: _user} do
non_existent_id = Ecto.UUID.generate()
conn = get(conn, ~p"/api/projects/#{non_existent_id}/credentials")
assert json_response(conn, 404) == %{"error" => "Not Found"}
end
test "returns 400 for malformed project_id", %{conn: conn, user: _user} do
conn = get(conn, ~p"/api/projects/not-a-uuid/credentials")
assert json_response(conn, 400) == %{"error" => "Bad Request"}
end
test "returns empty list when project has no credentials", %{
conn: conn,
user: user
} do
project =
insert(:project, project_users: [%{user_id: user.id, role: :owner}])
conn = get(conn, ~p"/api/projects/#{project.id}/credentials")
response = json_response(conn, 200)
assert %{
"credentials" => [],
"errors" => %{}
} = response
end
test "allows access for support users with project access", %{conn: conn} do
support_user = insert(:user, support_user: true)
project = insert(:project, allow_support_access: true)
_credential =
insert(:credential,
user: support_user,
name: "Support Credential",
schema: "raw",
project_credentials: [%{project_id: project.id}]
)
token = Lightning.Accounts.generate_api_token(support_user)
conn = conn |> Plug.Conn.put_req_header("authorization", "Bearer #{token}")
conn = get(conn, ~p"/api/projects/#{project.id}/credentials")
response = json_response(conn, 200)
assert %{"credentials" => [credential_data]} = response
assert credential_data["name"] == "Support Credential"
end
test "includes project information in credential data", %{
conn: conn,
user: user
} do
project =
insert(:project, project_users: [%{user_id: user.id, role: :admin}])
_credential =
insert(:credential,
user: user,
name: "Project Credential",
schema: "raw",
project_credentials: [%{project_id: project.id}]
)
conn = get(conn, ~p"/api/projects/#{project.id}/credentials")
response = json_response(conn, 200)
assert %{"credentials" => [credential_data]} = response
assert length(credential_data["projects"]) == 1
assert length(credential_data["project_credentials"]) == 1
project_data = List.first(credential_data["projects"])
assert project_data["id"] == project.id
assert project_data["name"] == project.name
end
end
describe "create" do
setup [:assign_bearer_for_api]
test "creates a basic credential without project associations", %{
conn: conn,
user: user
} do
credential_attrs = %{
"name" => "Test Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"username" => "test", "password" => "secret"}
}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
response = json_response(conn, 201)
assert %{
"credential" => %{
"id" => id,
"name" => "Test Credential",
"schema" => "raw",
"external_id" => nil,
"user_id" => user_id,
"project_credentials" => [],
"projects" => []
},
"errors" => %{}
} = response
assert is_binary(id)
assert user_id == user.id
# body should be excluded
refute Map.has_key?(response["credential"], "body")
# production field no longer exists
refute Map.has_key?(response["credential"], "production")
end
test "creates a credential with project associations when user has access",
%{conn: conn, user: user} do
project =
insert(:project, project_users: [%{user_id: user.id, role: :editor}])
credential_attrs = %{
"name" => "Project Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"api_key" => "secret"}
}
],
"project_credentials" => [
%{"project_id" => project.id}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
response = json_response(conn, 201)
assert %{
"credential" => %{
"id" => id,
"name" => "Project Credential",
"user_id" => user_id,
"project_credentials" => [project_credential],
"projects" => [project_data]
}
} = response
assert is_binary(id)
assert user_id == user.id
assert project_credential["project_id"] == project.id
assert project_data["id"] == project.id
assert project_data["name"] == project.name
end
test "creates a credential with multiple project associations", %{
conn: conn,
user: user
} do
project1 =
insert(:project, project_users: [%{user_id: user.id, role: :admin}])
project2 =
insert(:project, project_users: [%{user_id: user.id, role: :owner}])
credential_attrs = %{
"name" => "Multi-Project Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"token" => "abc123"}
}
],
"project_credentials" => [
%{"project_id" => project1.id},
%{"project_id" => project2.id}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
response = json_response(conn, 201)
assert %{
"credential" => %{
"project_credentials" => project_credentials,
"projects" => projects
}
} = response
assert length(project_credentials) == 2
assert length(projects) == 2
project_ids = Enum.map(projects, & &1["id"])
assert project1.id in project_ids
assert project2.id in project_ids
end
test "creates a credential with multiple environment bodies", %{
conn: conn,
user: user
} do
credential_attrs = %{
"name" => "Multi-Environment Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"api_key" => "main_key"}
},
%{
"name" => "production",
"body" => %{"api_key" => "prod_key"}
},
%{
"name" => "staging",
"body" => %{"api_key" => "staging_key"}
}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
response = json_response(conn, 201)
assert %{
"credential" => %{
"id" => id,
"name" => "Multi-Environment Credential",
"user_id" => user_id
}
} = response
assert is_binary(id)
assert user_id == user.id
# Verify bodies were created
credential = Lightning.Credentials.get_credential(id)
credential = Lightning.Repo.preload(credential, :credential_bodies)
assert length(credential.credential_bodies) == 3
env_names =
Enum.map(credential.credential_bodies, & &1.name) |> Enum.sort()
assert env_names == ["main", "production", "staging"]
end
test "fails when user lacks access to project", %{conn: conn, user: _user} do
other_user = insert(:user)
project =
insert(:project,
project_users: [%{user_id: other_user.id, role: :owner}]
)
credential_attrs = %{
"name" => "Unauthorized Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
],
"project_credentials" => [
%{"project_id" => project.id}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
assert json_response(conn, 403) == %{"error" => "Forbidden"}
end
test "fails when user has insufficient role (viewer) on project", %{
conn: conn,
user: user
} do
project =
insert(:project, project_users: [%{user_id: user.id, role: :viewer}])
credential_attrs = %{
"name" => "Viewer Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
],
"project_credentials" => [
%{"project_id" => project.id}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
assert json_response(conn, 403) == %{"error" => "Forbidden"}
end
test "fails when project does not exist", %{conn: conn, user: _user} do
credential_attrs = %{
"name" => "Nonexistent Project Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
],
"project_credentials" => [
%{"project_id" => Ecto.UUID.generate()}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
assert json_response(conn, 403) == %{"error" => "Forbidden"}
end
test "allows access when user is support user with project access", %{
conn: conn
} do
support_user = insert(:user, support_user: true)
project = insert(:project, allow_support_access: true)
token = Lightning.Accounts.generate_api_token(support_user)
conn = conn |> Plug.Conn.put_req_header("authorization", "Bearer #{token}")
credential_attrs = %{
"name" => "Support User Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
],
"project_credentials" => [
%{"project_id" => project.id}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
response = json_response(conn, 201)
assert %{
"credential" => %{
"name" => "Support User Credential",
"user_id" => user_id
}
} = response
assert user_id == support_user.id
end
test "fails with invalid credential data", %{conn: conn, user: _user} do
credential_attrs = %{
# Invalid: empty name
"name" => "",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
response = json_response(conn, 422)
assert %{"errors" => errors} = response
assert Map.has_key?(errors, "name")
end
test "fails when missing required fields", %{conn: conn, user: _user} do
credential_attrs = %{
"name" => ""
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
response = json_response(conn, 422)
assert %{"errors" => _errors} = response
end
test "cannot override user_id in request", %{conn: conn, user: user} do
other_user = insert(:user)
credential_attrs = %{
"name" => "Test Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
],
# This should be ignored
"user_id" => other_user.id
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
response = json_response(conn, 201)
# Should use the authenticated user, not the provided user_id
assert response["credential"]["user_id"] == user.id
refute response["credential"]["user_id"] == other_user.id
end
test "returns 201 created status", %{conn: conn, user: _user} do
credential_attrs = %{
"name" => "Status Test",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
assert json_response(conn, 201)
end
test "handles partial project access - allows accessible, denies inaccessible",
%{conn: conn, user: user} do
accessible_project =
insert(:project, project_users: [%{user_id: user.id, role: :editor}])
# No user access
inaccessible_project = insert(:project)
credential_attrs = %{
"name" => "Mixed Access Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
],
"project_credentials" => [
%{"project_id" => accessible_project.id},
%{"project_id" => inaccessible_project.id}
]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
assert json_response(conn, 403) == %{"error" => "Forbidden"}
end
end
describe "editor role permissions" do
setup [:assign_bearer_for_api]
test "editor can create project credentials", %{conn: conn, user: user} do
project =
insert(:project, project_users: [%{user_id: user.id, role: :editor}])
credential_attrs = %{
"name" => "Editor Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
],
"project_credentials" => [%{"project_id" => project.id}]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
assert json_response(conn, 201)
end
end
describe "admin role permissions" do
setup [:assign_bearer_for_api]
test "admin can create project credentials", %{conn: conn, user: user} do
project =
insert(:project, project_users: [%{user_id: user.id, role: :admin}])
credential_attrs = %{
"name" => "Admin Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
],
"project_credentials" => [%{"project_id" => project.id}]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
assert json_response(conn, 201)
end
end
describe "owner role permissions" do
setup [:assign_bearer_for_api]
test "owner can create project credentials", %{conn: conn, user: user} do
project =
insert(:project, project_users: [%{user_id: user.id, role: :owner}])
credential_attrs = %{
"name" => "Owner Credential",
"schema" => "raw",
"credential_bodies" => [
%{
"name" => "main",
"body" => %{"secret" => "value"}
}
],
"project_credentials" => [%{"project_id" => project.id}]
}
conn = post(conn, ~p"/api/credentials", credential_attrs)
assert json_response(conn, 201)
end
end
describe "delete" do
setup [:assign_bearer_for_api]
test "deletes a credential owned by the user", %{conn: conn, user: user} do
credential =
insert(:credential, user: user, name: "To Be Deleted", schema: "raw")
conn = delete(conn, ~p"/api/credentials/#{credential.id}")
assert response(conn, 204) == ""
# Verify credential is deleted
refute Lightning.Credentials.get_credential(credential.id)
end
test "returns 404 when credential does not exist", %{conn: conn, user: _user} do
non_existent_id = Ecto.UUID.generate()
conn = delete(conn, ~p"/api/credentials/#{non_existent_id}")
assert json_response(conn, 404) == %{"error" => "Not Found"}
end
test "returns 403 when trying to delete credential owned by another user", %{
conn: conn,
user: _user
} do
other_user = insert(:user)
other_credential =
insert(:credential,
user: other_user,
name: "Other User Credential",
schema: "raw"
)
conn = delete(conn, ~p"/api/credentials/#{other_credential.id}")
assert json_response(conn, 403) == %{"error" => "Forbidden"}
# Verify credential still exists
assert Lightning.Credentials.get_credential(other_credential.id)
end
test "deletes credential with project associations", %{
conn: conn,
user: user
} do
project =
insert(:project, project_users: [%{user_id: user.id, role: :owner}])
credential =
insert(:credential,
user: user,
name: "Credential with Projects",
schema: "raw",
project_credentials: [%{project_id: project.id}]
)
conn = delete(conn, ~p"/api/credentials/#{credential.id}")
assert response(conn, 204) == ""
# Verify credential and associations are deleted
refute Lightning.Credentials.get_credential(credential.id)
end
test "handles invalid UUID format", %{conn: conn, user: _user} do
conn = delete(conn, ~p"/api/credentials/invalid-uuid")
assert json_response(conn, 400) == %{"error" => "Bad Request"}
end
end
end