update_spec.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::Activity::Update do
  4. subject { described_class.new(json, sender) }
  5. let!(:sender) { Fabricate(:account, domain: 'example.com', inbox_url: 'https://example.com/foo/inbox', outbox_url: 'https://example.com/foo/outbox') }
  6. describe '#perform' do
  7. context 'with an Actor object' do
  8. let(:actor_json) do
  9. {
  10. '@context': [
  11. 'https://www.w3.org/ns/activitystreams',
  12. 'https://w3id.org/security/v1',
  13. {
  14. manuallyApprovesFollowers: 'as:manuallyApprovesFollowers',
  15. toot: 'http://joinmastodon.org/ns#',
  16. featured: { '@id': 'toot:featured', '@type': '@id' },
  17. featuredTags: { '@id': 'toot:featuredTags', '@type': '@id' },
  18. },
  19. ],
  20. id: sender.uri,
  21. type: 'Person',
  22. following: 'https://example.com/users/dfsdf/following',
  23. followers: 'https://example.com/users/dfsdf/followers',
  24. inbox: sender.inbox_url,
  25. outbox: sender.outbox_url,
  26. featured: 'https://example.com/users/dfsdf/featured',
  27. featuredTags: 'https://example.com/users/dfsdf/tags',
  28. preferredUsername: sender.username,
  29. name: 'Totally modified now',
  30. publicKey: {
  31. id: "#{sender.uri}#main-key",
  32. owner: sender.uri,
  33. publicKeyPem: sender.public_key,
  34. },
  35. }
  36. end
  37. let(:json) do
  38. {
  39. '@context': 'https://www.w3.org/ns/activitystreams',
  40. id: 'foo',
  41. type: 'Update',
  42. actor: sender.uri,
  43. object: actor_json,
  44. }.with_indifferent_access
  45. end
  46. before do
  47. stub_request(:get, actor_json[:outbox]).to_return(status: 404)
  48. stub_request(:get, actor_json[:followers]).to_return(status: 404)
  49. stub_request(:get, actor_json[:following]).to_return(status: 404)
  50. stub_request(:get, actor_json[:featured]).to_return(status: 404)
  51. stub_request(:get, actor_json[:featuredTags]).to_return(status: 404)
  52. subject.perform
  53. end
  54. it 'updates profile' do
  55. expect(sender.reload.display_name).to eq 'Totally modified now'
  56. end
  57. end
  58. context 'with a Question object' do
  59. let!(:at_time) { Time.now.utc }
  60. let!(:status) { Fabricate(:status, uri: 'https://example.com/statuses/poll', account: sender, poll: Poll.new(account: sender, options: %w(Bar Baz), cached_tallies: [0, 0], expires_at: at_time + 5.days)) }
  61. let(:json) do
  62. {
  63. '@context': 'https://www.w3.org/ns/activitystreams',
  64. id: 'foo',
  65. type: 'Update',
  66. actor: sender.uri,
  67. object: {
  68. type: 'Question',
  69. id: status.uri,
  70. content: 'Foo',
  71. endTime: (at_time + 5.days).iso8601,
  72. oneOf: [
  73. {
  74. type: 'Note',
  75. name: 'Bar',
  76. replies: {
  77. type: 'Collection',
  78. totalItems: 0,
  79. },
  80. },
  81. {
  82. type: 'Note',
  83. name: 'Baz',
  84. replies: {
  85. type: 'Collection',
  86. totalItems: 12,
  87. },
  88. },
  89. ],
  90. },
  91. }.with_indifferent_access
  92. end
  93. before do
  94. status.update!(uri: ActivityPub::TagManager.instance.uri_for(status))
  95. subject.perform
  96. end
  97. it 'updates poll numbers' do
  98. expect(status.preloadable_poll.cached_tallies).to eq [0, 12]
  99. end
  100. it 'does not set status as edited' do
  101. expect(status.edited_at).to be_nil
  102. end
  103. end
  104. context 'with a Note object' do
  105. let(:updated) { nil }
  106. let(:favourites) { 50 }
  107. let(:reblogs) { 100 }
  108. let!(:status) { Fabricate(:status, uri: 'https://example.com/statuses/poll', account: sender) }
  109. let(:json) do
  110. {
  111. '@context': 'https://www.w3.org/ns/activitystreams',
  112. id: 'foo',
  113. type: 'Update',
  114. actor: sender.uri,
  115. object: {
  116. type: 'Note',
  117. id: status.uri,
  118. content: 'Foo',
  119. updated: updated,
  120. likes: {
  121. id: "#{status.uri}/likes",
  122. type: 'Collection',
  123. totalItems: favourites,
  124. },
  125. shares: {
  126. id: "#{status.uri}/shares",
  127. type: 'Collection',
  128. totalItems: reblogs,
  129. },
  130. },
  131. }.with_indifferent_access
  132. end
  133. shared_examples 'updates counts' do
  134. it 'updates the reblog count' do
  135. expect(status.untrusted_reblogs_count).to eq reblogs
  136. end
  137. it 'updates the favourites count' do
  138. expect(status.untrusted_favourites_count).to eq favourites
  139. end
  140. end
  141. context 'with an implicit update' do
  142. before do
  143. status.update!(uri: ActivityPub::TagManager.instance.uri_for(status))
  144. subject.perform
  145. end
  146. it_behaves_like 'updates counts'
  147. end
  148. context 'with an explicit update' do
  149. let(:favourites) { 150 }
  150. let(:reblogs) { 200 }
  151. let(:updated) { Time.now.utc.iso8601 }
  152. before do
  153. status.update!(uri: ActivityPub::TagManager.instance.uri_for(status))
  154. subject.perform
  155. end
  156. it_behaves_like 'updates counts'
  157. end
  158. end
  159. end
  160. end