Skip to content

Commit e4e07b1

Browse files
authored
Reduce factory usage across spec/services area (mastodon#32098)
1 parent 4fe7f21 commit e4e07b1

21 files changed

+545
-619
lines changed

spec/services/account_statuses_cleanup_service_spec.rb

+41-40
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,35 @@
2727
end
2828

2929
context 'when given a normal budget of 10' do
30-
it 'reports 3 deleted statuses' do
31-
expect(subject.call(account_policy, 10)).to eq 3
32-
end
33-
34-
it 'records the last deleted id' do
35-
subject.call(account_policy, 10)
36-
expect(account_policy.last_inspected).to eq [old_status.id, another_old_status.id].max
37-
end
38-
39-
it 'actually deletes the statuses' do
40-
subject.call(account_policy, 10)
41-
expect(Status.find_by(id: [very_old_status.id, old_status.id, another_old_status.id])).to be_nil
42-
expect { recent_status.reload }.to_not raise_error
43-
end
44-
45-
it 'preserves recent and unrelated statuses' do
46-
subject.call(account_policy, 10)
47-
expect { unrelated_status.reload }.to_not raise_error
48-
expect { recent_status.reload }.to_not raise_error
30+
it 'reports 3 deleted statuses and records last deleted id, deletes statuses, preserves recent unrelated statuses' do
31+
expect(subject.call(account_policy, 10))
32+
.to eq(3)
33+
34+
expect(account_policy.last_inspected)
35+
.to eq [old_status.id, another_old_status.id].max
36+
37+
expect(Status.find_by(id: [very_old_status.id, old_status.id, another_old_status.id]))
38+
.to be_nil
39+
expect { recent_status.reload }
40+
.to_not raise_error
41+
expect { unrelated_status.reload }
42+
.to_not raise_error
43+
expect { recent_status.reload }
44+
.to_not raise_error
4945
end
5046
end
5147

5248
context 'when called repeatedly with a budget of 2' do
53-
it 'reports 2 then 1 deleted statuses' do
54-
expect(subject.call(account_policy, 2)).to eq 2
55-
expect(subject.call(account_policy, 2)).to eq 1
56-
end
57-
58-
it 'actually deletes the statuses in the expected order' do
59-
subject.call(account_policy, 2)
60-
expect(Status.find_by(id: very_old_status.id)).to be_nil
61-
subject.call(account_policy, 2)
62-
expect(Status.find_by(id: [very_old_status.id, old_status.id, another_old_status.id])).to be_nil
49+
it 'reports 2 then 1 deleted statuses and deletes in expected order' do
50+
expect(subject.call(account_policy, 2))
51+
.to eq(2)
52+
expect(Status.find_by(id: very_old_status.id))
53+
.to be_nil
54+
55+
expect(subject.call(account_policy, 2))
56+
.to eq(1)
57+
expect(Status.find_by(id: [very_old_status.id, old_status.id, another_old_status.id]))
58+
.to be_nil
6359
end
6460
end
6561

@@ -90,19 +86,24 @@
9086
end
9187
end
9288

93-
it 'reports 0 deleted statuses then 0 then 3 then 0 again' do
94-
expect(subject.call(account_policy, 10)).to eq 0
95-
expect(subject.call(account_policy, 10)).to eq 0
96-
expect(subject.call(account_policy, 10)).to eq 3
97-
expect(subject.call(account_policy, 10)).to eq 0
89+
it 'reports 0 deleted statuses then 0 then 3 then 0 again, and keeps id under oldest deletable record' do
90+
expect(subject.call(account_policy, 10))
91+
.to eq(0)
92+
expect(subject.call(account_policy, 10))
93+
.to eq(0)
94+
expect(subject.call(account_policy, 10))
95+
.to eq(3)
96+
expect(subject.call(account_policy, 10))
97+
.to eq(0)
98+
expect(account_policy.last_inspected)
99+
.to be < oldest_deletable_record_id
98100
end
99101

100-
it 'never causes the recorded id to get higher than oldest deletable toot' do
101-
subject.call(account_policy, 10)
102-
subject.call(account_policy, 10)
103-
subject.call(account_policy, 10)
104-
subject.call(account_policy, 10)
105-
expect(account_policy.last_inspected).to be < Mastodon::Snowflake.id_at(account_policy.min_status_age.seconds.ago, with_random: false)
102+
def oldest_deletable_record_id
103+
Mastodon::Snowflake.id_at(
104+
account_policy.min_status_age.seconds.ago,
105+
with_random: false
106+
)
106107
end
107108
end
108109
end

spec/services/activitypub/process_status_update_service_spec.rb

+33-37
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
require 'rails_helper'
44

5-
def poll_option_json(name, votes)
6-
{ type: 'Note', name: name, replies: { type: 'Collection', totalItems: votes } }
7-
end
8-
95
RSpec.describe ActivityPub::ProcessStatusUpdateService do
106
subject { described_class.new }
117

@@ -294,7 +290,6 @@ def poll_option_json(name, votes)
294290
context 'when originally without media attachments' do
295291
before do
296292
stub_request(:get, 'https://example.com/foo.png').to_return(body: attachment_fixture('emojo.png'))
297-
subject.call(status, json, json)
298293
end
299294

300295
let(:payload) do
@@ -310,19 +305,18 @@ def poll_option_json(name, votes)
310305
}
311306
end
312307

