verifier_spec.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. require 'rails_helper'
  2. describe ProofProvider::Keybase::Verifier do
  3. let(:my_domain) { Rails.configuration.x.local_domain }
  4. let(:keybase_proof) do
  5. local_proof = AccountIdentityProof.new(
  6. provider: 'Keybase',
  7. provider_username: 'cryptoalice',
  8. token: '11111111111111111111111111'
  9. )
  10. described_class.new('alice', 'cryptoalice', '11111111111111111111111111', my_domain)
  11. end
  12. let(:query_params) do
  13. "domain=#{my_domain}&kb_username=cryptoalice&sig_hash=11111111111111111111111111&username=alice"
  14. end
  15. describe '#valid?' do
  16. let(:base_url) { 'https://keybase.io/_/api/1.0/sig/proof_valid.json' }
  17. context 'when valid' do
  18. before do
  19. json_response_body = '{"status":{"code":0,"name":"OK"},"proof_valid":true}'
  20. stub_request(:get, "#{base_url}?#{query_params}").to_return(status: 200, body: json_response_body)
  21. end
  22. it 'calls out to keybase and returns true' do
  23. expect(keybase_proof.valid?).to eq true
  24. end
  25. end
  26. context 'when invalid' do
  27. before do
  28. json_response_body = '{"status":{"code":0,"name":"OK"},"proof_valid":false}'
  29. stub_request(:get, "#{base_url}?#{query_params}").to_return(status: 200, body: json_response_body)
  30. end
  31. it 'calls out to keybase and returns false' do
  32. expect(keybase_proof.valid?).to eq false
  33. end
  34. end
  35. context 'with an unexpected api response' do
  36. before do
  37. json_response_body = '{"status":{"code":100,"desc":"wrong size hex_id","fields":{"sig_hash":"wrong size hex_id"},"name":"INPUT_ERROR"}}'
  38. stub_request(:get, "#{base_url}?#{query_params}").to_return(status: 200, body: json_response_body)
  39. end
  40. it 'swallows the error and returns false' do
  41. expect(keybase_proof.valid?).to eq false
  42. end
  43. end
  44. end
  45. describe '#status' do
  46. let(:base_url) { 'https://keybase.io/_/api/1.0/sig/proof_live.json' }
  47. context 'with a normal response' do
  48. before do
  49. json_response_body = '{"status":{"code":0,"name":"OK"},"proof_live":false,"proof_valid":true}'
  50. stub_request(:get, "#{base_url}?#{query_params}").to_return(status: 200, body: json_response_body)
  51. end
  52. it 'calls out to keybase and returns the status fields as proof_valid and proof_live' do
  53. expect(keybase_proof.status).to include({ 'proof_valid' => true, 'proof_live' => false })
  54. end
  55. end
  56. context 'with an unexpected keybase response' do
  57. before do
  58. json_response_body = '{"status":{"code":100,"desc":"missing non-optional field sig_hash","fields":{"sig_hash":"missing non-optional field sig_hash"},"name":"INPUT_ERROR"}}'
  59. stub_request(:get, "#{base_url}?#{query_params}").to_return(status: 200, body: json_response_body)
  60. end
  61. it 'raises a ProofProvider::Keybase::UnexpectedResponseError' do
  62. expect { keybase_proof.status }.to raise_error ProofProvider::Keybase::UnexpectedResponseError
  63. end
  64. end
  65. end
  66. end