From 8a06af41eb9d0c0a119259fbf92864915031bab5 Mon Sep 17 00:00:00 2001 From: Adrienne Stilp Date: Fri, 21 Jun 2024 15:22:41 -0700 Subject: [PATCH] Add tests for other Accessor/Uploader audit classes Specifically, the AuditResults classes and the AuditTable classes. --- primed/cdsa/audit/accessor_audit.py | 10 +- primed/cdsa/audit/uploader_audit.py | 4 +- primed/cdsa/tests/test_audit.py | 254 ++++++++++++++++++++++++++++ 3 files changed, 258 insertions(+), 10 deletions(-) diff --git a/primed/cdsa/audit/accessor_audit.py b/primed/cdsa/audit/accessor_audit.py index 7e1f7804..9fdeedaa 100644 --- a/primed/cdsa/audit/accessor_audit.py +++ b/primed/cdsa/audit/accessor_audit.py @@ -20,10 +20,10 @@ class AccessorAuditResult(PRIMEDAuditResult): """Base class to hold results for auditing accessors for a SignedAgreement.""" signed_agreement: SignedAgreement - user: User - member: Union[Account, ManagedGroup] note: str has_access: bool + user: User = None + member: Union[Account, ManagedGroup] = None action: str = None def __post_init__(self): @@ -32,11 +32,6 @@ def __post_init__(self): elif isinstance(self.member, ManagedGroup) and self.user: raise ValueError("Cannot specify both a ManagedGroup member and a User.") - def get_action_url(self): - """The URL that handles the action needed.""" - # This is handled in the template with htmx, so None is fine. - return None - def get_table_dictionary(self): """Return a dictionary that can be used to populate an instance of `dbGaPDataAccessSnapshotAuditTable`.""" row = { @@ -46,7 +41,6 @@ def get_table_dictionary(self): "has_access": self.has_access, "note": self.note, "action": self.action, - "action_url": self.get_action_url(), } return row diff --git a/primed/cdsa/audit/uploader_audit.py b/primed/cdsa/audit/uploader_audit.py index 57f0d8f2..bda46bd3 100644 --- a/primed/cdsa/audit/uploader_audit.py +++ b/primed/cdsa/audit/uploader_audit.py @@ -20,11 +20,11 @@ class UploaderAuditResult(PRIMEDAuditResult): """Base class to hold results for auditing uploaders for a DataAffiliateAgreement.""" data_affiliate_agreement: DataAffiliateAgreement - user: User - member: Union[Account, ManagedGroup] note: str has_access: bool action: str = None + user: User = None + member: Union[Account, ManagedGroup] = None def __post_init__(self): if isinstance(self.member, Account) and hasattr(self.member, "user") and self.member.user != self.user: diff --git a/primed/cdsa/tests/test_audit.py b/primed/cdsa/tests/test_audit.py index e0b501bd..d9647a92 100644 --- a/primed/cdsa/tests/test_audit.py +++ b/primed/cdsa/tests/test_audit.py @@ -2078,6 +2078,260 @@ def test_two_rows(self): self.assertEqual(len(table.rows), 2) +class AccessorAuditResultTest(TestCase): + """General tests of the AccessorAuditResult dataclasses for SignedAgreements.""" + + def setUp(self): + super().setUp() + self.cdsa_group = ManagedGroupFactory.create(name=settings.ANVIL_CDSA_GROUP_NAME) + + def test_verified_access(self): + signed_agreement = factories.SignedAgreementFactory.create() + account = AccountFactory.create() + instance = accessor_audit.VerifiedAccess( + signed_agreement=signed_agreement, + member=account, + note="foo", + ) + self.assertIsNone(instance.action) + + def test_verified_no_access(self): + signed_agreement = factories.SignedAgreementFactory.create() + account = AccountFactory.create() + instance = accessor_audit.VerifiedNoAccess( + signed_agreement=signed_agreement, + member=account, + note="foo", + ) + self.assertIsNone(instance.action) + + def test_grant_access(self): + signed_agreement = factories.SignedAgreementFactory.create() + account = AccountFactory.create() + instance = accessor_audit.GrantAccess( + signed_agreement=signed_agreement, + member=account, + note="foo", + ) + self.assertEqual(instance.action, "Grant access") + + def test_remove_access(self): + signed_agreement = factories.SignedAgreementFactory.create() + account = AccountFactory.create() + instance = accessor_audit.RemoveAccess( + signed_agreement=signed_agreement, + member=account, + note="foo", + ) + self.assertEqual(instance.action, "Remove access") + + def test_error(self): + signed_agreement = factories.SignedAgreementFactory.create() + account = AccountFactory.create() + instance = accessor_audit.Error( + signed_agreement=signed_agreement, + member=account, + note="foo", + has_access=False, + ) + self.assertIsNone(instance.action) + + +class AccessorAuditTableTest(TestCase): + """Tests for the `SignedAgreementAccessorAuditTable` table.""" + + def test_no_rows(self): + """Table works with no rows.""" + table = accessor_audit.SignedAgreementAccessorAuditTable([]) + self.assertIsInstance(table, accessor_audit.SignedAgreementAccessorAuditTable) + self.assertEqual(len(table.rows), 0) + + def test_one_row(self): + """Table works with one row.""" + member_agreement = factories.MemberAgreementFactory.create() + account = AccountFactory.create(verified=True) + data = [ + { + "signed_agreement": member_agreement.signed_agreement, + "user": account.user, + "member": account, + "has_access": True, + "note": "a note", + } + ] + table = accessor_audit.SignedAgreementAccessorAuditTable(data) + self.assertIsInstance(table, accessor_audit.SignedAgreementAccessorAuditTable) + self.assertEqual(len(table.rows), 1) + self.assertIn( + str(member_agreement.signed_agreement.cc_id), + table.rows[0].get_cell("signed_agreement"), + ) + self.assertEqual(table.rows[0].get_cell("note"), "a note") + + def test_two_rows(self): + """Table works with two rows.""" + member_agreement_1 = factories.MemberAgreementFactory.create() + member_agreement_2 = factories.DataAffiliateAgreementFactory.create() + account_1 = AccountFactory.create(verified=True) + account_2 = AccountFactory.create(verified=True) + data = [ + { + "signed_agreement": member_agreement_1.signed_agreement, + "user": account_1.user, + "member": account_1, + "has_access": True, + "note": "note 1", + }, + { + "signed_agreement": member_agreement_2.signed_agreement, + "user": account_2.user, + "member": account_2, + "has_access": True, + "note": "note 2", + }, + ] + table = accessor_audit.SignedAgreementAccessorAuditTable(data) + self.assertIsInstance(table, accessor_audit.SignedAgreementAccessorAuditTable) + self.assertEqual(len(table.rows), 2) + self.assertIn( + str(member_agreement_1.signed_agreement.cc_id), + table.rows[0].get_cell("signed_agreement"), + ) + self.assertEqual(table.rows[0].get_cell("note"), "note 1") + self.assertIn( + str(member_agreement_2.signed_agreement.cc_id), + table.rows[1].get_cell("signed_agreement"), + ) + self.assertEqual(table.rows[1].get_cell("note"), "note 2") + + +class UploaderAuditResultTest(TestCase): + """General tests of the UploaderAuditResult dataclasses for DataAffiliateAgreements.""" + + def setUp(self): + super().setUp() + self.cdsa_group = ManagedGroupFactory.create(name=settings.ANVIL_CDSA_GROUP_NAME) + + def test_verified_access(self): + data_affiliate_agreement = factories.DataAffiliateAgreementFactory.create() + account = AccountFactory.create() + instance = uploader_audit.VerifiedAccess( + data_affiliate_agreement=data_affiliate_agreement, + member=account, + note="foo", + ) + self.assertIsNone(instance.action) + + def test_verified_no_access(self): + data_affiliate_agreement = factories.DataAffiliateAgreementFactory.create() + account = AccountFactory.create() + instance = uploader_audit.VerifiedNoAccess( + data_affiliate_agreement=data_affiliate_agreement, + member=account, + note="foo", + ) + self.assertIsNone(instance.action) + + def test_grant_access(self): + data_affiliate_agreement = factories.DataAffiliateAgreementFactory.create() + account = AccountFactory.create() + instance = uploader_audit.GrantAccess( + data_affiliate_agreement=data_affiliate_agreement, + member=account, + note="foo", + ) + self.assertEqual(instance.action, "Grant access") + + def test_remove_access(self): + data_affiliate_agreement = factories.DataAffiliateAgreementFactory.create() + account = AccountFactory.create() + instance = uploader_audit.RemoveAccess( + data_affiliate_agreement=data_affiliate_agreement, + member=account, + note="foo", + ) + self.assertEqual(instance.action, "Remove access") + + def test_error(self): + data_affiliate_agreement = factories.DataAffiliateAgreementFactory.create() + account = AccountFactory.create() + instance = uploader_audit.Error( + data_affiliate_agreement=data_affiliate_agreement, + member=account, + note="foo", + has_access=False, + ) + self.assertIsNone(instance.action) + + +class UploaderAuditTableTest(TestCase): + """Tests for the `DataAffiliateUploaderAuditTable` table.""" + + def test_no_rows(self): + """Table works with no rows.""" + table = uploader_audit.DataAffiliateUploaderAuditTable([]) + self.assertIsInstance(table, uploader_audit.DataAffiliateUploaderAuditTable) + self.assertEqual(len(table.rows), 0) + + def test_one_row(self): + """Table works with one row.""" + data_affiliate_agreement = factories.DataAffiliateAgreementFactory.create() + account = AccountFactory.create(verified=True) + data = [ + { + "data_affiliate_agreement": data_affiliate_agreement, + "user": account.user, + "member": account, + "has_access": True, + "note": "a note", + } + ] + table = uploader_audit.DataAffiliateUploaderAuditTable(data) + self.assertIsInstance(table, uploader_audit.DataAffiliateUploaderAuditTable) + self.assertEqual(len(table.rows), 1) + self.assertIn( + str(data_affiliate_agreement.signed_agreement.cc_id), + table.rows[0].get_cell("data_affiliate_agreement"), + ) + self.assertEqual(table.rows[0].get_cell("note"), "a note") + + def test_two_rows(self): + """Table works with two rows.""" + data_affiliate_agreement_1 = factories.DataAffiliateAgreementFactory.create() + data_affiliate_agreement_2 = factories.DataAffiliateAgreementFactory.create() + account_1 = AccountFactory.create(verified=True) + account_2 = AccountFactory.create(verified=True) + data = [ + { + "data_affiliate_agreement": data_affiliate_agreement_1, + "user": account_1.user, + "member": account_1, + "has_access": True, + "note": "note 1", + }, + { + "data_affiliate_agreement": data_affiliate_agreement_2, + "user": account_2.user, + "member": account_2, + "has_access": True, + "note": "note 2", + }, + ] + table = uploader_audit.DataAffiliateUploaderAuditTable(data) + self.assertIsInstance(table, uploader_audit.DataAffiliateUploaderAuditTable) + self.assertEqual(len(table.rows), 2) + self.assertIn( + str(data_affiliate_agreement_1.signed_agreement.cc_id), + table.rows[0].get_cell("data_affiliate_agreement"), + ) + self.assertEqual(table.rows[0].get_cell("note"), "note 1") + self.assertIn( + str(data_affiliate_agreement_2.signed_agreement.cc_id), + table.rows[1].get_cell("data_affiliate_agreement"), + ) + self.assertEqual(table.rows[1].get_cell("note"), "note 2") + + class SignedAgreementAccessorAuditTest(TestCase): """Tests for the SignedAgreementAccessorAudit classes."""