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.

203 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'=>'http://target.example','callback'=>'notaurl']
  100. ] as $params) {
  101. $response = $this->webmention($params);
  102. $this->assertEquals(400, $response->getStatusCode());
  103. $data = json_decode($response->getContent());
  104. $this->assertEquals('invalid_parameter', $data->error);
  105. }
  106. }
  107. public function testNoLinkToSource() {
  108. $this->_createExampleAccount();
  109. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nolink','target'=>'http://target.example.com']);
  110. $this->assertEquals(400, $response->getStatusCode());
  111. $data = json_decode($response->getContent());
  112. $this->assertEquals('no_link_found', $data->error);
  113. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nothtml','target'=>'http://target.example.com']);
  114. $this->assertEquals(400, $response->getStatusCode());
  115. $data = json_decode($response->getContent());
  116. $this->assertEquals('no_link_found', $data->error);
  117. }
  118. public function testHandlesMalformedHTMLWithLink() {
  119. $this->_createExampleAccount();
  120. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/invalidhtml','target'=>'http://target.example.com']);
  121. $this->assertEquals(201, $response->getStatusCode());
  122. $data = json_decode($response->getContent());
  123. $this->assertEquals(false, property_exists($data, 'error'));
  124. }
  125. public function testTargetQueuesWebmention() {
  126. $this->_createExampleAccount();
  127. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target'=>'http://target.example.com']);
  128. $this->assertEquals(201, $response->getStatusCode());
  129. $data = json_decode($response->getContent());
  130. $this->assertEquals(false, property_exists($data, 'error'));
  131. $this->assertEquals('queued', $data->status);
  132. $this->_assertQueued('http://source.example.com/basictest', 'http://target.example.com', $data->location);
  133. }
  134. public function testTargetDomainQueuesOneWebmention() {
  135. $this->_createExampleAccount();
  136. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target_domain'=>'target.example.com']);
  137. $body = $response->getContent();
  138. $this->assertEquals(201, $response->getStatusCode(), $body);
  139. $data = json_decode($body);
  140. $this->assertEquals(false, property_exists($data, 'error'), $body);
  141. $this->assertEquals('queued', $data->status, $body);
  142. $this->assertEquals(true, property_exists($data, 'location'), $body);
  143. $this->assertEquals(1, count($data->location), $body);
  144. $this->_assertQueued('http://source.example.com/basictest', 'http://target.example.com', $data->location[0]);
  145. }
  146. public function testTargetDomainQueuesMultipleWebmentions() {
  147. $this->_createExampleAccount();
  148. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target_domain'=>'example.com']);
  149. $body = $response->getContent();
  150. $this->assertEquals(201, $response->getStatusCode(), $body);
  151. $data = json_decode($body);
  152. $this->assertEquals(false, property_exists($data, 'error'), $body);
  153. $this->assertEquals('queued', $data->status, $body);
  154. $this->assertEquals(2, count($data->location), $body);
  155. $this->_assertQueued('http://source.example.com/basictest', 'http://target.example.com', $data->location[0]);
  156. $this->_assertQueued('http://source.example.com/basictest', 'http://target2.example.com', $data->location[1]);
  157. }
  158. public function testStatusNotFound() {
  159. $this->_createExampleAccount();
  160. $response = $this->status('foo');
  161. $this->assertEquals(404, $response->getStatusCode());
  162. $data = json_decode($response->getContent());
  163. $this->assertEquals('not_found', $data->status);
  164. }
  165. }