313-
it 'updates media attachments' do
314-
media_attachment = status.reload.ordered_media_attachments.first
308+
it 'updates media attachments, fetches attachment, records media change in edit' do
309+
subject.call(status, json, json)
315310

316-
expect(media_attachment).to_not be_nil
317-
expect(media_attachment.remote_url).to eq 'https://example.com/foo.png'
318-
end
311+
expect(status.reload.ordered_media_attachments.first)
312+
.to be_present
313+
.and(have_attributes(remote_url: 'https://example.com/foo.png'))
319314

320-
it 'fetches the attachment' do
321-
expect(a_request(:get, 'https://example.com/foo.png')).to have_been_made
322-
end
315+
expect(a_request(:get, 'https://example.com/foo.png'))
316+
.to have_been_made
323317

324-
it 'records media change in edit' do
325-
expect(status.edits.reload.last.ordered_media_attachment_ids).to_not be_empty
318+
expect(status.edits.reload.last.ordered_media_attachment_ids)
319+
.to_not be_empty
326320
end
327321
end
328322

@@ -344,38 +338,38 @@ def poll_option_json(name, votes)
344338

345339
before do
346340
allow(RedownloadMediaWorker).to receive(:perform_async)
347-
subject.call(status, json, json)
348341
end
349342

350-
it 'updates the existing media attachment in-place' do
351-
media_attachment = status.media_attachments.ordered.reload.first
343+
it 'updates the existing media attachment in-place, does not queue redownload, updates media, records media change' do
344+
subject.call(status, json, json)
352345

353-
expect(media_attachment).to_not be_nil
354-
expect(media_attachment.remote_url).to eq 'https://example.com/foo.png'
355-
expect(media_attachment.description).to eq 'A picture'
356-
end
346+
expect(status.media_attachments.ordered.reload.first)
347+
.to be_present
348+
.and have_attributes(
349+
remote_url: 'https://example.com/foo.png',
350+
description: 'A picture'
351+
)
357352

358-
it 'does not queue redownload for the existing media attachment' do
359-
expect(RedownloadMediaWorker).to_not have_received(:perform_async)
360-
end
353+
expect(RedownloadMediaWorker)
354+
.to_not have_received(:perform_async)
361355

