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.

226 lines
9.8 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->assertNotFalse($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. private function _assertNotQueued($source, $target) {
  54. # Verify it did not queue a mention in the database
  55. $d = ORM::for_table('webmentions')->where(['source' => $source, 'target' => $target])->find_one();
  56. $this->assertFalse($d);
  57. }
  58. public function testAuthentication() {
  59. $response = $this->webmention([]);
  60. $this->assertEquals(401, $response->getStatusCode());
  61. $data = json_decode($response->getContent());
  62. $this->assertEquals('authentication_required', $data->error);
  63. $this->_createExampleAccount();
  64. $response = $this->webmention(['token'=>'x','source'=>'http://source.example','target'=>'http://target.example']);
  65. $this->assertEquals(401, $response->getStatusCode());
  66. $data = json_decode($response->getContent());
  67. $this->assertEquals('invalid_token', $data->error);
  68. $response = $this->webmention(['token'=>'a']);
  69. $this->assertEquals(400, $response->getStatusCode());
  70. $data = json_decode($response->getContent());
  71. $this->assertEquals('missing_parameters', $data->error);
  72. }
  73. public function testMissingParameters() {
  74. $this->_createExampleAccount();
  75. $response = $this->webmention(['token'=>'a']);
  76. $this->assertEquals(400, $response->getStatusCode());
  77. $data = json_decode($response->getContent());
  78. $this->assertEquals('missing_parameters', $data->error);
  79. $response = $this->webmention(['token'=>'a','source'=>'foo']);
  80. $this->assertEquals(400, $response->getStatusCode());
  81. $data = json_decode($response->getContent());
  82. $this->assertEquals('missing_parameters', $data->error);
  83. $response = $this->webmention(['token'=>'a','target'=>'foo']);
  84. $this->assertEquals(400, $response->getStatusCode());
  85. $data = json_decode($response->getContent());
  86. $this->assertEquals('missing_parameters', $data->error);
  87. $response = $this->webmention(['token'=>'a','target_domain'=>'foo']);
  88. $this->assertEquals(400, $response->getStatusCode());
  89. $data = json_decode($response->getContent());
  90. $this->assertEquals('missing_parameters', $data->error);
  91. }
  92. public function testTargetAndTargetDomain() {
  93. $this->_createExampleAccount();
  94. $response = $this->webmention(['token'=>'a','source'=>'foo','target'=>'foo','target_domain'=>'foo']);
  95. $this->assertEquals(400, $response->getStatusCode());
  96. $data = json_decode($response->getContent());
  97. $this->assertEquals('invalid_parameter', $data->error);
  98. }
  99. public function testInvalidURLs() {
  100. $this->_createExampleAccount();
  101. foreach ([['token'=>'a','source'=>'notaurl','target'=>'alsonotaurl'],
  102. ['token'=>'a','source'=>'http://source.example','target'=>'alsonotaurl'],
  103. ['token'=>'a','source'=>'notaurl','target'=>'http://target.example'],
  104. ['token'=>'a','source'=>'http://source.example','target'=>'mailto:user@example.com'],
  105. ['token'=>'a','source'=>'http://source.example','target'=>'http://target.example','callback'=>'notaurl']
  106. ] as $params) {
  107. $response = $this->webmention($params);
  108. $this->assertEquals(400, $response->getStatusCode());
  109. $data = json_decode($response->getContent());
  110. $this->assertEquals('invalid_parameter', $data->error);
  111. }
  112. }
  113. public function testNoLinkToSource() {
  114. $this->_createExampleAccount();
  115. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nolink','target'=>'http://target.example.com']);
  116. $this->assertEquals(400, $response->getStatusCode());
  117. $data = json_decode($response->getContent());
  118. $this->assertEquals('no_link_found', $data->error);
  119. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nothtml','target'=>'http://target.example.com']);
  120. $this->assertEquals(400, $response->getStatusCode());
  121. $data = json_decode($response->getContent());
  122. $this->assertEquals('no_link_found', $data->error);
  123. }
  124. public function testHandlesMalformedHTMLWithLink() {
  125. $this->_createExampleAccount();
  126. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/invalidhtml','target'=>'http://target.example.com']);
  127. $this->assertEquals(201, $response->getStatusCode());
  128. $data = json_decode($response->getContent());
  129. $this->assertEquals(false, property_exists($data, 'error'));
  130. }
  131. public function testTargetQueuesWebmention() {
  132. $this->_createExampleAccount();
  133. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target'=>'http://target.example.com']);
  134. $this->assertEquals(201, $response->getStatusCode());
  135. $data = json_decode($response->getContent());
  136. $this->assertEquals(false, property_exists($data, 'error'));
  137. $this->assertEquals('queued', $data->status);
  138. $this->_assertQueued('http://source.example.com/basictest', 'http://target.example.com', $data->location);
  139. }
  140. public function testTargetDomainQueuesOneWebmention() {
  141. $this->_createExampleAccount();
  142. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target_domain'=>'target.example.com']);
  143. $body = $response->getContent();
  144. $this->assertEquals(201, $response->getStatusCode(), $body);
  145. $data = json_decode($body);
  146. $this->assertEquals(false, property_exists($data, 'error'), $body);
  147. $this->assertEquals('queued', $data->status, $body);
  148. $this->assertEquals(true, property_exists($data, 'location'), $body);
  149. $this->assertEquals(1, count($data->location), $body);
  150. $this->_assertQueued('http://source.example.com/basictest', 'http://target.example.com', $data->location[0]);
  151. }
  152. public function testTargetDomainQueuesMultipleWebmentions() {
  153. $this->_createExampleAccount();
  154. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target_domain'=>'example.com']);
  155. $body = $response->getContent();
  156. $this->assertEquals(201, $response->getStatusCode(), $body);
  157. $data = json_decode($body);
  158. $this->assertEquals(false, property_exists($data, 'error'), $body);
  159. $this->assertEquals('queued', $data->status, $body);
  160. $this->assertEquals(2, count($data->location), $body);
  161. $this->_assertQueued('http://source.example.com/basictest', 'http://target.example.com', $data->location[0]);
  162. $this->_assertQueued('http://source.example.com/basictest', 'http://target2.example.com', $data->location[1]);
  163. }
  164. public function testTargetDomainQueuesOnlyWebmentionsFromTargetDomain() {
  165. $this->_createExampleAccount();
  166. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/multipletest','target_domain'=>'example.com']);
  167. $body = $response->getContent();
  168. $this->assertEquals(201, $response->getStatusCode(), $body);
  169. $data = json_decode($body);
  170. $this->assertEquals(false, property_exists($data, 'error'), $body);
  171. $this->assertEquals('queued', $data->status, $body);
  172. $this->assertEquals(2, count($data->location), $body);
  173. $this->_assertQueued('http://source.example.com/multipletest', 'http://target.example.com', $data->location[0]);
  174. $this->_assertQueued('http://source.example.com/multipletest', 'http://target2.example.com', $data->location[1]);
  175. $this->_assertNotQueued('http://source.example.com/multipletest', 'http://target.example.org');
  176. $this->_assertNotQueued('http://source.example.com/multipletest', '/relativelink');
  177. $this->_assertNotQueued('http://source.example.com/multipletest', 'http://source.example.com/relativelink');
  178. }
  179. public function testStatusNotFound() {
  180. $this->_createExampleAccount();
  181. $response = $this->status('foo');
  182. $this->assertEquals(404, $response->getStatusCode());
  183. $data = json_decode($response->getContent());
  184. $this->assertEquals('not_found', $data->status);
  185. }
  186. }