"Fossies" - the Fresh Open Source Software Archive

Member "vagrant-2.2.14/test/unit/plugins/commands/cloud/auth/middleware/add_downloader_authentication_test.rb" (20 Nov 2020, 5475 Bytes) of package /linux/misc/vagrant-2.2.14.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 require File.expand_path("../../../../../../base", __FILE__)
    2 
    3 require Vagrant.source_root.join("plugins/commands/cloud/auth/middleware/add_downloader_authentication")
    4 require "vagrant/util/downloader"
    5 
    6 describe VagrantPlugins::CloudCommand::AddDownloaderAuthentication do
    7   include_context "unit"
    8 
    9   let(:app) { lambda { |env| } }
   10   let(:ui) { double("ui") }
   11   let(:env) { {
   12     env: iso_env,
   13     ui: ui
   14   } }
   15 
   16   let(:iso_env) { isolated_environment.create_vagrant_env }
   17   let(:server_url) { "http://vagrantcloud.com/box.box" }
   18   let(:dwnloader) { Vagrant::Util::Downloader.new(server_url, "/some/path", {}) }
   19 
   20   subject { described_class.new(app, env) }
   21 
   22   before do
   23     allow(Vagrant).to receive(:server_url).and_return(server_url)
   24     allow(ui).to receive(:warn)
   25     stub_env("ATLAS_TOKEN" => nil)
   26   end
   27 
   28   describe "#call" do
   29     context "non full paths" do
   30       let(:server_url) { "http://vagrantcloud.com" }
   31       let(:dwnloader) { Vagrant::Util::Downloader.new(server_url, "/some/path", {}) }
   32 
   33       it "does nothing if we have no server set" do
   34         allow(Vagrant).to receive(:server_url).and_return(nil)
   35         VagrantPlugins::CloudCommand::Client.new(iso_env).store_token("fooboohoo")
   36 
   37         env[:downloader] = dwnloader
   38         subject.call(env)
   39         expect(env[:downloader].headers.empty?).to eq(true)
   40       end
   41 
   42       it "does nothing if we aren't logged in" do
   43         env[:downloader] = dwnloader
   44         subject.call(env)
   45         expect(env[:downloader].headers.empty?).to eq(true)
   46       end
   47     end
   48 
   49     context "custom server" do
   50       let(:server_url) { "http://surprise.com/box.box" }
   51       let(:dwnloader) { Vagrant::Util::Downloader.new(server_url, "/some/path", {}) }
   52 
   53       it "warns when adding token to custom server" do
   54         server_url = "https://surprise.com"
   55         allow(Vagrant).to receive(:server_url).and_return(server_url)
   56   
   57         token = "foobarbaz"
   58         VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)
   59   
   60         expect(subject).to receive(:sleep).once
   61         expect(ui).to receive(:warn).once
   62   
   63         env[:downloader] = dwnloader
   64         subject.call(env)
   65   
   66         expect(env[:downloader].headers).to eq(["Authorization: Bearer #{token}"])
   67       end
   68     end
   69 
   70     context "replacement hosts" do
   71       let(:dwnloader) { Vagrant::Util::Downloader.new("https://app.vagrantup.com", "/some/path", {}) }
   72 
   73       it "modifies host URL to target if authorized host" do
   74         token = "foobarbaz"
   75         VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)
   76         env[:downloader] = dwnloader
   77         subject.call(env)
   78         expect(env[:downloader].headers).to eq(["Authorization: Bearer #{token}"])
   79         expect(URI.parse(env[:downloader].source).host).to eq(VagrantPlugins::CloudCommand::AddDownloaderAuthentication::TARGET_HOST)
   80       end
   81     end
   82 
   83     context "malformed url" do
   84       let(:bad_url) { "this is not a valid url" }
   85       let(:dwnloader) { Vagrant::Util::Downloader.new(bad_url, "/some/path", {}) }
   86 
   87       it "ignores urls that it cannot parse" do
   88         # Ensure the bad URL does cause an exception
   89         expect{ URI.parse(bad_url) }.to raise_error URI::Error
   90         env[:downloader] = dwnloader
   91         subject.call(env)
   92         expect(env[:downloader].source).to eq(bad_url)
   93       end
   94     end
   95 
   96     context "with an headers already added" do
   97       let(:auth_header) { "Authorization Bearer: token" }
   98       let(:other_header) {"some: thing"}
   99       let(:dwnloader) { Vagrant::Util::Downloader.new(server_url, "/some/path", {headers: [other_header]}) }
  100     
  101       it "appends the auth header" do
  102         token = "foobarbaz"
  103         VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)
  104   
  105         env[:downloader] = dwnloader
  106         subject.call(env)
  107 
  108         expect(env[:downloader].headers).to eq([other_header, "Authorization: Bearer #{token}"])
  109       end
  110 
  111       context "with local file path" do
  112         let(:file_path) { "file:////path/to/box.box" }
  113         let(:dwnloader) { Vagrant::Util::Downloader.new(file_path, "/some/path", {}) }
  114 
  115         it "returns original urls when not modified" do
  116           env[:downloader] = dwnloader
  117           subject.call(env)
  118           
  119           expect(env[:downloader].source).to eq(file_path)
  120           expect(env[:downloader].headers.empty?).to eq(true)
  121         end
  122       end
  123 
  124       it "does not append multiple access_tokens" do
  125         dwnloader.headers << auth_header
  126         token = "foobarbaz"
  127         VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)
  128   
  129         env[:downloader] = dwnloader
  130         subject.call(env)
  131 
  132         expect(env[:downloader].headers).to eq([other_header, auth_header])
  133       end
  134     end
  135 
  136     it "adds a token to the headers" do
  137       token = "foobarbaz"
  138       VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)
  139       env[:downloader] = dwnloader
  140       subject.call(env)
  141       expect(env[:downloader].headers).to eq(["Authorization: Bearer #{token}"])
  142     end
  143 
  144     it "does not append the access token to vagrantcloud.com URLs if Atlas" do
  145       server_url = "https://atlas.hashicorp.com"
  146       allow(Vagrant).to receive(:server_url).and_return(server_url)
  147       allow(subject).to receive(:sleep)
  148       token = "foobarbaz"
  149       VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)
  150       env[:downloader] = dwnloader
  151       subject.call(env)
  152       expect(env[:downloader].headers.empty?).to eq(true)
  153     end
  154   end
  155 end