362-
it 'updates media attachments' do
363-
expect(status.ordered_media_attachments.map(&:remote_url)).to eq %w(https://example.com/foo.png)
364-
end
356+
expect(status.ordered_media_attachments.map(&:remote_url))
357+
.to eq %w(https://example.com/foo.png)
365358

366-
it 'records media change in edit' do
367-
expect(status.edits.reload.last.ordered_media_attachment_ids).to_not be_empty
359+
expect(status.edits.reload.last.ordered_media_attachment_ids)
360+
.to_not be_empty
368361
end
369362
end
370363

371364
context 'when originally with a poll' do
372365
before do
373366
poll = Fabricate(:poll, status: status)
374367
status.update(preloadable_poll: poll)
375-
subject.call(status, json, json)
376368
end
377369

378370
it 'removes poll and records media change in edit' do
371+
subject.call(status, json, json)
372+
379373
expect(status.reload.poll).to be_nil
380374
expect(status.edits.reload.last.poll_options).to be_nil
381375
end
@@ -398,15 +392,13 @@ def poll_option_json(name, votes)
398392
}
399393
end
400394

401-
before do
395+
it 'creates a poll and records media change in edit' do
402396
subject.call(status, json, json)
403-
end
404397

405-
it 'creates a poll and records media change in edit' do
406-
poll = status.reload.poll
398+
expect(status.reload.poll)
399+
.to be_present
400+
.and have_attributes(options: %w(Foo Bar Baz))
407401

408-
expect(poll).to_not be_nil
409-
expect(poll.options).to eq %w(Foo Bar Baz)
410402
expect(status.edits.reload.last.poll_options).to eq %w(Foo Bar Baz)
411403
end
412404
end
@@ -419,4 +411,8 @@ def poll_option_json(name, votes)
419411
.to eq '2021-09-08 22:39:25 UTC'
420412
end
421413
end
414+
415+
def poll_option_json(name, votes)
416+
{ type: 'Note', name: name, replies: { type: 'Collection', totalItems: votes } }
417+
end
422418
end

spec/services/authorize_follow_service_spec.rb

+14-16
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
before do
1414
FollowRequest.create(account: bob, target_account: sender)
15-
subject.call(bob, sender)
1615
end
1716

18-
it 'removes follow request' do
19-
expect(bob.requested?(sender)).to be false
20-
end
17+
it 'removes follow request and creates follow relation' do
18+
subject.call(bob, sender)
2119

22-
it 'creates follow relation' do
23-
expect(bob.following?(sender)).to be true
20+
expect(bob)
21+
.to_not be_requested(sender)
22+
expect(bob)
23+
.to be_following(sender)
2424
end
2525
end
2626

@@ -30,19 +30,17 @@
3030
before do
3131
FollowRequest.create(account: bob, target_account: sender)
3232
stub_request(:post, bob.inbox_url).to_return(status: 200)
33-
subject.call(bob, sender)
3433
end
3534

36-
it 'removes follow request' do
37-
expect(bob.requested?(sender)).to be false
38-
end
39-
40-
it 'creates follow relation' do
41-
expect(bob.following?(sender)).to be true
42-
end
35+
it 'removes follow request, creates follow relation, send accept activity', :inline_jobs do
36+
subject.call(bob, sender)
4337

44-
it 'sends an accept activity', :inline_jobs do
45-
expect(a_request(:post, bob.inbox_url)).to have_been_made.once
38+
expect(bob)
39+
.to_not be_requested(sender)
40+
expect(bob)
41+
.to be_following(sender)
42+
expect(a_request(:post, bob.inbox_url))
43+
.to have_been_made.once
4644
end
4745
end
4846
end

spec/services/batched_remove_status_service_spec.rb

+24-18
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,38 @@
2424

2525
status_alice_hello
2626
status_alice_other
27+
end
2728

29+
it 'removes status records, removes from author and local follower feeds, notifies stream, sends delete' do
2830
subject.call([status_alice_hello, status_alice_other])
29-
end
3031

31-
it 'removes statuses' do
32-
expect { Status.find(status_alice_hello.id) }.to raise_error ActiveRecord::RecordNotFound
33-
expect { Status.find(status_alice_other.id) }.to raise_error ActiveRecord::RecordNotFound
34-
end
32+
expect { Status.find(status_alice_hello.id) }
33+
.to raise_error ActiveRecord::RecordNotFound
34+
expect { Status.find(status_alice_other.id) }
35+
.to raise_error ActiveRecord::RecordNotFound
3536

36-
it 'removes statuses from author\'s home feed' do
37-
expect(HomeFeed.new(alice).get(10).pluck(:id)).to_not include(status_alice_hello.id, status_alice_other.id)
38-
end
37+
expect(feed_ids_for(alice))
38+
.to_not include(status_alice_hello.id, status_alice_other.id)
3939

40-
it 'removes statuses from local follower\'s home feed' do
41-
expect(HomeFeed.new(jeff).get(10).pluck(:id)).to_not include(status_alice_hello.id, status_alice_other.id)
42-
end
40+
expect(feed_ids_for(jeff))
41+
.to_not include(status_alice_hello.id, status_alice_other.id)
4342

44-
it 'notifies streaming API of followers' do
45-
expect(redis).to have_received(:publish).with("timeline:#{jeff.id}", any_args).at_least(:once)
46-
end
43+
expect(redis)
44+
.to have_received(:publish)
45+
.with("timeline:#{jeff.id}", any_args).at_least(:once)
46+
47+
expect(redis)
48+
.to have_received(:publish)
49+
.with('timeline:public', any_args).at_least(:once)
4750

48-
it 'notifies streaming API of public timeline' do
49-
expect(redis).to have_received(:publish).with('timeline:public', any_args).at_least(:once)
51+
expect(a_request(:post, 'http://example.com/inbox'))
52+
.to have_been_made.at_least_once
5053
end
5154

52-
it 'sends delete activity to followers' do
53-
expect(a_request(:post, 'http://example.com/inbox')).to have_been_made.at_least_once
55+
def feed_ids_for(account)
56+
HomeFeed
57+
.new(account)
58+
.get(10)
59+
.pluck(:id)
5460
end
5561
end

spec/services/block_service_spec.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@
2626

2727
before do
2828
stub_request(:post, 'http://example.com/inbox').to_return(status: 200)
29-
subject.call(sender, bob)
3029
end
3130

32-
it 'creates a blocking relation' do
33-
expect(sender.blocking?(bob)).to be true
34-
end
31+
it 'creates a blocking relation and send block activity', :inline_jobs do
32+
subject.call(sender, bob)
33+
34+
expect(sender)
35+
.to be_blocking(bob)
3536

36-
it 'sends a block activity', :inline_jobs do
37-
expect(a_request(:post, 'http://example.com/inbox')).to have_been_made.once
37+
expect(a_request(:post, 'http://example.com/inbox'))
38+
.to have_been_made.once
3839
end
3940
end
4041
end

0 commit comments

Comments
 (0)