Tim Emiola | 4694df3 | 2015-01-30 18:50:30 -0800 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | spec_dir = File.expand_path(File.join(File.dirname(__FILE__))) |
| 31 | $LOAD_PATH.unshift(spec_dir) |
| 32 | $LOAD_PATH.uniq! |
| 33 | |
| 34 | require 'apply_auth_examples' |
| 35 | require 'faraday' |
| 36 | require 'grpc/auth/compute_engine' |
| 37 | require 'spec_helper' |
| 38 | |
Tim Emiola | 7e7911f | 2015-02-17 18:28:23 -0800 | [diff] [blame^] | 39 | describe GRPC::Auth::GCECredentials do |
Tim Emiola | 4694df3 | 2015-01-30 18:50:30 -0800 | [diff] [blame] | 40 | MD_URI = '/computeMetadata/v1/instance/service-accounts/default/token' |
Tim Emiola | 7e7911f | 2015-02-17 18:28:23 -0800 | [diff] [blame^] | 41 | GCECredentials = GRPC::Auth::GCECredentials |
Tim Emiola | 4694df3 | 2015-01-30 18:50:30 -0800 | [diff] [blame] | 42 | |
| 43 | before(:example) do |
| 44 | @client = GCECredentials.new |
| 45 | end |
| 46 | |
| 47 | def make_auth_stubs(with_access_token: '') |
| 48 | Faraday::Adapter::Test::Stubs.new do |stub| |
| 49 | stub.get(MD_URI) do |env| |
| 50 | headers = env[:request_headers] |
| 51 | expect(headers['Metadata-Flavor']).to eq('Google') |
| 52 | build_json_response( |
| 53 | 'access_token' => with_access_token, |
| 54 | 'token_type' => 'Bearer', |
| 55 | 'expires_in' => 3600) |
| 56 | end |
| 57 | end |
| 58 | end |
| 59 | |
| 60 | it_behaves_like 'apply/apply! are OK' |
| 61 | |
| 62 | describe '#on_gce?' do |
| 63 | it 'should be true when Metadata-Flavor is Google' do |
| 64 | stubs = Faraday::Adapter::Test::Stubs.new do |stub| |
| 65 | stub.get('/') do |_env| |
| 66 | [200, |
| 67 | { 'Metadata-Flavor' => 'Google' }, |
| 68 | ''] |
| 69 | end |
| 70 | end |
| 71 | c = Faraday.new do |b| |
| 72 | b.adapter(:test, stubs) |
| 73 | end |
| 74 | expect(GCECredentials.on_gce?(connection: c)).to eq(true) |
| 75 | stubs.verify_stubbed_calls |
| 76 | end |
| 77 | |
| 78 | it 'should be false when Metadata-Flavor is not Google' do |
| 79 | stubs = Faraday::Adapter::Test::Stubs.new do |stub| |
| 80 | stub.get('/') do |_env| |
| 81 | [200, |
| 82 | { 'Metadata-Flavor' => 'NotGoogle' }, |
| 83 | ''] |
| 84 | end |
| 85 | end |
| 86 | c = Faraday.new do |b| |
| 87 | b.adapter(:test, stubs) |
| 88 | end |
| 89 | expect(GCECredentials.on_gce?(connection: c)).to eq(false) |
| 90 | stubs.verify_stubbed_calls |
| 91 | end |
| 92 | |
| 93 | it 'should be false if the response is not 200' do |
| 94 | stubs = Faraday::Adapter::Test::Stubs.new do |stub| |
| 95 | stub.get('/') do |_env| |
| 96 | [404, |
| 97 | { 'Metadata-Flavor' => 'Google' }, |
| 98 | ''] |
| 99 | end |
| 100 | end |
| 101 | c = Faraday.new do |b| |
| 102 | b.adapter(:test, stubs) |
| 103 | end |
| 104 | expect(GCECredentials.on_gce?(connection: c)).to eq(false) |
| 105 | stubs.verify_stubbed_calls |
| 106 | end |
| 107 | end |
| 108 | end |