| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher_imp
l.h" | 5 #include "components/cryptauth/cryptauth_access_token_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "google_apis/gaia/fake_oauth2_token_service.h" | 11 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace proximity_auth { | 14 namespace cryptauth { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 const char kAccountId[] = "account_id"; | 18 const char kAccountId[] = "account_id"; |
| 19 const char kAccessToken[] = "access_token"; | 19 const char kAccessToken[] = "access_token"; |
| 20 const char kInvalidResult[] = "invalid_result"; | 20 const char kInvalidResult[] = "invalid_result"; |
| 21 | 21 |
| 22 // Callback that saves the fetched access token to the first argument. | 22 // Callback that saves the fetched access token to the first argument. |
| 23 void SaveAccessToken(std::string* out_token, const std::string& in_token) { | 23 void SaveAccessToken(std::string* out_token, const std::string& in_token) { |
| 24 *out_token = in_token; | 24 *out_token = in_token; |
| 25 } | 25 } |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 class ProximityAuthCryptAuthAccessTokenFetcherTest : public testing::Test { | 29 class CryptAuthAccessTokenFetcherTest : public testing::Test { |
| 30 protected: | 30 protected: |
| 31 ProximityAuthCryptAuthAccessTokenFetcherTest() | 31 CryptAuthAccessTokenFetcherTest() |
| 32 : fetcher_(&token_service_, kAccountId) { | 32 : fetcher_(&token_service_, kAccountId) { |
| 33 token_service_.AddAccount(kAccountId); | 33 token_service_.AddAccount(kAccountId); |
| 34 } | 34 } |
| 35 | 35 |
| 36 FakeOAuth2TokenService token_service_; | 36 FakeOAuth2TokenService token_service_; |
| 37 CryptAuthAccessTokenFetcherImpl fetcher_; | 37 CryptAuthAccessTokenFetcherImpl fetcher_; |
| 38 | 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthAccessTokenFetcherTest); | 39 DISALLOW_COPY_AND_ASSIGN(CryptAuthAccessTokenFetcherTest); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 TEST_F(ProximityAuthCryptAuthAccessTokenFetcherTest, FetchSuccess) { | 42 TEST_F(CryptAuthAccessTokenFetcherTest, FetchSuccess) { |
| 43 std::string result; | 43 std::string result; |
| 44 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result)); | 44 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result)); |
| 45 token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken, | 45 token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken, |
| 46 base::Time::Max()); | 46 base::Time::Max()); |
| 47 | 47 |
| 48 EXPECT_EQ(kAccessToken, result); | 48 EXPECT_EQ(kAccessToken, result); |
| 49 } | 49 } |
| 50 | 50 |
| 51 TEST_F(ProximityAuthCryptAuthAccessTokenFetcherTest, FetchFailure) { | 51 TEST_F(CryptAuthAccessTokenFetcherTest, FetchFailure) { |
| 52 std::string result(kInvalidResult); | 52 std::string result(kInvalidResult); |
| 53 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result)); | 53 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result)); |
| 54 token_service_.IssueErrorForAllPendingRequestsForAccount( | 54 token_service_.IssueErrorForAllPendingRequestsForAccount( |
| 55 kAccountId, | 55 kAccountId, |
| 56 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR)); | 56 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR)); |
| 57 | 57 |
| 58 EXPECT_EQ(std::string(), result); | 58 EXPECT_EQ(std::string(), result); |
| 59 } | 59 } |
| 60 | 60 |
| 61 TEST_F(ProximityAuthCryptAuthAccessTokenFetcherTest, FetcherReuse) { | 61 TEST_F(CryptAuthAccessTokenFetcherTest, FetcherReuse) { |
| 62 std::string result1; | 62 std::string result1; |
| 63 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result1)); | 63 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result1)); |
| 64 | 64 |
| 65 { | 65 { |
| 66 std::string result2(kInvalidResult); | 66 std::string result2(kInvalidResult); |
| 67 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result2)); | 67 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result2)); |
| 68 EXPECT_EQ(std::string(), result2); | 68 EXPECT_EQ(std::string(), result2); |
| 69 } | 69 } |
| 70 | 70 |
| 71 token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken, | 71 token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken, |
| 72 base::Time::Max()); | 72 base::Time::Max()); |
| 73 EXPECT_EQ(kAccessToken, result1); | 73 EXPECT_EQ(kAccessToken, result1); |
| 74 } | 74 } |
| 75 | 75 |
| 76 } // namespace proximity_auth | 76 } // namespace cryptauth |
| OLD | NEW |