atompub_test.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - the distributed open-source microblogging tool
  5. * Copyright (C) 2010, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. if (!defined('INSTALLDIR')) {
  21. define('INSTALLDIR', dirname(dirname(__DIR__)));
  22. }
  23. $shortoptions = 'n:p:';
  24. $longoptions = ['nickname=', 'password=', 'dry-run'];
  25. $helptext = <<<END_OF_HELP
  26. USAGE: atompub_test.php [options]
  27. Runs some tests on the AtomPub interface for the site. You must provide
  28. a user account to authenticate as; it will be used to make some test
  29. posts on the site.
  30. Options:
  31. -n<user> --nickname=<user> Nickname of account to post as
  32. -p<pass> --password=<pass> Password for account
  33. --dry-run Skip tests that modify the site (post, delete)
  34. END_OF_HELP;
  35. require_once INSTALLDIR . '/scripts/commandline.inc';
  36. $user = get_option_value('n', 'nickname');
  37. $pass = get_option_value('p', 'password');
  38. if (!$user) {
  39. die("Must set a user: --nickname=<username>\n");
  40. }
  41. if (!$pass) {
  42. die("Must set a password: --password=<username>\n");
  43. }
  44. // discover the feed...
  45. // @fixme will this actually work?
  46. $url = common_local_url('ApiTimelineUser', ['format' => 'atom', 'id' => $user]);
  47. echo "Collection URL is: {$url}\n";
  48. $collection = new AtomPubClient($url, $user, $pass);
  49. // confirm the feed has edit links ..... ?
  50. echo 'Posting an empty message (should fail)... ';
  51. try {
  52. $noticeUrl = $collection->post('');
  53. die("FAILED, succeeded!\n");
  54. } catch (Exception $e) {
  55. echo "ok\n";
  56. }
  57. echo 'Posting an invalid XML message (should fail)... ';
  58. try {
  59. $noticeUrl = $collection->post('<feed<entry>barf</yomomma>');
  60. die("FAILED, succeeded!\n");
  61. } catch (Exception $e) {
  62. echo "ok\n";
  63. }
  64. echo 'Posting a valid XML but non-Atom message (should fail)... ';
  65. try {
  66. $noticeUrl = $collection->post('<feed xmlns="http://notatom.com"><id>arf</id><entry><id>barf</id></entry></feed>');
  67. die("FAILED, succeeded!\n");
  68. } catch (Exception $e) {
  69. echo "ok\n";
  70. }
  71. // post!
  72. $rand = mt_rand(0, 99999);
  73. $atom = <<<END_ATOM
  74. <entry xmlns="http://www.w3.org/2005/Atom">
  75. <title>This is an AtomPub test post title ({$rand})</title>
  76. <content>This is an AtomPub test post content ({$rand})</content>
  77. </entry>
  78. END_ATOM;
  79. echo 'Posting a new message... ';
  80. $noticeUrl = $collection->post($atom);
  81. echo "ok, got {$noticeUrl}\n";
  82. echo 'Fetching the new notice... ';
  83. $notice = new AtomPubClient($noticeUrl, $user, $pass);
  84. $body = $notice->get();
  85. AtomPubClient::validateAtomEntry($body);
  86. echo "ok\n";
  87. echo 'Getting the notice ID URI... ';
  88. $noticeUri = AtomPubClient::entryId($body);
  89. echo "ok: {$noticeUri}\n";
  90. echo 'Confirming new entry points to itself right... ';
  91. $editUrl = AtomPubClient::entryEditURL($body);
  92. if ($editUrl != $noticeUrl) {
  93. die("Entry lists edit URL as {$editUrl}, no match!\n");
  94. }
  95. echo "OK\n";
  96. echo 'Refetching the collection... ';
  97. $feed = $collection->get();
  98. echo "ok\n";
  99. echo 'Confirming new entry is in the feed... ';
  100. $entry = AtomPubClient::getEntryInFeed($feed, $noticeUri);
  101. if (!$entry) {
  102. die("missing!\n");
  103. }
  104. // edit URL should match
  105. echo "ok\n";
  106. echo 'Editing notice (should fail)... ';
  107. try {
  108. $notice->put($target, $atom2);
  109. die("ERROR: editing a notice should have failed.\n");
  110. } catch (Exception $e) {
  111. echo "ok (failed as expected)\n";
  112. }
  113. echo 'Deleting notice... ';
  114. $notice->delete();
  115. echo "ok\n";
  116. echo "Refetching deleted notice to confirm it's gone... ";
  117. try {
  118. $body = $notice->get();
  119. var_dump($body);
  120. die("ERROR: notice should be gone now.\n");
  121. } catch (Exception $e) {
  122. echo "ok\n";
  123. }
  124. echo 'Refetching the collection.. ';
  125. $feed = $collection->get();
  126. echo "ok\n";
  127. echo 'Confirming deleted notice is no longer in the feed... ';
  128. $entry = AtomPubClient::getEntryInFeed($feed, $noticeUri);
  129. if ($entry) {
  130. die("still there!\n");
  131. }
  132. echo "ok\n";
  133. // make subscriptions
  134. // make some posts
  135. // make sure the posts go through or not depending on the subs
  136. // remove subscriptions
  137. // test that they don't go through now
  138. // group memberships too
  139. // make sure we can't post to someone else's feed!
  140. // make sure we can't delete someone else's messages
  141. // make sure we can't create/delete someone else's subscriptions
  142. // make sure we can't create/delete someone else's group memberships