ApiControllerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Packagist\WebBundle\Tests\Controller;
  3. use Packagist\WebBundle\Entity\Package;
  4. use Packagist\WebBundle\Entity\User;
  5. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  6. class ApiControllerTest extends WebTestCase
  7. {
  8. public function testGithubFailsCorrectly()
  9. {
  10. $client = self::createClient();
  11. $client->request('GET', '/api/github');
  12. $this->assertEquals(405, $client->getResponse()->getStatusCode(), 'GET method should not be allowed for GitHub Post-Receive URL');
  13. $payload = json_encode(array('repository' => array('url' => 'git://github.com/composer/composer',)));
  14. $client->request('POST', '/api/github?username=INVALID_USER&apiToken=INVALID_TOKEN', array('payload' => $payload,));
  15. $this->assertEquals(403, $client->getResponse()->getStatusCode(), 'POST method should return 403 "Forbidden" if invalid username and API Token are sent');
  16. }
  17. /**
  18. * @dataProvider githubApiProvider
  19. */
  20. public function testGithubApi($url)
  21. {
  22. $client = self::createClient();
  23. $package = new Package;
  24. $package->setRepository($url);
  25. $user = new User;
  26. $user->addPackages($package);
  27. $repo = $this->getMockBuilder('Packagist\WebBundle\Entity\UserRepository')->disableOriginalConstructor()->getMock();
  28. $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock();
  29. $updater = $this->getMockBuilder('Packagist\WebBundle\Package\Updater')->disableOriginalConstructor()->getMock();
  30. $repo->expects($this->once())
  31. ->method('findOneBy')
  32. ->with($this->equalTo(array('username' => 'test', 'apiToken' => 'token')))
  33. ->will($this->returnValue($user));
  34. static::$kernel->getContainer()->set('packagist.user_repository', $repo);
  35. static::$kernel->getContainer()->set('doctrine.orm.entity_manager', $em);
  36. static::$kernel->getContainer()->set('packagist.package_updater', $updater);
  37. $payload = json_encode(array('repository' => array('url' => 'git://github.com/composer/composer')));
  38. $client->request('POST', '/api/github?username=test&apiToken=token', array('payload' => $payload));
  39. $this->assertEquals(202, $client->getResponse()->getStatusCode());
  40. }
  41. public function githubApiProvider()
  42. {
  43. return array(
  44. array('https://github.com/composer/composer.git'),
  45. array('http://github.com/composer/composer.git'),
  46. array('http://github.com/composer/composer'),
  47. array('git@github.com:composer/composer.git'),
  48. );
  49. }
  50. /**
  51. * @depends testGithubFailsCorrectly
  52. * @dataProvider urlProvider
  53. */
  54. public function testUrlDetection($endpoint, $url, $expectedOK)
  55. {
  56. $client = self::createClient();
  57. if ($endpoint == 'bitbucket') {
  58. $canonUrl = substr($url, 0, 1);
  59. $absUrl = substr($url, 1);
  60. $payload = json_encode(array('canon_url' => $canonUrl, 'repository' => array('absolute_url' => $absUrl)));
  61. } else {
  62. $payload = json_encode(array('repository' => array('url' => $url)));
  63. }
  64. $client->request('POST', '/api/'.$endpoint.'?username=INVALID_USER&apiToken=INVALID_TOKEN', array('payload' => $payload));
  65. $status = $client->getResponse()->getStatusCode();
  66. if (!$expectedOK) {
  67. $this->assertEquals(406, $status, 'POST method should return 406 "Not Acceptable" if an unknown URL was sent');
  68. } else {
  69. $this->assertEquals(403, $status, 'POST method should return 403 "Forbidden" for a valid URL with bad credentials.');
  70. }
  71. }
  72. public function urlProvider()
  73. {
  74. return array(
  75. // valid github URLs
  76. array('github', 'github.com/user/repo', true),
  77. array('github', 'github.com/user/repo.git', true),
  78. array('github', 'http://github.com/user/repo', true),
  79. array('github', 'https://github.com/user/repo', true),
  80. array('github', 'https://github.com/user/repo.git', true),
  81. array('github', 'git://github.com/user/repo', true),
  82. array('github', 'git@github.com:user/repo.git', true),
  83. array('github', 'git@github.com:user/repo', true),
  84. array('github', 'https://github.com/user/repo/', true),
  85. // valid bitbucket URLs
  86. array('bitbucket', 'bitbucket.org/user/repo', true),
  87. array('bitbucket', 'http://bitbucket.org/user/repo', true),
  88. array('bitbucket', 'https://bitbucket.org/user/repo', true),
  89. // valid others
  90. array('update-package', 'https://ghe.example.org/user/repository', true),
  91. array('update-package', 'https://gitlab.org/user/repository', true),
  92. array('update-package', 'ssh://git@stash.xxxxx.com/uuuuu/qqqqq.git', true),
  93. array('update-package', 'ssh://git@stash.xxxxx.com:2222/uuuuu/qqqqq.git', true),
  94. // invalid URLs
  95. array('github', 'php://github.com/user/repository', false),
  96. array('github', 'javascript://github.com/user/repository', false),
  97. array('github', 'http://', false),
  98. array('github', 'https://github.com/user/', false),
  99. array('github', 'https://github.com/user', false),
  100. array('github', 'https://github.com/', false),
  101. array('github', 'https://github.com', false),
  102. array('update-package', 'ssh://git@stash.zzzzz.com/kkkkk.git', false),
  103. array('update-package', 'ssh://ghe.example.org/user/jjjjj.git', false),
  104. );
  105. }
  106. }