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 // A complete set of unit tests for OAuth2MintTokenFlow. |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "chrome/common/net/gaia/gaia_urls.h" |
| 12 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 13 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" |
| 14 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" |
| 15 #include "chrome/common/net/gaia/oauth2_mint_token_consumer.h" |
| 16 #include "chrome/common/net/gaia/oauth2_mint_token_fetcher.h" |
| 17 #include "chrome/common/net/gaia/oauth2_mint_token_flow.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 using testing::_; |
| 22 using testing::Return; |
| 23 |
| 24 namespace { |
| 25 std::vector<std::string> CreateTestScopes() { |
| 26 std::vector<std::string> scopes; |
| 27 scopes.push_back("scope1"); |
| 28 scopes.push_back("scope2"); |
| 29 return scopes; |
| 30 } |
| 31 } |
| 32 |
| 33 class MockDelegate : public OAuth2MintTokenFlow::Delegate { |
| 34 public: |
| 35 MockDelegate() {} |
| 36 ~MockDelegate() {} |
| 37 |
| 38 MOCK_METHOD1(OnMintTokenSuccess, void(const std::string& access_token)); |
| 39 MOCK_METHOD1(OnMintTokenFailure, |
| 40 void(const GoogleServiceAuthError& error)); |
| 41 }; |
| 42 |
| 43 class MockOAuth2AccessTokenFetcher : public OAuth2AccessTokenFetcher { |
| 44 public: |
| 45 MockOAuth2AccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, |
| 46 net::URLRequestContextGetter* getter) |
| 47 : OAuth2AccessTokenFetcher(consumer, getter) {} |
| 48 ~MockOAuth2AccessTokenFetcher() {} |
| 49 |
| 50 MOCK_METHOD4(Start, |
| 51 void (const std::string& client_id, |
| 52 const std::string& client_secret, |
| 53 const std::string& refresh_token, |
| 54 const std::vector<std::string>& scopes)); |
| 55 }; |
| 56 |
| 57 class MockOAuth2MintTokenFetcher : public OAuth2MintTokenFetcher { |
| 58 public: |
| 59 MockOAuth2MintTokenFetcher(OAuth2MintTokenConsumer* consumer, |
| 60 net::URLRequestContextGetter* getter) |
| 61 : OAuth2MintTokenFetcher(consumer, getter, "test") {} |
| 62 ~MockOAuth2MintTokenFetcher() {} |
| 63 |
| 64 MOCK_METHOD4(Start, |
| 65 void (const std::string& oauth_login_access_token, |
| 66 const std::string& client_id, |
| 67 const std::vector<std::string>& scopes, |
| 68 const std::string& origin)); |
| 69 }; |
| 70 |
| 71 class MockOAuth2MintTokenFlow : public OAuth2MintTokenFlow { |
| 72 public: |
| 73 explicit MockOAuth2MintTokenFlow(MockDelegate* delegate) |
| 74 : OAuth2MintTokenFlow(NULL, delegate) {} |
| 75 ~MockOAuth2MintTokenFlow() {} |
| 76 |
| 77 MOCK_METHOD0(CreateAccessTokenFetcher, OAuth2AccessTokenFetcher*()); |
| 78 MOCK_METHOD0(CreateMintTokenFetcher, OAuth2MintTokenFetcher*()); |
| 79 }; |
| 80 |
| 81 class OAuth2MintTokenFlowTest : public testing::Test { |
| 82 public: |
| 83 OAuth2MintTokenFlowTest() { |
| 84 flow_.reset(new MockOAuth2MintTokenFlow(&delegate_)); |
| 85 access_token_fetcher_.reset(new MockOAuth2AccessTokenFetcher( |
| 86 flow_.get(), NULL)); |
| 87 mint_token_fetcher_.reset(new MockOAuth2MintTokenFetcher( |
| 88 flow_.get(), NULL)); |
| 89 } |
| 90 |
| 91 virtual ~OAuth2MintTokenFlowTest() { } |
| 92 |
| 93 protected: |
| 94 void SetAccessTokenFetcherFailure(const std::string& rt, |
| 95 const GoogleServiceAuthError& error) { |
| 96 EXPECT_CALL(*access_token_fetcher_, |
| 97 Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
| 98 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
| 99 rt, std::vector<std::string>())) |
| 100 .Times(1); |
| 101 EXPECT_CALL(*flow_, CreateAccessTokenFetcher()) |
| 102 .WillOnce(Return(access_token_fetcher_.release())); |
| 103 EXPECT_CALL(delegate_, OnMintTokenFailure(error)) |
| 104 .Times(1); |
| 105 } |
| 106 |
| 107 void SetAccessTokenFetcherSuccess(const std::string& rt) { |
| 108 EXPECT_CALL(*access_token_fetcher_, |
| 109 Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
| 110 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
| 111 rt, std::vector<std::string>())) |
| 112 .Times(1); |
| 113 EXPECT_CALL(*flow_, CreateAccessTokenFetcher()) |
| 114 .WillOnce(Return(access_token_fetcher_.release())); |
| 115 } |
| 116 |
| 117 void SetMintTokenFetcherFailure(const std::string& rt, |
| 118 const std::string& ext_id, |
| 119 const std::string& client_id, |
| 120 const std::vector<std::string>& scopes, |
| 121 const GoogleServiceAuthError& error) { |
| 122 EXPECT_CALL(*mint_token_fetcher_, |
| 123 Start(rt, client_id, scopes, ext_id)) |
| 124 .Times(1); |
| 125 EXPECT_CALL(*flow_, CreateMintTokenFetcher()) |
| 126 .WillOnce(Return(mint_token_fetcher_.release())); |
| 127 EXPECT_CALL(delegate_, OnMintTokenFailure(error)) |
| 128 .Times(1); |
| 129 } |
| 130 |
| 131 void SetMintTokenFetcherSuccess(const std::string& rt, |
| 132 const std::string& ext_id, |
| 133 const std::string& client_id, |
| 134 const std::vector<std::string>& scopes, |
| 135 const std::string& at) { |
| 136 EXPECT_CALL(*mint_token_fetcher_, |
| 137 Start(rt, client_id, scopes, ext_id)) |
| 138 .Times(1); |
| 139 EXPECT_CALL(*flow_, CreateMintTokenFetcher()) |
| 140 .WillOnce(Return(mint_token_fetcher_.release())); |
| 141 EXPECT_CALL(delegate_, OnMintTokenSuccess(at)) |
| 142 .Times(1); |
| 143 } |
| 144 |
| 145 scoped_ptr<MockOAuth2MintTokenFlow> flow_; |
| 146 scoped_ptr<MockOAuth2AccessTokenFetcher> access_token_fetcher_; |
| 147 scoped_ptr<MockOAuth2MintTokenFetcher> mint_token_fetcher_; |
| 148 MockDelegate delegate_; |
| 149 }; |
| 150 |
| 151 TEST_F(OAuth2MintTokenFlowTest, LoginAccessTokenFailure) { |
| 152 std::string rt = "refresh_token"; |
| 153 std::string ext_id = "ext1"; |
| 154 std::string client_id = "client1"; |
| 155 std::vector<std::string> scopes(CreateTestScopes()); |
| 156 std::string at = "access_token"; |
| 157 GoogleServiceAuthError err(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 158 |
| 159 SetAccessTokenFetcherFailure(rt, err); |
| 160 |
| 161 flow_->Start(rt, ext_id, client_id, scopes); |
| 162 flow_->OnGetTokenFailure(err); |
| 163 } |
| 164 |
| 165 TEST_F(OAuth2MintTokenFlowTest, MintAccessTokenFailure) { |
| 166 std::string rt = "refresh_token"; |
| 167 std::string ext_id = "ext1"; |
| 168 std::string client_id = "client1"; |
| 169 std::vector<std::string> scopes(CreateTestScopes()); |
| 170 std::string at = "access_token"; |
| 171 GoogleServiceAuthError err(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 172 |
| 173 SetAccessTokenFetcherSuccess(rt); |
| 174 SetMintTokenFetcherFailure(at, ext_id, client_id, scopes, err); |
| 175 |
| 176 flow_->Start(rt, ext_id, client_id, scopes); |
| 177 flow_->OnGetTokenSuccess(at); |
| 178 flow_->OnMintTokenFailure(err); |
| 179 } |
| 180 |
| 181 TEST_F(OAuth2MintTokenFlowTest, Success) { |
| 182 std::string rt = "refresh_token"; |
| 183 std::string ext_id = "ext1"; |
| 184 std::string client_id = "client1"; |
| 185 std::vector<std::string> scopes(CreateTestScopes()); |
| 186 std::string at = "access_token"; |
| 187 std::string result = "app_access_token"; |
| 188 |
| 189 SetAccessTokenFetcherSuccess(rt); |
| 190 SetMintTokenFetcherSuccess(at, ext_id, client_id, scopes, result); |
| 191 |
| 192 flow_->Start(rt, ext_id, client_id, scopes); |
| 193 flow_->OnGetTokenSuccess(at); |
| 194 flow_->OnMintTokenSuccess(result); |
| 195 } |
OLD | NEW |