buffet: CheckDeviceRegistered works offline

CheckDeviceRegistered previously worked differently if buffet had never
been online vs if it just went offline. Basically it should just care if
the device_reg_info file has a registration configured. Specifically
it shouldn't try to refresh the access_token.

BUG=brillo:785
TEST=FEATURES=test emerge-$BOARD buffet

Change-Id: I7da25f7e9507a131a37fd16efeb2e8e7352985b8
Reviewed-on: https://chromium-review.googlesource.com/264792
Reviewed-by: Nathan Bullock <nathanbullock@google.com>
Tested-by: Nathan Bullock <nathanbullock@google.com>
Commit-Queue: Nathan Bullock <nathanbullock@google.com>
diff --git a/buffet/device_registration_info.h b/buffet/device_registration_info.h
index f153cda..1e63539 100644
--- a/buffet/device_registration_info.h
+++ b/buffet/device_registration_info.h
@@ -116,9 +116,8 @@
   // Loads the device registration information from cache.
   bool Load();
 
-  // Checks for the valid device registration as well as refreshes
-  // the device access token, if available.
-  bool CheckRegistration(chromeos::ErrorPtr* error);
+  // Checks whether we have credentials generated during registration.
+  bool HaveRegistrationCredentials(chromeos::ErrorPtr* error);
 
   // Gets the full device description JSON object, or nullptr if
   // the device is not registered or communication failure.
@@ -162,10 +161,11 @@
   // Saves the device registration to cache.
   bool Save() const;
 
-  // Checks whether we have credentials generated during registration.
-  bool HaveRegistrationCredentials(chromeos::ErrorPtr* error);
+  // Checks for the valid device registration as well as refreshes
+  // the device access token, if available.
+  bool CheckRegistration(chromeos::ErrorPtr* error);
 
-  // If we currently have an access token and it doesn't like like it
+  // If we currently have an access token and it doesn't look like it
   // has expired yet, returns true immediately. Otherwise calls
   // RefreshAccessToken().
   bool MaybeRefreshAccessToken(chromeos::ErrorPtr* error);
diff --git a/buffet/device_registration_info_unittest.cc b/buffet/device_registration_info_unittest.cc
index 7e09cfd..4d1279b 100644
--- a/buffet/device_registration_info_unittest.cc
+++ b/buffet/device_registration_info_unittest.cc
@@ -170,10 +170,16 @@
   static bool Save(DeviceRegistrationInfo* info) {
     return info->Save();
   }
+
   static void PublishCommands(DeviceRegistrationInfo* info,
                               const base::ListValue& commands) {
     return info->PublishCommands(commands);
   }
+
+  static bool CheckRegistration(DeviceRegistrationInfo* info,
+                                chromeos::ErrorPtr* error) {
+    return info->CheckRegistration(error);
+  }
 };
 
 class DeviceRegistrationInfoTest : public ::testing::Test {
@@ -274,7 +280,8 @@
 
 TEST_F(DeviceRegistrationInfoTest, CheckRegistration) {
   EXPECT_TRUE(dev_reg_->Load());
-  EXPECT_FALSE(dev_reg_->CheckRegistration(nullptr));
+  EXPECT_FALSE(DeviceRegistrationInfo::TestHelper::CheckRegistration(
+      dev_reg_.get(), nullptr));
   EXPECT_EQ(0, transport_->GetRequestCount());
 
   SetDefaultDeviceRegistration(&data_);
@@ -285,7 +292,8 @@
                          chromeos::http::request_type::kPost,
                          base::Bind(OAuth2Handler));
   transport_->ResetRequestCount();
-  EXPECT_TRUE(dev_reg_->CheckRegistration(nullptr));
+  EXPECT_TRUE(DeviceRegistrationInfo::TestHelper::CheckRegistration(
+      dev_reg_.get(), nullptr));
   EXPECT_EQ(1, transport_->GetRequestCount());
 }
 
@@ -300,7 +308,8 @@
                          base::Bind(OAuth2HandlerFail));
   transport_->ResetRequestCount();
   chromeos::ErrorPtr error;
-  EXPECT_FALSE(dev_reg_->CheckRegistration(&error));
+  EXPECT_FALSE(DeviceRegistrationInfo::TestHelper::CheckRegistration(
+      dev_reg_.get(), &error));
   EXPECT_EQ(1, transport_->GetRequestCount());
   EXPECT_TRUE(error->HasError(buffet::kErrorDomainOAuth2,
                               "unable_to_authenticate"));
@@ -318,7 +327,8 @@
                          base::Bind(OAuth2HandlerDeregister));
   transport_->ResetRequestCount();
   chromeos::ErrorPtr error;
-  EXPECT_FALSE(dev_reg_->CheckRegistration(&error));
+  EXPECT_FALSE(DeviceRegistrationInfo::TestHelper::CheckRegistration(
+      dev_reg_.get(), &error));
   EXPECT_EQ(1, transport_->GetRequestCount());
   EXPECT_TRUE(error->HasError(buffet::kErrorDomainOAuth2,
                               "invalid_grant"));
diff --git a/buffet/manager.cc b/buffet/manager.cc
index 897c8a4..33b1763 100644
--- a/buffet/manager.cc
+++ b/buffet/manager.cc
@@ -83,7 +83,7 @@
 void Manager::CheckDeviceRegistered(DBusMethodResponse<std::string> response) {
   LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()";
   chromeos::ErrorPtr error;
-  bool registered = device_info_->CheckRegistration(&error);
+  bool registered = device_info_->HaveRegistrationCredentials(&error);
   // If it fails due to any reason other than 'device not registered',
   // treat it as a real error and report it to the caller.
   if (!registered &&