File: credentials_retriever_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (86 lines) | stat: -rw-r--r-- 2,745 bytes parent folder | download
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
83
84
85
86
# frozen_string_literal: true

require 'lite_spec_helper'

describe Mongo::Crypt::KMS::Azure::CredentialsRetriever do
  # The tests here require fake azure server, which is started in FLE
  # configurations on evergreen. If you want to run these tests locally,
  # you need to start the server manually. See .evergreen/run-tests.sh
  # for the command to start the server.
  before do
    skip 'These tests require fake azure server to be running' unless SpecConfig.instance.fle?
  end

  let(:metadata_host) do
    'localhost:8080'
  end

  describe '.fetch_access_token' do
    context 'when response is valid' do
      let(:token) do
        described_class.fetch_access_token(metadata_host: metadata_host)
      end

      it 'returns access token' do
        expect(token.access_token).to eq('magic-cookie')
      end

      it 'returns expiration time' do
        expect(token.expires_in).to eq(70)
      end
    end

    context 'when response contains empty json' do
      it 'raises error' do
        expect do
          described_class.fetch_access_token(
            extra_headers: { 'X-MongoDB-HTTP-TestParams' => 'case=empty-json' }, metadata_host: metadata_host
          )
        end.to raise_error(Mongo::Crypt::KMS::CredentialsNotFound)
      end
    end

    context 'when response contains invalid json' do
      it 'raises error' do
        expect do
          described_class.fetch_access_token(
            extra_headers: { 'X-MongoDB-HTTP-TestParams' => 'case=bad-json' }, metadata_host: metadata_host
          )
        end.to raise_error(Mongo::Crypt::KMS::CredentialsNotFound)
      end
    end

    context 'when metadata host responds with 500' do
      it 'raises error' do
        expect do
          described_class.fetch_access_token(
            extra_headers: { 'X-MongoDB-HTTP-TestParams' => 'case=500' }, metadata_host: metadata_host
          )
        end.to raise_error(Mongo::Crypt::KMS::CredentialsNotFound)
      end
    end

    context 'when metadata host responds with 404' do
      it 'raises error' do
        expect do
          described_class.fetch_access_token(
            extra_headers: { 'X-MongoDB-HTTP-TestParams' => 'case=404' }, metadata_host: metadata_host
          )
        end.to raise_error(Mongo::Crypt::KMS::CredentialsNotFound)
      end
    end

    context 'when metadata host is slow' do
      # On JRuby Timeout.timeout does not work in this case.
      fails_on_jruby

      it 'raises error' do
        expect do
          described_class.fetch_access_token(
            extra_headers: { 'X-MongoDB-HTTP-TestParams' => 'case=slow' }, metadata_host: metadata_host
          )
        end.to raise_error(Mongo::Crypt::KMS::CredentialsNotFound)
      end
    end
  end
end