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 #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 namespace { |
| 27 |
| 28 class MockDelegate : public WebAuthFlow::Delegate { |
| 29 public: |
| 30 MOCK_METHOD1(OnAuthFlowSuccess, void(const std::string& redirect_url)); |
| 31 MOCK_METHOD0(OnAuthFlowFailure, void()); |
| 32 }; |
| 33 |
| 34 class MockWebAuthFlowWindow : public WebAuthFlowWindow { |
| 35 public: |
| 36 MockWebAuthFlowWindow() : WebAuthFlowWindow(NULL, NULL, NULL) { |
| 37 } |
| 38 |
| 39 virtual void Show() OVERRIDE { |
| 40 // Do nothing in tests. |
| 41 } |
| 42 }; |
| 43 |
| 44 class MockWebAuthFlow : public WebAuthFlow { |
| 45 public: |
| 46 MockWebAuthFlow( |
| 47 WebAuthFlow::Delegate* delegate, |
| 48 BrowserContext* browser_context, |
| 49 const std::string& extension_id, |
| 50 const GURL& provider_url) |
| 51 : WebAuthFlow(delegate, |
| 52 browser_context, |
| 53 extension_id, |
| 54 provider_url), |
| 55 browser_context_(browser_context), |
| 56 web_contents_(NULL), |
| 57 window_(NULL) { } |
| 58 |
| 59 virtual WebContents* CreateWebContents() OVERRIDE { |
| 60 CHECK(!web_contents_); |
| 61 web_contents_ = WebContentsTester::CreateTestWebContents( |
| 62 browser_context_, NULL); |
| 63 return web_contents_; |
| 64 } |
| 65 |
| 66 virtual WebAuthFlowWindow* CreateAuthWindow() OVERRIDE { |
| 67 CHECK(!window_); |
| 68 window_ = new MockWebAuthFlowWindow(); |
| 69 return window_; |
| 70 } |
| 71 |
| 72 WebContents* contents() { |
| 73 return web_contents_; |
| 74 } |
| 75 |
| 76 WebContentsTester* contents_tester() { |
| 77 return WebContentsTester::For(web_contents_); |
| 78 } |
| 79 |
| 80 MockWebAuthFlowWindow& window() { |
| 81 return *window_; |
| 82 } |
| 83 |
| 84 bool HasWindow() const { |
| 85 return window_ != NULL; |
| 86 } |
| 87 |
| 88 virtual ~MockWebAuthFlow() { } |
| 89 |
| 90 private: |
| 91 BrowserContext* browser_context_; |
| 92 WebContents* web_contents_; |
| 93 MockWebAuthFlowWindow* window_; |
| 94 }; |
| 95 |
| 96 } // namespace |
| 97 |
| 98 class WebAuthFlowTest : public ChromeRenderViewHostTestHarness { |
| 99 protected: |
| 100 WebAuthFlowTest() |
| 101 : thread_(BrowserThread::UI, &message_loop_) { |
| 102 } |
| 103 |
| 104 virtual void SetUp() { |
| 105 ChromeRenderViewHostTestHarness::SetUp(); |
| 106 } |
| 107 |
| 108 void CreateAuthFlow(const std::string& extension_id, const GURL& url) { |
| 109 flow_.reset(new MockWebAuthFlow(&delegate_, profile(), extension_id, url)); |
| 110 } |
| 111 |
| 112 MockWebAuthFlow& flow() { |
| 113 return *flow_.get(); |
| 114 } |
| 115 |
| 116 WebAuthFlow* flow_base() { |
| 117 return flow_.get(); |
| 118 } |
| 119 |
| 120 void CallOnClose() { |
| 121 flow_base()->OnClose(); |
| 122 } |
| 123 |
| 124 bool CallIsValidRedirectUrl(const GURL& url) { |
| 125 return flow_base()->IsValidRedirectUrl(url); |
| 126 } |
| 127 |
| 128 TestBrowserThread thread_; |
| 129 MockDelegate delegate_; |
| 130 scoped_ptr<MockWebAuthFlow> flow_; |
| 131 }; |
| 132 |
| 133 TEST_F(WebAuthFlowTest, SilentRedirectToChromiumAppUrl) { |
| 134 std::string ext_id = "abcdefghij"; |
| 135 GURL url("https://accounts.google.com/o/oauth2/auth"); |
| 136 GURL result("https://abcdefghij.chromiumapp.org/google_cb"); |
| 137 |
| 138 CreateAuthFlow(ext_id, url); |
| 139 EXPECT_CALL(delegate_, OnAuthFlowSuccess(result.spec())).Times(1); |
| 140 flow_->Start(); |
| 141 flow_->contents_tester()->NavigateAndCommit(result); |
| 142 } |
| 143 |
| 144 TEST_F(WebAuthFlowTest, SilentRedirectToChromeExtensionSchemeUrl) { |
| 145 std::string ext_id = "abcdefghij"; |
| 146 GURL url("https://accounts.google.com/o/oauth2/auth"); |
| 147 GURL result("chrome-extension://abcdefghij/google_cb"); |
| 148 |
| 149 CreateAuthFlow(ext_id, url); |
| 150 EXPECT_CALL(delegate_, OnAuthFlowSuccess(result.spec())).Times(1); |
| 151 flow_->Start(); |
| 152 flow_->contents_tester()->NavigateAndCommit(result); |
| 153 } |
| 154 |
| 155 TEST_F(WebAuthFlowTest, UIResultsInSuccess) { |
| 156 std::string ext_id = "abcdefghij"; |
| 157 GURL url("https://accounts.google.com/o/oauth2/auth"); |
| 158 GURL result("chrome-extension://abcdefghij/google_cb"); |
| 159 |
| 160 CreateAuthFlow(ext_id, url); |
| 161 EXPECT_CALL(delegate_, OnAuthFlowSuccess(result.spec())).Times(1); |
| 162 flow_->Start(); |
| 163 flow_->contents_tester()->TestSetIsLoading(false); |
| 164 EXPECT_TRUE(flow_->HasWindow()); |
| 165 flow_->contents_tester()->NavigateAndCommit(result); |
| 166 } |
| 167 |
| 168 TEST_F(WebAuthFlowTest, UIClosedByUser) { |
| 169 std::string ext_id = "abcdefghij"; |
| 170 GURL url("https://accounts.google.com/o/oauth2/auth"); |
| 171 GURL result("chrome-extension://abcdefghij/google_cb"); |
| 172 |
| 173 CreateAuthFlow(ext_id, url); |
| 174 EXPECT_CALL(delegate_, OnAuthFlowFailure()).Times(1); |
| 175 flow_->Start(); |
| 176 flow_->contents_tester()->TestSetIsLoading(false); |
| 177 EXPECT_TRUE(flow_->HasWindow()); |
| 178 CallOnClose(); |
| 179 } |
| 180 |
| 181 TEST_F(WebAuthFlowTest, IsValidRedirectUrl) { |
| 182 std::string ext_id = "abcdefghij"; |
| 183 GURL url("https://accounts.google.com/o/oauth2/auth"); |
| 184 |
| 185 CreateAuthFlow(ext_id, url); |
| 186 |
| 187 // Positive cases. |
| 188 EXPECT_TRUE(CallIsValidRedirectUrl( |
| 189 GURL("https://abcdefghij.chromiumapp.org/"))); |
| 190 EXPECT_TRUE(CallIsValidRedirectUrl( |
| 191 GURL("https://abcdefghij.chromiumapp.org/callback"))); |
| 192 EXPECT_TRUE(CallIsValidRedirectUrl( |
| 193 GURL("chrome-extension://abcdefghij/"))); |
| 194 EXPECT_TRUE(CallIsValidRedirectUrl( |
| 195 GURL("chrome-extension://abcdefghij/callback"))); |
| 196 |
| 197 // Negative cases. |
| 198 EXPECT_FALSE(CallIsValidRedirectUrl( |
| 199 GURL("https://www.foo.com/"))); |
| 200 // http scheme is not allowed. |
| 201 EXPECT_FALSE(CallIsValidRedirectUrl( |
| 202 GURL("http://abcdefghij.chromiumapp.org/callback"))); |
| 203 EXPECT_FALSE(CallIsValidRedirectUrl( |
| 204 GURL("https://abcd.chromiumapp.org/callback"))); |
| 205 EXPECT_FALSE(CallIsValidRedirectUrl( |
| 206 GURL("chrome-extension://abcd/callback"))); |
| 207 EXPECT_FALSE(CallIsValidRedirectUrl( |
| 208 GURL("chrome-extension://abcdefghijkl/"))); |
| 209 } |
OLD | NEW |