proofs_controller_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. require 'rails_helper'
  2. describe Api::ProofsController do
  3. let(:alice) { Fabricate(:account, username: 'alice') }
  4. before do
  5. stub_request(:get, 'https://keybase.io/_/api/1.0/sig/proof_valid.json?domain=cb6e6126.ngrok.io&kb_username=crypto_alice&sig_hash=111111111111111111111111111111111111111111111111111111111111111111&username=alice').to_return(status: 200, body: '{"proof_valid":true,"proof_live":false}')
  6. stub_request(:get, 'https://keybase.io/_/api/1.0/sig/proof_live.json?domain=cb6e6126.ngrok.io&kb_username=crypto_alice&sig_hash=111111111111111111111111111111111111111111111111111111111111111111&username=alice').to_return(status: 200, body: '{"proof_valid":true,"proof_live":true}')
  7. stub_request(:get, 'https://keybase.io/_/api/1.0/sig/proof_valid.json?domain=cb6e6126.ngrok.io&kb_username=hidden_alice&sig_hash=222222222222222222222222222222222222222222222222222222222222222222&username=alice').to_return(status: 200, body: '{"proof_valid":true,"proof_live":true}')
  8. stub_request(:get, 'https://keybase.io/_/api/1.0/sig/proof_live.json?domain=cb6e6126.ngrok.io&kb_username=hidden_alice&sig_hash=222222222222222222222222222222222222222222222222222222222222222222&username=alice').to_return(status: 200, body: '{"proof_valid":true,"proof_live":true}')
  9. end
  10. describe 'GET #index' do
  11. describe 'with a non-existent username' do
  12. it '404s' do
  13. get :index, params: { username: 'nonexistent', provider: 'keybase' }
  14. expect(response).to have_http_status(:not_found)
  15. end
  16. end
  17. describe 'with a user that has no proofs' do
  18. it 'is an empty list of signatures' do
  19. get :index, params: { username: alice.username, provider: 'keybase' }
  20. expect(body_as_json[:signatures]).to eq []
  21. end
  22. end
  23. describe 'with a user that has a live, valid proof' do
  24. let(:token1) { '111111111111111111111111111111111111111111111111111111111111111111' }
  25. let(:kb_name1) { 'crypto_alice' }
  26. before do
  27. Fabricate(:account_identity_proof, account: alice, verified: true, live: true, token: token1, provider_username: kb_name1)
  28. end
  29. it 'is a list with that proof in it' do
  30. get :index, params: { username: alice.username, provider: 'keybase' }
  31. expect(body_as_json[:signatures]).to eq [
  32. { kb_username: kb_name1, sig_hash: token1 },
  33. ]
  34. end
  35. describe 'add one that is neither live nor valid' do
  36. let(:token2) { '222222222222222222222222222222222222222222222222222222222222222222' }
  37. let(:kb_name2) { 'hidden_alice' }
  38. before do
  39. Fabricate(:account_identity_proof, account: alice, verified: false, live: false, token: token2, provider_username: kb_name2)
  40. end
  41. it 'is a list with both proofs' do
  42. get :index, params: { username: alice.username, provider: 'keybase' }
  43. expect(body_as_json[:signatures]).to eq [
  44. { kb_username: kb_name1, sig_hash: token1 },
  45. { kb_username: kb_name2, sig_hash: token2 },
  46. ]
  47. end
  48. end
  49. end
  50. describe 'a user that has an avatar' do
  51. let(:alice) { Fabricate(:account, username: 'alice', avatar: attachment_fixture('avatar.gif')) }
  52. context 'and a proof' do
  53. let(:token1) { '111111111111111111111111111111111111111111111111111111111111111111' }
  54. let(:kb_name1) { 'crypto_alice' }
  55. before do
  56. Fabricate(:account_identity_proof, account: alice, verified: true, live: true, token: token1, provider_username: kb_name1)
  57. get :index, params: { username: alice.username, provider: 'keybase' }
  58. end
  59. it 'has two keys: signatures and avatar' do
  60. expect(body_as_json.keys).to match_array [:signatures, :avatar]
  61. end
  62. it 'has the correct signatures' do
  63. expect(body_as_json[:signatures]).to eq [
  64. { kb_username: kb_name1, sig_hash: token1 },
  65. ]
  66. end
  67. it 'has the correct avatar url' do
  68. first_part = 'https://cb6e6126.ngrok.io/system/accounts/avatars/'
  69. last_part = 'original/avatar.gif'
  70. expect(body_as_json[:avatar]).to match /#{Regexp.quote(first_part)}(?:\d{3,5}\/){3}#{Regexp.quote(last_part)}/
  71. end
  72. end
  73. end
  74. end
  75. end