Verify the token is valid when saving

This commit is contained in:
Seth Vargo 2015-01-09 13:24:53 -05:00
parent 2ebd076817
commit 114858c7d2
3 changed files with 34 additions and 7 deletions

View File

@ -88,7 +88,14 @@ module VagrantPlugins
def execute_token(token)
@client.store_token(token)
@env.ui.success(I18n.t("login_command.token_saved"))
return 0
if @client.logged_in?
@env.ui.success(I18n.t("login_command.check_logged_in"))
return 0
else
@env.ui.error(I18n.t("login_command.invalid_token"))
return 1
end
end
end
end

View File

@ -24,6 +24,8 @@ en:
https://atlas.hashicorp.com.
invalid_login: |-
Invalid username or password. Please try again.
invalid_token: |-
Invalid token. Please try again.
logged_in: |-
You are now logged in.
logged_out: |-

View File

@ -64,14 +64,32 @@ describe VagrantPlugins::LoginCommand::Command do
context "with --token" do
let(:argv) { ["--token", "efgh5678"] }
it "returns 0" do
expect(subject.execute).to eq(0)
context "when the token is valid" do
before do
stub_request(:get, %r{^#{Vagrant.server_url}/api/v1/authenticate})
.to_return(status: 200)
end
it "sets the token" do
subject.execute
token = File.read(token_path).strip
expect(token).to eq("efgh5678")
end
it "returns 0" do
expect(subject.execute).to eq(0)
end
end
it "sets the token" do
subject.execute
token = File.read(token_path).strip
expect(token).to eq("efgh5678")
context "when the token is invalid" do
before do
stub_request(:get, %r{^#{Vagrant.server_url}/api/v1/authenticate})
.to_return(status: 401)
end
it "returns 1" do
expect(subject.execute).to eq(1)
end
end
end
end