Exemplo n.º 1
0
class TestAppConfig(TestCase):
    def setUp(self):
        app_config = apps.get_app_config('gdpr_assist')
        self.app_config = GdprAppConfig(app_config.name, app_config.module)

    @mock.patch('gdpr_assist.registry.registry.models', {})
    @mock.patch('gdpr_assist.registry.registry.watching_on_delete', [])
    def test_register_on_delete_anonymise__finds_target(self):
        TargetMockModel = mock.MagicMock()

        mock_field = mock.MagicMock(spec=models.ForeignKey)
        mock_field.remote_field = mock.MagicMock()
        mock_field.remote_field.on_delete = gdpr_assist.ANONYMISE(None)
        mock_field.related_model = TargetMockModel

        MockModel = mock.MagicMock()
        MockModel._meta.get_fields.return_value = [mock_field]

        registry.models[MockModel] = None

        self.app_config.register_on_delete_anonymise()

        self.assertEqual(
            registry.watching_on_delete,
            [TargetMockModel],
        )

    @mock.patch('gdpr_assist.registry.registry.models', {})
    def test_validate_on_delete_anonymise__registered__no_error(self):
        MockModel = mock.MagicMock()
        registry.models[MockModel] = None

        with mock.patch.object(apps, 'get_models'):
            apps.get_models.return_value = [MockModel]
            self.app_config.validate_on_delete_anonymise()

    @mock.patch('gdpr_assist.registry.registry.models', {})
    def test_validate_on_delete_anonymise__not_registered__raises_exception(self):
        mock_field = mock.MagicMock(spec=models.ForeignKey)
        mock_field.name = 'sample_field'
        mock_field.remote_field = mock.MagicMock()
        mock_field.remote_field.on_delete = gdpr_assist.ANONYMISE(None)

        MockModel = mock.MagicMock()
        MockModel._meta.get_fields.return_value = [mock_field]
        MockModel._meta.app_label = 'test'
        MockModel._meta.object_name = 'Sample'

        with mock.patch.object(apps, 'get_models'):
            apps.get_models.return_value = [MockModel]
            with self.assertRaises(ValueError) as cm:
                self.app_config.validate_on_delete_anonymise()
            self.assertEqual(
                (
                    'Relationship test.Sample.sample_field set to anonymise on '
                    'delete, but model is not registered with gdpr-assist'
                ),
                str(cm.exception),
            )
 def setUp(self):
     app_config = apps.get_app_config("gdpr_assist")
     self.app_config = GdprAppConfig(app_config.name, app_config.module)