Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/json/json_writer.h" | |
| 6 #include "base/memory/scoped_ptr.h" | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | |
| 10 #include "chrome/browser/supervised_user/permission_request_creator_apiary.h" | |
| 11 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h" | |
| 12 #include "net/url_request/test_url_fetcher_factory.h" | |
| 13 #include "net/url_request/url_request_test_util.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const char kAccountId[] = "account@gmail.com"; | |
| 20 const char kApiScope[] = "api_scope"; | |
| 21 const char kPermissionRequestApiUrl[] = "https://myapis.com/permissions"; | |
| 22 | |
| 23 std::string BuildResponse() { | |
| 24 base::DictionaryValue dict; | |
| 25 dict.SetStringWithoutPathExpansion("id", "requestid"); | |
| 26 std::string result; | |
| 27 base::JSONWriter::Write(&dict, &result); | |
| 28 return result; | |
| 29 } | |
| 30 | |
| 31 class FakeSupervisedUserSigninManagerWrapper | |
| 32 : public SupervisedUserSigninManagerWrapper { | |
| 33 public: | |
| 34 FakeSupervisedUserSigninManagerWrapper() | |
| 35 : SupervisedUserSigninManagerWrapper(NULL, NULL) {} | |
| 36 | |
| 37 virtual std::string GetEffectiveUsername() const OVERRIDE { | |
| 38 return kAccountId; | |
| 39 } | |
| 40 virtual std::string GetAccountIdToUse() const OVERRIDE { | |
| 41 return kAccountId; | |
| 42 } | |
| 43 virtual std::string GetSyncScopeToUse() const OVERRIDE { | |
| 44 return kApiScope; | |
| 45 } | |
| 46 }; | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 class PermissionRequestCreatorApiaryTest : public testing::Test { | |
| 51 public: | |
| 52 PermissionRequestCreatorApiaryTest() | |
| 53 : request_context_(new net::TestURLRequestContextGetter( | |
| 54 base::MessageLoopProxy::current())), | |
| 55 permission_creator_( | |
| 56 GURL(kPermissionRequestApiUrl), | |
| 57 &token_service_, | |
| 58 make_scoped_ptr(new FakeSupervisedUserSigninManagerWrapper), | |
| 59 request_context_.get()) { | |
| 60 token_service_.IssueRefreshTokenForUser(kAccountId, "refresh_token"); | |
| 61 } | |
| 62 | |
| 63 protected: | |
| 64 void IssueAccessTokens() { | |
| 65 token_service_.IssueAllTokensForAccount( | |
| 66 kAccountId, | |
| 67 "access_token", | |
| 68 base::Time::Now() + base::TimeDelta::FromHours(1)); | |
| 69 } | |
| 70 | |
| 71 void IssueAccessTokenErrors() { | |
| 72 token_service_.IssueErrorForAllPendingRequestsForAccount( | |
| 73 kAccountId, | |
| 74 GoogleServiceAuthError::FromServiceError("Error!")); | |
| 75 } | |
| 76 | |
| 77 void CreateRequest(int url_fetcher_id, const GURL& url) { | |
| 78 permission_creator_.set_url_fetcher_id_for_testing(url_fetcher_id); | |
| 79 permission_creator_.CreatePermissionRequest( | |
| 80 url, | |
| 81 base::Bind(&PermissionRequestCreatorApiaryTest::OnRequestCreated, | |
| 82 base::Unretained(this))); | |
| 83 } | |
| 84 | |
| 85 net::TestURLFetcher* GetURLFetcher(int id) { | |
| 86 net::TestURLFetcher* url_fetcher = url_fetcher_factory_.GetFetcherByID(id); | |
| 87 EXPECT_TRUE(url_fetcher); | |
| 88 return url_fetcher; | |
| 89 } | |
| 90 | |
| 91 void SendResponse(int url_fetcher_id, | |
| 92 net::URLRequestStatus::Status status, | |
| 93 const std::string& response) { | |
| 94 net::TestURLFetcher* url_fetcher = GetURLFetcher(url_fetcher_id); | |
| 95 url_fetcher->set_status(net::URLRequestStatus(status, 0)); | |
| 96 url_fetcher->set_response_code(net::HTTP_OK); | |
| 97 url_fetcher->SetResponseString(response); | |
| 98 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | |
| 99 } | |
| 100 | |
| 101 void SendValidResponse(int url_fetcher_id) { | |
| 102 SendResponse(url_fetcher_id, | |
| 103 net::URLRequestStatus::SUCCESS, | |
| 104 BuildResponse()); | |
| 105 } | |
| 106 | |
| 107 void SendFailedResponse(int url_fetcher_id) { | |
| 108 SendResponse(url_fetcher_id, | |
| 109 net::URLRequestStatus::CANCELED, | |
| 110 std::string()); | |
| 111 } | |
| 112 | |
| 113 MOCK_METHOD0(OnRequestCreated, void()); | |
| 114 | |
| 115 base::MessageLoop message_loop_; | |
| 116 FakeProfileOAuth2TokenService token_service_; | |
| 117 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | |
| 118 net::TestURLFetcherFactory url_fetcher_factory_; | |
| 119 PermissionRequestCreatorApiary permission_creator_; | |
| 120 }; | |
| 121 | |
| 122 TEST_F(PermissionRequestCreatorApiaryTest, Success) { | |
| 123 CreateRequest(0, GURL("http://randomurl.com")); | |
| 124 CreateRequest(1, GURL("http://anotherurl.com")); | |
| 125 | |
| 126 // We should have gotten a request for an access token. | |
| 127 EXPECT_LT(0U, token_service_.GetPendingRequests().size()); | |
|
Bernhard Bauer
2014/09/30 15:01:07
Non-equality operators use the "natural" parameter
Marc Treib
2014/09/30 15:28:45
Done.
| |
| 128 | |
| 129 IssueAccessTokens(); | |
| 130 | |
| 131 EXPECT_CALL(*this, OnRequestCreated()); | |
| 132 SendValidResponse(0); | |
| 133 EXPECT_CALL(*this, OnRequestCreated()); | |
| 134 SendValidResponse(1); | |
| 135 } | |
| 136 | |
| 137 TEST_F(PermissionRequestCreatorApiaryTest, AccessTokenError) { | |
| 138 CreateRequest(0, GURL("http://randomurl.com")); | |
| 139 | |
| 140 // We should have gotten a request for an access token. | |
| 141 EXPECT_EQ(1U, token_service_.GetPendingRequests().size()); | |
| 142 | |
| 143 // Our callback should get called immediately on an error. | |
| 144 EXPECT_CALL(*this, OnRequestCreated()); | |
| 145 IssueAccessTokenErrors(); | |
| 146 } | |
| 147 | |
| 148 TEST_F(PermissionRequestCreatorApiaryTest, NetworkError) { | |
| 149 CreateRequest(0, GURL("http://randomurl.com")); | |
| 150 | |
| 151 // We should have gotten a request for an access token. | |
| 152 EXPECT_EQ(1U, token_service_.GetPendingRequests().size()); | |
| 153 | |
| 154 IssueAccessTokens(); | |
| 155 | |
| 156 // Our callback should get called on an error. | |
| 157 EXPECT_CALL(*this, OnRequestCreated()); | |
| 158 SendFailedResponse(0); | |
| 159 } | |
| OLD | NEW |