Chromium Code Reviews| Index: components/proximity_auth/cryptauth/cryptauth_account_token_fetcher_unittest.cc |
| diff --git a/components/proximity_auth/cryptauth/cryptauth_account_token_fetcher_unittest.cc b/components/proximity_auth/cryptauth/cryptauth_account_token_fetcher_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..16e3dd1003796462da0306fd2db46bd1f6ba8b29 |
| --- /dev/null |
| +++ b/components/proximity_auth/cryptauth/cryptauth_account_token_fetcher_unittest.cc |
| @@ -0,0 +1,77 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/proximity_auth/cryptauth/cryptauth_account_token_fetcher.h" |
| + |
| +#include "base/bind.h" |
|
Ilya Sherman
2014/12/11 02:53:21
nit: If you keep the scoped_ptr, please #include t
Tim Song
2014/12/12 20:48:45
Done.
|
| +#include "google_apis/gaia/fake_oauth2_token_service.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace proximity_auth { |
| + |
| +namespace { |
| + |
| +const char kAccountId[] = "account_id"; |
| +const char kAccessToken[] = "access_token"; |
| +const char kInvalidResult[] = "invalid_result"; |
| + |
| +// Callback that saves the fetched access token to the first argument. |
| +void SaveAccessToken(std::string* out_token, const std::string& in_token) { |
|
Ilya Sherman
2014/12/11 02:53:21
nit: Please #include the header for std::string.
Tim Song
2014/12/12 20:48:45
Done.
|
| + *out_token = in_token; |
| +} |
| + |
| +} // namespace |
| + |
| +class ProximityAuthCryptAuthAccountTokenFetcherTest : public testing::Test { |
| + protected: |
| + ProximityAuthCryptAuthAccountTokenFetcherTest() {} |
| + virtual ~ProximityAuthCryptAuthAccountTokenFetcherTest() {} |
| + |
| + void SetUp() override { |
| + token_service_.AddAccount(kAccountId); |
| + fetcher_.reset( |
| + new CryptAuthAccountTokenFetcher(&token_service_, kAccountId)); |
|
Ilya Sherman
2014/12/11 02:53:22
Can this be done in the constructor, rather than i
Tim Song
2014/12/12 20:48:44
Done. From a simulation perspective, the token ser
|
| + } |
| + |
| + FakeOAuth2TokenService token_service_; |
| + scoped_ptr<CryptAuthAccessTokenFetcher> fetcher_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthAccountTokenFetcherTest); |
| +}; |
| + |
| +TEST_F(ProximityAuthCryptAuthAccountTokenFetcherTest, FetchSuccess) { |
| + std::string result; |
| + fetcher_->FetchAccessToken(base::Bind(SaveAccessToken, &result)); |
| + token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken, |
| + base::Time::Max()); |
| + |
| + EXPECT_EQ(kAccessToken, result); |
| +} |
| + |
| +TEST_F(ProximityAuthCryptAuthAccountTokenFetcherTest, FetchFailure) { |
| + std::string result(kInvalidResult); |
| + fetcher_->FetchAccessToken(base::Bind(SaveAccessToken, &result)); |
| + token_service_.IssueErrorForAllPendingRequestsForAccount( |
| + kAccountId, |
| + GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR)); |
| + |
| + EXPECT_EQ(std::string(), result); |
| +} |
| + |
| +TEST_F(ProximityAuthCryptAuthAccountTokenFetcherTest, FetcherReuse) { |
| + std::string result; |
| + fetcher_->FetchAccessToken(base::Bind(SaveAccessToken, &result)); |
| + |
| + { |
| + std::string result(kInvalidResult); |
|
Ilya Sherman
2014/12/11 02:53:21
I think there's actually a clang warning being add
Tim Song
2014/12/12 20:48:45
Done. However, I kind of like scoping the same var
|
| + fetcher_->FetchAccessToken(base::Bind(SaveAccessToken, &result)); |
| + EXPECT_EQ(std::string(), result); |
| + } |
| + |
| + token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken, |
| + base::Time::Max()); |
| + EXPECT_EQ(kAccessToken, result); |
| +} |
| + |
| +} // namespace proximity_auth |