File: add_authentication_test.rb

package info (click to toggle)
vagrant 2.2.14%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,800 kB
  • sloc: ruby: 97,301; sh: 375; makefile: 16; lisp: 1
file content (169 lines) | stat: -rw-r--r-- 5,375 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require File.expand_path("../../../../../../base", __FILE__)

require Vagrant.source_root.join("plugins/commands/cloud/auth/middleware/add_authentication")

describe VagrantPlugins::CloudCommand::AddAuthentication do
  include_context "unit"

  let(:app) { lambda { |env| } }
  let(:ui) { double("ui") }
  let(:env) { {
    env: iso_env,
    ui: ui
  } }

  let(:iso_env) { isolated_environment.create_vagrant_env }
  let(:server_url) { "http://vagrantcloud.com" }

  subject { described_class.new(app, env) }

  before do
    allow(Vagrant).to receive(:server_url).and_return(server_url)
    allow(ui).to receive(:warn)
    stub_env("ATLAS_TOKEN" => nil)
  end

  describe "#call" do
    it "does nothing if we have no server set" do
      allow(Vagrant).to receive(:server_url).and_return(nil)
      VagrantPlugins::CloudCommand::Client.new(iso_env).store_token("foo")

      original = ["foo", "#{server_url}/bar"]
      env[:box_urls] = original.dup

      subject.call(env)

      expect(env[:box_urls]).to eq(original)
    end

    it "does nothing if we aren't logged in" do
      original = ["foo", "#{server_url}/bar"]
      env[:box_urls] = original.dup

      subject.call(env)

      expect(env[:box_urls]).to eq(original)
    end

    it "appends the access token to the URL of server URLs" do
      token = "foobarbaz"
      VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)

      original = [
        "http://example.com/box.box",
        "#{server_url}/foo.box",
        "#{server_url}/bar.box?arg=true",
      ]

      expected = original.dup
      expected[1] = "#{original[1]}?access_token=#{token}"
      expected[2] = "#{original[2]}&access_token=#{token}"

      env[:box_urls] = original.dup
      subject.call(env)

      expect(env[:box_urls]).to eq(expected)
    end

    it "does not append the access token to vagrantcloud.com URLs if Atlas" do
      server_url = "https://atlas.hashicorp.com"
      allow(Vagrant).to receive(:server_url).and_return(server_url)
      allow(subject).to receive(:sleep)

      token = "foobarbaz"
      VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)

      original = [
        "http://example.com/box.box",
        "http://vagrantcloud.com/foo.box",
        "http://vagrantcloud.com/bar.box?arg=true",
      ]

      expected = original.dup

      env[:box_urls] = original.dup
      subject.call(env)

      expect(env[:box_urls]).to eq(expected)
    end

    it "warns when adding token to custom server" do
      server_url = "https://example.com"
      allow(Vagrant).to receive(:server_url).and_return(server_url)

      token = "foobarbaz"
      VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)

      original = [
        "http://example.org/box.box",
        "http://vagrantcloud.com/foo.box",
        "http://example.com/bar.box",
        "http://example.com/foo.box"
      ]

      expected = original.dup
      expected[2] = expected[2] + "?access_token=#{token}"
      expected[3] = expected[3] + "?access_token=#{token}"

      expect(subject).to receive(:sleep).once
      expect(ui).to receive(:warn).once

      env[:box_urls] = original.dup
      subject.call(env)

      expect(env[:box_urls]).to eq(expected)
    end

    it "modifies host URL to target if authorized host" do
      originals = VagrantPlugins::CloudCommand::AddAuthentication::
        REPLACEMENT_HOSTS.map{ |h| "http://#{h}/box.box" }
      expected = "http://#{VagrantPlugins::CloudCommand::AddAuthentication::TARGET_HOST}/box.box"
      env[:box_urls] = originals
      subject.call(env)
      env[:box_urls].each do |url|
        expect(url).to eq(expected)
      end
    end

    it "ignores urls that it cannot parse" do
      bad_url = "this is not a valid url"
      # Ensure the bad URL does cause an exception
      expect{ URI.parse(bad_url) }.to raise_error URI::Error
      env[:box_urls] = [bad_url]
      subject.call(env)
      expect(env[:box_urls].first).to eq(bad_url)
    end

    it "returns original urls when not modified" do
      to_persist = "file:////path/to/box.box"
      to_change = VagrantPlugins::CloudCommand::AddAuthentication::
        REPLACEMENT_HOSTS.map{ |h| "http://#{h}/box.box" }.first
      expected = "http://#{VagrantPlugins::CloudCommand::AddAuthentication::TARGET_HOST}/box.box"
      env[:box_urls] = [to_persist, to_change]
      subject.call(env)
      check_persist, check_change = env[:box_urls]
      expect(check_change).to eq(expected)
      expect(check_persist).to eq(to_persist)
      # NOTE: The behavior of URI.parse changes on Ruby 2.5 to produce
      # the same string value. To make the test worthwhile in checking
      # for the same value, check that the object IDs are still the same.
      expect(check_persist.object_id).to eq(to_persist.object_id)
    end

    it "does not append multiple access_tokens" do
      token = "foobarbaz"
      VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token)

      original = [
        "#{server_url}/foo.box?access_token=existing",
        "#{server_url}/bar.box?arg=true",
      ]

      env[:box_urls] = original.dup
      subject.call(env)

      expect(env[:box_urls][0]).to eq("#{server_url}/foo.box?access_token=existing")
      expect(env[:box_urls][1]).to eq("#{server_url}/bar.box?arg=true&access_token=foobarbaz")
    end
  end
end