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.

163 lines
6.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. public function testAuthentication() {
  41. $response = $this->webmention([]);
  42. $this->assertEquals(401, $response->getStatusCode());
  43. $data = json_decode($response->getContent());
  44. $this->assertEquals('authentication_required', $data->error);
  45. $this->_createExampleAccount();
  46. $response = $this->webmention(['token'=>'x','source'=>'http://source.example','target'=>'http://target.example']);
  47. $this->assertEquals(401, $response->getStatusCode());
  48. $data = json_decode($response->getContent());
  49. $this->assertEquals('invalid_token', $data->error);
  50. $response = $this->webmention(['token'=>'a']);
  51. $this->assertEquals(400, $response->getStatusCode());
  52. $data = json_decode($response->getContent());
  53. $this->assertEquals('missing_parameters', $data->error);
  54. }
  55. public function testMissingParameters() {
  56. $this->_createExampleAccount();
  57. $response = $this->webmention(['token'=>'a']);
  58. $this->assertEquals(400, $response->getStatusCode());
  59. $data = json_decode($response->getContent());
  60. $this->assertEquals('missing_parameters', $data->error);
  61. $response = $this->webmention(['token'=>'a','source'=>'foo']);
  62. $this->assertEquals(400, $response->getStatusCode());
  63. $data = json_decode($response->getContent());
  64. $this->assertEquals('missing_parameters', $data->error);
  65. $response = $this->webmention(['token'=>'a','target'=>'foo']);
  66. $this->assertEquals(400, $response->getStatusCode());
  67. $data = json_decode($response->getContent());
  68. $this->assertEquals('missing_parameters', $data->error);
  69. }
  70. public function testInvalidURLs() {
  71. $this->_createExampleAccount();
  72. $response = $this->webmention(['token'=>'a','source'=>'notaurl','target'=>'alsonotaurl']);
  73. $this->assertEquals(400, $response->getStatusCode());
  74. $data = json_decode($response->getContent());
  75. $this->assertEquals('invalid_parameter', $data->error);
  76. $response = $this->webmention(['token'=>'a','source'=>'http://source.example','target'=>'alsonotaurl']);
  77. $this->assertEquals(400, $response->getStatusCode());
  78. $data = json_decode($response->getContent());
  79. $this->assertEquals('invalid_parameter', $data->error);
  80. $response = $this->webmention(['token'=>'a','source'=>'notaurl','target'=>'http://target.example']);
  81. $this->assertEquals(400, $response->getStatusCode());
  82. $data = json_decode($response->getContent());
  83. $this->assertEquals('invalid_parameter', $data->error);
  84. }
  85. public function testNoLinkToSource() {
  86. $this->_createExampleAccount();
  87. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nolink','target'=>'http://target.example.com']);
  88. $this->assertEquals(400, $response->getStatusCode());
  89. $data = json_decode($response->getContent());
  90. $this->assertEquals('no_link_found', $data->error);
  91. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nothtml','target'=>'http://target.example.com']);
  92. $this->assertEquals(400, $response->getStatusCode());
  93. $data = json_decode($response->getContent());
  94. $this->assertEquals('no_link_found', $data->error);
  95. }
  96. public function testHandlesMalformedHTMLWithLink() {
  97. $this->_createExampleAccount();
  98. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/invalidhtml','target'=>'http://target.example.com']);
  99. $this->assertEquals(201, $response->getStatusCode());
  100. $data = json_decode($response->getContent());
  101. $this->assertEquals(false, property_exists($data, 'error'));
  102. }
  103. public function testQueuesWebmention() {
  104. $this->_createExampleAccount();
  105. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target'=>'http://target.example.com']);
  106. $this->assertEquals(201, $response->getStatusCode());
  107. $data = json_decode($response->getContent());
  108. $this->assertEquals(false, property_exists($data, 'error'));
  109. $this->assertEquals('queued', $data->status);
  110. $this->assertEquals(true, property_exists($data, 'location'));
  111. preg_match('/\/webmention\/(.+)/', $data->location, $match);
  112. $this->assertNotNull($match);
  113. # Verify it queued the mention in the database
  114. $d = ORM::for_table('webmentions')->where(['source' => 'http://source.example.com/basictest', 'target' => 'http://target.example.com'])->find_one();
  115. $this->assertNotNull($d);
  116. $this->assertEquals($match[1], $d->token);
  117. # Check the status endpoint to make sure it says it's still queued
  118. $response = $this->status($d->token);
  119. $this->assertEquals(200, $response->getStatusCode());
  120. $data = json_decode($response->getContent());
  121. $this->assertEquals('queued', $data->status);
  122. }
  123. public function testStatusNotFound() {
  124. $this->_createExampleAccount();
  125. $response = $this->status('foo');
  126. $this->assertEquals(404, $response->getStatusCode());
  127. $data = json_decode($response->getContent());
  128. $this->assertEquals('not_found', $data->status);
  129. }
  130. }