3 describe Settings
::IdentityProofsController do
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
}
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
}
20 let(:postable_params) do
21 { account_identity_proof
: new_proof_params
.merge(status_posting_params
) }
25 allow_any_instance_of(ProofProvider
::Keybase::Verifier).to
receive(:status) { { 'proof_valid' => true, 'proof_live' => true } }
26 sign_in user
, scope
: :user
29 describe
'new proof creation' do
31 context
'with all of the correct params' do
33 allow_any_instance_of(ProofProvider
::Keybase::Badge).to
receive(:avatar_url) { full_pack_url('media/images/void.png') }
36 it
'renders the template' do
37 get
:new, params
: new_proof_params
38 expect(response
).to
render_template(:new)
42 context
'without any params' do
43 it
'redirects to :index' do
45 expect(response
).to redirect_to settings_identity_proofs_path
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') }
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
)
59 context
'POST #create' do
60 context
'when saving works' 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
}
67 it
'serializes a ProofProvider::Keybase::Worker' do
68 expect(ProofProvider
::Keybase::Worker).to
receive(:perform_async)
69 post
:create, params
: postable_params
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
78 it
'does not post a status' do
79 expect(PostStatusService
).not_to
receive(:new)
80 post
:create, params
: postable_params
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' }
88 it
'posts a status' do
89 expect_any_instance_of(PostStatusService
).to
receive(:call).with(user
.account
, text
: status_text
)
91 post
:create, params
: postable_params_with_status
96 context
'when saving fails' do
98 allow_any_instance_of(ProofProvider
::Keybase::Verifier).to
receive(:valid?) { false }
101 it
'redirects to :index' do
102 post
:create, params
: postable_params
103 expect(response
).to redirect_to settings_identity_proofs_path
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')
112 context
'it can also do an update if the provider and username match an existing proof' 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
}
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
125 post
:create, params
: postable_params
131 describe
'GET #index' do
132 context
'with no existing proofs' do
133 it
'shows the helpful explanation' do
135 expect(response
.body
).to match I18n
.t('identity_proofs.explanation_html')
139 context
'with two proofs' 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!
) { }
148 it
'has the first proof username on the page' do
150 expect(response
.body
).to match
/#{Regexp.quote(@proof1.provider_username)}/
153 it
'has the second proof username on the page' do
155 expect(response
.body
).to match
/#{Regexp.quote(@proof2.provider_username)}/