diff --git a/plugins/commands/login/command.rb b/plugins/commands/login/command.rb index ce0dd7fc9..203b09009 100644 --- a/plugins/commands/login/command.rb +++ b/plugins/commands/login/command.rb @@ -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 diff --git a/plugins/commands/login/locales/en.yml b/plugins/commands/login/locales/en.yml index 74b7ee1cd..dba4d52d7 100644 --- a/plugins/commands/login/locales/en.yml +++ b/plugins/commands/login/locales/en.yml @@ -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: |- diff --git a/test/unit/plugins/commands/login/command_test.rb b/test/unit/plugins/commands/login/command_test.rb index 2d634c4ca..c92c51174 100644 --- a/test/unit/plugins/commands/login/command_test.rb +++ b/test/unit/plugins/commands/login/command_test.rb @@ -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