def test_constructor(self): """ Test the with passing auth store in constructor. """ auth = SimpleAuthenticator({'user': '******'}) assert auth.authenticate('user', 'pass') == True assert auth.authenticate('user1', 'pass') == False assert auth.authenticate('user', 'pass2') == False
def test_from_configfile_fp_invalid(self): """ Test loading store with missing section in config. """ fp = StringIO(u"[invalid]\nusername=password") auth = SimpleAuthenticator() try: auth.from_configfile(fp) self.fail("Expected error with missing section.") except ValueError as e: pass
def test_from_configfile_invalid(self): """ Test loading store with invalid file path. """ filename = resource_filename('tests.resources', 'auth-invlaid.ini') auth = SimpleAuthenticator() try: auth.from_configfile(filename) self.fail("Expected error with invalid filename.") except ValueError as e: pass
def test_from_configfile(self): """ Test the loading store from config file path. """ filename = resource_filename('tests.resources', 'auth.ini') auth = SimpleAuthenticator() auth.from_configfile(filename) assert auth.authenticate('juniper', 'b3rr1es') == True assert auth.authenticate('oak', 'ac$rrubrum') == True assert auth.authenticate('pinetree', 'str0bus') == True assert auth.authenticate('foo', 'bar') == False
def test_from_configfile_fp(self): """ Test loading store from file-like object. """ with open(resource_filename('tests.resources', 'auth.ini'), 'r') as fp: auth = SimpleAuthenticator() auth.from_configfile(fp) assert auth.authenticate('juniper', 'b3rr1es') == True assert auth.authenticate('oak', 'ac$rrubrum') == True assert auth.authenticate('pinetree', 'str0bus') == True assert auth.authenticate('foo', 'bar') == False
def test_connect_auth(self): """ Test connecting when auth is required. """ self.server.authenticator = SimpleAuthenticator(store={'user': '******'}) c1 = self._new_client(connect=False) c1.connect() r = c1.received_frames.get(timeout=1) self.assertEqual(r.cmd, 'error') self.assertIn(b'Auth', r.body) c2 = self._new_client(connect=False) c2.connect(headers={'login': '******', 'passcode': 'pass'}) r2 = c2.received_frames.get(timeout=1) self.assertEqual(r2.cmd.lower(), 'connected') c3 = self._new_client(connect=False) c3.connect(headers={'login': '******', 'passcode': 'pass-invalid'}) r3 = c3.received_frames.get(timeout=1) self.assertEqual(r3.cmd, 'error')
def test_connect_auth(self): """ Test connecting when auth is required. """ self.server.authenticator = SimpleAuthenticator(store={'user': '******'}) c1 = self._new_client(connect=False) c1.connect() r = c1.received_frames.get(timeout=1) assert r.command == 'ERROR' assert 'Auth' in r.body c2 = self._new_client(connect=False) c2.connect(headers={'login': '******', 'passcode': 'pass'}) r2 = c2.received_frames.get(timeout=1) print r2 assert r2.command == 'CONNECTED' c3 = self._new_client(connect=False) c3.connect(headers={'login': '******', 'passcode': 'pass-invalid'}) r3 = c3.received_frames.get(timeout=1) print r3 assert r3.command == 'ERROR'