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

Side by Side Diff: chrome/common/net/gaia/oauth2_token_mint_fetcher_unittest.cc

Issue 9549013: Add code to mint OAuth tokens from login-scoped OAuth token. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 // A complete set of unit tests for OAuth2TokenMintFetcher.
6
7 #include <string>
8 #include <vector>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h"
12 #include "chrome/common/net/gaia/gaia_urls.h"
13 #include "chrome/common/net/gaia/google_service_auth_error.h"
14 #include "chrome/common/net/gaia/oauth2_token_mint_consumer.h"
15 #include "chrome/common/net/gaia/oauth2_token_mint_fetcher.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/common/url_fetcher.h"
18 #include "content/public/common/url_fetcher_delegate.h"
19 #include "content/public/common/url_fetcher_factory.h"
20 #include "content/test/test_browser_thread.h"
21 #include "content/test/test_url_fetcher_factory.h"
22 #include "googleurl/src/gurl.h"
23 #include "net/http/http_status_code.h"
24 #include "net/url_request/url_request.h"
25 #include "net/url_request/url_request_status.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28
29 using content::BrowserThread;
30 using content::URLFetcher;
31 using content::URLFetcherDelegate;
32 using content::URLFetcherFactory;
33 using net::ResponseCookies;
34 using net::URLRequestStatus;
35 using testing::_;
36 using testing::Return;
37
38 namespace {
39 static const char kValidTokenResponse[] =
40 "{"
41 " \"token\": \"at1\","
42 " \"issueAdvice\": \"Auto\""
43 "}";
44 static const char kTokenResponseNoAccessToken[] =
45 "{"
46 " \"issueAdvice\": \"Auto\""
47 "}";
48 }
49
50 class MockUrlFetcherFactory : public ScopedURLFetcherFactory,
51 public URLFetcherFactory {
52 public:
53 MockUrlFetcherFactory()
54 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
55 }
56 virtual ~MockUrlFetcherFactory() {}
57
58 MOCK_METHOD4(
59 CreateURLFetcher,
60 URLFetcher* (int id,
61 const GURL& url,
62 URLFetcher::RequestType request_type,
63 URLFetcherDelegate* d));
64 };
65
66 class MockOAuth2TokenMintConsumer : public OAuth2TokenMintConsumer {
67 public:
68 MockOAuth2TokenMintConsumer() {}
69 ~MockOAuth2TokenMintConsumer() {}
70
71 MOCK_METHOD1(OnMintTokenSuccess, void(const std::string& access_token));
72 MOCK_METHOD1(OnMintTokenFailure,
73 void(const GoogleServiceAuthError& error));
74 };
75
76 class OAuth2TokenMintFetcherTest : public testing::Test {
77 public:
78 OAuth2TokenMintFetcherTest()
79 : ui_thread_(BrowserThread::UI, &message_loop_),
80 fetcher_(&consumer_, profile_.GetRequestContext(), "test") {
81 test_scopes_.push_back("scope1");
82 test_scopes_.push_back("scope1");
83 }
84
85 virtual ~OAuth2TokenMintFetcherTest() { }
86
87 virtual TestURLFetcher* SetupIssueToken(
88 bool fetch_succeeds, int response_code, const std::string& body) {
89 GURL url(GaiaUrls::GetInstance()->oauth2_issue_token_url());
90 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &fetcher_);
91 URLRequestStatus::Status status =
92 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED;
93 url_fetcher->set_status(URLRequestStatus(status, 0));
94
95 if (response_code != 0)
96 url_fetcher->set_response_code(response_code);
97
98 if (!body.empty())
99 url_fetcher->SetResponseString(body);
100
101 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _))
102 .WillOnce(Return(url_fetcher));
103 return url_fetcher;
104 }
105
106 protected:
107 MessageLoop message_loop_;
108 content::TestBrowserThread ui_thread_;
109 MockUrlFetcherFactory factory_;
110 MockOAuth2TokenMintConsumer consumer_;
111 TestingProfile profile_;
112 OAuth2TokenMintFetcher fetcher_;
113 std::vector<std::string> test_scopes_;
114 };
115
116 TEST_F(OAuth2TokenMintFetcherTest, GetAccessTokenRequestFailure) {
117 TestURLFetcher* url_fetcher = SetupIssueToken(false, 0, "");
118 EXPECT_CALL(consumer_, OnMintTokenFailure(_)).Times(1);
119 fetcher_.Start("access_token1", "client1", test_scopes_, "extension1");
120 fetcher_.OnURLFetchComplete(url_fetcher);
121 }
122
123 TEST_F(OAuth2TokenMintFetcherTest, GetAccessTokenResponseCodeFailure) {
124 TestURLFetcher* url_fetcher = SetupIssueToken(
125 false, net::HTTP_FORBIDDEN, "");
126 EXPECT_CALL(consumer_, OnMintTokenFailure(_)).Times(1);
127 fetcher_.Start("access_token1", "client1", test_scopes_, "extension1");
128 fetcher_.OnURLFetchComplete(url_fetcher);
129 }
130
131 TEST_F(OAuth2TokenMintFetcherTest, Success) {
132 TestURLFetcher* url_fetcher = SetupIssueToken(
133 true, net::HTTP_OK, kValidTokenResponse);
134 EXPECT_CALL(consumer_, OnMintTokenSuccess("at1")).Times(1);
135 fetcher_.Start("access_token1", "client1", test_scopes_, "extension1");
136 fetcher_.OnURLFetchComplete(url_fetcher);
137 }
138
139 TEST_F(OAuth2TokenMintFetcherTest, ParseMintTokenResponse) {
140 { // No body.
141 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL);
142
143 std::string at;
144 EXPECT_FALSE(OAuth2TokenMintFetcher::ParseMintTokenResponse(
145 &url_fetcher, &at));
146 EXPECT_TRUE(at.empty());
147 }
148 { // Bad json.
149 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL);
150 url_fetcher.SetResponseString("foo");
151
152 std::string at;
153 EXPECT_FALSE(OAuth2TokenMintFetcher::ParseMintTokenResponse(
154 &url_fetcher, &at));
155 EXPECT_TRUE(at.empty());
156 }
157 { // Valid json: access token missing.
158 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL);
159 url_fetcher.SetResponseString(kTokenResponseNoAccessToken);
160
161 std::string at;
162 EXPECT_FALSE(OAuth2TokenMintFetcher::ParseMintTokenResponse(
163 &url_fetcher, &at));
164 EXPECT_TRUE(at.empty());
165 }
166 { // Valid json: all good.
167 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL);
168 url_fetcher.SetResponseString(kValidTokenResponse);
169
170 std::string at;
171 EXPECT_TRUE(OAuth2TokenMintFetcher::ParseMintTokenResponse(
172 &url_fetcher, &at));
173 EXPECT_EQ("at1", at);
174 }
175 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698