Commit f57c7054 authored by Enrico Zini's avatar Enrico Zini
Browse files

Fixed some of the tests to look for Inconsistency records instead of logs. refs: #5

parent 240c6913
......@@ -5,6 +5,7 @@ from dsa.housekeeping import CheckLDAPConsistency
from backend.models import Person
import backend.const as const
from process.models import Process
from sitechecks.models import Inconsistency
from unittest import mock
from testfixtures import LogCapture
......@@ -40,15 +41,28 @@ class TestCheckLDAPConsistency(ProcessFixtureMixin, TestCase):
super().setUp()
self.hk = MockHousekeeper(self.persons.oldam)
def assertInconsistenciesEqual(self, items):
res = list(Inconsistency.objects.all())
self.assertEqual(len(res), len(items))
for inc, item in zip(res, items):
person = item.get("person")
if person is not None:
self.assertEqual(inc.person, person)
text = item.get("text")
if text is not None:
self.assertEqual(inc.text, text)
def test_new_removed(self):
with mock.patch("dsa.udldap.list_people") as m:
m.return_value = [
MockEntry("newdd", supplementaryGid=["Debian"], cn="tcn",
mn="tmn", sn="tsn", emailForward="test@example.org", accountStatus=None)
]
with LogCapture() as lc:
self.hk.run(CheckLDAPConsistency)
lc.check(("dsa.housekeeping", "WARNING", "None: newdd: created to mirror a removed DD account from LDAP"))
self.hk.run(CheckLDAPConsistency)
self.assertInconsistenciesEqual([
{"person": self.persons.newdd,
"text": "newdd: created to mirror a removed DD account from LDAP"},
])
p = Person.objects.get(ldap_fields__uid="newdd")
audit = ["{}:{}".format(l.author.lookup_key, l.notes) for l in p.audit_log.all()]
......@@ -69,10 +83,10 @@ class TestCheckLDAPConsistency(ProcessFixtureMixin, TestCase):
MockEntry("newguest", supplementaryGid=[], cn="tcn", mn="tmn",
sn="tsn", emailForward="test@example.org", accountStatus=None)
]
with LogCapture() as lc:
with self.assertLogs() as log:
self.hk.run(CheckLDAPConsistency)
lc.check(("dsa.housekeeping", "WARNING",
"None: newguest: created to mirror a removed guest account from LDAP"))
self.assertEqual(log.output, [
"WARNING:dsa.housekeeping:test.CheckLDAPConsistency: newguest: created to mirror a removed guest account from LDAP"])
p = Person.objects.get(ldap_fields__uid="newguest")
audit = ["{}:{}".format(l.author.lookup_key, l.notes) for l in p.audit_log.all()]
......@@ -94,11 +108,12 @@ class TestCheckLDAPConsistency(ProcessFixtureMixin, TestCase):
emailForward="test@example.org", keyFingerPrint="66B4DFB68CB24EBBD8650BC4F4B4B0CC797EBFAB",
accountStatus=None)
]
with LogCapture() as lc:
self.hk.run(CheckLDAPConsistency)
lc.check(("dsa.housekeeping", "WARNING",
"None: newdd has fingerprint 66B4DFB68CB24EBBD8650BC4F4B4B0CC797EBFAB and gid 800 in LDAP,"
" but is not in our db"))
self.hk.run(CheckLDAPConsistency)
self.assertInconsistenciesEqual([
{"person": self.persons.newdd,
"text": "person has fingerprint 66B4DFB68CB24EBBD8650BC4F4B4B0CC797EBFAB and gid 800 in LDAP,"
" but is not in our db"},
])
with self.assertRaises(Person.DoesNotExist):
Person.objects.get(ldap_fields__uid="newdd")
......@@ -139,11 +154,10 @@ class TestCheckLDAPConsistency(ProcessFixtureMixin, TestCase):
MockEntry("dm", supplementaryGid=["Debian"], cn="Dm", mn="", sn="", emailForward="dm@example.org",
keyFingerPrint="66B4DFB68CB24EBBD8650BC4F4B4B0CC797EBFAB", accountStatus=None)
]
with self.assertLogs() as log:
self.hk.run(CheckLDAPConsistency)
self.assertEqual(log.output, [
"WARNING:dsa.housekeeping:"
"None: dm has supplementaryGid 'Debian', but in our db the state is Debian Maintainer",
self.hk.run(CheckLDAPConsistency)
self.assertInconsistenciesEqual([
{"person": self.persons.dm,
"text": "person has supplementaryGid 'Debian', but in our db the state is Debian Maintainer"},
])
p = self.persons.dm
......@@ -157,12 +171,13 @@ class TestCheckLDAPConsistency(ProcessFixtureMixin, TestCase):
emailForward="dd_u@example.org", keyFingerPrint="66B4DFB68CB24EBBD8650BC4F4B4B0CC797EBFAB",
accountStatus="inactive 2018-03-20"),
]
with self.assertLogs() as log:
self.hk.run(CheckLDAPConsistency)
self.assertEqual(log.output, [
"WARNING:dsa.housekeeping:"
"None: dd_u has accountStatus 'inactive 2018-03-20' (comment: None)"
" but in our db the state is Debian Developer, uploading [inactive]",
self.hk.run(CheckLDAPConsistency)
self.assertInconsistenciesEqual([
{"person": self.persons.dd_u,
"text":
"person has accountStatus 'inactive 2018-03-20' (comment: None)"
" but in our db the state is Debian Developer, uploading [inactive]"}
])
p = self.persons.dd_u
......@@ -177,11 +192,14 @@ class TestCheckLDAPConsistency(ProcessFixtureMixin, TestCase):
keyFingerPrint="66B4DFB68CB24EBBD8650BC4F4B4B0CC797EBFAB",
accountStatus="inactive 2018-03-20", accountComment="RT#1234"),
]
with self.assertLogs() as log:
self.hk.run(CheckLDAPConsistency)
self.assertEqual(log.output, [
"WARNING:dsa.housekeeping:None: dd_u has accountStatus 'inactive 2018-03-20' "
'(comment: RT#1234) but in our db the state is Debian Developer, uploading [inactive]'])
self.hk.run(CheckLDAPConsistency)
self.assertInconsistenciesEqual([
{"person": self.persons.dd_u,
"text":
"person has accountStatus 'inactive 2018-03-20' "
'(comment: RT#1234) but in our db the state is Debian Developer, uploading [inactive]'}
])
p = self.persons.dd_u
p.refresh_from_db()
......@@ -195,12 +213,12 @@ class TestCheckLDAPConsistency(ProcessFixtureMixin, TestCase):
MockEntry("dd_u", supplementaryGid=["Debian"], cn="Dd_u", mn="", sn="",
emailForward="dd_u@example.org", accountStatus="retiring 2018-03-20"),
]
with self.assertLogs() as log:
self.hk.run(CheckLDAPConsistency)
self.assertEqual(log.output, [
"WARNING:dsa.housekeeping:None: dd_u has accountStatus 'retiring 2018-03-20' "
'(comment: None) but in our db the state is Debian Developer, uploading '
'[retiring]',
self.hk.run(CheckLDAPConsistency)
self.assertInconsistenciesEqual([
{"person": self.persons.dd_u,
"text": "person has accountStatus 'retiring 2018-03-20' "
'(comment: None) but in our db the state is Debian Developer, uploading '
'[retiring]'},
])
process.approved_by = self.persons.dam
......@@ -209,7 +227,7 @@ class TestCheckLDAPConsistency(ProcessFixtureMixin, TestCase):
with self.assertLogs() as log:
self.hk.run(CheckLDAPConsistency)
self.assertEqual(log.output, [
"INFO:dsa.housekeeping:None: dd_u closed from dsa: retiring 2018-03-20",
"INFO:dsa.housekeeping:test.CheckLDAPConsistency: dd_u closed from dsa: retiring 2018-03-20",
])
p = self.persons.dd_u
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment