Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire committed Jan 19, 2021
1 parent f7b2f24 commit 7895074
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 25 deletions.
37 changes: 35 additions & 2 deletions spec/controllers/settings/migrations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
it_behaves_like 'authenticate user'
end

context 'when user is sign in' do
context 'when user is signed in' do
subject { post :create, params: { account_migration: { acct: acct, current_password: '12345678' } } }

let(:user) { Fabricate(:user, password: '12345678') }
Expand All @@ -67,12 +67,45 @@
end
end

context 'when acct is a current account' do
context 'when acct is the current account' do
let(:acct) { user.account }

it 'renders show' do
is_expected.to render_template :show
end

it 'does not update the moved account' do
expect(user.account.reload.moved_to_account_id).to be_nil
end
end

context 'when target account does not reference the account being moved from' do
let(:acct) { Fabricate(:account, also_known_as: []) }

it 'renders show' do
is_expected.to render_template :show
end

it 'does not update the moved account' do
expect(user.account.reload.moved_to_account_id).to be_nil
end
end

context 'when a recent migration already exists ' do
let(:acct) { Fabricate(:account, also_known_as: [ActivityPub::TagManager.instance.uri_for(user.account)]) }

before do
moved_to = Fabricate(:account, also_known_as: [ActivityPub::TagManager.instance.uri_for(user.account)])
user.account.migrations.create!(acct: moved_to.acct)
end

it 'renders show' do
is_expected.to render_template :show
end

it 'does not update the moved account' do
expect(user.account.reload.moved_to_account_id).to be_nil
end
end
end
end
Expand Down
99 changes: 76 additions & 23 deletions spec/lib/activitypub/activity/move_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
require 'rails_helper'

RSpec.describe ActivityPub::Activity::Move do
let(:follower) { Fabricate(:account) }
let(:old_account) { Fabricate(:account) }
let(:new_account) { Fabricate(:account) }

before do
follower.follow!(old_account)

old_account.update!(uri: 'https://example.org/alice', domain: 'example.org', protocol: :activitypub, inbox_url: 'https://example.org/inbox')
new_account.update!(uri: 'https://example.com/alice', domain: 'example.com', protocol: :activitypub, inbox_url: 'https://example.com/inbox', also_known_as: [old_account.uri])

stub_request(:post, 'https://example.org/inbox').to_return(status: 200)
stub_request(:post, 'https://example.com/inbox').to_return(status: 200)

service_stub = double
allow(ActivityPub::FetchRemoteAccountService).to receive(:new).and_return(service_stub)
allow(service_stub).to receive(:call).and_return(new_account)
end
let(:follower) { Fabricate(:account) }
let(:old_account) { Fabricate(:account, uri: 'https://example.org/alice', domain: 'example.org', protocol: :activitypub, inbox_url: 'https://example.org/inbox') }
let(:new_account) { Fabricate(:account, uri: 'https://example.com/alice', domain: 'example.com', protocol: :activitypub, inbox_url: 'https://example.com/inbox', also_known_as: also_known_as) }
let(:also_known_as) { [old_account.uri] }
let(:returned_account) { new_account }

let(:json) do
{
Expand All @@ -30,23 +18,88 @@
}.with_indifferent_access
end

before do
follower.follow!(old_account)

stub_request(:post, old_account.inbox_url).to_return(status: 200)
stub_request(:post, new_account.inbox_url).to_return(status: 200)

service_stub = double
allow(ActivityPub::FetchRemoteAccountService).to receive(:new).and_return(service_stub)
allow(service_stub).to receive(:call).and_return(returned_account)
end

describe '#perform' do
subject { described_class.new(json, old_account) }

before do
subject.perform
end

it 'sets moved account on old account' do
expect(old_account.reload.moved_to_account_id).to eq new_account.id
context 'when all conditions are met' do
it 'sets moved account on old account' do
expect(old_account.reload.moved_to_account_id).to eq new_account.id
end

it 'makes followers unfollow old account' do
expect(follower.following?(old_account)).to be false
end

it 'makes followers follow-request the new account' do
expect(follower.requested?(new_account)).to be true
end
end

it 'makes followers unfollow old account' do
expect(follower.following?(old_account)).to be false
context "when the new account can't be resolved" do
let(:returned_account) { nil }

it 'does not set moved account on old account' do
expect(old_account.reload.moved_to_account_id).to be_nil
end

it 'does not make followers unfollow old account' do
expect(follower.following?(old_account)).to be true
end

it 'does not make followers follow-request the new account' do
expect(follower.requested?(new_account)).to be false
end
end

it 'makes followers follow-request the new account' do
expect(follower.requested?(new_account)).to be true
context 'when the new account does not references the old account' do
let(:also_known_as) { [] }

it 'does not set moved account on old account' do
expect(old_account.reload.moved_to_account_id).to be_nil
end

it 'does not make followers unfollow old account' do
expect(follower.following?(old_account)).to be true
end

it 'does not make followers follow-request the new account' do
expect(follower.requested?(new_account)).to be false
end
end

context 'when a Move has been recently processed' do
around do |example|
Redis.current.set("move_in_progress:#{old_account.id}", true, nx: true, ex: 7.days.seconds)
example.run
Redis.current.del("move_in_progress:#{old_account.id}")
end

it 'does not set moved account on old account' do
expect(old_account.reload.moved_to_account_id).to be_nil
end

it 'does not make followers unfollow old account' do
expect(follower.following?(old_account)).to be true
end

it 'does not make followers follow-request the new account' do
expect(follower.requested?(new_account)).to be false
end
end
end
end

0 comments on commit 7895074

Please sign in to comment.