Example #1
0
    def test_pre_delete_async(self, settings, save_model_action, extract_content_data, cache, extract_process_data):
        settings.RUN_ASYNC = True
        model = TestModel(
            string_field='Test',
            integer_field=1,
            float_field=1.0,
        )
        old_object = serialize_model_instance(model)
        model.string_field = None
        model.integer_field = None
        model.float_field = None
        model.id = None
        new_object = serialize_model_instance(model)
        content = signals._extract_content_data(old_object, new_object)
        extract_content_data.return_value = content

        signals._pre_delete(model, instance=model)

        # Check that extract process and access was done successfully
        self.assertEqual(cache.get_last_access.call_count, 1)
        self.assertEqual(cache.get_process.call_count, 1)
        self.assertEqual(extract_process_data.call_count, 1)

        # Check that was called asynchronously
        self.assertEqual(save_model_action.call_count, 0)
        self.assertEqual(save_model_action.apply_async.call_count, 1)
Example #2
0
    def test_post_save_fail_save_model_action(self, settings, save_model_action, extract_content_data, cache,
                                              extract_process_data, logger):
        settings.RUN_ASYNC = False
        save_model_action.side_effect = Exception
        model = TestModel(
            string_field='Test',
            integer_field=1,
            float_field=1.0,
        )
        old_object = serialize_model_instance(model)
        model.string_field = 'Modified'
        model.integer_field = 2
        model.float_field = 2.0
        new_object = serialize_model_instance(model)
        content = signals._extract_content_data(old_object, new_object)
        extract_content_data.return_value = content

        signals._post_save(model, instance=model)

        # Check that extract process and access was done successfully
        self.assertEqual(cache.get_last_access.call_count, 1)
        self.assertEqual(cache.get_process.call_count, 1)
        self.assertEqual(extract_process_data.call_count, 1)

        # Check that save process wasn't done successfully
        self.assertEqual(logger.exception.call_count, 1)
Example #3
0
    def test_post_save_async(self, settings, save_model_action, extract_content_data, cache, extract_process_data):
        settings.RUN_ASYNC = True
        model = TestModel(
            string_field='Test',
            integer_field=1,
            float_field=1.0,
        )
        old_object = serialize_model_instance(model)
        with patch.dict('audit_tools.audit.signals._CACHE', {id(model): old_object}):
            model.string_field = 'Modified'
            model.integer_field = 2
            model.float_field = 2.0
            new_object = serialize_model_instance(model)
            content = signals._extract_content_data(old_object, new_object)
            extract_content_data.return_value = content

            signals._post_save(model, instance=model)

            # Check that extract process and access was done successfully
            self.assertEqual(cache.get_last_access.call_count, 1)
            self.assertEqual(cache.get_process.call_count, 1)
            self.assertEqual(extract_process_data.call_count, 1)

            # Check that was called asynchronously
            self.assertEqual(save_model_action.call_count, 0)
            self.assertEqual(save_model_action.apply_async.call_count, 1)
Example #4
0
    def test_content_create(self):
        model = TestModel()
        model.string_field = 'Test'
        model.integer_field = 1
        model.float_field = 1.0
        new_object = serialize_model_instance(model)
        keys = ['id', 'string_field', 'integer_field', 'float_field']

        content = signals._extract_content_data(None, new_object)

        self.assertItemsEqual(content['new'].keys(), keys)
        self.assertIsNone(content['old'])
        self.assertEqual(content['new']['string_field'], 'Test')
        self.assertEqual(content['new']['integer_field'], 1)
        self.assertEqual(content['new']['float_field'], 1.0)
        self.assertItemsEqual(content['changes'].keys(), keys)
Example #5
0
    def test_content_update(self):
        model = TestModel(
            string_field='Test',
            integer_field=1,
            float_field=1.0,
        )
        old_object = serialize_model_instance(model)
        model.string_field = 'Modified'
        new_object = serialize_model_instance(model)
        keys = ['id', 'string_field', 'integer_field', 'float_field']

        content = signals._extract_content_data(old_object, new_object)

        self.assertItemsEqual(content['new'].keys(), keys)
        self.assertItemsEqual(content['old'].keys(), keys)
        self.assertEqual(content['new']['string_field'], 'Modified')
        self.assertEqual(content['old']['string_field'], 'Test')
        self.assertItemsEqual(content['changes'].keys(), ['string_field'])
Example #6
0
    def test_pre_delete_fail_save_model_action(self, settings, save_model_action, extract_content_data, cache,
                                               extract_process_data, logger):
        settings.RUN_ASYNC = False
        model = TestModel(
            string_field='Test',
            integer_field=1,
            float_field=1.0,
        )
        content = signals._extract_content_data()
        extract_content_data.return_value = content

        signals._pre_delete(model, instance=model)

        # Check that extract process and access was done successfully
        self.assertEqual(cache.get_last_access.call_count, 1)
        self.assertEqual(cache.get_process.call_count, 1)
        self.assertEqual(extract_process_data.call_count, 1)

        # Check that save process wasn't done successfully
        self.assertEqual(logger.exception.call_count, 1)
Example #7
0
    def test_content_delete(self):
        model = TestModel(
            string_field='Test',
            integer_field=1,
            float_field=1.0,
        )
        old_object = serialize_model_instance(model)
        model.string_field = None
        model.integer_field = None
        model.float_field = None
        model.id = None
        keys = ['id', 'string_field', 'integer_field', 'float_field']

        content = signals._extract_content_data(old_object, None)

        self.assertIsNone(content['new'])
        self.assertItemsEqual(content['old'].keys(), keys)
        self.assertEqual(content['old']['string_field'], 'Test')
        self.assertEqual(content['old']['integer_field'], 1)
        self.assertEqual(content['old']['float_field'], 1.0)
        self.assertItemsEqual(content['changes'].keys(), keys)
Example #8
0
    def test_content_empty(self):
        content = signals._extract_content_data()

        self.assertIsNone(content['old'])
        self.assertIsNone(content['new'])
        self.assertEqual(content['changes'], {})