OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/values.h" | |
6 #include "chrome/browser/policy/cloud/user_info_fetcher.h" | |
7 #include "google_apis/gaia/google_service_auth_error.h" | |
8 #include "net/http/http_status_code.h" | |
9 #include "net/url_request/test_url_fetcher_factory.h" | |
10 #include "testing/gmock/include/gmock/gmock.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 using testing::_; | |
14 | |
15 namespace policy { | |
16 | |
17 namespace { | |
18 | |
19 static const char kUserInfoResponse[] = | |
20 "{" | |
21 " \"email\": \"test_user@test.com\"," | |
22 " \"verified_email\": true," | |
23 " \"hd\": \"test.com\"" | |
24 "}"; | |
25 | |
26 class MockUserInfoFetcherDelegate : public UserInfoFetcher::Delegate { | |
27 public: | |
28 MockUserInfoFetcherDelegate() {} | |
29 ~MockUserInfoFetcherDelegate() {} | |
30 MOCK_METHOD1(OnGetUserInfoFailure, | |
31 void(const GoogleServiceAuthError& error)); | |
32 MOCK_METHOD1(OnGetUserInfoSuccess, void(const DictionaryValue* result)); | |
33 }; | |
34 | |
35 MATCHER_P(MatchDict, expected, "matches DictionaryValue") { | |
36 return arg->Equals(expected); | |
37 } | |
38 | |
39 class UserInfoFetcherTest : public testing::Test { | |
40 public: | |
41 UserInfoFetcherTest() {} | |
42 net::TestURLFetcherFactory url_factory_; | |
43 }; | |
44 | |
45 TEST_F(UserInfoFetcherTest, FailedFetch) { | |
46 MockUserInfoFetcherDelegate delegate; | |
47 UserInfoFetcher fetcher(&delegate, NULL); | |
48 fetcher.Start("access_token"); | |
49 | |
50 // Fake a failed fetch - should result in the failure callback being invoked. | |
51 EXPECT_CALL(delegate, OnGetUserInfoFailure(_)); | |
52 net::TestURLFetcher* url_fetcher = url_factory_.GetFetcherByID(0); | |
53 url_fetcher->set_status(net::URLRequestStatus( | |
54 net::URLRequestStatus::FAILED, -1)); | |
55 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | |
56 } | |
57 | |
58 TEST_F(UserInfoFetcherTest, SuccessfulFetch) { | |
59 MockUserInfoFetcherDelegate delegate; | |
60 UserInfoFetcher fetcher(&delegate, NULL); | |
61 fetcher.Start("access_token"); | |
62 | |
63 // Generate what we expect our result will look like (should match | |
64 // parsed kUserInfoResponse). | |
65 scoped_ptr<DictionaryValue> dict(new DictionaryValue()); | |
66 dict->SetString("email", "test_user@test.com"); | |
67 dict->SetBoolean("verified_email", true); | |
68 dict->SetString("hd", "test.com"); | |
69 | |
70 // Fake a successful fetch - should result in the data being parsed and | |
71 // the values passed off to the success callback. | |
72 EXPECT_CALL(delegate, OnGetUserInfoSuccess(MatchDict(dict.get()))); | |
73 net::TestURLFetcher* url_fetcher = url_factory_.GetFetcherByID(0); | |
74 url_fetcher->set_response_code(net::HTTP_OK); | |
75 url_fetcher->SetResponseString(kUserInfoResponse); | |
76 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | |
77 } | |
78 } // namespace | |
79 | |
80 } // namespace policy | |
OLD | NEW |