-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathAuthorizationSecurityTest.php
More file actions
817 lines (671 loc) · 29.8 KB
/
Copy pathAuthorizationSecurityTest.php
File metadata and controls
817 lines (671 loc) · 29.8 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
<?php
/*
* Copyright (c) 2025-2026 Netresearch DTT GmbH
* SPDX-License-Identifier: AGPL-3.0-only
*/
declare(strict_types=1);
namespace Tests\Controller;
use App\Entity\Customer;
use App\Entity\Preset;
use App\Entity\Team;
use Doctrine\ORM\EntityManagerInterface;
use RuntimeException;
use Tests\AbstractWebTestCase;
use function assert;
use function is_array;
/**
* Comprehensive authorization tests for critical Admin endpoints.
*
* This test suite focuses on high-risk delete actions and admin save operations
* that require proper PL (Project Leader) authorization to prevent unauthorized
* data deletion and system configuration changes.
*
* Tests both positive (PL user allowed) and negative (DEV user denied) scenarios
* to ensure proper access control enforcement.
*
* @internal
*
* @coversNothing
*/
final class AuthorizationSecurityTest extends AbstractWebTestCase
{
// ============================================================================
// DELETE ACTIONS - Highest Security Risk (Data Loss Prevention)
// ============================================================================
/**
* Test that PL users can delete activities.
*/
public function testDeleteActivityActionWithPl(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'id' => 1, // Activity ID from test fixtures (may be referenced by entries)
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/activity/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
// Activity ID 1 may have referential constraints preventing deletion
$this->assertStatusCode(422);
$responseContent = $this->client->getResponse()->getContent();
// Should return an error message about why deletion failed
self::assertNotEmpty($responseContent);
}
/**
* Test that DEV users cannot delete activities - CRITICAL security test.
* Unauthorized activity deletion could result in loss of time tracking categories.
*/
public function testDeleteActivityActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'id' => 1, // Activity ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/activity/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that PL users can delete customers.
*/
public function testDeleteCustomerActionWithPl(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'id' => 2, // Customer ID from test fixtures (safe to delete)
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/customer/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(200);
$responseContent = $this->client->getResponse()->getContent();
$responseData = json_decode((string) $responseContent, true);
assert(is_array($responseData));
self::assertTrue($responseData['success'] ?? false, 'Expected successful customer deletion');
}
/**
* Test that DEV users cannot delete customers - CRITICAL security test.
* Unauthorized customer deletion could result in loss of business data and client relationships.
*/
public function testDeleteCustomerActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'id' => 2, // Customer ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/customer/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that PL users can delete projects.
*/
public function testDeleteProjectActionWithPl(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'id' => 2, // Project ID from test fixtures (safe to delete)
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/project/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(200);
$responseContent = $this->client->getResponse()->getContent();
$responseData = json_decode((string) $responseContent, true);
assert(is_array($responseData));
self::assertTrue($responseData['success'] ?? false, 'Expected successful project deletion');
}
/**
* Test that DEV users cannot delete projects - CRITICAL security test.
* Unauthorized project deletion could result in loss of project history and time entries.
*/
public function testDeleteProjectActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'id' => 2, // Project ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/project/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that PL users can delete users.
*/
public function testDeleteUserActionWithPl(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'id' => 4, // User ID from test fixtures (testGroupByActionUser - safe to delete)
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/user/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(200);
$responseContent = $this->client->getResponse()->getContent();
$responseData = json_decode((string) $responseContent, true);
assert(is_array($responseData));
self::assertTrue($responseData['success'] ?? false, 'Expected successful user deletion');
}
/**
* Test that DEV users cannot delete users - CRITICAL security test.
* Unauthorized user deletion could result in loss of user accounts and access control compromise.
*/
public function testDeleteUserActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'id' => 4, // User ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/user/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that PL users can delete ticket systems.
*/
public function testDeleteTicketSystemActionWithPl(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'id' => 1, // Ticket system ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/ticketsystem/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(200);
$responseContent = $this->client->getResponse()->getContent();
$responseData = json_decode((string) $responseContent, true);
assert(is_array($responseData));
self::assertTrue($responseData['success'] ?? false, 'Expected successful ticket system deletion');
}
/**
* Test that DEV users cannot delete ticket systems - CRITICAL security test.
* Unauthorized ticket system deletion could break integrations and project workflows.
*/
public function testDeleteTicketSystemActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'id' => 1, // Ticket system ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/ticketsystem/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that PL users can delete teams.
*/
public function testDeleteTeamActionWithPl(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'id' => 2, // Team ID from test fixtures (safe to delete)
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/team/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(200);
$responseContent = $this->client->getResponse()->getContent();
$responseData = json_decode((string) $responseContent, true);
assert(is_array($responseData));
self::assertTrue($responseData['success'] ?? false, 'Expected successful team deletion');
}
/**
* Test that DEV users cannot delete teams - CRITICAL security test.
* Unauthorized team deletion could disrupt organizational structure and access controls.
*/
public function testDeleteTeamActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'id' => 2, // Team ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/team/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that PL users can delete presets.
*/
public function testDeletePresetActionWithPl(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'id' => 1, // Preset ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/preset/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(200);
$responseContent = $this->client->getResponse()->getContent();
$responseData = json_decode((string) $responseContent, true);
assert(is_array($responseData));
self::assertTrue($responseData['success'] ?? false, 'Expected successful preset deletion');
}
/**
* Test that DEV users cannot delete presets - CRITICAL security test.
* Unauthorized preset deletion could remove user productivity tools and configurations.
*/
public function testDeletePresetActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'id' => 1, // Preset ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/preset/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that a non-admin (DEV) user CAN read the preset list. Bulk entry is a
* user feature whose presets are shared, user-readable templates, so reading
* /getAllPresets must not be admin-gated (creating/deleting presets stays so).
*/
public function testGetPresetsActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$this->client->request('GET', '/getAllPresets');
$this->assertStatusCode(200);
}
/**
* A non-admin user must only receive presets for customers they may access:
* a preset tied to a team-restricted customer they do not belong to is
* filtered out (the information-disclosure guard), while a preset on a global
* customer stays visible.
*/
public function testGetPresetsActionFiltersPresetsByUserAccess(): void
{
$manager = self::getContainer()->get('doctrine')->getManager();
assert($manager instanceof EntityManagerInterface);
$team = new Team()->setName('Team Without Developer');
$globalCustomer = new Customer()->setName('Preset Global Co')->setActive(true)->setGlobal(true);
$restrictedCustomer = new Customer()->setName('Preset Restricted Co')->setActive(true)->setGlobal(false);
$restrictedCustomer->addTeam($team);
$visiblePreset = new Preset()->setName('VisibleGlobalPreset')->setCustomer($globalCustomer)->setDescription('visible');
$hiddenPreset = new Preset()->setName('HiddenRestrictedPreset')->setCustomer($restrictedCustomer)->setDescription('hidden');
foreach ([$team, $globalCustomer, $restrictedCustomer, $visiblePreset, $hiddenPreset] as $entity) {
$manager->persist($entity);
}
$manager->flush();
$this->logInSession('developer'); // DEV user, member of no team
$this->client->request('GET', '/getAllPresets');
$this->assertStatusCode(200);
$names = [];
foreach ($this->getJsonResponse($this->client->getResponse()) as $item) {
if (is_array($item) && is_array($item['preset'] ?? null)) {
$names[] = $item['preset']['name'] ?? null;
}
}
self::assertContains('VisibleGlobalPreset', $names, 'a global-customer preset must stay visible to any user');
self::assertNotContains('HiddenRestrictedPreset', $names, 'a preset for a team-restricted customer the user cannot access must be filtered out');
}
/**
* Test that PL users can delete contracts.
*/
public function testDeleteContractActionWithPl(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'id' => 1, // Contract ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/contract/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(200);
$responseContent = $this->client->getResponse()->getContent();
$responseData = json_decode((string) $responseContent, true);
assert(is_array($responseData));
self::assertTrue($responseData['success'] ?? false, 'Expected successful contract deletion');
}
/**
* Test that DEV users cannot delete contracts - CRITICAL security test.
* Unauthorized contract deletion could result in loss of employment/billing data.
*/
public function testDeleteContractActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'id' => 1, // Contract ID from test fixtures
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/contract/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
// ============================================================================
// SAVE ACTIONS - High Security Risk (Configuration Changes)
// ============================================================================
/**
* Test that PL users can save ticket systems.
*/
public function testSaveTicketSystemActionWithPl(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'name' => 'SecurityTestSystem',
'type' => 'JIRA',
'url' => 'https://test.example.com',
'ticketUrl' => 'https://test.example.com/ticket/{ticketId}',
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/ticketsystem/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(200);
$responseContent = $this->client->getResponse()->getContent();
$responseData = json_decode((string) $responseContent, true);
assert(is_array($responseData));
self::assertArrayHasKey('id', $responseData, 'Expected ticket system to be created with ID');
self::assertEquals('SecurityTestSystem', $responseData['name'] ?? '', 'Expected correct ticket system name');
}
/**
* Test that DEV users cannot save ticket systems - CRITICAL security test.
* Unauthorized ticket system configuration changes could compromise system integrations.
*/
public function testSaveTicketSystemActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'id' => null, // New ticket system
'name' => 'UnauthorizedSystem',
'type' => 'jira',
'url' => 'https://malicious.example.com',
'ticketUrl' => 'https://malicious.example.com/ticket/{ticketId}',
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/ticketsystem/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that PL users can update existing ticket systems.
*/
public function testUpdateTicketSystemActionWithPl(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'id' => 1, // Existing ticket system ID
'name' => 'UpdatedTestSystem',
'type' => 'JIRA',
'url' => 'https://updated.example.com',
'ticketUrl' => 'https://updated.example.com/ticket/{ticketId}',
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/ticketsystem/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(200);
$responseContent = $this->client->getResponse()->getContent();
$responseData = json_decode((string) $responseContent, true);
assert(is_array($responseData));
self::assertEquals('UpdatedTestSystem', $responseData['name'] ?? '', 'Expected ticket system name to be updated');
}
/**
* Test that DEV users cannot update existing ticket systems - CRITICAL security test.
* Unauthorized updates could redirect integrations to malicious systems.
*/
public function testUpdateTicketSystemActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'id' => 1, // Existing ticket system ID
'name' => 'HijackedSystem',
'type' => 'jira',
'url' => 'https://malicious.example.com',
'ticketUrl' => 'https://malicious.example.com/steal/{ticketId}',
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/ticketsystem/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
// ============================================================================
// ADDITIONAL SAVE ACTION AUTHORIZATION TESTS
// ============================================================================
/**
* Test that DEV users cannot save activities - prevents unauthorized activity configuration.
*/
public function testSaveActivityActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'name' => 'UnauthorizedActivity',
'factor' => 1.5,
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/activity/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that DEV users cannot save customers - prevents unauthorized customer creation/modification.
*/
public function testSaveCustomerActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'name' => 'UnauthorizedCustomer',
'active' => true,
'global' => false,
'teams' => [1],
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/customer/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that DEV users cannot save projects - prevents unauthorized project creation/modification.
*/
public function testSaveProjectActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'customer' => 1,
'name' => 'UnauthorizedProject',
'active' => true,
'global' => false,
'jiraId' => 'HACK',
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/project/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that DEV users cannot save users - prevents unauthorized user account management.
*/
public function testSaveUserActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'username' => 'unauthorized_user',
'abbr' => 'UNA',
'teams' => [1],
'locale' => 'en',
'type' => 'PL', // Attempting to create PL user
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/user/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that DEV users cannot save teams - prevents unauthorized team management.
*/
public function testSaveTeamActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'name' => 'UnauthorizedTeam',
'lead_user_id' => 1,
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/team/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that DEV users cannot save contracts - prevents unauthorized contract management.
*/
public function testSaveContractActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'user_id' => 2,
'start' => '2024-01-01',
'end' => '2024-12-31',
'hours_per_week' => 40,
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/contract/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
/**
* Test that DEV users cannot save presets - prevents unauthorized preset management.
*/
public function testSavePresetActionWithDev(): void
{
$this->logInSession('developer'); // DEV user
$jsonContent = json_encode([
'name' => 'UnauthorizedPreset',
'projectId' => 1,
'activityId' => 1,
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/preset/save', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
$this->assertStatusCode(403);
$this->assertMessage('You are not allowed to perform this action.');
}
// ============================================================================
// EDGE CASE AND SECURITY BOUNDARY TESTS
// ============================================================================
/**
* Test that invalid JSON payloads are handled properly for delete operations.
*/
public function testDeleteActionWithInvalidJsonPayload(): void
{
$this->logInSession('unittest'); // PL user
// Send invalid JSON - Symfony 8 converts this to 400 Bad Request
$this->client->request('POST', '/activity/delete', [], [], ['CONTENT_TYPE' => 'application/json'], 'invalid-json');
// Expect 400 Bad Request for malformed JSON
$this->assertStatusCode(400);
}
/**
* Test that missing ID parameter is handled properly for delete operations.
*/
public function testDeleteActionWithMissingId(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/activity/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
// Should return 422 Unprocessable Entity for missing required field
$this->assertStatusCode(422);
}
/**
* Test that non-existent ID is handled properly for delete operations.
*/
public function testDeleteActionWithNonExistentId(): void
{
$this->logInSession('unittest'); // PL user
$jsonContent = json_encode([
'id' => 99999, // Non-existent activity ID
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/activity/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
// Should return error for already deleted/non-existent item
$this->assertStatusCode(422);
$responseContent = $this->client->getResponse()->getContent();
self::assertStringContainsString('Der Datensatz konnte nicht enfernt werden!', (string) $responseContent);
}
/**
* Test that unauthenticated requests are properly rejected.
*/
public function testDeleteActionWithoutAuthentication(): void
{
// Clear any existing authentication by restarting the client session
$this->client->restart();
$jsonContent = json_encode([
'id' => 1,
]);
if (false === $jsonContent) {
throw new RuntimeException('Failed to encode JSON payload');
}
$this->client->request('POST', '/activity/delete', [], [], ['CONTENT_TYPE' => 'application/json'], $jsonContent);
// Should redirect to login or return 401/403
$response = $this->client->getResponse();
$statusCode = $response->getStatusCode();
self::assertTrue(
$response->isRedirection()
|| 401 === $statusCode
|| 403 === $statusCode,
"Unauthenticated requests should be rejected. Got status code: {$statusCode}",
);
}
/**
* Test authorization enforcement across different HTTP methods.
* Ensure that GET requests to delete endpoints are properly rejected.
*/
public function testDeleteActionWithWrongHttpMethod(): void
{
$this->logInSession('unittest'); // PL user
// Attempt to access delete endpoint with GET method
$this->client->request('GET', '/activity/delete');
$this->assertStatusCode(405);
}
}