|
| 1 | +<?php |
| 2 | +namespace Vyuldashev\LaravelOpenApi\Tests\Builders; |
| 3 | + |
| 4 | +use Vyuldashev\LaravelOpenApi\Tests\TestCase; |
| 5 | +use GoldSpecDigital\ObjectOrientedOAS\OpenApi; |
| 6 | +use Vyuldashev\LaravelOpenApi\RouteInformation; |
| 7 | +use GoldSpecDigital\ObjectOrientedOAS\Objects\PathItem; |
| 8 | +use GoldSpecDigital\ObjectOrientedOAS\Objects\Operation; |
| 9 | +use GoldSpecDigital\ObjectOrientedOAS\Objects\Components; |
| 10 | +use GoldSpecDigital\ObjectOrientedOAS\Objects\SecurityScheme; |
| 11 | +use Vyuldashev\LaravelOpenApi\Factories\SecuritySchemeFactory; |
| 12 | +use GoldSpecDigital\ObjectOrientedOAS\Objects\SecurityRequirement; |
| 13 | +use Vyuldashev\LaravelOpenApi\Builders\Paths\Operation\SecurityBuilder; |
| 14 | +use Vyuldashev\LaravelOpenApi\Attributes\Operation as AttributesOperation; |
| 15 | + |
| 16 | +class SecurityBuilderTest extends TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * We're just making sure we're getting the expected output |
| 20 | + */ |
| 21 | + public function testWeCanBuildUpTheSecurityScheme(): void |
| 22 | + { |
| 23 | + $securityFactory = resolve(JwtSecurityScheme::class); |
| 24 | + $testJwtScheme = $securityFactory->build(); |
| 25 | + |
| 26 | + $globalRequirement = SecurityRequirement::create('JWT') |
| 27 | + ->securityScheme($testJwtScheme); |
| 28 | + |
| 29 | + $components = Components::create() |
| 30 | + ->securitySchemes($testJwtScheme); |
| 31 | + |
| 32 | + $operation = Operation::create() |
| 33 | + ->action('get'); |
| 34 | + |
| 35 | + $openApi = OpenApi::create() |
| 36 | + ->security($globalRequirement) |
| 37 | + ->components($components) |
| 38 | + ->paths( |
| 39 | + PathItem::create() |
| 40 | + ->route('/foo') |
| 41 | + ->operations($operation) |
| 42 | + ); |
| 43 | + |
| 44 | + self::assertSame([ |
| 45 | + 'paths' => [ |
| 46 | + '/foo' => [ |
| 47 | + 'get' => [], |
| 48 | + ], |
| 49 | + ], |
| 50 | + 'components' => [ |
| 51 | + 'securitySchemes' => [ |
| 52 | + 'JWT' => [ |
| 53 | + 'type' => 'http', |
| 54 | + 'name' => 'TestScheme', |
| 55 | + 'in' => 'header', |
| 56 | + 'scheme' => 'bearer', |
| 57 | + 'bearerFormat' => 'JWT', |
| 58 | + ], |
| 59 | + ], |
| 60 | + ], |
| 61 | + 'security' => [ |
| 62 | + [ |
| 63 | + 'JWT' => [], |
| 64 | + ] |
| 65 | + ] |
| 66 | + ], $openApi->toArray()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * We're just verifying that the builder is capable of |
| 71 | + * adding security information to the operation |
| 72 | + */ |
| 73 | + public function testWeCanAddOperationSecurityUsingBuilder() |
| 74 | + { |
| 75 | + $securityFactory = resolve(JwtSecurityScheme::class); |
| 76 | + $testJwtScheme = $securityFactory->build(); |
| 77 | + |
| 78 | + $globalRequirement = SecurityRequirement::create('JWT') |
| 79 | + ->securityScheme($testJwtScheme); |
| 80 | + |
| 81 | + $components = Components::create() |
| 82 | + ->securitySchemes($testJwtScheme); |
| 83 | + |
| 84 | + $routeInfo = new RouteInformation; |
| 85 | + $routeInfo->action = 'get'; |
| 86 | + $routeInfo->name = 'test route'; |
| 87 | + $routeInfo->actionAttributes = collect([ |
| 88 | + new AttributesOperation(security: JwtSecurityScheme::class), |
| 89 | + ]); |
| 90 | + $routeInfo->uri = '/example'; |
| 91 | + |
| 92 | + /** @var SecurityBuilder */ |
| 93 | + $builder = resolve(SecurityBuilder::class); |
| 94 | + |
| 95 | + $operation = Operation::create() |
| 96 | + ->security(...$builder->build($routeInfo)) |
| 97 | + ->action('get'); |
| 98 | + |
| 99 | + $openApi = OpenApi::create() |
| 100 | + ->security($globalRequirement) |
| 101 | + ->components($components) |
| 102 | + ->paths( |
| 103 | + PathItem::create() |
| 104 | + ->route('/foo') |
| 105 | + ->operations($operation) |
| 106 | + ); |
| 107 | + |
| 108 | + self::assertSame([ |
| 109 | + 'paths' => [ |
| 110 | + '/foo' => [ |
| 111 | + 'get' => [ |
| 112 | + 'security' => [ |
| 113 | + [ |
| 114 | + 'JWT' => [] |
| 115 | + ], |
| 116 | + ], |
| 117 | + ], |
| 118 | + ], |
| 119 | + ], |
| 120 | + 'components' => [ |
| 121 | + 'securitySchemes' => [ |
| 122 | + 'JWT' => [ |
| 123 | + 'type' => 'http', |
| 124 | + 'name' => 'TestScheme', |
| 125 | + 'in' => 'header', |
| 126 | + 'scheme' => 'bearer', |
| 127 | + 'bearerFormat' => 'JWT', |
| 128 | + ], |
| 129 | + ], |
| 130 | + ], |
| 131 | + 'security' => [ |
| 132 | + [ |
| 133 | + 'JWT' => [], |
| 134 | + ] |
| 135 | + ] |
| 136 | + ], $openApi->toArray()); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * He's the main part of the PR. It's not possible to turn |
| 141 | + * off security for an operation. |
| 142 | + */ |
| 143 | + public function testWeCanAddTurnOffOperationSecurityUsingBuilder() |
| 144 | + { |
| 145 | + $securityFactory = resolve(JwtSecurityScheme::class); |
| 146 | + $testJwtScheme = $securityFactory->build(); |
| 147 | + |
| 148 | + $globalRequirement = SecurityRequirement::create('JWT') |
| 149 | + ->securityScheme($testJwtScheme); |
| 150 | + |
| 151 | + $components = Components::create() |
| 152 | + ->securitySchemes($testJwtScheme); |
| 153 | + |
| 154 | + $routeInfo = new RouteInformation; |
| 155 | + $routeInfo->action = 'get'; |
| 156 | + $routeInfo->name = 'test route'; |
| 157 | + $routeInfo->actionAttributes = collect([ |
| 158 | + new AttributesOperation(security: ''), |
| 159 | + ]); |
| 160 | + $routeInfo->uri = '/example'; |
| 161 | + |
| 162 | + /** @var SecurityBuilder */ |
| 163 | + $builder = resolve(SecurityBuilder::class); |
| 164 | + |
| 165 | + $operation = Operation::create() |
| 166 | + ->security(...$builder->build($routeInfo)) |
| 167 | + ->action('get'); |
| 168 | + |
| 169 | + $openApi = OpenApi::create() |
| 170 | + ->security($globalRequirement) |
| 171 | + ->components($components) |
| 172 | + ->paths( |
| 173 | + PathItem::create() |
| 174 | + ->route('/foo') |
| 175 | + ->operations($operation) |
| 176 | + ); |
| 177 | + |
| 178 | + self::assertSame([ |
| 179 | + 'paths' => [ |
| 180 | + '/foo' => [ |
| 181 | + 'get' => [ |
| 182 | + 'security' => [], |
| 183 | + ], |
| 184 | + ], |
| 185 | + ], |
| 186 | + 'components' => [ |
| 187 | + 'securitySchemes' => [ |
| 188 | + 'JWT' => [ |
| 189 | + 'type' => 'http', |
| 190 | + 'name' => 'TestScheme', |
| 191 | + 'in' => 'header', |
| 192 | + 'scheme' => 'bearer', |
| 193 | + 'bearerFormat' => 'JWT', |
| 194 | + ], |
| 195 | + ], |
| 196 | + ], |
| 197 | + 'security' => [ |
| 198 | + [ |
| 199 | + 'JWT' => [], |
| 200 | + ] |
| 201 | + ] |
| 202 | + ], $openApi->toArray()); |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +class JwtSecurityScheme extends SecuritySchemeFactory |
| 207 | +{ |
| 208 | + public function build(): SecurityScheme |
| 209 | + { |
| 210 | + return SecurityScheme::create('JWT') |
| 211 | + ->name('TestScheme') |
| 212 | + ->type(SecurityScheme::TYPE_HTTP) |
| 213 | + ->in(SecurityScheme::IN_HEADER) |
| 214 | + ->scheme('bearer') |
| 215 | + ->bearerFormat('JWT'); |
| 216 | + } |
| 217 | +} |
0 commit comments