146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)# Copyright 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)import os
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import tempfile
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)import unittest
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)from telemetry.core import browser_credentials
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SIMPLE_CREDENTIALS_STRING = """
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "google": {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "username": "example",
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "password": "asdf"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"""
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BackendStub(object):
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  def __init__(self, credentials_type):
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    self.login_needed_called = None
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    self.login_no_longer_needed_called = None
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    self.credentials_type = credentials_type
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  def LoginNeeded(self, config, _, tab):
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    self.login_needed_called = (config, tab)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return True
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  def LoginNoLongerNeeded(self, tab):
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    self.login_no_longer_needed_called = (tab, )
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TestBrowserCredentials(unittest.TestCase):
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  def testCredentialsInfrastructure(self):
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    google_backend = BackendStub("google")
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    othersite_backend = BackendStub("othersite")
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    browser_cred = browser_credentials.BrowserCredentials(
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      [google_backend,
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       othersite_backend])
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    try:
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      with tempfile.NamedTemporaryFile(delete=False) as f:
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        f.write(SIMPLE_CREDENTIALS_STRING)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      browser_cred.credentials_path = f.name
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      # Should true because it has a password and a backend.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self.assertTrue(browser_cred.CanLogin('google'))
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      # Should be false succeed because it has no password.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self.assertFalse(browser_cred.CanLogin('othersite'))
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      # Should fail because it has no backend.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self.assertRaises(
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        Exception,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        lambda: browser_cred.CanLogin('foobar'))
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      tab = {}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ret = browser_cred.LoginNeeded(tab, 'google')
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self.assertTrue(ret)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self.assertTrue(google_backend.login_needed_called is not None)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self.assertEqual(tab, google_backend.login_needed_called[0])
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self.assertEqual("example",
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       google_backend.login_needed_called[1]["username"])
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self.assertEqual("asdf",
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       google_backend.login_needed_called[1]["password"])
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      browser_cred.LoginNoLongerNeeded(tab, 'google')
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self.assertTrue(google_backend.login_no_longer_needed_called is not None)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self.assertEqual(tab, google_backend.login_no_longer_needed_called[0])
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    finally:
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      os.remove(f.name)
73