|
4 | 4 |
|
5 | 5 | namespace Unit\Factory; |
6 | 6 |
|
| 7 | +use Flowpack\ContentSecurityPolicy\Exceptions\InvalidDirectiveException; |
7 | 8 | use Flowpack\ContentSecurityPolicy\Factory\PolicyFactory; |
8 | 9 | use Flowpack\ContentSecurityPolicy\Model\Directive; |
9 | 10 | use Flowpack\ContentSecurityPolicy\Model\Nonce; |
|
15 | 16 | #[CoversClass(PolicyFactory::class)] |
16 | 17 | #[UsesClass(Policy::class)] |
17 | 18 | #[UsesClass(Directive::class)] |
| 19 | +#[UsesClass(InvalidDirectiveException::class)] |
18 | 20 | class PolicyFactoryTest extends TestCase |
19 | 21 | { |
20 | | - public function testCreateShouldReturnPolicy(): void |
| 22 | + public function testCreateShouldReturnPolicyAndMergeCustomWithDefaultDirective(): void |
21 | 23 | { |
22 | 24 | $policyFactory = new PolicyFactory(); |
23 | 25 | $nonceMock = $this->createMock(Nonce::class); |
24 | 26 |
|
25 | 27 | $defaultDirective = [ |
26 | 28 | 'base-uri' => [ |
| 29 | + 'test.com', |
| 30 | + ], |
| 31 | + 'script-src' => [ |
| 32 | + 'test.com', |
| 33 | + ], |
| 34 | + ]; |
| 35 | + $customDirective = [ |
| 36 | + 'script-src' => [ |
| 37 | + 'custom.com', |
| 38 | + ], |
| 39 | + ]; |
| 40 | + |
| 41 | + $expected = [ |
| 42 | + 'base-uri' => [ |
| 43 | + 'test.com', |
| 44 | + ], |
| 45 | + 'script-src' => [ |
| 46 | + 'test.com', |
| 47 | + 'custom.com', |
| 48 | + ], |
| 49 | + ]; |
| 50 | + |
| 51 | + $result = $policyFactory->create($nonceMock, $defaultDirective, $customDirective); |
| 52 | + |
| 53 | + self::assertSame($expected, $result->getDirectives()); |
| 54 | + } |
| 55 | + |
| 56 | + public function testCreateShouldReturnPolicyAndHandleSpecialDirectives(): void |
| 57 | + { |
| 58 | + $policyFactory = new PolicyFactory(); |
| 59 | + $nonceMock = $this->createMock(Nonce::class); |
| 60 | + |
| 61 | + $defaultDirective = [ |
| 62 | + 'script-src' => [ |
| 63 | + '{nonce}', |
27 | 64 | 'self', |
28 | 65 | ], |
| 66 | + ]; |
| 67 | + $customDirective = []; |
| 68 | + |
| 69 | + $expected = [ |
29 | 70 | 'script-src' => [ |
| 71 | + "'nonce-'", |
| 72 | + "'self'", |
| 73 | + ], |
| 74 | + ]; |
| 75 | + |
| 76 | + $result = $policyFactory->create($nonceMock, $defaultDirective, $customDirective); |
| 77 | + |
| 78 | + self::assertSame($expected, $result->getDirectives()); |
| 79 | + } |
| 80 | + |
| 81 | + public function testCreateShouldFailWithInvalidDirective(): void |
| 82 | + { |
| 83 | + $policyFactory = new PolicyFactory(); |
| 84 | + $nonceMock = $this->createMock(Nonce::class); |
| 85 | + |
| 86 | + $defaultDirective = [ |
| 87 | + 'invalid' => [ |
30 | 88 | 'self', |
31 | 89 | ], |
| 90 | + 'script-src' => [ |
| 91 | + 'self', |
| 92 | + ], |
| 93 | + ]; |
| 94 | + $customDirective = []; |
| 95 | + |
| 96 | + $this->expectException(InvalidDirectiveException::class); |
| 97 | + $policyFactory->create($nonceMock, $defaultDirective, $customDirective); |
| 98 | + } |
| 99 | + |
| 100 | + public function testCreateShouldReturnPolicyWithUniqueValues(): void |
| 101 | + { |
| 102 | + $policyFactory = new PolicyFactory(); |
| 103 | + $nonceMock = $this->createMock(Nonce::class); |
| 104 | + |
| 105 | + $defaultDirective = [ |
| 106 | + 'base-uri' => [ |
| 107 | + 'test.com', |
| 108 | + ], |
| 109 | + 'script-src' => [ |
| 110 | + 'test.com', |
| 111 | + ], |
32 | 112 | ]; |
33 | 113 | $customDirective = [ |
| 114 | + 'base-uri' => [ |
| 115 | + 'test.com', |
| 116 | + 'test.com', |
| 117 | + ], |
34 | 118 | 'script-src' => [ |
35 | | - '{nonce}', |
| 119 | + 'test.com', |
36 | 120 | ], |
37 | 121 | ]; |
38 | 122 |
|
39 | 123 | $expected = [ |
40 | 124 | 'base-uri' => [ |
41 | | - "'self'", |
| 125 | + 'test.com', |
42 | 126 | ], |
43 | 127 | 'script-src' => [ |
44 | | - "'self'", |
45 | | - "'nonce-'", |
| 128 | + 'test.com', |
| 129 | + ], |
| 130 | + ]; |
| 131 | + |
| 132 | + $result = $policyFactory->create($nonceMock, $defaultDirective, $customDirective); |
| 133 | + |
| 134 | + self::assertSame($expected, $result->getDirectives()); |
| 135 | + } |
| 136 | + |
| 137 | + public function testCreateShouldAddDirectiveWhichIsPresentInCustomButNotDefaultConfiguration(): void |
| 138 | + { |
| 139 | + $policyFactory = new PolicyFactory(); |
| 140 | + $nonceMock = $this->createMock(Nonce::class); |
| 141 | + |
| 142 | + $defaultDirective = [ |
| 143 | + 'base-uri' => [ |
| 144 | + 'test.com', |
| 145 | + ], |
| 146 | + 'script-src' => [ |
| 147 | + 'test.com', |
| 148 | + ], |
| 149 | + ]; |
| 150 | + $customDirective = [ |
| 151 | + 'worker-src' => [ |
| 152 | + 'test.com', |
| 153 | + ], |
| 154 | + ]; |
| 155 | + |
| 156 | + $expected = [ |
| 157 | + 'base-uri' => [ |
| 158 | + "test.com", |
| 159 | + ], |
| 160 | + 'script-src' => [ |
| 161 | + "test.com", |
| 162 | + ], |
| 163 | + 'worker-src' => [ |
| 164 | + "test.com", |
46 | 165 | ], |
47 | 166 | ]; |
48 | 167 |
|
|
0 commit comments