| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 // A complete set of unit tests for OAuth2AccessTokenFetcher. | 5 // A complete set of unit tests for OAuth2AccessTokenFetcher. |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 using content::BrowserThread; | 28 using content::BrowserThread; |
| 29 using content::URLFetcher; | 29 using content::URLFetcher; |
| 30 using content::URLFetcherDelegate; | 30 using content::URLFetcherDelegate; |
| 31 using content::URLFetcherFactory; | 31 using content::URLFetcherFactory; |
| 32 using net::ResponseCookies; | 32 using net::ResponseCookies; |
| 33 using net::URLRequestStatus; | 33 using net::URLRequestStatus; |
| 34 using testing::_; | 34 using testing::_; |
| 35 using testing::Return; | 35 using testing::Return; |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 typedef std::vector<std::string> ScopeList; |
| 39 |
| 38 static const char kValidTokenResponse[] = | 40 static const char kValidTokenResponse[] = |
| 39 "{" | 41 "{" |
| 40 " \"access_token\": \"at1\"," | 42 " \"access_token\": \"at1\"," |
| 41 " \"expires_in\": 3600," | 43 " \"expires_in\": 3600," |
| 42 " \"token_type\": \"Bearer\"" | 44 " \"token_type\": \"Bearer\"" |
| 43 "}"; | 45 "}"; |
| 44 static const char kTokenResponseNoAccessToken[] = | 46 static const char kTokenResponseNoAccessToken[] = |
| 45 "{" | 47 "{" |
| 46 " \"expires_in\": 3600," | 48 " \"expires_in\": 3600," |
| 47 " \"token_type\": \"Bearer\"" | 49 " \"token_type\": \"Bearer\"" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 content::TestBrowserThread ui_thread_; | 109 content::TestBrowserThread ui_thread_; |
| 108 MockUrlFetcherFactory factory_; | 110 MockUrlFetcherFactory factory_; |
| 109 MockOAuth2AccessTokenConsumer consumer_; | 111 MockOAuth2AccessTokenConsumer consumer_; |
| 110 TestingProfile profile_; | 112 TestingProfile profile_; |
| 111 OAuth2AccessTokenFetcher fetcher_; | 113 OAuth2AccessTokenFetcher fetcher_; |
| 112 }; | 114 }; |
| 113 | 115 |
| 114 TEST_F(OAuth2AccessTokenFetcherTest, GetAccessTokenRequestFailure) { | 116 TEST_F(OAuth2AccessTokenFetcherTest, GetAccessTokenRequestFailure) { |
| 115 TestURLFetcher* url_fetcher = SetupGetAccessToken(false, 0, ""); | 117 TestURLFetcher* url_fetcher = SetupGetAccessToken(false, 0, ""); |
| 116 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); | 118 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); |
| 117 fetcher_.Start("client_id", "client_secret", "refresh_token"); | 119 fetcher_.Start("client_id", "client_secret", "refresh_token", ScopeList()); |
| 118 fetcher_.OnURLFetchComplete(url_fetcher); | 120 fetcher_.OnURLFetchComplete(url_fetcher); |
| 119 } | 121 } |
| 120 | 122 |
| 121 TEST_F(OAuth2AccessTokenFetcherTest, GetAccessTokenResponseCodeFailure) { | 123 TEST_F(OAuth2AccessTokenFetcherTest, GetAccessTokenResponseCodeFailure) { |
| 122 TestURLFetcher* url_fetcher = SetupGetAccessToken(true, RC_FORBIDDEN, ""); | 124 TestURLFetcher* url_fetcher = SetupGetAccessToken(true, RC_FORBIDDEN, ""); |
| 123 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); | 125 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); |
| 124 fetcher_.Start("client_id", "client_secret", "refresh_token"); | 126 fetcher_.Start("client_id", "client_secret", "refresh_token", ScopeList()); |
| 125 fetcher_.OnURLFetchComplete(url_fetcher); | 127 fetcher_.OnURLFetchComplete(url_fetcher); |
| 126 } | 128 } |
| 127 | 129 |
| 128 TEST_F(OAuth2AccessTokenFetcherTest, Success) { | 130 TEST_F(OAuth2AccessTokenFetcherTest, Success) { |
| 129 TestURLFetcher* url_fetcher = SetupGetAccessToken( | 131 TestURLFetcher* url_fetcher = SetupGetAccessToken( |
| 130 true, RC_REQUEST_OK, kValidTokenResponse); | 132 true, RC_REQUEST_OK, kValidTokenResponse); |
| 131 EXPECT_CALL(consumer_, OnGetTokenSuccess("at1")).Times(1); | 133 EXPECT_CALL(consumer_, OnGetTokenSuccess("at1")).Times(1); |
| 132 fetcher_.Start("client_id", "client_secret", "refresh_token"); | 134 fetcher_.Start("client_id", "client_secret", "refresh_token", ScopeList()); |
| 133 fetcher_.OnURLFetchComplete(url_fetcher); | 135 fetcher_.OnURLFetchComplete(url_fetcher); |
| 134 } | 136 } |
| 135 | 137 |
| 138 TEST_F(OAuth2AccessTokenFetcherTest, MakeGetAccessTokenBody) { |
| 139 { // No scope. |
| 140 std::string body = |
| 141 "client_id=cid1&" |
| 142 "client_secret=cs1&" |
| 143 "grant_type=refresh_token&" |
| 144 "refresh_token=rt1"; |
| 145 EXPECT_EQ(body, OAuth2AccessTokenFetcher::MakeGetAccessTokenBody( |
| 146 "cid1", "cs1", "rt1", ScopeList())); |
| 147 } |
| 148 |
| 149 { // One scope. |
| 150 std::string body = |
| 151 "client_id=cid1&" |
| 152 "client_secret=cs1&" |
| 153 "grant_type=refresh_token&" |
| 154 "refresh_token=rt1&" |
| 155 "scope=https://www.googleapis.com/foo"; |
| 156 ScopeList scopes; |
| 157 scopes.push_back("https://www.googleapis.com/foo"); |
| 158 EXPECT_EQ(body, OAuth2AccessTokenFetcher::MakeGetAccessTokenBody( |
| 159 "cid1", "cs1", "rt1", scopes)); |
| 160 } |
| 161 |
| 162 { // Multiple scopes. |
| 163 std::string body = |
| 164 "client_id=cid1&" |
| 165 "client_secret=cs1&" |
| 166 "grant_type=refresh_token&" |
| 167 "refresh_token=rt1&" |
| 168 "scope=https://www.googleapis.com/foo," |
| 169 "https://www.googleapis.com/bar," |
| 170 "https://www.googleapis.com/baz"; |
| 171 ScopeList scopes; |
| 172 scopes.push_back("https://www.googleapis.com/foo"); |
| 173 scopes.push_back("https://www.googleapis.com/bar"); |
| 174 scopes.push_back("https://www.googleapis.com/baz"); |
| 175 EXPECT_EQ(body, OAuth2AccessTokenFetcher::MakeGetAccessTokenBody( |
| 176 "cid1", "cs1", "rt1", scopes)); |
| 177 } |
| 178 } |
| 179 |
| 136 TEST_F(OAuth2AccessTokenFetcherTest, ParseGetAccessTokenResponse) { | 180 TEST_F(OAuth2AccessTokenFetcherTest, ParseGetAccessTokenResponse) { |
| 137 { // No body. | 181 { // No body. |
| 138 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | 182 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); |
| 139 | 183 |
| 140 std::string at; | 184 std::string at; |
| 141 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | 185 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( |
| 142 &url_fetcher, &at)); | 186 &url_fetcher, &at)); |
| 143 EXPECT_TRUE(at.empty()); | 187 EXPECT_TRUE(at.empty()); |
| 144 } | 188 } |
| 145 { // Bad json. | 189 { // Bad json. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 163 { // Valid json: all good. | 207 { // Valid json: all good. |
| 164 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | 208 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); |
| 165 url_fetcher.SetResponseString(kValidTokenResponse); | 209 url_fetcher.SetResponseString(kValidTokenResponse); |
| 166 | 210 |
| 167 std::string at; | 211 std::string at; |
| 168 EXPECT_TRUE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | 212 EXPECT_TRUE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( |
| 169 &url_fetcher, &at)); | 213 &url_fetcher, &at)); |
| 170 EXPECT_EQ("at1", at); | 214 EXPECT_EQ("at1", at); |
| 171 } | 215 } |
| 172 } | 216 } |
| OLD | NEW |