You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
8.4 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class APITest extends PHPUnit_Framework_TestCase {
  5. private $client;
  6. public function setUp() {
  7. $this->client = new API();
  8. $this->client->http = new Telegraph\HTTPTest();
  9. ORM::for_table('users')->raw_query('TRUNCATE users')->delete_many();
  10. ORM::for_table('roles')->raw_query('TRUNCATE roles')->delete_many();
  11. ORM::for_table('sites')->raw_query('TRUNCATE sites')->delete_many();
  12. ORM::for_table('webmentions')->raw_query('TRUNCATE webmentions')->delete_many();
  13. ORM::for_table('webmention_status')->raw_query('TRUNCATE webmention_status')->delete_many();
  14. }
  15. private function webmention($params) {
  16. $request = new Request($params);
  17. $response = new Response();
  18. return $this->client->webmention($request, $response);
  19. }
  20. private function status($code) {
  21. $request = new Request();
  22. $response = new Response();
  23. return $this->client->webmention_status($request, $response, ['code'=>$code]);
  24. }
  25. private function _createExampleAccount() {
  26. $user = ORM::for_table('users')->create();
  27. $user->url = 'http://example.com';
  28. $user->save();
  29. $site = ORM::for_table('sites')->create();
  30. $site->name = 'Example';
  31. $site->created_by = $user->id();
  32. $site->save();
  33. $role = ORM::for_table('roles')->create();
  34. $role->site_id = $site->id();
  35. $role->user_id = $user->id();
  36. $role->role = 'owner';
  37. $role->token = 'a';
  38. $role->save();
  39. }
  40. private function _assertQueued($source, $target, $status_url) {
  41. preg_match('/\/webmention\/(.+)/', $status_url, $match);
  42. $this->assertNotNull($match);
  43. # Verify it queued the mention in the database
  44. $d = ORM::for_table('webmentions')->where(['source' => $source, 'target' => $target])->find_one();
  45. $this->assertNotNull($d);
  46. $this->assertEquals($match[1], $d->token);
  47. # Check the status endpoint to make sure it says it's still queued
  48. $response = $this->status($d->token);
  49. $this->assertEquals(200, $response->getStatusCode());
  50. $data = json_decode($response->getContent());
  51. $this->assertEquals('queued', $data->status);
  52. }
  53. public function testAuthentication() {
  54. $response = $this->webmention([]);
  55. $this->assertEquals(401, $response->getStatusCode());
  56. $data = json_decode($response->getContent());
  57. $this->assertEquals('authentication_required', $data->error);
  58. $this->_createExampleAccount();
  59. $response = $this->webmention(['token'=>'x','source'=>'http://source.example','target'=>'http://target.example']);
  60. $this->assertEquals(401, $response->getStatusCode());
  61. $data = json_decode($response->getContent());
  62. $this->assertEquals('invalid_token', $data->error);
  63. $response = $this->webmention(['token'=>'a']);
  64. $this->assertEquals(400, $response->getStatusCode());
  65. $data = json_decode($response->getContent());
  66. $this->assertEquals('missing_parameters', $data->error);
  67. }
  68. public function testMissingParameters() {
  69. $this->_createExampleAccount();
  70. $response = $this->webmention(['token'=>'a']);
  71. $this->assertEquals(400, $response->getStatusCode());
  72. $data = json_decode($response->getContent());
  73. $this->assertEquals('missing_parameters', $data->error);
  74. $response = $this->webmention(['token'=>'a','source'=>'foo']);
  75. $this->assertEquals(400, $response->getStatusCode());
  76. $data = json_decode($response->getContent());
  77. $this->assertEquals('missing_parameters', $data->error);
  78. $response = $this->webmention(['token'=>'a','target'=>'foo']);
  79. $this->assertEquals(400, $response->getStatusCode());
  80. $data = json_decode($response->getContent());
  81. $this->assertEquals('missing_parameters', $data->error);
  82. $response = $this->webmention(['token'=>'a','target_domain'=>'foo']);
  83. $this->assertEquals(400, $response->getStatusCode());
  84. $data = json_decode($response->getContent());
  85. $this->assertEquals('missing_parameters', $data->error);
  86. }
  87. public function testTargetAndTargetDomain() {
  88. $this->_createExampleAccount();
  89. $response = $this->webmention(['token'=>'a','source'=>'foo','target'=>'foo','target_domain'=>'foo']);
  90. $this->assertEquals(400, $response->getStatusCode());
  91. $data = json_decode($response->getContent());
  92. $this->assertEquals('invalid_parameter', $data->error);
  93. }
  94. public function testInvalidURLs() {
  95. $this->_createExampleAccount();
  96. foreach ([['token'=>'a','source'=>'notaurl','target'=>'alsonotaurl'],
  97. ['token'=>'a','source'=>'http://source.example','target'=>'alsonotaurl'],
  98. ['token'=>'a','source'=>'notaurl','target'=>'http://target.example'],
  99. ['token'=>'a','source'=>'http://source.example','target'=>'mailto:user@example.com'],
  100. ['token'=>'a','source'=>'http://source.example','target'=>'http://target.example','callback'=>'notaurl']
  101. ] as $params) {
  102. $response = $this->webmention($params);
  103. $this->assertEquals(400, $response->getStatusCode());
  104. $data = json_decode($response->getContent());
  105. $this->assertEquals('invalid_parameter', $data->error);
  106. }
  107. }
  108. public function testNoLinkToSource() {
  109. $this->_createExampleAccount();
  110. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nolink','target'=>'http://target.example.com']);
  111. $this->assertEquals(400, $response->getStatusCode());
  112. $data = json_decode($response->getContent());
  113. $this->assertEquals('no_link_found', $data->error);
  114. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nothtml','target'=>'http://target.example.com']);
  115. $this->assertEquals(400, $response->getStatusCode());
  116. $data = json_decode($response->getContent());
  117. $this->assertEquals('no_link_found', $data->error);
  118. }
  119. public function testHandlesMalformedHTMLWithLink() {
  120. $this->_createExampleAccount();
  121. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/invalidhtml','target'=>'http://target.example.com']);
  122. $this->assertEquals(201, $response->getStatusCode());
  123. $data = json_decode($response->getContent());
  124. $this->assertEquals(false, property_exists($data, 'error'));
  125. }
  126. public function testTargetQueuesWebmention() {
  127. $this->_createExampleAccount();
  128. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target'=>'http://target.example.com']);
  129. $this->assertEquals(201, $response->getStatusCode());
  130. $data = json_decode($response->getContent());
  131. $this->assertEquals(false, property_exists($data, 'error'));
  132. $this->assertEquals('queued', $data->status);
  133. $this->_assertQueued('http://source.example.com/basictest', 'http://target.example.com', $data->location);
  134. }
  135. public function testTargetDomainQueuesOneWebmention() {
  136. $this->_createExampleAccount();
  137. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target_domain'=>'target.example.com']);
  138. $body = $response->getContent();
  139. $this->assertEquals(201, $response->getStatusCode(), $body);
  140. $data = json_decode($body);
  141. $this->assertEquals(false, property_exists($data, 'error'), $body);
  142. $this->assertEquals('queued', $data->status, $body);
  143. $this->assertEquals(true, property_exists($data, 'location'), $body);
  144. $this->assertEquals(1, count($data->location), $body);
  145. $this->_assertQueued('http://source.example.com/basictest', 'http://target.example.com', $data->location[0]);
  146. }
  147. public function testTargetDomainQueuesMultipleWebmentions() {
  148. $this->_createExampleAccount();
  149. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target_domain'=>'example.com']);
  150. $body = $response->getContent();
  151. $this->assertEquals(201, $response->getStatusCode(), $body);
  152. $data = json_decode($body);
  153. $this->assertEquals(false, property_exists($data, 'error'), $body);
  154. $this->assertEquals('queued', $data->status, $body);
  155. $this->assertEquals(2, count($data->location), $body);
  156. $this->_assertQueued('http://source.example.com/basictest', 'http://target.example.com', $data->location[0]);
  157. $this->_assertQueued('http://source.example.com/basictest', 'http://target2.example.com', $data->location[1]);
  158. }
  159. public function testStatusNotFound() {
  160. $this->_createExampleAccount();
  161. $response = $this->status('foo');
  162. $this->assertEquals(404, $response->getStatusCode());
  163. $data = json_decode($response->getContent());
  164. $this->assertEquals('not_found', $data->status);
  165. }
  166. }