AnnounceTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Tests\Unit\ActivityPub\Verb;
  3. use App\Util\ActivityPub\Validator\Announce;
  4. use PHPUnit\Framework\Attributes\Test;
  5. use Tests\TestCase;
  6. class AnnounceTest extends TestCase
  7. {
  8. protected array $validAnnounce;
  9. protected array $invalidAnnounce;
  10. protected array $invalidDate;
  11. protected array $contextMissing;
  12. protected array $audienceMissing;
  13. protected array $audienceMissing2;
  14. protected array $invalidActor;
  15. protected array $invalidActor2;
  16. protected array $mastodonAnnounce;
  17. public function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->validAnnounce = [
  21. '@context' => 'https://www.w3.org/ns/activitystreams',
  22. 'id' => 'https://example.org/users/alice/statuses/100000000000001/activity',
  23. 'type' => 'Announce',
  24. 'actor' => 'https://example.org/users/alice',
  25. 'published' => '2018-12-31T23:59:59Z',
  26. 'to' => [
  27. 'https://www.w3.org/ns/activitystreams#Public'
  28. ],
  29. 'cc' => [
  30. 'https://example.org/users/bob',
  31. 'https://example.org/users/alice/followers'
  32. ],
  33. 'object' => 'https://example.org/p/bob/100000000000000',
  34. ];
  35. $this->invalidAnnounce = [
  36. '@context' => 'https://www.w3.org/ns/activitystreams',
  37. 'id' => 'https://example.org/users/alice/statuses/100000000000001/activity',
  38. 'type' => 'Announce2',
  39. 'actor' => 'https://example.org/users/alice',
  40. 'published' => '2018-12-31T23:59:59Z',
  41. 'to' => [
  42. 'https://www.w3.org/ns/activitystreams#Public'
  43. ],
  44. 'cc' => [
  45. 'https://example.org/users/bob',
  46. 'https://example.org/users/alice/followers'
  47. ],
  48. 'object' => 'https://example.org/p/bob/100000000000000',
  49. ];
  50. $this->contextMissing = [
  51. 'id' => 'https://example.org/users/alice/statuses/100000000000001/activity',
  52. 'type' => 'Announce',
  53. 'actor' => 'https://example.org/users/alice',
  54. 'published' => '2018-12-31T23:59:59Z',
  55. 'to' => [
  56. 'https://www.w3.org/ns/activitystreams#Public'
  57. ],
  58. 'cc' => [
  59. 'https://example.org/users/bob',
  60. 'https://example.org/users/alice/followers'
  61. ],
  62. 'object' => 'https://example.org/p/bob/100000000000000',
  63. ];
  64. $this->invalidActor = [
  65. '@context' => 'https://www.w3.org/ns/activitystreams',
  66. 'id' => 'https://example.org/users/alice/statuses/100000000000001/activity',
  67. 'type' => 'Announce',
  68. 'actor' => '10000',
  69. 'published' => '2018-12-31T23:59:59Z',
  70. 'to' => [
  71. 'https://www.w3.org/ns/activitystreams#Public'
  72. ],
  73. 'cc' => [
  74. 'https://example.org/users/bob',
  75. 'https://example.org/users/alice/followers'
  76. ],
  77. 'object' => 'https://example.org/p/bob/100000000000000',
  78. ];
  79. $this->invalidActor2 = [
  80. '@context' => 'https://www.w3.org/ns/activitystreams',
  81. 'id' => 'https://example.org/users/alice/statuses/100000000000001/activity',
  82. 'type' => 'Announce',
  83. 'published' => '2018-12-31T23:59:59Z',
  84. 'to' => [
  85. 'https://www.w3.org/ns/activitystreams#Public'
  86. ],
  87. 'cc' => [
  88. 'https://example.org/users/bob',
  89. 'https://example.org/users/alice/followers'
  90. ],
  91. 'object' => 'https://example.org/p/bob/100000000000000',
  92. ];
  93. $this->mastodonAnnounce = [
  94. 'type' => 'Announce',
  95. 'to' => [
  96. 'https://www.w3.org/ns/activitystreams#Public',
  97. ],
  98. 'signature' => [
  99. 'type' => 'RsaSignature2017',
  100. 'signatureValue' => 'T95DRE0eAligvMuRMkQA01lsoz2PKi4XXF+cyZ0BqbrO12p751TEWTyyRn5a+HH0e4kc77EUhQVXwMq80WAYDzHKVUTf2XBJPBa68vl0j6RXw3+HK4ef5hR4KWFNBU34yePS7S1fEmc1mTG4Yx926wtmZwDpEMTp1CXOeVEjCYzmdyHpepPPH2ZZettiacmPRSqBLPGWZoot7kH/SioIdnrMGY0I7b+rqkIdnnEcdhu9N1BKPEO9Sr+KmxgAUiidmNZlbBXX6gCxp8BiIdH4ABsIcwoDcGNkM5EmWunGW31LVjsEQXhH5c1Wly0ugYYPCg/0eHLNBOhKkY/teSM8Lg==',
  101. 'creator' => 'https://mastodon.example.org/users/admin#main-key',
  102. 'created' => '2018-02-17T19:39:15Z',
  103. ],
  104. 'published' => '2018-02-17T19:39:15Z',
  105. 'object' => 'https://mastodon.example.org/@admin/99541947525187367',
  106. 'id' => 'https://mastodon.example.org/users/admin/statuses/99542391527669785/activity',
  107. 'cc' => [
  108. 'https://mastodon.example.org/users/admin',
  109. 'https://mastodon.example.org/users/admin/followers',
  110. ],
  111. 'atomUri' => 'https://mastodon.example.org/users/admin/statuses/99542391527669785/activity',
  112. 'actor' => 'https://mastodon.example.org/users/admin',
  113. '@context' => [
  114. 'https://www.w3.org/ns/activitystreams',
  115. 'https://w3id.org/security/v1',
  116. [
  117. 'toot' => 'https://joinmastodon.org/ns#',
  118. 'sensitive' => 'as:sensitive',
  119. 'ostatus' => 'https://ostatus.org#',
  120. 'movedTo' => 'as:movedTo',
  121. 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
  122. 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri',
  123. 'conversation' => 'ostatus:conversation',
  124. 'atomUri' => 'ostatus:atomUri',
  125. 'Hashtag' => 'as:Hashtag',
  126. 'Emoji' => 'toot:Emoji',
  127. ],
  128. ],
  129. ];
  130. }
  131. #[Test]
  132. public function basic_accept()
  133. {
  134. $this->assertTrue(Announce::validate($this->validAnnounce));
  135. }
  136. #[Test]
  137. public function invalid_accept()
  138. {
  139. $this->assertFalse(Announce::validate($this->invalidAnnounce));
  140. }
  141. #[Test]
  142. public function context_missing()
  143. {
  144. $this->assertFalse(Announce::validate($this->contextMissing));
  145. }
  146. #[Test]
  147. public function invalid_actor()
  148. {
  149. $this->assertFalse(Announce::validate($this->invalidActor));
  150. $this->assertFalse(Announce::validate($this->invalidActor2));
  151. }
  152. #[Test]
  153. public function mastodon_announce()
  154. {
  155. $this->assertTrue(Announce::validate($this->mastodonAnnounce));
  156. }
  157. }