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

Side by Side Diff: ios/chrome/browser/web/http_auth_egtest.mm

Issue 2644223002: Integration tests for WebStateDelegate::OnAuthRequired callback. (Closed)
Patch Set: Addressed review comments Created 3 years, 11 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
« no previous file with comments | « no previous file | ios/web/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <EarlGrey/EarlGrey.h> 5 #import <EarlGrey/EarlGrey.h>
6 6
7 #include "base/base64.h"
8 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/sys_string_conversions.h" 8 #include "base/strings/sys_string_conversions.h"
11 #include "components/strings/grit/components_strings.h" 9 #include "components/strings/grit/components_strings.h"
12 #include "ios/chrome/browser/ui/ui_util.h" 10 #include "ios/chrome/browser/ui/ui_util.h"
13 #include "ios/chrome/grit/ios_strings.h" 11 #include "ios/chrome/grit/ios_strings.h"
14 #include "ios/chrome/test/app/navigation_test_util.h" 12 #include "ios/chrome/test/app/navigation_test_util.h"
15 #import "ios/chrome/test/earl_grey/chrome_matchers.h" 13 #import "ios/chrome/test/earl_grey/chrome_matchers.h"
16 #import "ios/chrome/test/earl_grey/chrome_test_case.h" 14 #import "ios/chrome/test/earl_grey/chrome_test_case.h"
17 #import "ios/testing/wait_util.h" 15 #import "ios/testing/wait_util.h"
18 #import "ios/web/public/test/http_server.h" 16 #import "ios/web/public/test/http_server.h"
19 #include "ios/web/public/test/http_server_util.h" 17 #include "ios/web/public/test/http_server_util.h"
20 #include "ios/web/public/test/response_providers/html_response_provider.h" 18 #import "ios/web/public/test/response_providers/http_auth_response_provider.h"
21 #import "ios/testing/earl_grey/disabled_test_macros.h" 19 #import "ios/testing/earl_grey/disabled_test_macros.h"
22 #include "ui/base/l10n/l10n_util_mac.h" 20 #include "ui/base/l10n/l10n_util_mac.h"
23 #include "url/gurl.h" 21 #include "url/gurl.h"
24 22
25 using testing::WaitUntilConditionOrTimeout; 23 using testing::WaitUntilConditionOrTimeout;
26 using testing::kWaitForPageLoadTimeout; 24 using testing::kWaitForPageLoadTimeout;
27 using chrome_test_util::webViewContainingText; 25 using chrome_test_util::webViewContainingText;
28 26
29 namespace { 27 namespace {
30 28
31 // Serves a page which requires Basic HTTP Authentication.
32 class HttpAuthResponseProvider : public HtmlResponseProvider {
33 public:
34 // Constructs provider which will respond to the given |url| and will use the
35 // given authenticaion |realm|. |username| and |password| are credentials
36 // required for sucessfull authentication. Use different realms and
37 // username/password combination for different tests to prevent credentials
38 // caching.
39 HttpAuthResponseProvider(const GURL& url,
40 const std::string& realm,
41 const std::string& username,
42 const std::string& password)
43 : url_(url), realm_(realm), username_(username), password_(password) {}
44 ~HttpAuthResponseProvider() override {}
45
46 // HtmlResponseProvider overrides:
47 bool CanHandleRequest(const Request& request) override {
48 return request.url == url_;
49 }
50 void GetResponseHeadersAndBody(
51 const Request& request,
52 scoped_refptr<net::HttpResponseHeaders>* headers,
53 std::string* response_body) override {
54 if (HeadersHaveValidCredentials(request.headers)) {
55 *response_body = page_text();
56 *headers = GetDefaultResponseHeaders();
57 } else {
58 *headers = GetResponseHeaders("", net::HTTP_UNAUTHORIZED);
59 (*headers)->AddHeader(base::StringPrintf(
60 "WWW-Authenticate: Basic realm=\"%s\"", realm_.c_str()));
61 }
62 }
63
64 // Text returned in response if authentication was successfull.
65 static std::string page_text() { return "authenticated"; }
66
67 private:
68 // Check if authorization header has valid credintials:
69 // https://tools.ietf.org/html/rfc1945#section-10.2
70 bool HeadersHaveValidCredentials(const net::HttpRequestHeaders& headers) {
71 std::string header;
72 if (headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &header)) {
73 std::string auth =
74 base::StringPrintf("%s:%s", username_.c_str(), password_.c_str());
75 std::string encoded_auth;
76 base::Base64Encode(auth, &encoded_auth);
77 return header == base::StringPrintf("Basic %s", encoded_auth.c_str());
78 }
79 return false;
80 }
81
82 // URL this provider responds to.
83 GURL url_;
84 // HTTP Authentication realm.
85 std::string realm_;
86 // Correct username to pass authentication
87 std::string username_;
88 // Correct password to pass authentication
89 std::string password_;
90 };
91
92 // Returns matcher for HTTP Authentication dialog. 29 // Returns matcher for HTTP Authentication dialog.
93 id<GREYMatcher> httpAuthDialog() { 30 id<GREYMatcher> httpAuthDialog() {
94 NSString* title = l10n_util::GetNSStringWithFixup(IDS_LOGIN_DIALOG_TITLE); 31 NSString* title = l10n_util::GetNSStringWithFixup(IDS_LOGIN_DIALOG_TITLE);
95 return chrome_test_util::staticTextWithAccessibilityLabel(title); 32 return chrome_test_util::staticTextWithAccessibilityLabel(title);
96 } 33 }
97 34
98 // Returns matcher for Username text field. 35 // Returns matcher for Username text field.
99 id<GREYMatcher> usernameField() { 36 id<GREYMatcher> usernameField() {
100 return chrome_test_util::staticTextWithAccessibilityLabelId( 37 return chrome_test_util::staticTextWithAccessibilityLabelId(
101 IDS_IOS_HTTP_LOGIN_DIALOG_USERNAME_PLACEHOLDER); 38 IDS_IOS_HTTP_LOGIN_DIALOG_USERNAME_PLACEHOLDER);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 // Tests Basic HTTP Authentication with correct username and password. 73 // Tests Basic HTTP Authentication with correct username and password.
137 - (void)testSuccessfullBasicAuth { 74 - (void)testSuccessfullBasicAuth {
138 if (IsIPadIdiom()) { 75 if (IsIPadIdiom()) {
139 // EG does not allow interactions with HTTP Dialog when loading spinner is 76 // EG does not allow interactions with HTTP Dialog when loading spinner is
140 // animated. TODO(crbug.com/680290): Enable this test on iPad when EarlGrey 77 // animated. TODO(crbug.com/680290): Enable this test on iPad when EarlGrey
141 // allows tapping dialog buttons with active page load spinner. 78 // allows tapping dialog buttons with active page load spinner.
142 EARL_GREY_TEST_DISABLED(@"Tab Title not displayed on handset."); 79 EARL_GREY_TEST_DISABLED(@"Tab Title not displayed on handset.");
143 } 80 }
144 81
145 GURL URL = web::test::HttpServer::MakeUrl("http://good-auth"); 82 GURL URL = web::test::HttpServer::MakeUrl("http://good-auth");
146 web::test::SetUpHttpServer(base::MakeUnique<HttpAuthResponseProvider>( 83 web::test::SetUpHttpServer(base::MakeUnique<web::HttpAuthResponseProvider>(
147 URL, "GoodRealm", "gooduser", "goodpass")); 84 URL, "GoodRealm", "gooduser", "goodpass"));
148 chrome_test_util::LoadUrl(URL); 85 chrome_test_util::LoadUrl(URL);
149 WaitForHttpAuthDialog(); 86 WaitForHttpAuthDialog();
150 87
151 // Enter valid username and password. 88 // Enter valid username and password.
152 [[EarlGrey selectElementWithMatcher:usernameField()] 89 [[EarlGrey selectElementWithMatcher:usernameField()]
153 performAction:grey_typeText(@"gooduser")]; 90 performAction:grey_typeText(@"gooduser")];
154 [[EarlGrey selectElementWithMatcher:passwordField()] 91 [[EarlGrey selectElementWithMatcher:passwordField()]
155 performAction:grey_typeText(@"goodpass")]; 92 performAction:grey_typeText(@"goodpass")];
156 [[EarlGrey selectElementWithMatcher:loginButton()] performAction:grey_tap()]; 93 [[EarlGrey selectElementWithMatcher:loginButton()] performAction:grey_tap()];
157 94
158 const std::string pageText = HttpAuthResponseProvider::page_text(); 95 const std::string pageText = web::HttpAuthResponseProvider::page_text();
159 [[EarlGrey selectElementWithMatcher:webViewContainingText(pageText)] 96 [[EarlGrey selectElementWithMatcher:webViewContainingText(pageText)]
160 assertWithMatcher:grey_notNil()]; 97 assertWithMatcher:grey_notNil()];
161 } 98 }
162 99
163 // Tests Basic HTTP Authentication with incorrect username and password. 100 // Tests Basic HTTP Authentication with incorrect username and password.
164 - (void)testUnsuccessfullBasicAuth { 101 - (void)testUnsuccessfullBasicAuth {
165 if (IsIPadIdiom()) { 102 if (IsIPadIdiom()) {
166 // EG does not allow interactions with HTTP Dialog when loading spinner is 103 // EG does not allow interactions with HTTP Dialog when loading spinner is
167 // animated. TODO(crbug.com/680290): Enable this test on iPad when EarlGrey 104 // animated. TODO(crbug.com/680290): Enable this test on iPad when EarlGrey
168 // allows tapping dialog buttons with active page load spinner. 105 // allows tapping dialog buttons with active page load spinner.
169 EARL_GREY_TEST_DISABLED(@"Tab Title not displayed on handset."); 106 EARL_GREY_TEST_DISABLED(@"Tab Title not displayed on handset.");
170 } 107 }
171 108
172 GURL URL = web::test::HttpServer::MakeUrl("http://bad-auth"); 109 GURL URL = web::test::HttpServer::MakeUrl("http://bad-auth");
173 web::test::SetUpHttpServer(base::MakeUnique<HttpAuthResponseProvider>( 110 web::test::SetUpHttpServer(base::MakeUnique<web::HttpAuthResponseProvider>(
174 URL, "BadRealm", "baduser", "badpass")); 111 URL, "BadRealm", "baduser", "badpass"));
175 chrome_test_util::LoadUrl(URL); 112 chrome_test_util::LoadUrl(URL);
176 WaitForHttpAuthDialog(); 113 WaitForHttpAuthDialog();
177 114
178 // Enter invalid username and password. 115 // Enter invalid username and password.
179 [[EarlGrey selectElementWithMatcher:usernameField()] 116 [[EarlGrey selectElementWithMatcher:usernameField()]
180 performAction:grey_typeText(@"gooduser")]; 117 performAction:grey_typeText(@"gooduser")];
181 [[EarlGrey selectElementWithMatcher:passwordField()] 118 [[EarlGrey selectElementWithMatcher:passwordField()]
182 performAction:grey_typeText(@"goodpass")]; 119 performAction:grey_typeText(@"goodpass")];
183 [[EarlGrey selectElementWithMatcher:loginButton()] performAction:grey_tap()]; 120 [[EarlGrey selectElementWithMatcher:loginButton()] performAction:grey_tap()];
184 121
185 // Verifies that authentication was requested again. 122 // Verifies that authentication was requested again.
186 WaitForHttpAuthDialog(); 123 WaitForHttpAuthDialog();
187 } 124 }
188 125
189 // Tests Cancelling Basic HTTP Authentication. 126 // Tests Cancelling Basic HTTP Authentication.
190 - (void)testCancellingBasicAuth { 127 - (void)testCancellingBasicAuth {
191 if (IsIPadIdiom()) { 128 if (IsIPadIdiom()) {
192 // EG does not allow interactions with HTTP Dialog when loading spinner is 129 // EG does not allow interactions with HTTP Dialog when loading spinner is
193 // animated. TODO(crbug.com/680290): Enable this test on iPad when EarlGrey 130 // animated. TODO(crbug.com/680290): Enable this test on iPad when EarlGrey
194 // allows tapping dialog buttons with active page load spinner. 131 // allows tapping dialog buttons with active page load spinner.
195 EARL_GREY_TEST_DISABLED(@"Tab Title not displayed on handset."); 132 EARL_GREY_TEST_DISABLED(@"Tab Title not displayed on handset.");
196 } 133 }
197 134
198 GURL URL = web::test::HttpServer::MakeUrl("http://cancel-auth"); 135 GURL URL = web::test::HttpServer::MakeUrl("http://cancel-auth");
199 web::test::SetUpHttpServer(base::MakeUnique<HttpAuthResponseProvider>( 136 web::test::SetUpHttpServer(base::MakeUnique<web::HttpAuthResponseProvider>(
200 URL, "CancellingRealm", "", "")); 137 URL, "CancellingRealm", "", ""));
201 chrome_test_util::LoadUrl(URL); 138 chrome_test_util::LoadUrl(URL);
202 WaitForHttpAuthDialog(); 139 WaitForHttpAuthDialog();
203 140
204 [[EarlGrey selectElementWithMatcher:chrome_test_util::cancelButton()] 141 [[EarlGrey selectElementWithMatcher:chrome_test_util::cancelButton()]
205 performAction:grey_tap()]; 142 performAction:grey_tap()];
206 [[EarlGrey selectElementWithMatcher:httpAuthDialog()] 143 [[EarlGrey selectElementWithMatcher:httpAuthDialog()]
207 assertWithMatcher:grey_nil()]; 144 assertWithMatcher:grey_nil()];
208 } 145 }
209 146
210 @end 147 @end
OLDNEW
« no previous file with comments | « no previous file | ios/web/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698