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

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

Issue 10178020: Start implementing an auth flow for platform apps to be able to do auth (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 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 | Annotate | Revision Log
OLDNEW
(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 #include "base/message_loop.h"
6 #include "chrome/browser/extensions/api/identity/web_auth_flow.h"
7 #include "chrome/browser/ui/extensions/web_auth_flow_window.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/test/test_browser_thread.h"
12 #include "content/test/web_contents_tester.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using content::BrowserContext;
17 using content::BrowserThread;
18 using content::TestBrowserThread;
19 using content::WebContents;
20 using content::WebContentsDelegate;
21 using content::WebContentsTester;
22 using extensions::WebAuthFlow;
23 using testing::Return;
24 using testing::ReturnRef;
25
26 class MockDelegate : public WebAuthFlow::Delegate {
27 public:
28 MOCK_METHOD1(OnAuthFlowSuccess, void(const std::string& redirect_url));
29 MOCK_METHOD0(OnAuthFlowFailure, void());
30 };
31
32 class MockWebAuthFlowWindow : public WebAuthFlowWindow {
33 public:
34 MockWebAuthFlowWindow() : WebAuthFlowWindow(NULL, NULL, NULL) {
35 }
36
37 virtual void Show() OVERRIDE {
38 // Do nothing in tests.
39 }
40 };
41
42 class MockWebAuthFlow : public WebAuthFlow {
43 public:
44 MockWebAuthFlow(
45 MockDelegate* delegate,
46 BrowserContext* browser_context,
47 const std::string& extension_id,
48 const GURL& provider_url)
49 : WebAuthFlow(delegate,
50 browser_context,
51 extension_id,
52 provider_url),
53 browser_context_(browser_context),
54 web_contents_(NULL),
55 window_(NULL) { }
56
57 virtual WebContents* CreateWebContents() OVERRIDE {
58 web_contents_ = WebContentsTester::CreateTestWebContents(
59 browser_context_, NULL);
60 return web_contents_;
61 }
62
63 virtual WebAuthFlowWindow* CreateAuthWindow() OVERRIDE {
64 window_ = new MockWebAuthFlowWindow();
65 return window_;
66 }
67
68 WebContents* contents() {
69 return web_contents_;
70 }
71
72 WebContentsTester* contents_tester() {
73 return WebContentsTester::For(web_contents_);
74 }
75
76 MockWebAuthFlowWindow& window() {
77 return *window_;
78 }
79
80 bool HasWindow() const {
81 return window_ != NULL;
82 }
83
84 virtual ~MockWebAuthFlow() { }
85
86 private:
87 BrowserContext* browser_context_;
88 WebContents* web_contents_;
89 MockWebAuthFlowWindow* window_;
90 };
91
92 class WebAuthFlowTest : public ChromeRenderViewHostTestHarness {
93 protected:
94 WebAuthFlowTest()
95 : thread_(BrowserThread::UI, &message_loop_) {
96 }
97
98 virtual void SetUp() {
99 ChromeRenderViewHostTestHarness::SetUp();
100 }
101
102 void CreateAuthFlow(const std::string& extension_id, const GURL& url) {
103 flow_.reset(new MockWebAuthFlow(&delegate_, profile(), extension_id, url));
104 }
105
106 MockWebAuthFlow& flow() {
107 return *flow_.get();
108 }
109
110 WebAuthFlow* flow_base() {
111 return flow_.get();
112 }
113
114 void CallOnClose() {
115 flow_base()->OnClose();
116 }
117
118 bool CallIsValidRedirectUrl(const GURL& url) {
119 return flow_base()->IsValidRedirectUrl(url);
120 }
121
122 TestBrowserThread thread_;
123 scoped_ptr<MockWebAuthFlow> flow_;
124 MockDelegate delegate_;
125 };
126
127 TEST_F(WebAuthFlowTest, SilentRedirectToChromiumAppUrl) {
128 std::string ext_id = "abcdefghij";
129 GURL url("https://accounts.google.com/o/oauth2/auth");
130 GURL result("https://abcdefghij.chromiumapp.org/google_cb");
131
132 CreateAuthFlow(ext_id, url);
133 EXPECT_CALL(delegate_, OnAuthFlowSuccess(result.spec())).Times(1);
134 flow_->Start();
135 flow_->contents_tester()->NavigateAndCommit(result);
136 }
137
138 TEST_F(WebAuthFlowTest, SilentRedirectToChromeExtensionSchemeUrl) {
139 std::string ext_id = "abcdefghij";
140 GURL url("https://accounts.google.com/o/oauth2/auth");
141 GURL result("chrome-extension://abcdefghij/google_cb");
142
143 CreateAuthFlow(ext_id, url);
144 EXPECT_CALL(delegate_, OnAuthFlowSuccess(result.spec())).Times(1);
145 flow_->Start();
146 flow_->contents_tester()->NavigateAndCommit(result);
147 }
148
149 TEST_F(WebAuthFlowTest, UIResultsInSuccess) {
150 std::string ext_id = "abcdefghij";
151 GURL url("https://accounts.google.com/o/oauth2/auth");
152 GURL result("chrome-extension://abcdefghij/google_cb");
153
154 CreateAuthFlow(ext_id, url);
155 EXPECT_CALL(delegate_, OnAuthFlowSuccess(result.spec())).Times(1);
156 flow_->Start();
157 flow_->contents_tester()->TestSetIsLoading(false);
158 EXPECT_TRUE(flow_->HasWindow());
159 flow_->contents_tester()->NavigateAndCommit(result);
160 }
161
162 TEST_F(WebAuthFlowTest, UIClosedByUser) {
163 std::string ext_id = "abcdefghij";
164 GURL url("https://accounts.google.com/o/oauth2/auth");
165 GURL result("chrome-extension://abcdefghij/google_cb");
166
167 CreateAuthFlow(ext_id, url);
168 EXPECT_CALL(delegate_, OnAuthFlowFailure()).Times(1);
169 flow_->Start();
170 flow_->contents_tester()->TestSetIsLoading(false);
171 EXPECT_TRUE(flow_->HasWindow());
172 CallOnClose();
173 }
174
175 TEST_F(WebAuthFlowTest, IsValidRedirectUrl) {
176 std::string ext_id = "abcdefghij";
177 GURL url("https://accounts.google.com/o/oauth2/auth");
178
179 CreateAuthFlow(ext_id, url);
180
181 // Positive cases.
182 EXPECT_TRUE(CallIsValidRedirectUrl(
183 GURL("https://abcdefghij.chromiumapp.org/")));
184 EXPECT_TRUE(CallIsValidRedirectUrl(
185 GURL("https://abcdefghij.chromiumapp.org/callback")));
186 EXPECT_TRUE(CallIsValidRedirectUrl(
187 GURL("chrome-extension://abcdefghij/")));
188 EXPECT_TRUE(CallIsValidRedirectUrl(
189 GURL("chrome-extension://abcdefghij/callback")));
190
191 // Negative cases.
192 EXPECT_FALSE(CallIsValidRedirectUrl(
193 GURL("https://www.foo.com/")));
194 // http scheme is not allowed.
195 EXPECT_FALSE(CallIsValidRedirectUrl(
196 GURL("http://abcdefghij.chromiumapp.org/callback")));
197 EXPECT_FALSE(CallIsValidRedirectUrl(
198 GURL("https://abcd.chromiumapp.org/callback")));
199 EXPECT_FALSE(CallIsValidRedirectUrl(
200 GURL("chrome-extension://abcd/callback")));
201 EXPECT_FALSE(CallIsValidRedirectUrl(
202 GURL("chrome-extension://abcdefghijkl/")));
203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698