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.

142 lines
5.7 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 _createExampleAccount() {
  21. $user = ORM::for_table('users')->create();
  22. $user->url = 'http://example.com';
  23. $user->save();
  24. $site = ORM::for_table('sites')->create();
  25. $site->name = 'Example';
  26. $site->created_by = $user->id();
  27. $site->save();
  28. $role = ORM::for_table('roles')->create();
  29. $role->site_id = $site->id();
  30. $role->user_id = $user->id();
  31. $role->role = 'owner';
  32. $role->token = 'a';
  33. $role->save();
  34. }
  35. public function testAuthentication() {
  36. $response = $this->webmention([]);
  37. $this->assertEquals(401, $response->getStatusCode());
  38. $data = json_decode($response->getContent());
  39. $this->assertEquals('authentication_required', $data->error);
  40. $this->_createExampleAccount();
  41. $response = $this->webmention(['token'=>'x','source'=>'http://source.example','target'=>'http://target.example']);
  42. $this->assertEquals(401, $response->getStatusCode());
  43. $data = json_decode($response->getContent());
  44. $this->assertEquals('invalid_token', $data->error);
  45. $response = $this->webmention(['token'=>'a']);
  46. $this->assertEquals(400, $response->getStatusCode());
  47. $data = json_decode($response->getContent());
  48. $this->assertEquals('missing_parameters', $data->error);
  49. }
  50. public function testMissingParameters() {
  51. $this->_createExampleAccount();
  52. $response = $this->webmention(['token'=>'a']);
  53. $this->assertEquals(400, $response->getStatusCode());
  54. $data = json_decode($response->getContent());
  55. $this->assertEquals('missing_parameters', $data->error);
  56. $response = $this->webmention(['token'=>'a','source'=>'foo']);
  57. $this->assertEquals(400, $response->getStatusCode());
  58. $data = json_decode($response->getContent());
  59. $this->assertEquals('missing_parameters', $data->error);
  60. $response = $this->webmention(['token'=>'a','target'=>'foo']);
  61. $this->assertEquals(400, $response->getStatusCode());
  62. $data = json_decode($response->getContent());
  63. $this->assertEquals('missing_parameters', $data->error);
  64. }
  65. public function testInvalidURLs() {
  66. $this->_createExampleAccount();
  67. $response = $this->webmention(['token'=>'a','source'=>'notaurl','target'=>'alsonotaurl']);
  68. $this->assertEquals(400, $response->getStatusCode());
  69. $data = json_decode($response->getContent());
  70. $this->assertEquals('invalid_parameter', $data->error);
  71. $response = $this->webmention(['token'=>'a','source'=>'http://source.example','target'=>'alsonotaurl']);
  72. $this->assertEquals(400, $response->getStatusCode());
  73. $data = json_decode($response->getContent());
  74. $this->assertEquals('invalid_parameter', $data->error);
  75. $response = $this->webmention(['token'=>'a','source'=>'notaurl','target'=>'http://target.example']);
  76. $this->assertEquals(400, $response->getStatusCode());
  77. $data = json_decode($response->getContent());
  78. $this->assertEquals('invalid_parameter', $data->error);
  79. }
  80. public function testNoLinkToSource() {
  81. $this->_createExampleAccount();
  82. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nolink','target'=>'http://target.example.com']);
  83. $this->assertEquals(400, $response->getStatusCode());
  84. $data = json_decode($response->getContent());
  85. $this->assertEquals('no_link_found', $data->error);
  86. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/nothtml','target'=>'http://target.example.com']);
  87. $this->assertEquals(400, $response->getStatusCode());
  88. $data = json_decode($response->getContent());
  89. $this->assertEquals('no_link_found', $data->error);
  90. }
  91. public function testHandlesMalformedHTMLWithLink() {
  92. $this->_createExampleAccount();
  93. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/invalidhtml','target'=>'http://target.example.com']);
  94. $this->assertEquals(201, $response->getStatusCode());
  95. $data = json_decode($response->getContent());
  96. $this->assertEquals(false, property_exists($data, 'error'));
  97. }
  98. public function testQueuesWebmention() {
  99. $this->_createExampleAccount();
  100. $response = $this->webmention(['token'=>'a','source'=>'http://source.example.com/basictest','target'=>'http://target.example.com']);
  101. $this->assertEquals(201, $response->getStatusCode());
  102. $data = json_decode($response->getContent());
  103. $this->assertEquals(false, property_exists($data, 'error'));
  104. $this->assertEquals('queued', $data->result);
  105. $this->assertEquals(true, property_exists($data, 'status'));
  106. preg_match('/\/webmention\/(.+)/', $data->status, $match);
  107. $this->assertNotNull($match);
  108. # Verify it queued the mention in the database
  109. $d = ORM::for_table('webmentions')->where(['source' => 'http://source.example.com/basictest', 'target' => 'http://target.example.com'])->find_one();
  110. $this->assertNotNull($d);
  111. $this->assertEquals($match[1], $d->token);
  112. }
  113. }