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.

159 lines
6.2 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. foreach ([['token'=>'a','source'=>'notaurl','target'=>'alsonotaurl'],
  73. ['token'=>'a','source'=>'http://source.example','target'=>'alsonotaurl'],
  74. ['token'=>'a','source'=>'notaurl','target'=>'http://target.example'],
  75. ['token'=>'a','source'=>'http://source.example','target'=>'http://target.example','callback'=>'notaurl']
  76. ] as $params) {
  77. $response = $this->webmention($params);
  78. $this->assertEquals(400, $response->getStatusCode());
  79. $data = json_decode($response->getContent());
  80. $this->assertEquals('invalid_parameter', $data->error);
  81. }
  82. }
  83. public function testNoLinkToSource() {
  84. $this->_createExampleAccount();
  85. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nolink','target'=>'http://target.example.com']);
  86. $this->assertEquals(400, $response->getStatusCode());
  87. $data = json_decode($response->getContent());
  88. $this->assertEquals('no_link_found', $data->error);
  89. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nothtml','target'=>'http://target.example.com']);
  90. $this->assertEquals(400, $response->getStatusCode());
  91. $data = json_decode($response->getContent());
  92. $this->assertEquals('no_link_found', $data->error);
  93. }
  94. public function testHandlesMalformedHTMLWithLink() {
  95. $this->_createExampleAccount();
  96. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/invalidhtml','target'=>'http://target.example.com']);
  97. $this->assertEquals(201, $response->getStatusCode());
  98. $data = json_decode($response->getContent());
  99. $this->assertEquals(false, property_exists($data, 'error'));
  100. }
  101. public function testQueuesWebmention() {
  102. $this->_createExampleAccount();
  103. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target'=>'http://target.example.com']);
  104. $this->assertEquals(201, $response->getStatusCode());
  105. $data = json_decode($response->getContent());
  106. $this->assertEquals(false, property_exists($data, 'error'));
  107. $this->assertEquals('queued', $data->status);
  108. $this->assertEquals(true, property_exists($data, 'location'));
  109. preg_match('/\/webmention\/(.+)/', $data->location, $match);
  110. $this->assertNotNull($match);
  111. # Verify it queued the mention in the database
  112. $d = ORM::for_table('webmentions')->where(['source' => 'http://source.example.com/basictest', 'target' => 'http://target.example.com'])->find_one();
  113. $this->assertNotNull($d);
  114. $this->assertEquals($match[1], $d->token);
  115. # Check the status endpoint to make sure it says it's still queued
  116. $response = $this->status($d->token);
  117. $this->assertEquals(200, $response->getStatusCode());
  118. $data = json_decode($response->getContent());
  119. $this->assertEquals('queued', $data->status);
  120. }
  121. public function testStatusNotFound() {
  122. $this->_createExampleAccount();
  123. $response = $this->status('foo');
  124. $this->assertEquals(404, $response->getStatusCode());
  125. $data = json_decode($response->getContent());
  126. $this->assertEquals('not_found', $data->status);
  127. }
  128. }