]> cat aescling's git repositories - mastodon.git/blob - spec/controllers/settings/identity_proofs_controller_spec.rb
squashed identity proof updates (#10375)
[mastodon.git] / spec / controllers / settings / identity_proofs_controller_spec.rb
1 require 'rails_helper'
2
3 describe Settings::IdentityProofsController do
4 include RoutingHelper
5 render_views
6
7 let(:user) { Fabricate(:user) }
8 let(:valid_token) { '1'*66 }
9 let(:kbname) { 'kbuser' }
10 let(:provider) { 'keybase' }
11 let(:findable_id) { Faker::Number.number(5) }
12 let(:unfindable_id) { Faker::Number.number(5) }
13 let(:new_proof_params) do
14 { provider: provider, provider_username: kbname, token: valid_token, username: user.account.username }
15 end
16 let(:status_text) { "i just proved that i am also #{kbname} on #{provider}." }
17 let(:status_posting_params) do
18 { post_status: '0', status_text: status_text }
19 end
20 let(:postable_params) do
21 { account_identity_proof: new_proof_params.merge(status_posting_params) }
22 end
23
24 before do
25 allow_any_instance_of(ProofProvider::Keybase::Verifier).to receive(:status) { { 'proof_valid' => true, 'proof_live' => true } }
26 sign_in user, scope: :user
27 end
28
29 describe 'new proof creation' do
30 context 'GET #new' do
31 context 'with all of the correct params' do
32 before do
33 allow_any_instance_of(ProofProvider::Keybase::Badge).to receive(:avatar_url) { full_pack_url('media/images/void.png') }
34 end
35
36 it 'renders the template' do
37 get :new, params: new_proof_params
38 expect(response).to render_template(:new)
39 end
40 end
41
42 context 'without any params' do
43 it 'redirects to :index' do
44 get :new, params: {}
45 expect(response).to redirect_to settings_identity_proofs_path
46 end
47 end
48
49 context 'with params to prove a different, not logged-in user' do
50 let(:wrong_user_params) { new_proof_params.merge(username: 'someone_else') }
51
52 it 'shows a helpful alert' do
53 get :new, params: wrong_user_params
54 expect(flash[:alert]).to eq I18n.t('identity_proofs.errors.wrong_user', proving: 'someone_else', current: user.account.username)
55 end
56 end
57 end
58
59 context 'POST #create' do
60 context 'when saving works' do
61 before do
62 allow(ProofProvider::Keybase::Worker).to receive(:perform_async)
63 allow_any_instance_of(ProofProvider::Keybase::Verifier).to receive(:valid?) { true }
64 allow_any_instance_of(AccountIdentityProof).to receive(:on_success_path) { root_url }
65 end
66
67 it 'serializes a ProofProvider::Keybase::Worker' do
68 expect(ProofProvider::Keybase::Worker).to receive(:perform_async)
69 post :create, params: postable_params
70 end
71
72 it 'delegates redirection to the proof provider' do
73 expect_any_instance_of(AccountIdentityProof).to receive(:on_success_path)
74 post :create, params: postable_params
75 expect(response).to redirect_to root_url
76 end
77
78 it 'does not post a status' do
79 expect(PostStatusService).not_to receive(:new)
80 post :create, params: postable_params
81 end
82
83 context 'and the user has requested to post a status' do
84 let(:postable_params_with_status) do
85 postable_params.tap { |p| p[:account_identity_proof][:post_status] = '1' }
86 end
87
88 it 'posts a status' do
89 expect_any_instance_of(PostStatusService).to receive(:call).with(user.account, text: status_text)
90
91 post :create, params: postable_params_with_status
92 end
93 end
94 end
95
96 context 'when saving fails' do
97 before do
98 allow_any_instance_of(ProofProvider::Keybase::Verifier).to receive(:valid?) { false }
99 end
100
101 it 'redirects to :index' do
102 post :create, params: postable_params
103 expect(response).to redirect_to settings_identity_proofs_path
104 end
105
106 it 'flashes a helpful message' do
107 post :create, params: postable_params
108 expect(flash[:alert]).to eq I18n.t('identity_proofs.errors.failed', provider: 'Keybase')
109 end
110 end
111
112 context 'it can also do an update if the provider and username match an existing proof' do
113 before do
114 allow_any_instance_of(ProofProvider::Keybase::Verifier).to receive(:valid?) { true }
115 allow(ProofProvider::Keybase::Worker).to receive(:perform_async)
116 Fabricate(:account_identity_proof, account: user.account, provider: provider, provider_username: kbname)
117 allow_any_instance_of(AccountIdentityProof).to receive(:on_success_path) { root_url }
118 end
119
120 it 'calls update with the new token' do
121 expect_any_instance_of(AccountIdentityProof).to receive(:save) do |proof|
122 expect(proof.token).to eq valid_token
123 end
124
125 post :create, params: postable_params
126 end
127 end
128 end
129 end
130
131 describe 'GET #index' do
132 context 'with no existing proofs' do
133 it 'shows the helpful explanation' do
134 get :index
135 expect(response.body).to match I18n.t('identity_proofs.explanation_html')
136 end
137 end
138
139 context 'with two proofs' do
140 before do
141 allow_any_instance_of(ProofProvider::Keybase::Verifier).to receive(:valid?) { true }
142 @proof1 = Fabricate(:account_identity_proof, account: user.account)
143 @proof2 = Fabricate(:account_identity_proof, account: user.account)
144 allow_any_instance_of(AccountIdentityProof).to receive(:badge) { double(avatar_url: '', profile_url: '', proof_url: '') }
145 allow_any_instance_of(AccountIdentityProof).to receive(:refresh!) { }
146 end
147
148 it 'has the first proof username on the page' do
149 get :index
150 expect(response.body).to match /#{Regexp.quote(@proof1.provider_username)}/
151 end
152
153 it 'has the second proof username on the page' do
154 get :index
155 expect(response.body).to match /#{Regexp.quote(@proof2.provider_username)}/
156 end
157 end
158 end
159 end
This page took 0.114132 seconds and 4 git commands to generate.