Refactor and improve tests (#17386)

* Change account and user fabricators to simplify and improve tests

- `Fabricate(:account)` implicitly fabricates an associated `user` if
  no `domain` attribute is given (an account with `domain: nil` is
  considered a local account, but no user record was created), unless
  `user: nil` is passed
- `Fabricate(:account, user: Fabricate(:user))` should still be possible
  but is discouraged.

* Fix and refactor tests

- avoid passing unneeded attributes to `Fabricate(:user)` or
  `Fabricate(:account)`
- avoid embedding `Fabricate(:user)` into a `Fabricate(:account)` or the other
  way around
- prefer `Fabricate(:user, account_attributes: …)` to
  `Fabricate(:user, account: Fabricate(:account, …)`
- also, some tests were using remote accounts with local user records, which is
  not representative of production code.
This commit is contained in:
Claire 2022-01-28 00:46:42 +01:00 committed by GitHub
parent 03d59340da
commit e38fc319dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
95 changed files with 187 additions and 185 deletions

View file

@ -6,7 +6,7 @@ RSpec.describe AuthorizeFollowService, type: :service do
subject { AuthorizeFollowService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account, username: 'bob') }
before do
FollowRequest.create(account: bob, target_account: sender)
@ -23,7 +23,7 @@ RSpec.describe AuthorizeFollowService, type: :service do
end
describe 'remote ActivityPub' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox')).account }
let(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }
before do
FollowRequest.create(account: bob, target_account: sender)

View file

@ -5,7 +5,7 @@ RSpec.describe BatchedRemoveStatusService, type: :service do
let!(:alice) { Fabricate(:account) }
let!(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com') }
let!(:jeff) { Fabricate(:user).account }
let!(:jeff) { Fabricate(:account) }
let!(:hank) { Fabricate(:account, username: 'hank', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }
let(:status1) { PostStatusService.new.call(alice, text: 'Hello @bob@example.com') }

View file

@ -6,7 +6,7 @@ RSpec.describe BlockService, type: :service do
subject { BlockService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account, username: 'bob') }
before do
subject.call(sender, bob)
@ -18,7 +18,7 @@ RSpec.describe BlockService, type: :service do
end
describe 'remote ActivityPub' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox')).account }
let(:bob) { Fabricate(:account, username: 'bob', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }
before do
stub_request(:post, 'http://example.com/inbox').to_return(status: 200)

View file

@ -3,9 +3,9 @@ require 'rails_helper'
RSpec.describe FanOutOnWriteService, type: :service do
let(:last_active_at) { Time.now.utc }
let!(:alice) { Fabricate(:user, current_sign_in_at: last_active_at, account: Fabricate(:account, username: 'alice')).account }
let!(:bob) { Fabricate(:user, current_sign_in_at: last_active_at, account: Fabricate(:account, username: 'bob')).account }
let!(:tom) { Fabricate(:user, current_sign_in_at: last_active_at, account: Fabricate(:account, username: 'tom')).account }
let!(:alice) { Fabricate(:user, current_sign_in_at: last_active_at).account }
let!(:bob) { Fabricate(:user, current_sign_in_at: last_active_at, account_attributes: { username: 'bob' }).account }
let!(:tom) { Fabricate(:user, current_sign_in_at: last_active_at).account }
subject { described_class.new }

View file

@ -6,7 +6,7 @@ RSpec.describe FavouriteService, type: :service do
subject { FavouriteService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account) }
let(:status) { Fabricate(:status, account: bob) }
before do
@ -19,7 +19,7 @@ RSpec.describe FavouriteService, type: :service do
end
describe 'remote ActivityPub' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, protocol: :activitypub, username: 'bob', domain: 'example.com', inbox_url: 'http://example.com/inbox')).account }
let(:bob) { Fabricate(:account, protocol: :activitypub, username: 'bob', domain: 'example.com', inbox_url: 'http://example.com/inbox') }
let(:status) { Fabricate(:status, account: bob) }
before do

View file

