예제 #1
0
def save_model_action(model_action_data, access, process):
    from audit_tools.audit.models import Access
    from audit_tools.audit.models.models_factory import create_model_action
    try:
        logger.debug("Pre save ModelAction")
        try:
            a = Access.objects.get(id=access.id)
            m = create_model_action(model_action_data, a, process)
        except AttributeError:
            m = create_model_action(model_action_data, None, process)
        except ValidationError:
            access.save()
            a = Access.objects.get(id=access.id)
            m = create_model_action(model_action_data, a, process)
        m.save()
        logger.debug("Post save ModelAction: %s", m.id)
    except:
        logger.exception("Error saving ModelAction document")

    return True
    def test_create_model_action(self):
        model_action_data = {}
        access = MagicMock()
        process = MagicMock()
        model_action = MagicMock()

        with patch('audit_tools.audit.models.models_factory._model_action_factory', return_value=model_action)\
                as maf_mock:
            result = create_model_action(model_action_data, access, process)

        self.assertIn('access', model_action_data)
        self.assertIn('process', model_action_data)
        self.assertEqual(maf_mock.call_count, 1)
        self.assertEqual(result, model_action)
    def test_create_model_action(self):
        model_action_data = {}
        access = MagicMock()
        process = MagicMock()
        model_action = MagicMock()

        with patch('audit_tools.audit.models.models_factory._model_action_factory', return_value=model_action)\
                as maf_mock:
            result = create_model_action(model_action_data, access, process)

        self.assertIn('access', model_action_data)
        self.assertIn('process', model_action_data)
        self.assertEqual(maf_mock.call_count, 1)
        self.assertEqual(result, model_action)