| 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 import unittest | |
| 5 import tempfile | |
| 6 | |
| 7 from telemetry.core import browser_credentials | |
| 8 | |
| 9 SIMPLE_CREDENTIALS_STRING = """ | |
| 10 { | |
| 11 "google": { | |
| 12 "username": "example", | |
| 13 "password": "asdf" | |
| 14 } | |
| 15 } | |
| 16 """ | |
| 17 | |
| 18 class BackendStub(object): | |
| 19 def __init__(self, credentials_type): | |
| 20 self.login_needed_called = None | |
| 21 self.login_no_longer_needed_called = None | |
| 22 self.credentials_type = credentials_type | |
| 23 | |
| 24 def LoginNeeded(self, config, tab): | |
| 25 self.login_needed_called = (config, tab) | |
| 26 return True | |
| 27 | |
| 28 def LoginNoLongerNeeded(self, tab): | |
| 29 self.login_no_longer_needed_called = (tab, ) | |
| 30 | |
| 31 | |
| 32 class TestBrowserCredentials(unittest.TestCase): | |
| 33 def testCredentialsInfrastructure(self): | |
| 34 google_backend = BackendStub("google") | |
| 35 othersite_backend = BackendStub("othersite") | |
| 36 browser_cred = browser_credentials.BrowserCredentials( | |
| 37 [google_backend, | |
| 38 othersite_backend]) | |
| 39 with tempfile.NamedTemporaryFile() as f: | |
| 40 f.write(SIMPLE_CREDENTIALS_STRING) | |
| 41 f.flush() | |
| 42 | |
| 43 browser_cred.credentials_path = f.name | |
| 44 | |
| 45 # Should true because it has a password and a backend. | |
| 46 self.assertTrue(browser_cred.CanLogin('google')) | |
| 47 | |
| 48 # Should be false succeed because it has no password. | |
| 49 self.assertFalse(browser_cred.CanLogin('othersite')) | |
| 50 | |
| 51 # Should fail because it has no backend. | |
| 52 self.assertRaises( | |
| 53 Exception, | |
| 54 lambda: browser_cred.CanLogin('foobar')) | |
| 55 | |
| 56 tab = {} | |
| 57 ret = browser_cred.LoginNeeded(tab, 'google') | |
| 58 self.assertTrue(ret) | |
| 59 self.assertTrue(google_backend.login_needed_called is not None) | |
| 60 self.assertEqual(tab, google_backend.login_needed_called[0]) | |
| 61 self.assertEqual("example", | |
| 62 google_backend.login_needed_called[1]["username"]) | |
| 63 self.assertEqual("asdf", | |
| 64 google_backend.login_needed_called[1]["password"]) | |
| 65 | |
| 66 browser_cred.LoginNoLongerNeeded(tab, 'google') | |
| 67 self.assertTrue(google_backend.login_no_longer_needed_called is not None) | |
| 68 self.assertEqual(tab, google_backend.login_no_longer_needed_called[0]) | |
| OLD | NEW |