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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
require File.expand_path("../../../../base", __FILE__)
require Vagrant.source_root.join("plugins/commands/cloud/client/client")
describe VagrantPlugins::CloudCommand::Client do
include_context "unit"
let(:env) { isolated_environment.create_vagrant_env }
let(:token) { nil }
let(:vc_client) { double("vagrantcloud-client", access_token: token) }
subject(:client) { described_class.new(env) }
before(:all) do
I18n.load_path << Vagrant.source_root.join("plugins/commands/cloud/locales/en.yml")
I18n.reload!
end
before do
stub_env("ATLAS_TOKEN" => nil)
stub_env("VAGRANT_CLOUD_TOKEN" => nil)
allow(VagrantCloud::Client).to receive(:new).and_return(vc_client)
allow(Vagrant::Util::CredentialScrubber).to receive(:sensitive)
end
after do
described_class.reset!
Vagrant::Util::CredentialScrubber.reset!
end
describe "#logged_in?" do
before { allow(subject).to receive(:token).and_return(token) }
context "when token is not set" do
it "should return false" do
expect(subject.logged_in?).to be_falsey
end
end
context "when token is set" do
let(:token) { double("token") }
before do
allow(vc_client).to receive(:authentication_token_validate)
end
it "should return true when token is valid" do
expect(subject.logged_in?).to be_truthy
end
it "should validate the set token" do
expect(vc_client).to receive(:authentication_token_validate)
subject.logged_in?
end
it "should return false when token does not validate" do
expect(vc_client).to receive(:authentication_token_validate).
and_raise(Excon::Error::Unauthorized.new(StandardError.new))
expect(subject.logged_in?).to be_falsey
end
it "should add token to scrubber" do
expect(Vagrant::Util::CredentialScrubber).to receive(:sensitive).with(token)
subject.logged_in?
end
end
end
describe "#login" do
let(:new_token) { double("new-token") }
let(:result) { {token: new_token} }
let(:password) { double("password") }
let(:username) { double("username") }
before do
subject.username_or_email = username
subject.password = password
allow(vc_client).to receive(:authentication_token_create).
and_return(result)
end
it "should add password to scrubber" do
expect(Vagrant::Util::CredentialScrubber).to receive(:sensitive).with(password)
subject.login
end
it "should create an authentication token" do
expect(vc_client).to receive(:authentication_token_create).
and_return(result)
subject.login
end
it "should wrap remote request to handle errors" do
expect(subject).to receive(:with_error_handling)
subject.login
end
it "should add new token to scrubber" do
expect(Vagrant::Util::CredentialScrubber).to receive(:sensitive).with(new_token)
subject.login
end
it "should create a new internal client" do
expect(VagrantCloud::Client).to receive(:new).with(access_token: new_token, url_base: anything)
subject.login
end
it "should create authentication token using username and password" do
expect(vc_client).to receive(:authentication_token_create).
with(hash_including(username: username, password: password)).and_return(result)
subject.login
end
it "should return the new token" do
expect(subject.login).to eq(new_token)
end
context "with description and code" do
let(:description) { double("description") }
let(:code) { double("code") }
it "should create authentication token using description and code" do
expect(vc_client).to receive(:authentication_token_create).with(
hash_including(username: username, password: password,
description: description, code: code))
subject.login(description: description, code: code)
end
end
end
describe "#request_code" do
let(:password) { double("password") }
let(:username) { double("username") }
let(:delivery_method) { double("delivery-method", upcase: nil) }
let(:result) { {two_factor: two_factor} }
let(:two_factor) { {obfuscated_destination: obfuscated_destination} }
let(:obfuscated_destination) { double("obfuscated-destination", to_s: "2FA_DESTINATION") }
before do
subject.password = password
subject.username_or_email = username
allow(vc_client).to receive(:authentication_request_2fa_code).and_return(result)
end
it "should add password to scrubber" do
expect(Vagrant::Util::CredentialScrubber).to receive(:sensitive).with(password)
subject.request_code(delivery_method)
end
it "should request the code" do
expect(vc_client).to receive(:authentication_request_2fa_code).with(
hash_including(username: username, password: password, delivery_method: delivery_method))
subject.request_code(delivery_method)
end
it "should print the destination" do
expect(env.ui).to receive(:success).with(/2FA_DESTINATION/)
subject.request_code(delivery_method)
end
end
describe "#store_token" do
let(:token_path) { double("token-path") }
let(:new_token) { double("new-token") }
before do
allow(subject).to receive(:token_path).and_return(token_path)
allow(token_path).to receive(:open)
end
it "should add token to scrubber" do
expect(Vagrant::Util::CredentialScrubber).to receive(:sensitive).with(new_token)
subject.store_token(new_token)
end
it "should create a new internal client with token" do
expect(VagrantCloud::Client).to receive(:new).with(access_token: new_token, url_base: anything)
subject.store_token(new_token)
end
it "should open the token path and write the new token" do
f = double("file")
expect(token_path).to receive(:open).with("w").and_yield(f)
expect(f).to receive(:write).with(new_token)
subject.store_token(new_token)
end
end
describe "#token" do
let(:env_token) { "ENV_TOKEN" }
let(:file_token) { "FILE_TOKEN" }
let(:token_path) { double("token-path", read: file_token) }
let(:path_exists) { false }
before do
allow(subject).to receive(:token).and_call_original
allow(subject).to receive(:token_path).and_return(token_path)
allow(token_path).to receive(:exist?).and_return(path_exists)
end
context "when VAGRANT_CLOUD_TOKEN env var is set" do
before { stub_env("VAGRANT_CLOUD_TOKEN" => env_token) }
it "should return the env token" do
expect(subject.token).to eq(env_token)
end
context "when token path exists" do
let(:path_exists) { true }
it "should return the env token" do
expect(subject.token).to eq(env_token)
end
it "should print warning of two tokens" do
expect(env.ui).to receive(:warn)
subject.token
end
it "should only print warning of two tokens once" do
expect(env.ui).to receive(:warn).with(/detected/).once
3.times { subject.token }
end
end
end
context "when token path exists" do
let(:path_exists) { true }
it "should return the stored token" do
expect(subject.token).to eq(file_token)
end
context "when VAGRANT_CLOUD_TOKEN env var is set" do
before { stub_env("VAGRANT_CLOUD_TOKEN" => env_token) }
it "should return the env token" do
expect(subject.token).to eq(env_token)
end
end
end
context "when ATLAS_TOKEN env var is set" do
before { stub_env("ATLAS_TOKEN" => env_token) }
it "should return the env token" do
expect(subject.token).to eq(env_token)
end
context "when VAGRANT_CLOUD_TOKEN is set" do
let(:vc_token) { "VC_TOKEN" }
before { stub_env("VAGRANT_CLOUD_TOKEN" => vc_token) }
it "should return the VAGRANT_CLOUD_TOKEN value" do
expect(subject.token).to eq(vc_token)
end
end
context "when file exists" do
let(:path_exists) { true }
it "should return the file token" do
expect(subject.token).to eq(file_token)
end
end
end
end
end
|