Best Python code snippet using ATX
committer_auth_unittest.py
Source:committer_auth_unittest.py  
1#!/usr/bin/env python2#3# Copyright (C) 2011 Apple Inc. All rights reserved.4#5# Redistribution and use in source and binary forms, with or without6# modification, are permitted provided that the following conditions7# are met:8# 1.  Redistributions of source code must retain the above copyright9#     notice, this list of conditions and the following disclaimer.10# 2.  Redistributions in binary form must reproduce the above copyright11#     notice, this list of conditions and the following disclaimer in the12#     documentation and/or other materials provided with the distribution.13#14# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE17# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR18# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR20# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER21# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,22# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE23# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24import StringIO25import __builtin__26import buildbot.status.web.auth27import contextlib28import os29import unittest30from committer_auth import CommitterAuth31# This subclass of StringIO supports the context manager protocol so it works32# with "with" statements, just like real files.33class CMStringIO(StringIO.StringIO):34    def __enter__(self):35        return self36    def __exit__(self, exception, value, traceback):37        self.close()38@contextlib.contextmanager39def open_override(func):40    original_open = __builtin__.open41    __builtin__.open = func42    yield43    __builtin__.open = original_open44class CommitterAuthTest(unittest.TestCase):45    def setUp(self):46        self.auth = CommitterAuth('path/to/auth.json')47        self.auth.open_auth_json_file = self.fake_auth_json_file48        self.auth.open_webkit_committers_file = self.fake_committers_file49        self.auth.open_trac_credentials_file = self.fake_htdigest_file50    def fake_open_function(self, expected_filename):51        def fake_open(name, mode='r'):52            self.fake_open_was_called = True53            self.assertEqual(expected_filename, name)54        return fake_open55    def test_authentication_success(self):56        self.assertTrue(self.auth.authenticate('committer@webkit.org', 'committerpassword'))57        self.assertEqual('', self.auth.errmsg())58        self.assertTrue(self.auth.authenticate('committer2@example.com', 'committer2password'))59        self.assertEqual('', self.auth.errmsg())60    def test_committer_without_trac_credentials_fails(self):61        self.assertFalse(self.auth.authenticate('committer3@webkit.org', 'committer3password'))62        self.assertEqual('Invalid username/password', self.auth.errmsg())63    def test_fail_to_open_auth_json_file(self):64        def raise_IOError():65            raise IOError(2, 'No such file or directory', 'path/to/auth.json')66        auth = CommitterAuth('path/to/auth.json')67        auth.open_auth_json_file = raise_IOError68        self.assertFalse(auth.authenticate('committer@webkit.org', 'committerpassword'))69        self.assertEqual('Error opening auth.json file: No such file or directory', auth.errmsg())70    def test_fail_to_open_trac_credentials_file(self):71        def raise_IOError():72            raise IOError(2, 'No such file or directory', 'path/to/trac/credentials')73        self.auth.open_trac_credentials_file = raise_IOError74        self.assertFalse(self.auth.authenticate('committer@webkit.org', 'committerpassword'))75        self.assertEqual('Error opening Trac credentials file: No such file or directory', self.auth.errmsg())76    def test_fail_to_open_webkit_committers_file(self):77        def raise_IOError():78            raise IOError(2, 'No such file or directory', 'path/to/webkit/committers')79        self.auth.open_webkit_committers_file = raise_IOError80        self.assertFalse(self.auth.authenticate('committer@webkit.org', 'committerpassword'))81        self.assertEqual('Error opening WebKit committers file: No such file or directory', self.auth.errmsg())82    def test_implements_IAuth(self):83        self.assertTrue(buildbot.status.web.auth.IAuth.implementedBy(CommitterAuth))84    def test_invalid_auth_json_file(self):85        auth = CommitterAuth('path/to/auth.json')86        auth.open_auth_json_file = self.invalid_auth_json_file87        self.assertFalse(auth.authenticate('committer@webkit.org', 'committerpassword'))88        self.assertEqual('Error parsing auth.json file: No JSON object could be decoded', auth.errmsg())89    def test_invalid_committers_file(self):90        self.auth.open_webkit_committers_file = self.invalid_committers_file91        self.assertFalse(self.auth.authenticate('committer@webkit.org', 'committerpassword'))92        self.assertEqual('Error parsing WebKit committers file', self.auth.errmsg())93    def test_invalid_trac_credentials_file(self):94        self.auth.open_trac_credentials_file = self.invalid_htdigest_file95        self.assertFalse(self.auth.authenticate('committer@webkit.org', 'committerpassword'))96        self.assertEqual('Error parsing Trac credentials file', self.auth.errmsg())97    def test_missing_auth_json_keys(self):98        auth = CommitterAuth('path/to/auth.json')99        auth.open_auth_json_file = lambda: CMStringIO('{ "trac_credentials": "path/to/trac/credentials" }')100        self.assertFalse(auth.authenticate('committer@webkit.org', 'committerpassword'))101        self.assertEqual('auth.json file is missing "webkit_committers" key', auth.errmsg())102        auth.open_auth_json_file = lambda: CMStringIO('{ "webkit_committers": "path/to/webkit/committers" }')103        auth.open_webkit_committers_file = self.fake_committers_file104        self.assertFalse(auth.authenticate('committer@webkit.org', 'committerpassword'))105        self.assertEqual('auth.json file is missing "trac_credentials" key', auth.errmsg())106    def test_open_auth_json_file(self):107        auth = CommitterAuth('path/to/auth.json')108        self.fake_open_was_called = False109        with open_override(self.fake_open_function(auth.auth_json_filename())):110            auth.open_auth_json_file()111        self.assertTrue(self.fake_open_was_called)112    def test_open_trac_credentials_file(self):113        auth = CommitterAuth('path/to/auth.json')114        auth.trac_credentials_filename = lambda: 'trac credentials filename'115        self.fake_open_was_called = False116        with open_override(self.fake_open_function(auth.trac_credentials_filename())):117            auth.open_trac_credentials_file()118        self.assertTrue(self.fake_open_was_called)119    def test_open_webkit_committers_file(self):120        auth = CommitterAuth('path/to/auth.json')121        auth.webkit_committers_filename = lambda: 'webkit committers filename'122        self.fake_open_was_called = False123        with open_override(self.fake_open_function(auth.webkit_committers_filename())):124            auth.open_webkit_committers_file()125        self.assertTrue(self.fake_open_was_called)126    def test_non_committer_fails(self):127        self.assertFalse(self.auth.authenticate('noncommitter@example.com', 'noncommitterpassword'))128        self.assertEqual('Invalid username/password', self.auth.errmsg())129    def test_trac_credentials_filename(self):130        self.assertEqual('path/to/trac/credentials', self.auth.trac_credentials_filename())131    def test_unknown_user_fails(self):132        self.assertFalse(self.auth.authenticate('nobody@example.com', 'nobodypassword'))133        self.assertEqual('Invalid username/password', self.auth.errmsg())134    def test_username_is_prefix_of_valid_user(self):135        self.assertFalse(self.auth.authenticate('committer@webkit.orgg', 'committerpassword'))136        self.assertEqual('Invalid username/password', self.auth.errmsg())137    def test_webkit_committers(self):138        self.assertEqual(['committer@webkit.org', 'committer2@example.com', 'committer3@webkit.org'], self.auth.webkit_committers())139    def test_webkit_committers_filename(self):140        self.assertEqual('path/to/webkit/committers', self.auth.webkit_committers_filename())141    def test_wrong_password_fails(self):142        self.assertFalse(self.auth.authenticate('committer@webkit.org', 'wrongpassword'))143        self.assertEqual('Invalid username/password', self.auth.errmsg())144    def fake_auth_json_file(self):145        return CMStringIO("""{146    "trac_credentials": "path/to/trac/credentials",147    "webkit_committers": "path/to/webkit/committers"148}""")149    def invalid_auth_json_file(self):150        return CMStringIO('~!@#$%^&*()_+')151    def fake_committers_file(self):152        return CMStringIO("""[groups]153group1 = user@example.com,user2@example.com154group2 = user3@example.com155group3 =156group4 =157webkit = committer@webkit.org,committer2@example.com,committer3@webkit.org158[service:/]159*    = r160""")161    def invalid_committers_file(self):162        return CMStringIO("""[groups]163[[groups2]164""")165    def fake_htdigest_file(self):166        return CMStringIO("""committer@webkit.org:Mac OS Forge:761c8dcb7d9b5908007ed142f62fe73a167committer2@example.com:Mac OS Forge:faeee69acc2e49af3a0dbb15bd593ef4168noncommitter@example.com:Mac OS Forge:b99aa7ad32306a654ca4d57839fde9c1169""")170    def invalid_htdigest_file(self):171        return CMStringIO("""committer@webkit.org:Mac OS Forge:761c8dcb7d9b5908007ed142f62fe73a172committer2@example.com:Mac OS Forge:faeee69acc2e49af3a0dbb15bd593ef4173noncommitter@example.com:Mac OS Forge:b99aa7ad32306a654ca4d57839fde9c1174committer4@example.com:Mac OS Forge:::175""")176if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
