| 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 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 friend class WebAuthFlowTest; |
| 88 |
| 89 BrowserContext* browser_context_; |
| 90 WebContents* web_contents_; |
| 91 MockWebAuthFlowWindow* window_; |
| 92 }; |
| 93 |
| 94 class WebAuthFlowTest : public ChromeRenderViewHostTestHarness { |
| 95 protected: |
| 96 WebAuthFlowTest() |
| 97 : thread_(BrowserThread::UI, &message_loop_) { |
| 98 } |
| 99 |
| 100 virtual void SetUp() { |
| 101 ChromeRenderViewHostTestHarness::SetUp(); |
| 102 } |
| 103 |
| 104 void CreateAuthFlow(const std::string& extension_id, const GURL& url) { |
| 105 flow_.reset(new MockWebAuthFlow(&delegate_, profile(), extension_id, url)); |
| 106 } |
| 107 |
| 108 MockWebAuthFlow& flow() { |
| 109 return *flow_.get(); |
| 110 } |
| 111 |
| 112 WebAuthFlow* flow_base() { |
| 113 return flow_.get(); |
| 114 } |
| 115 |
| 116 void CallOnClose() { |
| 117 flow_base()->OnClose(); |
| 118 } |
| 119 |
| 120 bool CallIsValidRedirectUrl(const GURL& url) { |
| 121 return flow_base()->IsValidRedirectUrl(url); |
| 122 } |
| 123 |
| 124 TestBrowserThread thread_; |
| 125 scoped_ptr<MockWebAuthFlow> flow_; |
| 126 MockDelegate delegate_; |
| 127 }; |
| 128 |
| 129 TEST_F(WebAuthFlowTest, SilentRedirectToChromiumAppUrl) { |
| 130 std::string ext_id = "abcdefghij"; |
| 131 GURL url("https://accounts.google.com/o/oauth2/auth"); |
| 132 GURL result("https://abcdefghij.chromiumapp.org/google_cb"); |
| 133 |
| 134 CreateAuthFlow(ext_id, url); |
| 135 EXPECT_CALL(delegate_, OnAuthFlowSuccess(result.spec())).Times(1); |
| 136 flow_->Start(); |
| 137 flow_->contents_tester()->NavigateAndCommit(result); |
| 138 } |
| 139 |
| 140 TEST_F(WebAuthFlowTest, SilentRedirectToChromeExtensionSchemeUrl) { |
| 141 std::string ext_id = "abcdefghij"; |
| 142 GURL url("https://accounts.google.com/o/oauth2/auth"); |
| 143 GURL result("chrome-extension://abcdefghij/google_cb"); |
| 144 |
| 145 CreateAuthFlow(ext_id, url); |
| 146 EXPECT_CALL(delegate_, OnAuthFlowSuccess(result.spec())).Times(1); |
| 147 flow_->Start(); |
| 148 flow_->contents_tester()->NavigateAndCommit(result); |
| 149 } |
| 150 |
| 151 TEST_F(WebAuthFlowTest, UIResultsInSuccess) { |
| 152 std::string ext_id = "abcdefghij"; |
| 153 GURL url("https://accounts.google.com/o/oauth2/auth"); |
| 154 GURL result("chrome-extension://abcdefghij/google_cb"); |
| 155 |
| 156 CreateAuthFlow(ext_id, url); |
| 157 EXPECT_CALL(delegate_, OnAuthFlowSuccess(result.spec())).Times(1); |
| 158 flow_->Start(); |
| 159 flow_->contents_tester()->TestSetIsLoading(false); |
| 160 EXPECT_TRUE(flow_->HasWindow()); |
| 161 flow_->contents_tester()->NavigateAndCommit(result); |
| 162 } |
| 163 |
| 164 TEST_F(WebAuthFlowTest, UIClosedByUser) { |
| 165 std::string ext_id = "abcdefghij"; |
| 166 GURL url("https://accounts.google.com/o/oauth2/auth"); |
| 167 GURL result("chrome-extension://abcdefghij/google_cb"); |
| 168 |
| 169 CreateAuthFlow(ext_id, url); |
| 170 EXPECT_CALL(delegate_, OnAuthFlowFailure()).Times(1); |
| 171 flow_->Start(); |
| 172 flow_->contents_tester()->TestSetIsLoading(false); |
| 173 EXPECT_TRUE(flow_->HasWindow()); |
| 174 CallOnClose(); |
| 175 } |
| 176 |
| 177 TEST_F(WebAuthFlowTest, IsValidRedirectUrl) { |
| 178 std::string ext_id = "abcdefghij"; |
| 179 GURL url("https://accounts.google.com/o/oauth2/auth"); |
| 180 |
| 181 CreateAuthFlow(ext_id, url); |
| 182 |
| 183 // Positive cases. |
| 184 EXPECT_TRUE(CallIsValidRedirectUrl( |
| 185 GURL("https://abcdefghij.chromiumapp.org/"))); |
| 186 EXPECT_TRUE(CallIsValidRedirectUrl( |
| 187 GURL("https://abcdefghij.chromiumapp.org/callback"))); |
| 188 EXPECT_TRUE(CallIsValidRedirectUrl( |
| 189 GURL("chrome-extension://abcdefghij/"))); |
| 190 EXPECT_TRUE(CallIsValidRedirectUrl( |
| 191 GURL("chrome-extension://abcdefghij/callback"))); |
| 192 |
| 193 // Negative cases. |
| 194 EXPECT_FALSE(CallIsValidRedirectUrl( |
| 195 GURL("https://www.foo.com/"))); |
| 196 // http scheme is not allowed. |
| 197 EXPECT_FALSE(CallIsValidRedirectUrl( |
| 198 GURL("http://abcdefghij.chromiumapp.org/callback"))); |
| 199 EXPECT_FALSE(CallIsValidRedirectUrl( |
| 200 GURL("https://abcd.chromiumapp.org/callback"))); |
| 201 EXPECT_FALSE(CallIsValidRedirectUrl( |
| 202 GURL("chrome-extension://abcd/callback"))); |
| 203 EXPECT_FALSE(CallIsValidRedirectUrl( |
| 204 GURL("chrome-extension://abcdefghijkl/"))); |
| 205 } |
| OLD | NEW |