SecuritySchemesTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApiTests;
  6. use OpenApi\Analyser;
  7. use OpenApi\Annotations\Info;
  8. use OpenApi\Annotations\SecurityScheme;
  9. use OpenApi\Annotations\Server;
  10. /**
  11. * Class SecuritySchemesTest
  12. *
  13. * Security openapi test
  14. */
  15. class SecuritySchemesTest extends OpenApiTestCase
  16. {
  17. /**
  18. * Test parse servers
  19. */
  20. public function testParseServers()
  21. {
  22. $comment = <<<INFO
  23. /**
  24. * @OA\Info(
  25. * title="Simple api",
  26. * description="Simple api description",
  27. * )
  28. * @OA\Server(
  29. * url="http://example.com",
  30. * description="First host"
  31. * )
  32. * @OA\Server(
  33. * url="http://example-second.com",
  34. * description="Second host"
  35. * )
  36. */
  37. INFO;
  38. $analysis = $this->getAnalysis($comment);
  39. $this->assertCount(3, $analysis);
  40. $this->assertInstanceOf(Info::class, $analysis[0]);
  41. $this->assertInstanceOf(Server::class, $analysis[1]);
  42. $this->assertInstanceOf(Server::class, $analysis[2]);
  43. $this->assertEquals('http://example.com', $analysis[1]->url);
  44. $this->assertEquals('First host', $analysis[1]->description);
  45. $this->assertEquals('http://example-second.com', $analysis[2]->url);
  46. $this->assertEquals('Second host', $analysis[2]->description);
  47. }
  48. /**
  49. * Test parse security scheme
  50. */
  51. public function testImplicitFlowAnnotation()
  52. {
  53. $comment = <<<SCHEME
  54. /**
  55. * @OA\SecurityScheme(
  56. * @OA\Flow(
  57. * flow="implicit",
  58. * tokenUrl="http://auth.test.com/token",
  59. * refreshUrl="http://auth.test.com/refresh-token"
  60. * ),
  61. * securityScheme="oauth2",
  62. * in="header",
  63. * type="oauth2",
  64. * description="Oauth2 security",
  65. * name="oauth2",
  66. * scheme="https",
  67. * bearerFormat="bearer",
  68. * openIdConnectUrl="http://test.com",
  69. * )
  70. */
  71. SCHEME;
  72. $analysis = $this->getAnalysis($comment);
  73. $this->assertCount(1, $analysis);
  74. /** @var \OpenApi\Annotations\SecurityScheme $security */
  75. $security = $analysis[0];
  76. $this->assertInstanceOf(SecurityScheme::class, $security);
  77. $this->assertCount(1, $security->flows);
  78. $this->assertEquals('implicit', $security->flows[0]->flow);
  79. $this->assertEquals('http://auth.test.com/token', $security->flows[0]->tokenUrl);
  80. $this->assertEquals('http://auth.test.com/refresh-token', $security->flows[0]->refreshUrl);
  81. }
  82. public function testMultipleAnnotations()
  83. {
  84. $comment = <<<SCHEME
  85. /**
  86. * @OA\SecurityScheme(
  87. * @OA\Flow(
  88. * flow="implicit",
  89. * tokenUrl="http://auth.test.com/token",
  90. * refreshUrl="http://auth.test.com/refresh-token"
  91. * ),
  92. * @OA\Flow(
  93. * flow="client_credentials",
  94. * authorizationUrl="http://authClient.test.com",
  95. * tokenUrl="http://authClient.test.com/token",
  96. * refreshUrl="http://authClient.test.com/refresh-token"
  97. * ),
  98. * securityScheme="oauth2",
  99. * in="header",
  100. * type="oauth2",
  101. * description="Oauth2 security",
  102. * name="oauth2",
  103. * scheme="https",
  104. * bearerFormat="bearer",
  105. * openIdConnectUrl="http://test.com",
  106. * )
  107. */
  108. SCHEME;
  109. $analysis = $this->getAnalysis($comment);
  110. $this->assertCount(1, $analysis);
  111. /** @var \OpenApi\Annotations\SecurityScheme $security */
  112. $security = $analysis[0];
  113. $this->assertCount(2, $security->flows);
  114. $this->assertEquals('implicit', $security->flows[0]->flow);
  115. $this->assertEquals('http://auth.test.com/token', $security->flows[0]->tokenUrl);
  116. $this->assertEquals('http://auth.test.com/refresh-token', $security->flows[0]->refreshUrl);
  117. $this->assertEquals('client_credentials', $security->flows[1]->flow);
  118. $this->assertEquals('http://authClient.test.com', $security->flows[1]->authorizationUrl);
  119. $this->assertEquals('http://authClient.test.com/token', $security->flows[1]->tokenUrl);
  120. $this->assertEquals('http://authClient.test.com/refresh-token', $security->flows[1]->refreshUrl);
  121. }
  122. /**
  123. * Get scheme analysis
  124. *
  125. * @param string $comment
  126. *
  127. * @return array
  128. */
  129. private function getAnalysis($comment)
  130. {
  131. $analyser = new Analyser();
  132. $analysis = $analyser->fromComment($comment, null);
  133. return $analysis;
  134. }
  135. }