PostmanCollectionWriterTest.php 12 KB

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