Read the ATLAS_TOKEN in vagrant-login

This commit is contained in:
Seth Vargo 2014-12-10 15:08:43 -08:00
parent 2385305f6f
commit 7d6a6cd263
2 changed files with 76 additions and 40 deletions

View File

@ -58,13 +58,21 @@ module VagrantPlugins
nil nil
end end
# Reads the access token if there is one, or returns nil otherwise. # Reads the access token if there is one. This will first read the
# `ATLAS_TOKEN` environment variable and then fallback to the stored
# access token on disk.
# #
# @return [String] # @return [String]
def token def token
token_path.read if ENV["ATLAS_TOKEN"] && !ENV["ATLAS_TOKEN"].empty?
rescue Errno::ENOENT return ENV["ATLAS_TOKEN"]
return nil end
if token_path.exist?
return token_path.read.strip
end
nil
end end
protected protected

View File

@ -5,49 +5,59 @@ require Vagrant.source_root.join("plugins/commands/login/command")
describe VagrantPlugins::LoginCommand::Client do describe VagrantPlugins::LoginCommand::Client do
include_context "unit" include_context "unit"
def stub_env(key, value = nil)
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[])
.with(key)
.and_return(value)
end
let(:env) { isolated_environment.create_vagrant_env } let(:env) { isolated_environment.create_vagrant_env }
subject { described_class.new(env) } subject { described_class.new(env) }
before do
stub_env("ATLAS_TOKEN", nil)
subject.clear_token
end
describe "#logged_in?" do describe "#logged_in?" do
it "quickly returns false if no token is set" do let(:url) { "#{Vagrant.server_url}/api/v1/authenticate?access_token=#{token}" }
expect(subject).to_not be_logged_in let(:headers) { { "Content-Type" => "application/json" } }
before { allow(subject).to receive(:token).and_return(token) }
context "when there is no token" do
let(:token) { nil }
it "returns false" do
expect(subject.logged_in?).to be(false)
end
end end
it "returns true if the endpoint returns 200" do context "when there is a token" do
subject.store_token("foo") let(:token) { "ABCD1234" }
response = { it "returns true if the endpoint returns a 200" do
"token" => "baz", stub_request(:get, url)
} .with(headers: headers)
.to_return(body: JSON.pretty_generate("token" => token))
expect(subject.logged_in?).to be(true)
end
headers = { "Content-Type" => "application/json" } it "returns false if the endpoint returns a non-200" do
url = "#{Vagrant.server_url}/api/v1/authenticate?access_token=foo" stub_request(:get, url)
stub_request(:get, url). .with(headers: headers)
with(headers: headers). .to_return(body: JSON.pretty_generate("bad" => true), status: 401)
to_return(status: 200, body: JSON.dump(response)) expect(subject.logged_in?).to be(false)
end
expect(subject).to be_logged_in it "raises an exception if the server cannot be found" do
end stub_request(:get, url)
.to_raise(SocketError)
it "returns false if 401 is returned" do expect { subject.logged_in? }
subject.store_token("foo") .to raise_error(VagrantPlugins::LoginCommand::Errors::ServerUnreachable)
end
url = "#{Vagrant.server_url}/api/v1/authenticate?access_token=foo"
stub_request(:get, url).
to_return(status: 401, body: "")
expect(subject).to_not be_logged_in
end
it "raises an exception if it can't reach the sever" do
subject.store_token("foo")
url = "#{Vagrant.server_url}/api/v1/authenticate?access_token=foo"
stub_request(:get, url).to_raise(SocketError)
expect { subject.logged_in? }.
to raise_error(VagrantPlugins::LoginCommand::Errors::ServerUnreachable)
end end
end end
@ -89,11 +99,29 @@ describe VagrantPlugins::LoginCommand::Client do
end end
end end
describe "#token, #store_token, #clear_token" do describe "#token" do
it "returns nil if there is no token" do it "reads ATLAS_TOKEN" do
expect(subject.token).to be_nil stub_env("ATLAS_TOKEN", "ABCD1234")
expect(subject.token).to eq("ABCD1234")
end end
it "reads the stored file" do
subject.store_token("EFGH5678")
expect(subject.token).to eq("EFGH5678")
end
it "prefers the environment variable" do
stub_env("ATLAS_TOKEN", "ABCD1234")
subject.store_token("EFGH5678")
expect(subject.token).to eq("ABCD1234")
end
it "returns nil if there's no token set" do
expect(subject.token).to be(nil)
end
end
describe "#store_token, #clear_token" do
it "stores the token and can re-access it" do it "stores the token and can re-access it" do
subject.store_token("foo") subject.store_token("foo")
expect(subject.token).to eq("foo") expect(subject.token).to eq("foo")