Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(714)

Side by Side Diff: components/proximity_auth/cryptauth/cryptauth_access_token_fetcher_unittest.cc

Issue 1066453002: Refactor CryptAuth component to be more testable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cryptauth_securemessage
Patch Set: moved classes to impl files Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_account_token_fetcher.h"
6
7 #include <string> 5 #include <string>
8 6
9 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher_imp l.h"
Ilya Sherman 2015/04/06 23:48:02 nit: The tested header file is supposed to be at t
Tim Song 2015/04/07 02:22:57 Done.
10 #include "google_apis/gaia/fake_oauth2_token_service.h" 9 #include "google_apis/gaia/fake_oauth2_token_service.h"
11 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
12 11
13 namespace proximity_auth { 12 namespace proximity_auth {
14 13
15 namespace { 14 namespace {
16 15
17 const char kAccountId[] = "account_id"; 16 const char kAccountId[] = "account_id";
18 const char kAccessToken[] = "access_token"; 17 const char kAccessToken[] = "access_token";
19 const char kInvalidResult[] = "invalid_result"; 18 const char kInvalidResult[] = "invalid_result";
20 19
21 // Callback that saves the fetched access token to the first argument. 20 // Callback that saves the fetched access token to the first argument.
22 void SaveAccessToken(std::string* out_token, const std::string& in_token) { 21 void SaveAccessToken(std::string* out_token, const std::string& in_token) {
23 *out_token = in_token; 22 *out_token = in_token;
24 } 23 }
25 24
26 } // namespace 25 } // namespace
27 26
28 class ProximityAuthCryptAuthAccountTokenFetcherTest : public testing::Test { 27 class ProximityAuthCryptAuthAccessTokenFetcherTest : public testing::Test {
29 protected: 28 protected:
30 ProximityAuthCryptAuthAccountTokenFetcherTest() 29 ProximityAuthCryptAuthAccessTokenFetcherTest()
31 : fetcher_(&token_service_, kAccountId) { 30 : fetcher_(
31 new CryptAuthAccessTokenFetcherImpl(&token_service_, kAccountId)) {
32 token_service_.AddAccount(kAccountId); 32 token_service_.AddAccount(kAccountId);
33 } 33 }
34 34
35 FakeOAuth2TokenService token_service_; 35 FakeOAuth2TokenService token_service_;
36 CryptAuthAccountTokenFetcher fetcher_; 36 scoped_ptr<CryptAuthAccessTokenFetcher> fetcher_;
Ilya Sherman 2015/04/06 23:48:02 nit: Does this need to be a scoped_ptr, or can we
Tim Song 2015/04/07 02:22:57 Done.
37 37
38 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthAccountTokenFetcherTest); 38 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthAccessTokenFetcherTest);
39 }; 39 };
40 40
41 TEST_F(ProximityAuthCryptAuthAccountTokenFetcherTest, FetchSuccess) { 41 TEST_F(ProximityAuthCryptAuthAccessTokenFetcherTest, FetchSuccess) {
42 std::string result; 42 std::string result;
43 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result)); 43 fetcher_->FetchAccessToken(base::Bind(SaveAccessToken, &result));
44 token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken, 44 token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken,
45 base::Time::Max()); 45 base::Time::Max());
46 46
47 EXPECT_EQ(kAccessToken, result); 47 EXPECT_EQ(kAccessToken, result);
48 } 48 }
49 49
50 TEST_F(ProximityAuthCryptAuthAccountTokenFetcherTest, FetchFailure) { 50 TEST_F(ProximityAuthCryptAuthAccessTokenFetcherTest, FetchFailure) {
51 std::string result(kInvalidResult); 51 std::string result(kInvalidResult);
52 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result)); 52 fetcher_->FetchAccessToken(base::Bind(SaveAccessToken, &result));
53 token_service_.IssueErrorForAllPendingRequestsForAccount( 53 token_service_.IssueErrorForAllPendingRequestsForAccount(
54 kAccountId, 54 kAccountId,
55 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR)); 55 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR));
56 56
57 EXPECT_EQ(std::string(), result); 57 EXPECT_EQ(std::string(), result);
58 } 58 }
59 59
60 TEST_F(ProximityAuthCryptAuthAccountTokenFetcherTest, FetcherReuse) { 60 TEST_F(ProximityAuthCryptAuthAccessTokenFetcherTest, FetcherReuse) {
61 std::string result1; 61 std::string result1;
62 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result1)); 62 fetcher_->FetchAccessToken(base::Bind(SaveAccessToken, &result1));
63 63
64 { 64 {
65 std::string result2(kInvalidResult); 65 std::string result2(kInvalidResult);
66 fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result2)); 66 fetcher_->FetchAccessToken(base::Bind(SaveAccessToken, &result2));
67 EXPECT_EQ(std::string(), result2); 67 EXPECT_EQ(std::string(), result2);
68 } 68 }
69 69
70 token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken, 70 token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken,
71 base::Time::Max()); 71 base::Time::Max());
72 EXPECT_EQ(kAccessToken, result1); 72 EXPECT_EQ(kAccessToken, result1);
73 } 73 }
74 74
75 } // namespace proximity_auth 75 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698