OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/json/json_writer.h" | |
6 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
7 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
8 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
9 #include "base/values.h" | 8 #include "base/values.h" |
10 #include "chrome/browser/supervised_user/child_accounts/permission_request_creat
or_apiary.h" | 9 #include "chrome/browser/supervised_user/experimental/safe_search_url_reporter.h
" |
11 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 10 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
12 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
13 #include "net/url_request/test_url_fetcher_factory.h" | 12 #include "net/url_request/test_url_fetcher_factory.h" |
14 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 const char kAccountId[] = "account@gmail.com"; | 19 const char kAccountId[] = "account@gmail.com"; |
21 | 20 |
22 std::string BuildResponse() { | |
23 base::DictionaryValue dict; | |
24 base::DictionaryValue* permission_dict = new base::DictionaryValue; | |
25 permission_dict->SetStringWithoutPathExpansion("id", "requestid"); | |
26 dict.SetWithoutPathExpansion("permissionRequest", permission_dict); | |
27 std::string result; | |
28 base::JSONWriter::Write(dict, &result); | |
29 return result; | |
30 } | |
31 | |
32 } // namespace | 21 } // namespace |
33 | 22 |
34 class PermissionRequestCreatorApiaryTest : public testing::Test { | 23 class SafeSearchUrlReporterTest : public testing::Test { |
35 public: | 24 public: |
36 PermissionRequestCreatorApiaryTest() | 25 SafeSearchUrlReporterTest() |
37 : request_context_(new net::TestURLRequestContextGetter( | 26 : request_context_(new net::TestURLRequestContextGetter( |
38 base::ThreadTaskRunnerHandle::Get())), | 27 base::ThreadTaskRunnerHandle::Get())), |
39 permission_creator_(&token_service_, | 28 report_url_(&token_service_, kAccountId, request_context_.get()) { |
40 kAccountId, | |
41 request_context_.get()) { | |
42 token_service_.UpdateCredentials(kAccountId, "refresh_token"); | 29 token_service_.UpdateCredentials(kAccountId, "refresh_token"); |
43 } | 30 } |
44 | 31 |
45 protected: | 32 protected: |
46 void IssueAccessTokens() { | 33 void IssueAccessTokens() { |
47 token_service_.IssueAllTokensForAccount( | 34 token_service_.IssueAllTokensForAccount( |
48 kAccountId, | 35 kAccountId, "access_token", |
49 "access_token", | |
50 base::Time::Now() + base::TimeDelta::FromHours(1)); | 36 base::Time::Now() + base::TimeDelta::FromHours(1)); |
51 } | 37 } |
52 | 38 |
53 void IssueAccessTokenErrors() { | 39 void IssueAccessTokenErrors() { |
54 token_service_.IssueErrorForAllPendingRequestsForAccount( | 40 token_service_.IssueErrorForAllPendingRequestsForAccount( |
55 kAccountId, | 41 kAccountId, GoogleServiceAuthError::FromServiceError("Error!")); |
56 GoogleServiceAuthError::FromServiceError("Error!")); | |
57 } | 42 } |
58 | 43 |
59 void CreateRequest(int url_fetcher_id, const GURL& url) { | 44 void CreateRequest(int url_fetcher_id, const GURL& url) { |
60 permission_creator_.set_url_fetcher_id_for_testing(url_fetcher_id); | 45 report_url_.set_url_fetcher_id_for_testing(url_fetcher_id); |
61 permission_creator_.CreateURLAccessRequest( | 46 report_url_.ReportUrl( |
62 url, | 47 url, base::Bind(&SafeSearchUrlReporterTest::OnRequestCreated, |
63 base::Bind(&PermissionRequestCreatorApiaryTest::OnRequestCreated, | 48 base::Unretained(this))); |
64 base::Unretained(this))); | |
65 } | 49 } |
66 | 50 |
67 net::TestURLFetcher* GetURLFetcher(int id) { | 51 net::TestURLFetcher* GetURLFetcher(int id) { |
68 net::TestURLFetcher* url_fetcher = url_fetcher_factory_.GetFetcherByID(id); | 52 net::TestURLFetcher* url_fetcher = url_fetcher_factory_.GetFetcherByID(id); |
69 EXPECT_TRUE(url_fetcher); | 53 EXPECT_TRUE(url_fetcher); |
70 return url_fetcher; | 54 return url_fetcher; |
71 } | 55 } |
72 | 56 |
73 void SendResponse(int url_fetcher_id, | 57 void SendResponse(int url_fetcher_id, net::Error error) { |
74 net::Error error, | |
75 const std::string& response) { | |
76 net::TestURLFetcher* url_fetcher = GetURLFetcher(url_fetcher_id); | 58 net::TestURLFetcher* url_fetcher = GetURLFetcher(url_fetcher_id); |
77 url_fetcher->set_status(net::URLRequestStatus::FromError(error)); | 59 url_fetcher->set_status(net::URLRequestStatus::FromError(error)); |
78 url_fetcher->set_response_code(net::HTTP_OK); | 60 url_fetcher->set_response_code(net::HTTP_OK); |
79 url_fetcher->SetResponseString(response); | |
80 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 61 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
81 } | 62 } |
82 | 63 |
83 void SendValidResponse(int url_fetcher_id) { | 64 void SendValidResponse(int url_fetcher_id) { |
84 SendResponse(url_fetcher_id, net::OK, BuildResponse()); | 65 SendResponse(url_fetcher_id, net::OK); |
85 } | 66 } |
86 | 67 |
87 void SendFailedResponse(int url_fetcher_id) { | 68 void SendFailedResponse(int url_fetcher_id) { |
88 SendResponse(url_fetcher_id, net::ERR_ABORTED, std::string()); | 69 SendResponse(url_fetcher_id, net::ERR_ABORTED); |
89 } | 70 } |
90 | 71 |
91 MOCK_METHOD1(OnRequestCreated, void(bool success)); | 72 MOCK_METHOD1(OnRequestCreated, void(bool success)); |
92 | 73 |
93 base::MessageLoop message_loop_; | 74 base::MessageLoop message_loop_; |
94 FakeProfileOAuth2TokenService token_service_; | 75 FakeProfileOAuth2TokenService token_service_; |
95 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 76 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
96 net::TestURLFetcherFactory url_fetcher_factory_; | 77 net::TestURLFetcherFactory url_fetcher_factory_; |
97 PermissionRequestCreatorApiary permission_creator_; | 78 SafeSearchUrlReporter report_url_; |
98 }; | 79 }; |
99 | 80 |
100 TEST_F(PermissionRequestCreatorApiaryTest, Success) { | 81 TEST_F(SafeSearchUrlReporterTest, Success) { |
101 CreateRequest(0, GURL("http://randomurl.com")); | 82 CreateRequest(0, GURL("http://google.com")); |
102 CreateRequest(1, GURL("http://anotherurl.com")); | 83 CreateRequest(1, GURL("http://url.com")); |
103 | 84 |
104 // We should have gotten a request for an access token. | |
105 EXPECT_GT(token_service_.GetPendingRequests().size(), 0U); | 85 EXPECT_GT(token_service_.GetPendingRequests().size(), 0U); |
106 | 86 |
107 IssueAccessTokens(); | 87 IssueAccessTokens(); |
108 | 88 |
109 EXPECT_CALL(*this, OnRequestCreated(true)); | 89 EXPECT_CALL(*this, OnRequestCreated(true)); |
110 SendValidResponse(0); | 90 SendValidResponse(0); |
111 EXPECT_CALL(*this, OnRequestCreated(true)); | 91 EXPECT_CALL(*this, OnRequestCreated(true)); |
112 SendValidResponse(1); | 92 SendValidResponse(1); |
113 } | 93 } |
114 | 94 |
115 TEST_F(PermissionRequestCreatorApiaryTest, AccessTokenError) { | 95 TEST_F(SafeSearchUrlReporterTest, AccessTokenError) { |
116 CreateRequest(0, GURL("http://randomurl.com")); | 96 CreateRequest(0, GURL("http://google.com")); |
117 | 97 |
118 // We should have gotten a request for an access token. | |
119 EXPECT_EQ(1U, token_service_.GetPendingRequests().size()); | 98 EXPECT_EQ(1U, token_service_.GetPendingRequests().size()); |
120 | 99 |
121 // Our callback should get called immediately on an error. | |
122 EXPECT_CALL(*this, OnRequestCreated(false)); | 100 EXPECT_CALL(*this, OnRequestCreated(false)); |
123 IssueAccessTokenErrors(); | 101 IssueAccessTokenErrors(); |
124 } | 102 } |
125 | 103 |
126 TEST_F(PermissionRequestCreatorApiaryTest, NetworkError) { | 104 TEST_F(SafeSearchUrlReporterTest, NetworkError) { |
127 CreateRequest(0, GURL("http://randomurl.com")); | 105 CreateRequest(0, GURL("http://google.com")); |
128 | 106 |
129 // We should have gotten a request for an access token. | |
130 EXPECT_EQ(1U, token_service_.GetPendingRequests().size()); | 107 EXPECT_EQ(1U, token_service_.GetPendingRequests().size()); |
131 | 108 |
132 IssueAccessTokens(); | 109 IssueAccessTokens(); |
133 | 110 |
134 // Our callback should get called on an error. | |
135 EXPECT_CALL(*this, OnRequestCreated(false)); | 111 EXPECT_CALL(*this, OnRequestCreated(false)); |
136 SendFailedResponse(0); | 112 SendFailedResponse(0); |
137 } | 113 } |
OLD | NEW |