OLD | NEW |
| (Empty) |
1 # Copyright (c) 2013 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 import os | |
5 import unittest | |
6 | |
7 from telemetry.core import browser_finder | |
8 from telemetry.test import simple_mock | |
9 from telemetry.test import options_for_unittests | |
10 | |
11 _ = simple_mock.DONT_CARE | |
12 | |
13 class FormBasedCredentialsBackendUnitTestBase(unittest.TestCase): | |
14 def setUp(self): | |
15 self._credentials_type = None | |
16 | |
17 def testRealLoginIfPossible(self): | |
18 credentials_path = os.path.join( | |
19 os.path.dirname(__file__), | |
20 '..', '..', 'perf', 'data', 'credentials.json') | |
21 if not os.path.exists(credentials_path): | |
22 return | |
23 | |
24 options = options_for_unittests.GetCopy() | |
25 with browser_finder.FindBrowser(options).Create() as b: | |
26 b.credentials.credentials_path = credentials_path | |
27 if not b.credentials.CanLogin(self._credentials_type): | |
28 return | |
29 ret = b.credentials.LoginNeeded(b.tabs[0], self._credentials_type) | |
30 self.assertTrue(ret) | |
31 | |
32 def testRealLoginWithDontOverrideProfileIfPossible(self): | |
33 credentials_path = os.path.join( | |
34 os.path.dirname(__file__), | |
35 '..', '..', 'perf', 'data', 'credentials.json') | |
36 if not os.path.exists(credentials_path): | |
37 return | |
38 | |
39 options = options_for_unittests.GetCopy() | |
40 | |
41 # Login once to make sure our default profile is logged in. | |
42 with browser_finder.FindBrowser(options).Create() as b: | |
43 b.credentials.credentials_path = credentials_path | |
44 | |
45 if not b.credentials.CanLogin(self._credentials_type): | |
46 return | |
47 | |
48 tab = b.tabs[0] | |
49 | |
50 # Should not be logged in, since this is a fresh credentials | |
51 # instance. | |
52 self.assertFalse(b.credentials.IsLoggedIn(self._credentials_type)) | |
53 | |
54 # Log in. | |
55 ret = b.credentials.LoginNeeded(tab, self._credentials_type) | |
56 | |
57 # Make sure login was successful. | |
58 self.assertTrue(ret) | |
59 self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type)) | |
60 | |
61 # Reset state. Now the backend thinks we're logged out, even | |
62 # though we are logged in in our current browser session. This | |
63 # simulates the effects of running with --dont-override-profile. | |
64 b.credentials._ResetLoggedInState() # pylint: disable=W0212 | |
65 | |
66 # Make sure the backend thinks we're logged out. | |
67 self.assertFalse(b.credentials.IsLoggedIn(self._credentials_type)) | |
68 self.assertTrue(b.credentials.CanLogin(self._credentials_type)) | |
69 | |
70 # Attempt to login again. This should detect that we've hit | |
71 # the 'logged in' page instead of the login form, and succeed | |
72 # instead of timing out. | |
73 ret = b.credentials.LoginNeeded(tab, self._credentials_type) | |
74 | |
75 # Make sure our login attempt did in fact succeed and set the | |
76 # backend's internal state to 'logged in'. | |
77 self.assertTrue(ret) | |
78 self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type)) | |
79 | |
80 def testLoginUsingMock(self): | |
81 raise NotImplementedError() | |
82 | |
83 def _LoginUsingMock(self, backend, login_page_url, email_element_id, | |
84 password_element_id): # pylint: disable=R0201 | |
85 tab = simple_mock.MockObject() | |
86 | |
87 config = {'username': 'blah', | |
88 'password': 'blargh'} | |
89 | |
90 tab.ExpectCall('Navigate', login_page_url) | |
91 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False) | |
92 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(True) | |
93 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False) | |
94 tab.ExpectCall('WaitForDocumentReadyStateToBeInteractiveOrBetter') | |
95 | |
96 def VerifyEmail(js): | |
97 assert email_element_id in js | |
98 assert 'blah' in js | |
99 tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyEmail) | |
100 | |
101 def VerifyPw(js): | |
102 assert password_element_id in js | |
103 assert 'largh' in js | |
104 tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyPw) | |
105 | |
106 def VerifySubmit(js): | |
107 assert '.submit' in js | |
108 tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifySubmit) | |
109 | |
110 # Checking for form still up. | |
111 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False) | |
112 | |
113 backend.LoginNeeded(tab, config) | |
OLD | NEW |