identity_proofs_controller_spec.rb 6.7 KB

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