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

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

Issue 2617393003: [ios] EarlGrey tests for HTTP Authentication. (Closed)
Patch Set: Actually disabled tests on iPad 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 | « ios/chrome/browser/web/forms_egtest.mm ('k') | ios/chrome/test/earl_grey/chrome_matchers.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #import <EarlGrey/EarlGrey.h>
6
7 #include "base/base64.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "components/strings/grit/components_strings.h"
12 #include "ios/chrome/browser/ui/ui_util.h"
13 #include "ios/chrome/grit/ios_strings.h"
14 #include "ios/chrome/test/app/navigation_test_util.h"
15 #import "ios/chrome/test/earl_grey/chrome_matchers.h"
16 #import "ios/chrome/test/earl_grey/chrome_test_case.h"
17 #import "ios/testing/wait_util.h"
18 #import "ios/web/public/test/http_server.h"
19 #include "ios/web/public/test/http_server_util.h"
20 #include "ios/web/public/test/response_providers/html_response_provider.h"
21 #import "ios/testing/earl_grey/disabled_test_macros.h"
22 #include "ui/base/l10n/l10n_util_mac.h"
23 #include "url/gurl.h"
24
25 using testing::WaitUntilConditionOrTimeout;
26 using testing::kWaitForPageLoadTimeout;
27 using chrome_test_util::webViewContainingText;
28
29 namespace {
30
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.
93 id<GREYMatcher> httpAuthDialog() {
94 NSString* title = l10n_util::GetNSStringWithFixup(IDS_LOGIN_DIALOG_TITLE);
95 return chrome_test_util::staticTextWithAccessibilityLabel(title);
96 }
97
98 // Returns matcher for Username text field.
99 id<GREYMatcher> usernameField() {
100 return chrome_test_util::staticTextWithAccessibilityLabelId(
101 IDS_IOS_HTTP_LOGIN_DIALOG_USERNAME_PLACEHOLDER);
102 }
103
104 // Returns matcher for Password text field.
105 id<GREYMatcher> passwordField() {
106 return chrome_test_util::staticTextWithAccessibilityLabelId(
107 IDS_IOS_HTTP_LOGIN_DIALOG_PASSWORD_PLACEHOLDER);
108 }
109
110 // Returns matcher for Login button.
111 id<GREYMatcher> loginButton() {
112 return chrome_test_util::buttonWithAccessibilityLabelId(
113 IDS_LOGIN_DIALOG_OK_BUTTON_LABEL);
114 }
115
116 // Waits until static text with IDS_LOGIN_DIALOG_TITLE label is displayed.
117 void WaitForHttpAuthDialog() {
118 BOOL dialog_shown = WaitUntilConditionOrTimeout(kWaitForPageLoadTimeout, ^{
119 NSError* error = nil;
120 [[EarlGrey selectElementWithMatcher:httpAuthDialog()]
121 assertWithMatcher:grey_notNil()
122 error:&error];
123 return !error;
124 });
125 GREYAssert(dialog_shown, @"HTTP Authentication dialog was not shown");
126 }
127
128 } // namespace
129
130 // Test case for HTTP Authentication flow.
131 @interface HTTPAuthTestCase : ChromeTestCase
132 @end
133
134 @implementation HTTPAuthTestCase
135
136 // Tests Basic HTTP Authentication with correct username and password.
137 - (void)testSuccessfullBasicAuth {
138 if (IsIPadIdiom()) {
139 // 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
141 // allows tapping dialog buttons with active page load spinner.
baxley 2017/01/12 23:57:18 Sorry if I forgot from our conversation... Disabli
Eugene But (OOO till 7-30) 2017/01/13 00:05:49 No. EG can not Tap and complains about ongoing ani
142 EARL_GREY_TEST_DISABLED(@"Tab Title not displayed on handset.");
143 }
144
145 GURL URL = web::test::HttpServer::MakeUrl("http://good-auth");
146 web::test::SetUpHttpServer(base::MakeUnique<HttpAuthResponseProvider>(
147 URL, "GoodRealm", "gooduser", "goodpass"));
148 chrome_test_util::LoadUrl(URL);
149 WaitForHttpAuthDialog();
150
151 // Enter valid username and password.
152 [[EarlGrey selectElementWithMatcher:usernameField()]
153 performAction:grey_typeText(@"gooduser")];
154 [[EarlGrey selectElementWithMatcher:passwordField()]
155 performAction:grey_typeText(@"goodpass")];
156 [[EarlGrey selectElementWithMatcher:loginButton()] performAction:grey_tap()];
157
158 const std::string pageText = HttpAuthResponseProvider::page_text();
159 [[EarlGrey selectElementWithMatcher:webViewContainingText(pageText)]
160 assertWithMatcher:grey_notNil()];
161 }
162
163 // Tests Basic HTTP Authentication with incorrect username and password.
164 - (void)testUnsuccessfullBasicAuth {
165 if (IsIPadIdiom()) {
166 // 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
168 // allows tapping dialog buttons with active page load spinner.
169 EARL_GREY_TEST_DISABLED(@"Tab Title not displayed on handset.");
170 }
171
172 GURL URL = web::test::HttpServer::MakeUrl("http://bad-auth");
173 web::test::SetUpHttpServer(base::MakeUnique<HttpAuthResponseProvider>(
174 URL, "BadRealm", "baduser", "badpass"));
baxley 2017/01/12 23:57:18 optional nit: should this these arguments start wi
Eugene But (OOO till 7-30) 2017/01/13 00:05:50 No. After successful authorization (which happens
175 chrome_test_util::LoadUrl(URL);
176 WaitForHttpAuthDialog();
177
178 // Enter invalid username and password.
179 [[EarlGrey selectElementWithMatcher:usernameField()]
180 performAction:grey_typeText(@"gooduser")];
181 [[EarlGrey selectElementWithMatcher:passwordField()]
182 performAction:grey_typeText(@"goodpass")];
183 [[EarlGrey selectElementWithMatcher:loginButton()] performAction:grey_tap()];
184
185 // Verifies that authentication was requested again.
186 WaitForHttpAuthDialog();
187 }
188
189 // Tests Cancelling Basic HTTP Authentication.
190 - (void)testCancellingBasicAuth {
191 if (IsIPadIdiom()) {
192 // 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
194 // allows tapping dialog buttons with active page load spinner.
195 EARL_GREY_TEST_DISABLED(@"Tab Title not displayed on handset.");
196 }
197
198 GURL URL = web::test::HttpServer::MakeUrl("http://cancel-auth");
199 web::test::SetUpHttpServer(base::MakeUnique<HttpAuthResponseProvider>(
200 URL, "CancellingRealm", "", ""));
201 chrome_test_util::LoadUrl(URL);
202 WaitForHttpAuthDialog();
203
204 [[EarlGrey selectElementWithMatcher:chrome_test_util::cancelButton()]
205 performAction:grey_tap()];
206 [[EarlGrey selectElementWithMatcher:httpAuthDialog()]
207 assertWithMatcher:grey_nil()];
208 }
209
210 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/web/forms_egtest.mm ('k') | ios/chrome/test/earl_grey/chrome_matchers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698