ActivitypubProfileTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * ActivityPub implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. * @link http://www.gnu.org/software/social/
  24. */
  25. namespace Tests\Unit;
  26. use Tests\TestCase;
  27. class ProfileObjectTest extends TestCase
  28. {
  29. public function testLibraryInstalled()
  30. {
  31. $this->assertTrue(class_exists('\Activitypub_profile'));
  32. }
  33. public function testActivitypubProfile()
  34. {
  35. // Mimic proper ACCEPT header
  36. $_SERVER['HTTP_ACCEPT'] = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams';
  37. /* Test do_insert() */
  38. $aprofile = new \Activitypub_profile();
  39. $aprofile->uri = 'https://testinstance.net/index.php/user/1';
  40. $aprofile->nickname = 'test1';
  41. $aprofile->fullname = 'Test User 1';
  42. $aprofile->bio = 'I am a nice test 1 guy';
  43. $aprofile->inboxuri = "https://testinstance.net/index.php/user/1/inbox.json";
  44. $aprofile->sharedInboxuri = "https://testinstance.net/inbox.json";
  45. $aprofile->do_insert();
  46. /* Test local_profile() */
  47. $profile = $aprofile->local_profile();
  48. /* Test from_profile() and create_from_local_profile() */
  49. $this->assertTrue($this->compare_aprofiles($aprofile, \Activitypub_profile::from_profile($profile)));
  50. /* Create Keys for Test User 1 */
  51. $apRSA = new \Activitypub_rsa();
  52. $apRSA->profile_id = $profile->getID();
  53. \Activitypub_rsa::generate_keys($apRSA->private_key, $apRSA->public_key);
  54. $apRSA->store_keys();
  55. /* Test profile_to_array() */
  56. // Fetch ActivityPub Actor Object representation
  57. $profile_array = \Activitypub_profile::profile_to_array($profile);
  58. // Check type
  59. $this->assertTrue(is_array($profile_array));
  60. // Test with Explorer's Profile Tester
  61. $this->assertTrue(\Activitypub_explorer::validate_remote_response($profile_array));
  62. /* Test get_inbox() */
  63. $this->assertTrue($aprofile->sharedInboxuri == $aprofile->get_inbox());
  64. /* Test getUri() */
  65. $this->assertTrue($aprofile->uri == $aprofile->getUri());
  66. /* Test getUrl() */
  67. $this->assertTrue($profile->getUrl() == $aprofile->getUrl());
  68. /* Test getID() */
  69. $this->assertTrue($profile->getID() == $aprofile->getID());
  70. /* Test fromUri() */
  71. $this->assertTrue($this->compare_aprofiles($aprofile, \Activitypub_profile::fromUri($aprofile->uri)));
  72. /* Remove Remote User Test 1 */
  73. $old_id = $profile->getID();
  74. $apRSA->delete();
  75. $aprofile->delete();
  76. $profile->delete();
  77. // Check if successfuly removed
  78. try {
  79. \Profile::getById($old_id);
  80. $this->assertTrue(false);
  81. } catch (\NoResultException $e) {
  82. $this->assertTrue(true);
  83. }
  84. /* Test ensure_web_finger() */
  85. // TODO: Maybe elaborate on this function's tests
  86. try {
  87. \Activitypub_profile::ensure_webfinger('test1@testinstance.net');
  88. $this->assertTrue(false);
  89. } catch (\Exception $e) {
  90. $this->assertTrue($e->getMessage() == 'Not a valid webfinger address.' ||
  91. $e->getMessage() == 'Not a valid webfinger address (via cache).');
  92. }
  93. }
  94. // Helpers
  95. private function compare_profiles(\Profile $a, \Profile $b)
  96. {
  97. if (($av = $a->getID()) != ($bv = $b->getID())) {
  98. throw new Exception('Compare Profiles 1 Fail: $a: '.$av.' is different from $b: '.$bv);
  99. }
  100. if (($av = $a->getNickname()) != ($bv = $b->getNickname())) {
  101. throw new Exception('Compare Profiles 2 Fail: $a: '.$av.' is different from $b: '.$bv);
  102. }
  103. if (($av = $a->getFullname()) != ($bv = $b->getFullname())) {
  104. throw new Exception('Compare Profiles 3 Fail: $a: '.$av.' is different from $b: '.$bv);
  105. }
  106. if (($av = $a->getUrl()) != ($bv = $b->getUrl())) {
  107. throw new Exception('Compare Profiles 4 Fail: $a: '.$av.' is different from $b: '.$bv);
  108. }
  109. if (($av = $a->getDescription()) != ($bv = $b->getDescription())) {
  110. throw new Exception('Compare Profiles 5 Fail: $a: '.$av.' is different from $b: '.$bv);
  111. }
  112. if (($av = $a->getLocation()) != ($bv = $b->getLocation())) {
  113. throw new Exception('Compare Profiles 6 Fail: $a: '.$av.' is different from $b: '.$bv);
  114. }
  115. if (($av = $a->getNickname()) != ($bv = $b->getNickname())) {
  116. throw new Exception('Compare Profiles 7 Fail: $a: '.$av.' is different from $b: '.$bv);
  117. }
  118. if (($av = $a->lat) != ($bv = $b->lat)) {
  119. throw new Exception('Compare Profiles 8 Fail: $a: '.$av.' is different from $b: '.$bv);
  120. }
  121. if (($av = $a->lon) != ($bv = $b->lon)) {
  122. throw new Exception('Compare Profiles 9 Fail: $a: '.$av.' is different from $b: '.$bv);
  123. }
  124. return true;
  125. }
  126. private function compare_aprofiles(\Activitypub_profile $a, \Activitypub_profile $b)
  127. {
  128. if (($av = $a->getUri()) != ($bv = $b->getUri())) {
  129. throw new Exception('Compare AProfiles 1 Fail: $a: '.$av.' is different from $b: '.$bv);
  130. }
  131. if (($av = $a->getUrl()) != ($bv = $b->getUrl())) {
  132. throw new Exception('Compare AProfiles 2 Fail: $a: '.$av.' is different from $b: '.$bv);
  133. }
  134. if (($av = $a->getID()) != ($bv = $b->getID())) {
  135. throw new Exception('Compare AProfiles 3 Fail: $a: '.$av.' is different from $b: '.$bv);
  136. }
  137. if (($av = $a->profile_id) != ($bv = $b->profile_id)) {
  138. throw new Exception('Compare AProfiles 4 Fail: $a: '.$av.' is different from $b: '.$bv);
  139. }
  140. if (($av = $a->inboxuri) != ($bv = $b->inboxuri)) {
  141. throw new Exception('Compare AProfiles 5 Fail: $a: '.$av.' is different from $b: '.$bv);
  142. }
  143. if (($av = $a->sharedInboxuri) != ($bv = $b->sharedInboxuri)) {
  144. throw new Exception('Compare AProfiles 6 Fail: $a: '.$av.' is different from $b: '.$bv);
  145. }
  146. return true;
  147. }
  148. }