about summary refs log tree commit diff
path: root/spec/controllers/settings/migrations_controller_spec.rb
blob: 9b12bc40f13f52ddb46a0a5a984d0a236f82aa89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# frozen_string_literal: true

require 'rails_helper'

describe Settings::MigrationsController do
  render_views

  shared_examples 'authenticate user' do
    it 'redirects to sign_in page' do
      expect(subject).to redirect_to new_user_session_path
    end
  end

  describe 'GET #show' do
    context 'when user is not sign in' do
      subject { get :show }

      it_behaves_like 'authenticate user'
    end

    context 'when user is sign in' do
      subject { get :show }

      let(:user) { Fabricate(:account, moved_to_account: moved_to_account).user }

      before { sign_in user, scope: :user }

      context 'when user does not have moved to account' do
        let(:moved_to_account) { nil }

        it 'renders show page' do
          expect(subject).to have_http_status 200
          expect(subject).to render_template :show
        end
      end

      context 'when user has a moved to account' do
        let(:moved_to_account) { Fabricate(:account) }

        it 'renders show page' do
          expect(subject).to have_http_status 200
          expect(subject).to render_template :show
        end
      end
    end
  end

  describe 'POST #create' do
    context 'when user is not sign in' do
      subject { post :create }

      it_behaves_like 'authenticate user'
    end

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

      let(:user) { Fabricate(:user, password: '12345678') }

      before { sign_in user, scope: :user }

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

        it 'updates moved to account' do
          expect(subject).to redirect_to settings_migration_path
          expect(user.account.reload.moved_to_account_id).to eq acct.id
        end
      end

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

        it 'renders show' do
          expect(subject).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
          expect(subject).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
          expect(subject).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
end