atompub_test.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
  21. $shortoptions = 'n:p:';
  22. $longoptions = array('nickname=', 'password=', 'dry-run');
  23. $helptext = <<<END_OF_HELP
  24. USAGE: atompub_test.php [options]
  25. Runs some tests on the AtomPub interface for the site. You must provide
  26. a user account to authenticate as; it will be used to make some test
  27. posts on the site.
  28. Options:
  29. -n<user> --nickname=<user> Nickname of account to post as
  30. -p<pass> --password=<pass> Password for account
  31. --dry-run Skip tests that modify the site (post, delete)
  32. END_OF_HELP;
  33. require_once INSTALLDIR.'/scripts/commandline.inc';
  34. $user = get_option_value('n', 'nickname');
  35. $pass = get_option_value('p', 'password');
  36. if (!$user) {
  37. die("Must set a user: --nickname=<username>\n");
  38. }
  39. if (!$pass) {
  40. die("Must set a password: --password=<username>\n");
  41. }
  42. // discover the feed...
  43. // @fixme will this actually work?
  44. $url = common_local_url('ApiTimelineUser', array('format' => 'atom', 'id' => $user));
  45. echo "Collection URL is: $url\n";
  46. $collection = new AtomPubClient($url, $user, $pass);
  47. // confirm the feed has edit links ..... ?
  48. echo "Posting an empty message (should fail)... ";
  49. try {
  50. $noticeUrl = $collection->post('');
  51. die("FAILED, succeeded!\n");
  52. } catch (Exception $e) {
  53. echo "ok\n";
  54. }
  55. echo "Posting an invalid XML message (should fail)... ";
  56. try {
  57. $noticeUrl = $collection->post('<feed<entry>barf</yomomma>');
  58. die("FAILED, succeeded!\n");
  59. } catch (Exception $e) {
  60. echo "ok\n";
  61. }
  62. echo "Posting a valid XML but non-Atom message (should fail)... ";
  63. try {
  64. $noticeUrl = $collection->post('<feed xmlns="http://notatom.com"><id>arf</id><entry><id>barf</id></entry></feed>');
  65. die("FAILED, succeeded!\n");
  66. } catch (Exception $e) {
  67. echo "ok\n";
  68. }
  69. // post!
  70. $rand = mt_rand(0, 99999);
  71. $atom = <<<END_ATOM
  72. <entry xmlns="http://www.w3.org/2005/Atom">
  73. <title>This is an AtomPub test post title ($rand)</title>
  74. <content>This is an AtomPub test post content ($rand)</content>
  75. </entry>
  76. END_ATOM;
  77. echo "Posting a new message... ";
  78. $noticeUrl = $collection->post($atom);
  79. echo "ok, got $noticeUrl\n";
  80. echo "Fetching the new notice... ";
  81. $notice = new AtomPubClient($noticeUrl, $user, $pass);
  82. $body = $notice->get();
  83. AtomPubClient::validateAtomEntry($body);
  84. echo "ok\n";
  85. echo "Getting the notice ID URI... ";
  86. $noticeUri = AtomPubClient::entryId($body);
  87. echo "ok: $noticeUri\n";
  88. echo "Confirming new entry points to itself right... ";
  89. $editUrl = AtomPubClient::entryEditURL($body);
  90. if ($editUrl != $noticeUrl) {
  91. die("Entry lists edit URL as $editUrl, no match!\n");
  92. }
  93. echo "OK\n";
  94. echo "Refetching the collection... ";
  95. $feed = $collection->get();
  96. echo "ok\n";
  97. echo "Confirming new entry is in the feed... ";
  98. $entry = AtomPubClient::getEntryInFeed($feed, $noticeUri);
  99. if (!$entry) {
  100. die("missing!\n");
  101. }
  102. // edit URL should match
  103. echo "ok\n";
  104. echo "Editing notice (should fail)... ";
  105. try {
  106. $notice->put($target, $atom2);
  107. die("ERROR: editing a notice should have failed.\n");
  108. } catch (Exception $e) {
  109. echo "ok (failed as expected)\n";
  110. }
  111. echo "Deleting notice... ";
  112. $notice->delete();
  113. echo "ok\n";
  114. echo "Refetching deleted notice to confirm it's gone... ";
  115. try {
  116. $body = $notice->get();
  117. var_dump($body);
  118. die("ERROR: notice should be gone now.\n");
  119. } catch (Exception $e) {
  120. echo "ok\n";
  121. }
  122. echo "Refetching the collection.. ";
  123. $feed = $collection->get();
  124. echo "ok\n";
  125. echo "Confirming deleted notice is no longer in the feed... ";
  126. $entry = AtomPubClient::getEntryInFeed($feed, $noticeUri);
  127. if ($entry) {
  128. die("still there!\n");
  129. }
  130. echo "ok\n";
  131. // make subscriptions
  132. // make some posts
  133. // make sure the posts go through or not depending on the subs
  134. // remove subscriptions
  135. // test that they don't go through now
  136. // group memberships too
  137. // make sure we can't post to someone else's feed!
  138. // make sure we can't delete someone else's messages
  139. // make sure we can't create/delete someone else's subscriptions
  140. // make sure we can't create/delete someone else's group memberships