@ -7,7 +7,7 @@ RSpec.describe FollowService, type: :service do
context 'local account' do
describe 'locked account' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, locked: true, username: 'bob')).account }
let(:bob) { Fabricate(:account, locked: true, username: 'bob') }
before do
subject.call(sender, bob)
@ -19,7 +19,7 @@ RSpec.describe FollowService, type: :service do
end
describe 'locked account, no reblogs' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, locked: true, username: 'bob')).account }
let(:bob) { Fabricate(:account, locked: true, username: 'bob') }
before do
subject.call(sender, bob, reblogs: false)
@ -31,7 +31,7 @@ RSpec.describe FollowService, type: :service do
end
describe 'unlocked account, from silenced account' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account, username: 'bob') }
before do
sender.touch(:silenced_at)
@ -44,7 +44,7 @@ RSpec.describe FollowService, type: :service do
end
describe 'unlocked account, from a muted account' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account, username: 'bob') }
before do
bob.mute!(sender)
@ -58,7 +58,7 @@ RSpec.describe FollowService, type: :service do
end
describe 'unlocked account' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account, username: 'bob') }
before do
subject.call(sender, bob)
@ -71,7 +71,7 @@ RSpec.describe FollowService, type: :service do
end
describe 'unlocked account, no reblogs' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account, username: 'bob') }
before do
subject.call(sender, bob, reblogs: false)
@ -84,7 +84,7 @@ RSpec.describe FollowService, type: :service do
end
describe 'already followed account' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account, username: 'bob') }
before do
sender.follow!(bob)
@ -97,7 +97,7 @@ RSpec.describe FollowService, type: :service do
end
describe 'already followed account, turning reblogs off' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account, username: 'bob') }
before do
sender.follow!(bob, reblogs: true)
@ -110,7 +110,7 @@ RSpec.describe FollowService, type: :service do
end
describe 'already followed account, turning reblogs on' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account, username: 'bob') }
before do
sender.follow!(bob, reblogs: false)
@ -124,7 +124,7 @@ RSpec.describe FollowService, type: :service do
end
context 'remote ActivityPub account' do
let(:bob) { Fabricate(:user, account: Fabricate(:account, username: 'bob', domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox')).account }
let(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }
before do
stub_request(:post, "http://example.com/inbox").to_return(:status => 200, :body => "", :headers => {})

View file

@ -6,7 +6,7 @@ RSpec.describe RejectFollowService, type: :service do
subject { RejectFollowService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account) }
before do
FollowRequest.create(account: bob, target_account: sender)
@ -23,7 +23,7 @@ RSpec.describe RejectFollowService, type: :service do
end
describe 'remote ActivityPub' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox')).account }
let(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }
before do
FollowRequest.create(account: bob, target_account: sender)

View file

@ -3,7 +3,7 @@ require 'rails_helper'
RSpec.describe RemoveStatusService, type: :service do
subject { RemoveStatusService.new }
let!(:alice) { Fabricate(:account, user: Fabricate(:user)) }
let!(:alice) { Fabricate(:account) }
let!(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com') }
let!(:jeff) { Fabricate(:account) }
let!(:hank) { Fabricate(:account, username: 'hank', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }

View file

@ -3,7 +3,7 @@ require 'rails_helper'
RSpec.describe ReportService, type: :service do
subject { described_class.new }
let(:source_account) { Fabricate(:user).account }
let(:source_account) { Fabricate(:account) }
context 'for a remote account' do
let(:remote_account) { Fabricate(:account, domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }

View file

@ -6,7 +6,7 @@ RSpec.describe UnblockService, type: :service do
subject { UnblockService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account) }
before do
sender.block!(bob)
@ -19,7 +19,7 @@ RSpec.describe UnblockService, type: :service do
end
describe 'remote ActivityPub' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox')).account }
let(:bob) { Fabricate(:account, username: 'bob', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }
before do
sender.block!(bob)

View file

@ -6,7 +6,7 @@ RSpec.describe UnfollowService, type: :service do
subject { UnfollowService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:bob) { Fabricate(:account, username: 'bob') }
before do
sender.follow!(bob)
@ -19,7 +19,7 @@ RSpec.describe UnfollowService, type: :service do
end
describe 'remote ActivityPub' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox')).account }
let(:bob) { Fabricate(:account, username: 'bob', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }
before do
sender.follow!(bob)
@ -37,7 +37,7 @@ RSpec.describe UnfollowService, type: :service do
end
describe 'remote ActivityPub (reverse)' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox')).account }
let(:bob) { Fabricate(:account, username: 'bob', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }
before do
bob.follow!(sender)

View file

@ -5,9 +5,9 @@ RSpec.describe UpdateAccountService, type: :service do
describe 'switching form locked to unlocked accounts' do
let(:account) { Fabricate(:account, locked: true) }
let(:alice) { Fabricate(:user, email: 'alice@example.com', account: Fabricate(:account, username: 'alice')).account }
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:eve) { Fabricate(:user, email: 'eve@example.com', account: Fabricate(:account, username: 'eve')).account }
let(:alice) { Fabricate(:account) }
let(:bob) { Fabricate(:account) }
let(:eve) { Fabricate(:account) }
before do
bob.touch(:silenced_at)