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

Side by Side Diff: chrome/browser/extensions/api/identity/gaia_web_auth_flow_unittest.cc

Issue 15148007: Identity API: web-based scope approval dialogs for getAuthToken (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address reviewer comments Created 7 years, 7 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
OLDNEW
(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_METHOD3(OnGaiaFlowFailure,
58 void(GaiaWebAuthFlow::Failure failure,
59 GoogleServiceAuthError service_error,
60 const std::string& oauth_error));
61 MOCK_METHOD2(OnGaiaFlowCompleted,
62 void(const std::string& access_token,
63 const std::string& expiration));
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_, OnGaiaFlowFailure(
101 GaiaWebAuthFlow::OAUTH_ERROR,
102 GoogleServiceAuthError(GoogleServiceAuthError::NONE),
103 "access_denied"));
104 flow->OnAuthFlowTitleChange(GetFinalTitle("error=access_denied"));
105 }
106
107 TEST_F(IdentityGaiaWebAuthFlowTest, Token) {
108 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
109 flow->Start();
110 EXPECT_CALL(delegate_, OnGaiaFlowCompleted("fake_access_token", ""));
111 flow->OnAuthFlowTitleChange(GetFinalTitle("access_token=fake_access_token"));
112 }
113
114 TEST_F(IdentityGaiaWebAuthFlowTest, TokenAndExpiration) {
115 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
116 flow->Start();
117 EXPECT_CALL(delegate_, OnGaiaFlowCompleted("fake_access_token", "3600"));
118 flow->OnAuthFlowTitleChange(
119 GetFinalTitle("access_token=fake_access_token&expires_in=3600"));
120 }
121
122 TEST_F(IdentityGaiaWebAuthFlowTest, ExtraFragmentParametersSuccess) {
123 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
124 flow->Start();
125 EXPECT_CALL(delegate_,
126 OnGaiaFlowCompleted("fake_access_token", "3600"));
127 flow->OnAuthFlowTitleChange(GetFinalTitle("chaff1=stuff&"
128 "expires_in=3600&"
129 "chaff2=and&"
130 "nonerror=fake_error&"
131 "chaff3=nonsense&"
132 "access_token=fake_access_token&"
133 "chaff4="));
134 }
135
136 TEST_F(IdentityGaiaWebAuthFlowTest, ExtraFragmentParametersError) {
137 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
138 flow->Start();
139 EXPECT_CALL(delegate_, OnGaiaFlowFailure(
140 GaiaWebAuthFlow::OAUTH_ERROR,
141 GoogleServiceAuthError(GoogleServiceAuthError::NONE),
142 "fake_error"));
143 flow->OnAuthFlowTitleChange(GetFinalTitle("chaff1=stuff&"
144 "expires_in=3600&"
145 "chaff2=and&"
146 "error=fake_error&"
147 "chaff3=nonsense&"
148 "access_token=fake_access_token&"
149 "chaff4="));
150 }
151
152 TEST_F(IdentityGaiaWebAuthFlowTest, TitleSpam) {
153 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
154 flow->Start();
155 flow->OnAuthFlowTitleChange(
156 "Loading https://extension_id.chromiumapp.org/#error=non_final_title");
157 flow->OnAuthFlowTitleChange("I'm feeling entitled.");
158 flow->OnAuthFlowTitleChange("");
159 flow->OnAuthFlowTitleChange(
160 "Loading id.client.fake:/bad_extension_id#error=non_final_title");
161 flow->OnAuthFlowTitleChange(
162 "Loading bad.id.client.fake:/extension_id#error=non_final_title");
163 EXPECT_CALL(delegate_, OnGaiaFlowCompleted("fake_access_token", ""));
164 flow->OnAuthFlowTitleChange(GetFinalTitle("access_token=fake_access_token"));
165 }
166
167 TEST_F(IdentityGaiaWebAuthFlowTest, EmptyFragment) {
168 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
169 flow->Start();
170 EXPECT_CALL(
171 delegate_,
172 OnGaiaFlowFailure(
173 GaiaWebAuthFlow::INVALID_REDIRECT,
174 GoogleServiceAuthError(GoogleServiceAuthError::NONE),
175 ""));
176 flow->OnAuthFlowTitleChange(GetFinalTitle(""));
177 }
178
179 TEST_F(IdentityGaiaWebAuthFlowTest, JunkFragment) {
180 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
181 flow->Start();
182 EXPECT_CALL(
183 delegate_,
184 OnGaiaFlowFailure(
185 GaiaWebAuthFlow::INVALID_REDIRECT,
186 GoogleServiceAuthError(GoogleServiceAuthError::NONE),
187 ""));
188 flow->OnAuthFlowTitleChange(GetFinalTitle("thisisjustabunchofjunk"));
189 }
190
191 TEST_F(IdentityGaiaWebAuthFlowTest, NoFragment) {
192 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
193 flow->Start();
194 // This won't be recognized as an interesting title.
195 flow->OnAuthFlowTitleChange("Loading id.client.fake:/extension_id");
196 }
197
198 TEST_F(IdentityGaiaWebAuthFlowTest, Host) {
199 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
200 flow->Start();
201 // These won't be recognized as interesting titles.
202 flow->OnAuthFlowTitleChange(
203 "Loading id.client.fake://extension_id#access_token=fake_access_token");
204 flow->OnAuthFlowTitleChange(
205 "Loading id.client.fake://extension_id/#access_token=fake_access_token");
206 flow->OnAuthFlowTitleChange(
207 "Loading "
208 "id.client.fake://host/extension_id/#access_token=fake_access_token");
209 }
210
211 TEST_F(IdentityGaiaWebAuthFlowTest, UbertokenFailure) {
212 set_ubertoken_error(GoogleServiceAuthError::CONNECTION_FAILED);
213 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
214 EXPECT_CALL(
215 delegate_,
216 OnGaiaFlowFailure(
217 GaiaWebAuthFlow::SERVICE_AUTH_ERROR,
218 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED),
219 ""));
220 flow->Start();
221 }
222
223 TEST_F(IdentityGaiaWebAuthFlowTest, AuthFlowFailure) {
224 scoped_ptr<TestGaiaWebAuthFlow> flow = CreateTestFlow();
225 flow->Start();
226 EXPECT_CALL(
227 delegate_,
228 OnGaiaFlowFailure(
229 GaiaWebAuthFlow::WINDOW_CLOSED,
230 GoogleServiceAuthError(GoogleServiceAuthError::NONE),
231 ""));
232 flow->OnAuthFlowFailure(WebAuthFlow::WINDOW_CLOSED);
233 }
234
235 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/identity/gaia_web_auth_flow.cc ('k') | chrome/browser/extensions/api/identity/identity_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698