def test_gerrit_add_in_acc_external(self): class FakeDB(): def __init__(self, success=True): self.success = success def cursor(self): return FakeCursor(self.success) def commit(self): pass class FakeCursor(): def __init__(self, success): self.success = success def execute(self, sql): if not self.success: raise Exception ger = Gerrit(self.conf) with patch('cauth.utils.userdetails.MySQLdb') as m: m.connect = lambda *args, **kwargs: FakeDB() ret = ger.add_in_acc_external(42, 'john') self.assertEqual(True, ret) with patch('cauth.utils.userdetails.MySQLdb') as m: m.connect = lambda *args, **kwargs: FakeDB(False) ret = ger.add_in_acc_external(42, 'john') self.assertEqual(False, ret)
def test_create_gerrit_user(self): ger = Gerrit(self.conf) with patch('cauth.utils.userdetails.requests') as r: r.put = lambda *args, **kwargs: None r.get = self.gerrit_get_account_id_mock ger.add_in_acc_external = Mock() ger.create_gerrit_user('john', '*****@*****.**', 'John Doe', []) self.assertEqual(True, ger.add_in_acc_external.called) with patch('cauth.utils.userdetails.requests') as r: r.put = lambda *args, **kwargs: None r.get = self.gerrit_get_account_id_mock2 ger.add_in_acc_external = Mock() ger.create_gerrit_user('john', '*****@*****.**', 'John Doe', []) self.assertEqual(False, ger.add_in_acc_external.called)