"Fossies" - the Fresh Open Source Software Archive

Member "vagrant-2.2.14/test/unit/plugins/commands/cloud/auth/middleware/add_authentication_test.rb" (20 Nov 2020, 5375 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. See also the last Fossies "Diffs" side-by-side code changes report for "add_authentication_test.rb": 2.2.9_vs_2.2.10.

    1 require File.expand_path("../../../../../../base", __FILE__)
    2 
    3 require Vagrant.source_root.join("plugins/commands/cloud/auth/middleware/add_authentication")
    4 
    5 describe VagrantPlugins::CloudCommand::AddAuthentication do
    6   include_context "unit"
    7 
    8   let(:app) { lambda { |env| } }
    9   let(:ui) { double("ui") }
   10   let(:env) { {
   11     env: iso_env,
   12     ui: ui
   13   } }
   14 
   15   let(:iso_env) { isolated_environment.create_vagrant_env }
   16   let(:server_url) { "http://vagrantcloud.com" }
   17 
   18   subject { described_class.new(app, env) }
   19 
   20   before do
   21     allow(Vagrant).to receive(:server_url).and_return(server_url)
   22     allow(ui).to receive(:warn)
   23     stub_env("ATLAS_TOKEN" => nil)
   24   end
   25 
   26   describe "#call" do
   27     it "does nothing if we have no server set" do
   28       allow(Vagrant).to receive(:server_url).and_return(nil)
   29       VagrantPlugins::CloudCommand::Client.new(iso_env).store_token("foo")
   30 
   31       original = ["foo", "#{server_url}/bar"]
   32       env[:box_urls] = original.dup
   33 
   34       subject.call(env)
   35 
   36       expect(env[:box_urls]).to eq(original)
   37     end
   38 
   39     it "does nothing if we aren't logged in" do
   40       original = ["foo", "#{server_url}/bar"]
   41       env[:box_urls] = original.dup
   42 
   43       subject.call(env)
   44 
   45       expect(env[:box_urls]).to eq(original)
   46     end
   47 
   48     it "appends the access token to the URL of server URLs" do
   49       token = "foobarbaz"
   50       VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)
   51 
   52       original = [
   53         "http://example.com/box.box",
   54         "#{server_url}/foo.box",
   55         "#{server_url}/bar.box?arg=true",
   56       ]
   57 
   58       expected = original.dup
   59       expected[1] = "#{original[1]}?access_token=#{token}"
   60       expected[2] = "#{original[2]}&access_token=#{token}"
   61 
   62       env[:box_urls] = original.dup
   63       subject.call(env)
   64 
   65       expect(env[:box_urls]).to eq(expected)
   66     end
   67 
   68     it "does not append the access token to vagrantcloud.com URLs if Atlas" do
   69       server_url = "https://atlas.hashicorp.com"
   70       allow(Vagrant).to receive(:server_url).and_return(server_url)
   71       allow(subject).to receive(:sleep)
   72 
   73       token = "foobarbaz"
   74       VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)
   75 
   76       original = [
   77         "http://example.com/box.box",
   78         "http://vagrantcloud.com/foo.box",
   79         "http://vagrantcloud.com/bar.box?arg=true",
   80       ]
   81 
   82       expected = original.dup
   83 
   84       env[:box_urls] = original.dup
   85       subject.call(env)
   86 
   87       expect(env[:box_urls]).to eq(expected)
   88     end
   89 
   90     it "warns when adding token to custom server" do
   91       server_url = "https://example.com"
   92       allow(Vagrant).to receive(:server_url).and_return(server_url)
   93 
   94       token = "foobarbaz"
   95       VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)
   96 
   97       original = [
   98         "http://example.org/box.box",
   99         "http://vagrantcloud.com/foo.box",
  100         "http://example.com/bar.box",
  101         "http://example.com/foo.box"
  102       ]
  103 
  104       expected = original.dup
  105       expected[2] = expected[2] + "?access_token=#{token}"
  106       expected[3] = expected[3] + "?access_token=#{token}"
  107 
  108       expect(subject).to receive(:sleep).once
  109       expect(ui).to receive(:warn).once
  110 
  111       env[:box_urls] = original.dup
  112       subject.call(env)
  113 
  114       expect(env[:box_urls]).to eq(expected)
  115     end
  116 
  117     it "modifies host URL to target if authorized host" do
  118       originals = VagrantPlugins::CloudCommand::AddAuthentication::
  119         REPLACEMENT_HOSTS.map{ |h| "http://#{h}/box.box" }
  120       expected = "http://#{VagrantPlugins::CloudCommand::AddAuthentication::TARGET_HOST}/box.box"
  121       env[:box_urls] = originals
  122       subject.call(env)
  123       env[:box_urls].each do |url|
  124         expect(url).to eq(expected)
  125       end
  126     end
  127 
  128     it "ignores urls that it cannot parse" do
  129       bad_url = "this is not a valid url"
  130       # Ensure the bad URL does cause an exception
  131       expect{ URI.parse(bad_url) }.to raise_error URI::Error
  132       env[:box_urls] = [bad_url]
  133       subject.call(env)
  134       expect(env[:box_urls].first).to eq(bad_url)
  135     end
  136 
  137     it "returns original urls when not modified" do
  138       to_persist = "file:////path/to/box.box"
  139       to_change = VagrantPlugins::CloudCommand::AddAuthentication::
  140         REPLACEMENT_HOSTS.map{ |h| "http://#{h}/box.box" }.first
  141       expected = "http://#{VagrantPlugins::CloudCommand::AddAuthentication::TARGET_HOST}/box.box"
  142       env[:box_urls] = [to_persist, to_change]
  143       subject.call(env)
  144       check_persist, check_change = env[:box_urls]
  145       expect(check_change).to eq(expected)
  146       expect(check_persist).to eq(to_persist)
  147       # NOTE: The behavior of URI.parse changes on Ruby 2.5 to produce
  148       # the same string value. To make the test worthwhile in checking
  149       # for the same value, check that the object IDs are still the same.
  150       expect(check_persist.object_id).to eq(to_persist.object_id)
  151     end
  152 
  153     it "does not append multiple access_tokens" do
  154       token = "foobarbaz"
  155       VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)
  156 
  157       original = [
  158         "#{server_url}/foo.box?access_token=existing",
  159         "#{server_url}/bar.box?arg=true",
  160       ]
  161 
  162       env[:box_urls] = original.dup
  163       subject.call(env)
  164 
  165       expect(env[:box_urls][0]).to eq("#{server_url}/foo.box?access_token=existing")
  166       expect(env[:box_urls][1]).to eq("#{server_url}/bar.box?arg=true&access_token=foobarbaz")
  167     end
  168   end
  169 end