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