The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

113 lines
4.0 KiB

  1. require 'rails_helper'
  2. describe Settings::IdentityProofsController do
  3. render_views
  4. let(:user) { Fabricate(:user) }
  5. let(:valid_token) { '1'*66 }
  6. let(:kbname) { 'kbuser' }
  7. let(:provider) { 'keybase' }
  8. let(:findable_id) { Faker::Number.number(5) }
  9. let(:unfindable_id) { Faker::Number.number(5) }
  10. let(:postable_params) do
  11. { account_identity_proof: { provider: provider, provider_username: kbname, token: valid_token } }
  12. end
  13. before do
  14. allow_any_instance_of(ProofProvider::Keybase::Verifier).to receive(:status) { { 'proof_valid' => true, 'proof_live' => true } }
  15. sign_in user, scope: :user
  16. end
  17. describe 'new proof creation' do
  18. context 'GET #new with no existing proofs' do
  19. it 'redirects to :index' do
  20. get :new
  21. expect(response).to redirect_to settings_identity_proofs_path
  22. end
  23. end
  24. context 'POST #create' do
  25. context 'when saving works' do
  26. before do
  27. allow(ProofProvider::Keybase::Worker).to receive(:perform_async)
  28. allow_any_instance_of(ProofProvider::Keybase::Verifier).to receive(:valid?) { true }
  29. allow_any_instance_of(AccountIdentityProof).to receive(:on_success_path) { root_url }
  30. end
  31. it 'serializes a ProofProvider::Keybase::Worker' do
  32. expect(ProofProvider::Keybase::Worker).to receive(:perform_async)
  33. post :create, params: postable_params
  34. end
  35. it 'delegates redirection to the proof provider' do
  36. expect_any_instance_of(AccountIdentityProof).to receive(:on_success_path)
  37. post :create, params: postable_params
  38. expect(response).to redirect_to root_url
  39. end
  40. end
  41. context 'when saving fails' do
  42. before do
  43. allow_any_instance_of(ProofProvider::Keybase::Verifier).to receive(:valid?) { false }
  44. end
  45. it 'redirects to :index' do
  46. post :create, params: postable_params
  47. expect(response).to redirect_to settings_identity_proofs_path
  48. end
  49. it 'flashes a helpful message' do
  50. post :create, params: postable_params
  51. expect(flash[:alert]).to eq I18n.t('identity_proofs.errors.failed', provider: 'Keybase')
  52. end
  53. end
  54. context 'it can also do an update if the provider and username match an existing proof' do
  55. before do
  56. allow_any_instance_of(ProofProvider::Keybase::Verifier).to receive(:valid?) { true }
  57. allow(ProofProvider::Keybase::Worker).to receive(:perform_async)
  58. Fabricate(:account_identity_proof, account: user.account, provider: provider, provider_username: kbname)
  59. allow_any_instance_of(AccountIdentityProof).to receive(:on_success_path) { root_url }
  60. end
  61. it 'calls update with the new token' do
  62. expect_any_instance_of(AccountIdentityProof).to receive(:save) do |proof|
  63. expect(proof.token).to eq valid_token
  64. end
  65. post :create, params: postable_params
  66. end
  67. end
  68. end
  69. end
  70. describe 'GET #index' do
  71. context 'with no existing proofs' do
  72. it 'shows the helpful explanation' do
  73. get :index
  74. expect(response.body).to match I18n.t('identity_proofs.explanation_html')
  75. end
  76. end
  77. context 'with two proofs' do
  78. before do
  79. allow_any_instance_of(ProofProvider::Keybase::Verifier).to receive(:valid?) { true }
  80. @proof1 = Fabricate(:account_identity_proof, account: user.account)
  81. @proof2 = Fabricate(:account_identity_proof, account: user.account)
  82. allow_any_instance_of(AccountIdentityProof).to receive(:badge) { double(avatar_url: '', profile_url: '', proof_url: '') }
  83. allow_any_instance_of(AccountIdentityProof).to receive(:refresh!) { }
  84. end
  85. it 'has the first proof username on the page' do
  86. get :index
  87. expect(response.body).to match /#{Regexp.quote(@proof1.provider_username)}/
  88. end
  89. it 'has the second proof username on the page' do
  90. get :index
  91. expect(response.body).to match /#{Regexp.quote(@proof2.provider_username)}/
  92. end
  93. end
  94. end
  95. end