Skip to content

Commit

Permalink
FIXUP: added test
Browse files Browse the repository at this point in the history
  • Loading branch information
bsclifton committed Oct 31, 2024
1 parent 29c91ed commit c61a963
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
56 changes: 30 additions & 26 deletions components/brave_vpn/browser/brave_vpn_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,36 @@ void BraveVpnService::UpdatePurchasedStateForSessionExpired(
return;
}

// If expiry is in the future, the person ran out of credentials.
// This should only happen if communication bewteen client and VPN provider
// is lost after the credential is redeemed (multiple times).
//
// It's safe to check this first because kBraveVPNLastCredentialExpiry is only
// set after getting a valid credential. If the session is expired, this value
// might be set but would be in the past.
const auto last_credential_expiry =
local_prefs_->GetTime(prefs::kBraveVPNLastCredentialExpiry);
if (!last_credential_expiry.is_null() &&
last_credential_expiry > base::Time::Now()) {
std::string expiry_message;
base::TimeDelta delta = (last_credential_expiry - base::Time::Now());
if (delta.InHours() == 0) {
expiry_message = base::StringPrintf(
"Out of credentials; check again in %d minutes.", delta.InMinutes());
} else {
int delta_hours = delta.InHours();
base::TimeDelta delta_minutes = (delta - base::Hours(delta_hours));
expiry_message = base::StringPrintf(
"Out of credentials; check again in %d hours %d minutes.",
delta_hours, delta_minutes.InMinutes());
}
LOG(ERROR) << __func__ << " : " << expiry_message;
SetPurchasedState(env, PurchasedState::OUT_OF_CREDENTIALS,
l10n_util::GetStringUTF8(
IDS_BRAVE_VPN_MAIN_PANEL_OUT_OF_CREDENTIALS_CONTENT));
return;
}

const auto session_expired_time =
local_prefs_->GetTime(prefs::kBraveVPNSessionExpiredDate);
// If it's not cached, it means this session expiration is first time since
Expand Down Expand Up @@ -386,32 +416,6 @@ void BraveVpnService::UpdatePurchasedStateForSessionExpired(
return;
}

// If expiry is in the future, person ran out of credentials.
// This should only happen if communication bewteen client and VPN provider
// is lost after the credential is redeemed (multiple times).
const auto last_credential_expiry =
local_prefs_->GetTime(prefs::kBraveVPNLastCredentialExpiry);
if (!last_credential_expiry.is_null() &&
last_credential_expiry > base::Time::Now()) {
std::string expiry_message;
base::TimeDelta delta = (last_credential_expiry - base::Time::Now());
if (delta.InHours() == 0) {
expiry_message = base::StringPrintf(
"Out of credentials; check again in %d minutes.", delta.InMinutes());
} else {
int delta_hours = delta.InHours();
base::TimeDelta delta_minutes = (delta - base::Hours(delta_hours));
expiry_message = base::StringPrintf(
"Out of credentials; check again in %d hours %d minutes.",
delta_hours, delta_minutes.InMinutes());
}
LOG(ERROR) << __func__ << " : " << expiry_message;
SetPurchasedState(env, PurchasedState::OUT_OF_CREDENTIALS,
l10n_util::GetStringUTF8(
IDS_BRAVE_VPN_MAIN_PANEL_OUT_OF_CREDENTIALS_CONTENT));
return;
}

// Expiry is in the past - they ran out of credentials completely.
// They'll need to login to account.brave.com again.
SetPurchasedState(env, PurchasedState::SESSION_EXPIRED);
Expand Down
17 changes: 17 additions & 0 deletions components/brave_vpn/browser/brave_vpn_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,23 @@ TEST_F(BraveVPNServiceTest, LoadPurchasedStateSessionExpiredTest) {
EXPECT_TRUE(session_expired_time.is_null());
}

TEST_F(BraveVPNServiceTest, LoadPurchasedStateOutOfCredentialsTest) {
std::string env = skus::GetDefaultEnvironment();
std::string domain = skus::GetDomain("vpn", env);

// Set an expiry in the future - this would be when the last redeemed
// credential (whether it was successful or not) expires.
local_pref_service_.SetTime(prefs::kBraveVPNLastCredentialExpiry,
base::Time::Now() + base::Days(1));

SetPurchasedState(env, PurchasedState::LOADING);
OnFetchRegionList(GetRegionsData(), true);
OnCredentialSummary(
domain, R"({ "active": true, "remaining_credential_count": 0 } )");
EXPECT_EQ(PurchasedState::OUT_OF_CREDENTIALS, GetPurchasedInfoSync());
}


TEST_F(BraveVPNServiceTest, LoadPurchasedStateTest) {
std::string env = skus::GetDefaultEnvironment();
std::string domain = skus::GetDomain("vpn", env);
Expand Down

0 comments on commit c61a963

Please sign in to comment.