OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace extensions { |
| 13 |
| 14 class FakeWebAuthFlow : public WebAuthFlow { |
| 15 public: |
| 16 explicit FakeWebAuthFlow(WebAuthFlow::Delegate* delegate) |
| 17 : WebAuthFlow(delegate, |
| 18 NULL, |
| 19 GURL(), |
| 20 WebAuthFlow::INTERACTIVE, |
| 21 gfx::Rect(), |
| 22 chrome::GetActiveDesktop()) {} |
| 23 |
| 24 virtual void Start() OVERRIDE {} |
| 25 }; |
| 26 |
| 27 class TestGaiaWebAuthFlow : public GaiaWebAuthFlow { |
| 28 public: |
| 29 TestGaiaWebAuthFlow(GaiaWebAuthFlow::Delegate* delegate, |
| 30 const std::string& extension_id, |
| 31 const OAuth2Info& oauth2_info, |
| 32 GoogleServiceAuthError::State ubertoken_error_state) |
| 33 : GaiaWebAuthFlow(delegate, |
| 34 NULL, |
| 35 chrome::GetActiveDesktop(), |
| 36 "extension_id", |
| 37 oauth2_info), |
| 38 ubertoken_error_(ubertoken_error_state) {} |
| 39 |
| 40 virtual void Start() OVERRIDE { |
| 41 if (ubertoken_error_.state() == GoogleServiceAuthError::NONE) |
| 42 OnUbertokenSuccess("fake_ubertoken"); |
| 43 else |
| 44 OnUbertokenFailure(ubertoken_error_); |
| 45 } |
| 46 |
| 47 private: |
| 48 virtual scoped_ptr<WebAuthFlow> CreateWebAuthFlow(GURL url) OVERRIDE { |
| 49 return scoped_ptr<WebAuthFlow>(new FakeWebAuthFlow(this)); |
| 50 } |
| 51 |
| 52 GoogleServiceAuthError ubertoken_error_; |
| 53 }; |
| 54 |
| 55 class MockGaiaWebAuthFlowDelegate : public GaiaWebAuthFlow::Delegate { |
| 56 public: |
| 57 MOCK_METHOD2(OnGaiaFlowFailure, |
| 58 void(GaiaWebAuthFlow::Failure failure, |
| 59 GoogleServiceAuthError service_error)); |
| 60 MOCK_METHOD3(OnGaiaFlowCompleted, |
| 61 void(const std::string& access_token, |
| 62 const std::string& expiration, |
| 63 const std::string& error)); |
| 64 }; |
| 65 |
| 66 class IdentityGaiaWebAuthFlowTest : public testing::Test { |
| 67 public: |
| 68 IdentityGaiaWebAuthFlowTest() |
| 69 : ubertoken_error_state_(GoogleServiceAuthError::NONE) {} |
| 70 |
| 71 scoped_ptr<TestGaiaWebAuthFlow> CreateTestFlow() { |
| 72 OAuth2Info oauth2_info; |
| 73 oauth2_info.client_id = "fake.client.id"; |
| 74 return scoped_ptr<TestGaiaWebAuthFlow>(new TestGaiaWebAuthFlow( |
| 75 &delegate_, "extension_id", oauth2_info, ubertoken_error_state_)); |
| 76 |
| 77 } |
| 78 |
| 79 std::string GetFinalTitle(const std::string& fragment) { |
| 80 return std::string("Loading id.client.fake:/extension_id#") + fragment; |
| 81 } |
| 82 |
| 83 GoogleServiceAuthError GetNoneServiceError() { |
| 84 return GoogleServiceAuthError(GoogleServiceAuthError::NONE); |
| 85 } |
| 86 |
| 87 void set_ubertoken_error( |
| 88 GoogleServiceAuthError::State ubertoken_error_state) { |
| 89 ubertoken_error_state_ = ubertoken_error_state; |
| 90 } |
| 91 |
| 92 protected: |
| 93 testing::StrictMock<MockGaiaWebAuthFlowDelegate> delegate_; |
| 94 GoogleServiceAuthError::State ubertoken_error_state_; |
| 95 }; |
| 96 |
| 97 TEST_F(IdentityGaiaWebAuthFlowTest, OAuthError) { |
| 98 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 99 flow->Start(); |
| 100 EXPECT_CALL(delegate_, OnGaiaFlowCompleted("", "", "access_denied")); |
| 101 flow->OnAuthFlowTitleChange(GetFinalTitle("error=access_denied")); |
| 102 } |
| 103 |
| 104 TEST_F(IdentityGaiaWebAuthFlowTest, Token) { |
| 105 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 106 flow->Start(); |
| 107 EXPECT_CALL(delegate_, OnGaiaFlowCompleted("fake_access_token", "", "")); |
| 108 flow->OnAuthFlowTitleChange(GetFinalTitle("access_token=fake_access_token")); |
| 109 } |
| 110 |
| 111 TEST_F(IdentityGaiaWebAuthFlowTest, TokenAndExpiration) { |
| 112 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 113 flow->Start(); |
| 114 EXPECT_CALL(delegate_, OnGaiaFlowCompleted("fake_access_token", "3600", "")); |
| 115 flow->OnAuthFlowTitleChange( |
| 116 GetFinalTitle("access_token=fake_access_token&expires_in=3600")); |
| 117 } |
| 118 |
| 119 TEST_F(IdentityGaiaWebAuthFlowTest, ExtraFragmentParameters) { |
| 120 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 121 flow->Start(); |
| 122 EXPECT_CALL(delegate_, |
| 123 OnGaiaFlowCompleted("fake_access_token", "3600", "fake_error")); |
| 124 flow->OnAuthFlowTitleChange(GetFinalTitle("chaff1=stuff&" |
| 125 "expires_in=3600&" |
| 126 "chaff2=and&" |
| 127 "error=fake_error&" |
| 128 "chaff3=nonsense&" |
| 129 "access_token=fake_access_token&" |
| 130 "chaff4=")); |
| 131 } |
| 132 |
| 133 TEST_F(IdentityGaiaWebAuthFlowTest, TitleSpam) { |
| 134 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 135 flow->Start(); |
| 136 flow->OnAuthFlowTitleChange( |
| 137 "Loading https://extension_id.chromiumapp.org/#error=non_final_title"); |
| 138 flow->OnAuthFlowTitleChange("I'm feeling entitled."); |
| 139 flow->OnAuthFlowTitleChange(""); |
| 140 flow->OnAuthFlowTitleChange( |
| 141 "Loading id.client.fake:/bad_extension_id#error=non_final_title"); |
| 142 flow->OnAuthFlowTitleChange( |
| 143 "Loading bad.id.client.fake:/extension_id#error=non_final_title"); |
| 144 EXPECT_CALL(delegate_, OnGaiaFlowCompleted("fake_access_token", "", "")); |
| 145 flow->OnAuthFlowTitleChange(GetFinalTitle("access_token=fake_access_token")); |
| 146 } |
| 147 |
| 148 TEST_F(IdentityGaiaWebAuthFlowTest, EmptyFragment) { |
| 149 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 150 flow->Start(); |
| 151 EXPECT_CALL( |
| 152 delegate_, |
| 153 OnGaiaFlowFailure( |
| 154 GaiaWebAuthFlow::INVALID_REDIRECT, |
| 155 GoogleServiceAuthError(GoogleServiceAuthError::NONE))); |
| 156 flow->OnAuthFlowTitleChange(GetFinalTitle("")); |
| 157 } |
| 158 |
| 159 TEST_F(IdentityGaiaWebAuthFlowTest, JunkFragment) { |
| 160 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 161 flow->Start(); |
| 162 EXPECT_CALL( |
| 163 delegate_, |
| 164 OnGaiaFlowFailure( |
| 165 GaiaWebAuthFlow::INVALID_REDIRECT, |
| 166 GoogleServiceAuthError(GoogleServiceAuthError::NONE))); |
| 167 flow->OnAuthFlowTitleChange(GetFinalTitle("thisisjustabunchofjunk")); |
| 168 } |
| 169 |
| 170 TEST_F(IdentityGaiaWebAuthFlowTest, NoFragment) { |
| 171 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 172 flow->Start(); |
| 173 // This won't be recognized as an interesting title. |
| 174 flow->OnAuthFlowTitleChange("Loading id.client.fake:/extension_id"); |
| 175 } |
| 176 |
| 177 TEST_F(IdentityGaiaWebAuthFlowTest, Host) { |
| 178 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 179 flow->Start(); |
| 180 // These won't be recognized as interesting titles. |
| 181 flow->OnAuthFlowTitleChange( |
| 182 "Loading id.client.fake://extension_id#access_token=fake_access_token"); |
| 183 flow->OnAuthFlowTitleChange( |
| 184 "Loading id.client.fake://extension_id/#access_token=fake_access_token"); |
| 185 flow->OnAuthFlowTitleChange( |
| 186 "Loading " |
| 187 "id.client.fake://host/extension_id/#access_token=fake_access_token"); |
| 188 } |
| 189 |
| 190 TEST_F(IdentityGaiaWebAuthFlowTest, UbertokenFailure) { |
| 191 set_ubertoken_error(GoogleServiceAuthError::CONNECTION_FAILED); |
| 192 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 193 EXPECT_CALL( |
| 194 delegate_, |
| 195 OnGaiaFlowFailure( |
| 196 GaiaWebAuthFlow::SERVICE_AUTH_ERROR, |
| 197 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED))); |
| 198 flow->Start(); |
| 199 } |
| 200 |
| 201 TEST_F(IdentityGaiaWebAuthFlowTest, AuthFlowFailure) { |
| 202 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow(); |
| 203 flow->Start(); |
| 204 EXPECT_CALL( |
| 205 delegate_, |
| 206 OnGaiaFlowFailure( |
| 207 GaiaWebAuthFlow::WINDOW_CLOSED, |
| 208 GoogleServiceAuthError(GoogleServiceAuthError::NONE))); |
| 209 flow->OnAuthFlowFailure(WebAuthFlow::WINDOW_CLOSED); |
| 210 } |
| 211 |
| 212 } // namespace extensions |
OLD | NEW |