PostmanCollectionWriterTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Illuminate\Support\Collection;
  4. use Knuckles\Scribe\Extracting\Generator;
  5. use Knuckles\Scribe\Writing\PostmanCollectionWriter;
  6. use Orchestra\Testbench\TestCase;
  7. class PostmanCollectionWriterTest extends TestCase
  8. {
  9. public function testCorrectStructureIsFollowed()
  10. {
  11. \Config::set('scribe.title', 'Test API');
  12. \Config::set('scribe.postman', [
  13. 'description' => 'A fake description',
  14. ]);
  15. $writer = new PostmanCollectionWriter(new Collection(), '');
  16. $collection = $writer->generatePostmanCollection();
  17. $this->assertSame('Test API', $collection['info']['name']);
  18. $this->assertSame('A fake description', $collection['info']['description']);
  19. }
  20. public function testFallbackCollectionNameIsUsed()
  21. {
  22. \Config::set('app.name', 'Fake App');
  23. $writer = new PostmanCollectionWriter(new Collection(), '');
  24. $collection = $writer->generatePostmanCollection();
  25. $this->assertSame('Fake App API', $collection['info']['name']);
  26. }
  27. public function testAuthIsNotIncludedWhenNull()
  28. {
  29. $writer = new PostmanCollectionWriter(new Collection(), '');
  30. $collection = $writer->generatePostmanCollection();
  31. $this->assertArrayNotHasKey('auth', $collection);
  32. }
  33. public function testAuthIsIncludedVerbatim()
  34. {
  35. $auth = [
  36. 'type' => 'test',
  37. 'test' => ['a' => 1],
  38. ];
  39. \Config::set('scribe.postman', [
  40. 'auth' => $auth,
  41. ]);
  42. $writer = new PostmanCollectionWriter(new Collection(), '');
  43. $collection = $writer->generatePostmanCollection();
  44. $this->assertSame($auth, $collection['auth']);
  45. }
  46. public function testEndpointIsParsed()
  47. {
  48. $route = $this->createMockRouteData('some/path');
  49. // Ensure method is set correctly for assertion later
  50. $route['methods'] = ['GET'];
  51. $collection = $this->createMockRouteGroup([$route], 'Group');
  52. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  53. $collection = $writer->generatePostmanCollection();
  54. $this->assertSame('Group', data_get($collection, 'item.0.name'), 'Group name exists');
  55. $item = data_get($collection, 'item.0.item.0');
  56. $this->assertSame('some/path', $item['name'], 'Name defaults to path');
  57. $this->assertSame('http', data_get($item, 'request.url.protocol'), 'Protocol defaults to http');
  58. $this->assertSame('fake.localhost', data_get($item, 'request.url.host'), 'Host uses what\'s given');
  59. $this->assertSame('some/path', data_get($item, 'request.url.path'), 'Path is set correctly');
  60. $this->assertEmpty(data_get($item, 'request.url.query'), 'Query parameters are empty');
  61. $this->assertSame('GET', data_get($item, 'request.method'), 'Method is correctly resolved');
  62. $this->assertContains([
  63. 'key' => 'Accept',
  64. 'value' => 'application/json',
  65. ], data_get($item, 'request.header'), 'JSON Accept header is added');
  66. }
  67. public function testHttpsProtocolIsDetected()
  68. {
  69. $collection = $this->createMockRouteGroup([$this->createMockRouteData('fake')]);
  70. $writer = new PostmanCollectionWriter($collection, 'https://fake.localhost');
  71. $collection = $writer->generatePostmanCollection();
  72. $this->assertSame('https', data_get($collection, 'item.0.item.0.request.url.protocol'));
  73. }
  74. public function testHeadersArePulledFromRoute()
  75. {
  76. $route = $this->createMockRouteData('some/path');
  77. $route['headers'] = ['X-Fake' => 'Test'];
  78. $collection = $this->createMockRouteGroup([$route], 'Group');
  79. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  80. $collection = $writer->generatePostmanCollection();
  81. $this->assertContains([
  82. 'key' => 'X-Fake',
  83. 'value' => 'Test',
  84. ], data_get($collection, 'item.0.item.0.request.header'));
  85. }
  86. /** @test */
  87. public function url_parameters_are_represented_properly()
  88. {
  89. $fakeRoute = $this->createMockRouteData('fake/{param}');
  90. $fakeRoute['urlParameters'] = ['param' => [
  91. 'description' => 'A test description for the test param',
  92. 'required' => true,
  93. 'value' => 'foobar',
  94. ]];
  95. $collection = $this->createMockRouteGroup([$fakeRoute]);
  96. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  97. $collection = $writer->generatePostmanCollection();
  98. $item = data_get($collection, 'item.0.item.0');
  99. $this->assertSame('fake/{param}', $item['name'], 'Name defaults to URL path');
  100. $this->assertSame('fake/:param', data_get($item, 'request.url.path'), 'Path is converted');
  101. $variableData = data_get($collection, 'item.0.item.0.request.url.variable');
  102. $this->assertCount(1, $variableData);
  103. $this->assertEquals([
  104. 'id' => 'param',
  105. 'key' => 'param',
  106. 'value' => 'foobar',
  107. 'description' => 'A test description for the test param',
  108. ], $variableData[0]);
  109. }
  110. /** @test */
  111. public function testQueryParametersAreDocumented()
  112. {
  113. $fakeRoute = $this->createMockRouteData('fake/path');
  114. $fakeRoute['queryParameters'] = [
  115. 'limit' => [
  116. 'description' => 'A fake limit for my fake endpoint',
  117. 'required' => true,
  118. 'value' => 5,
  119. ],
  120. 'filters.*' => [
  121. 'description' => 'Filters',
  122. 'required' => true,
  123. 'value' => '34,12',
  124. ],
  125. ];
  126. $fakeRoute['cleanQueryParameters'] = Generator::cleanParams($fakeRoute['queryParameters']);
  127. $collection = $this->createMockRouteGroup([$fakeRoute]);
  128. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  129. $collection = $writer->generatePostmanCollection();
  130. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  131. $this->assertCount(2, $variableData);
  132. $this->assertEquals([
  133. 'key' => 'limit',
  134. 'value' => '5',
  135. 'description' => 'A fake limit for my fake endpoint',
  136. 'disabled' => false,
  137. ], $variableData[0]);
  138. $this->assertEquals([
  139. 'key' => 'filters',
  140. 'value' => urlencode("34,12"),
  141. 'description' => 'Filters',
  142. 'disabled' => false,
  143. ], $variableData[1]);
  144. }
  145. public function testUrlParametersAreNotIncludedIfMissingFromPath()
  146. {
  147. $fakeRoute = $this->createMockRouteData('fake/path');
  148. $fakeRoute['urlParameters'] = ['limit' => [
  149. 'description' => 'A fake limit for my fake endpoint',
  150. 'required' => false,
  151. 'value' => 5,
  152. ]];
  153. $collection = $this->createMockRouteGroup([$fakeRoute]);
  154. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  155. $collection = $writer->generatePostmanCollection();
  156. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  157. $this->assertCount(0, $variableData);
  158. }
  159. public function testQueryParametersAreDisabledWithNoValueWhenNotRequired()
  160. {
  161. $fakeRoute = $this->createMockRouteData('fake/path');
  162. $fakeRoute['queryParameters'] = [
  163. 'required' => [
  164. 'description' => 'A required param with a null value',
  165. 'required' => true,
  166. 'value' => null,
  167. ],
  168. 'not_required' => [
  169. 'description' => 'A not required param with a null value',
  170. 'required' => false,
  171. 'value' => null,
  172. ],
  173. ];
  174. $fakeRoute['cleanQueryParameters'] = Generator::cleanParams($fakeRoute['queryParameters']);
  175. $collection = $this->createMockRouteGroup([$fakeRoute]);
  176. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  177. $collection = $writer->generatePostmanCollection();
  178. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  179. $this->assertCount(2, $variableData);
  180. $this->assertContains([
  181. 'key' => 'required',
  182. 'value' => null,
  183. 'description' => 'A required param with a null value',
  184. 'disabled' => false,
  185. ], $variableData);
  186. $this->assertContains([
  187. 'key' => 'not_required',
  188. 'value' => null,
  189. 'description' => 'A not required param with a null value',
  190. 'disabled' => true,
  191. ], $variableData);
  192. }
  193. /**
  194. * @dataProvider provideAuthConfigHeaderData
  195. */
  196. public function testAuthAutoExcludesHeaderDefinitions(array $authConfig, array $expectedRemovedHeaders)
  197. {
  198. \Config::set('scribe.postman', [
  199. 'auth' => $authConfig,
  200. ]);
  201. $route = $this->createMockRouteData('some/path');
  202. $route['headers'] = $expectedRemovedHeaders;
  203. $collection = $this->createMockRouteGroup([$route], 'Group');
  204. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  205. $collection = $writer->generatePostmanCollection();
  206. foreach ($expectedRemovedHeaders as $key => $value) {
  207. $this->assertNotContains(compact('key', 'value'), data_get($collection, 'item.0.item.0.request.header'));
  208. }
  209. }
  210. public function provideAuthConfigHeaderData()
  211. {
  212. yield [
  213. ['type' => 'bearer', 'bearer' => ['token' => 'Test']],
  214. ['Authorization' => 'Bearer Test'],
  215. ];
  216. yield [
  217. ['type' => 'apikey', 'apikey' => ['value' => 'Test', 'key' => 'X-Authorization']],
  218. ['X-Authorization' => 'Test'],
  219. ];
  220. }
  221. public function testApiKeyAuthIsIgnoredIfExplicitlyNotInHeader()
  222. {
  223. \Config::set('scribe.postman', [
  224. 'auth' => ['type' => 'apikey', 'apikey' => [
  225. 'value' => 'Test',
  226. 'key' => 'X-Authorization',
  227. 'in' => 'notheader',
  228. ]],
  229. ]);
  230. $route = $this->createMockRouteData('some/path');
  231. $route['headers'] = ['X-Authorization' => 'Test'];
  232. $collection = $this->createMockRouteGroup([$route], 'Group');
  233. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  234. $collection = $writer->generatePostmanCollection();
  235. $this->assertContains([
  236. 'key' => 'X-Authorization',
  237. 'value' => 'Test',
  238. ], data_get($collection, 'item.0.item.0.request.header'));
  239. }
  240. protected function createMockRouteData($path, $title = '')
  241. {
  242. return [
  243. 'uri' => $path,
  244. 'methods' => ['GET'],
  245. 'headers' => [],
  246. 'metadata' => [
  247. 'groupDescription' => '',
  248. 'title' => $title,
  249. ],
  250. 'urlParameters' => [],
  251. 'cleanUrlParameters' => [],
  252. 'queryParameters' => [],
  253. 'cleanQueryParameters' => [],
  254. 'bodyParameters' => [],
  255. 'cleanBodyParameters' => [],
  256. 'fileParameters' => [],
  257. 'responses' => [],
  258. 'responseFields' => [],
  259. ];
  260. }
  261. protected function createMockRouteGroup(array $routes, $groupName = 'Group')
  262. {
  263. return collect([$groupName => collect($routes)]);
  264. }
  265. }