about summary refs log tree commit diff
path: root/spec/lib/proof_provider/keybase/verifier_spec.rb
blob: 4ce67da9c56facaa0c22615038e6907a7c6d2fec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'rails_helper'

describe ProofProvider::Keybase::Verifier do
  let(:my_domain) { Rails.configuration.x.local_domain }

  let(:keybase_proof) do
    local_proof = AccountIdentityProof.new(
      provider: 'Keybase',
      provider_username: 'cryptoalice',
      token: '11111111111111111111111111'
    )

    described_class.new('alice', 'cryptoalice', '11111111111111111111111111')
  end

  let(:query_params) do
    "domain=#{my_domain}&kb_username=cryptoalice&sig_hash=11111111111111111111111111&username=alice"
  end

  describe '#valid?' do
    let(:base_url) { 'https://keybase.io/_/api/1.0/sig/proof_valid.json' }

    context 'when valid' do
      before do
        json_response_body = '{"status":{"code":0,"name":"OK"},"proof_valid":true}'
        stub_request(:get, "#{base_url}?#{query_params}").to_return(status: 200, body: json_response_body)
      end

      it 'calls out to keybase and returns true' do
        expect(keybase_proof.valid?).to eq true
      end
    end

    context 'when invalid' do
      before do
        json_response_body = '{"status":{"code":0,"name":"OK"},"proof_valid":false}'
        stub_request(:get, "#{base_url}?#{query_params}").to_return(status: 200, body: json_response_body)
      end

      it 'calls out to keybase and returns false' do
        expect(keybase_proof.valid?).to eq false
      end
    end

    context 'with an unexpected api response' do
      before do
        json_response_body = '{"status":{"code":100,"desc":"wrong size hex_id","fields":{"sig_hash":"wrong size hex_id"},"name":"INPUT_ERROR"}}'
        stub_request(:get, "#{base_url}?#{query_params}").to_return(status: 200, body: json_response_body)
      end

      it 'swallows the error and returns false' do
        expect(keybase_proof.valid?).to eq false
      end
    end
  end

  describe '#status' do
    let(:base_url) { 'https://keybase.io/_/api/1.0/sig/proof_live.json' }

    context 'with a normal response' do
      before do
        json_response_body = '{"status":{"code":0,"name":"OK"},"proof_live":false,"proof_valid":true}'
        stub_request(:get, "#{base_url}?#{query_params}").to_return(status: 200, body: json_response_body)
      end

      it 'calls out to keybase and returns the status fields as proof_valid and proof_live' do
        expect(keybase_proof.status).to include({ 'proof_valid' => true, 'proof_live' => false })
      end
    end

    context 'with an unexpected keybase response' do
      before do
        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"}}'
        stub_request(:get, "#{base_url}?#{query_params}").to_return(status: 200, body: json_response_body)
      end

      it 'raises a ProofProvider::Keybase::UnexpectedResponseError' do
        expect { keybase_proof.status }.to raise_error ProofProvider::Keybase::UnexpectedResponseError
      end
    end
  end
end