Exemplo n.º 1
0
    def test_changes(self):
        model_action = ModelAction(
            model=None,
            action=self.action,
            content=self.ma_content,
            instance=None,
            timestamp=None,
            access=None,
            process=None,
        )

        # Test getter
        self.assertEqual(self.content_changes, model_action.changes)

        # Test setter
        changes = {}
        model_action.changes = changes
        self.assertEqual(changes, model_action.changes)

        # Test deleter
        del model_action.changes
        self.assertEqual({}, model_action.changes)

        # Let AccessRequest in his old status
        model_action.changes = self.content_changes
Exemplo n.º 2
0
    def test_changes(self):
        model_action = ModelAction(
                model=None,
                action=self.action,
                content=self.ma_content,
                instance=None,
                timestamp=None,
                access=None,
                process=None,
        )

        # Test getter
        self.assertEqual(self.content_changes, model_action.changes)

        # Test setter
        changes = {}
        model_action.changes = changes
        self.assertEqual(changes, model_action.changes)

        # Test deleter
        del model_action.changes
        self.assertEqual({}, model_action.changes)

        # Let AccessRequest in his old status
        model_action.changes = self.content_changes
Exemplo n.º 3
0
    def test_get_model(self):
        model_action = ModelAction(
            model=self.ma_model,
            action=self.action,
            content=self.ma_content,
            instance=None,
            timestamp=None,
            access=None,
            process=None,
        )

        # Test getter
        self.assertEqual(Foo, model_action.get_model())
Exemplo n.º 4
0
    def test_get_model(self):
        model_action = ModelAction(
                model=self.ma_model,
                action=self.action,
                content=self.ma_content,
                instance=None,
                timestamp=None,
                access=None,
                process=None,
        )

        # Test getter
        self.assertEqual(Foo, model_action.get_model())
Exemplo n.º 5
0
    def test_verbose_str(self):
        model_action = ModelAction(
                model=self.ma_model,
                action=self.action,
                content=self.ma_content,
                instance=self.ma_instance,
                timestamp=self.timestamp,
                access=None,
                process=None,
        )

        expected = '{} instance {} of model {} from app {} ({})'.format(
                self.action, self.instance_id, self.model_name, self.model_app, self.timestamp)
        self.assertEqual(expected, model_action.verbose_str())
Exemplo n.º 6
0
    def test_get_instance_fail(self):
        model_action = ModelAction(
                model=self.ma_model,
                action=self.action,
                content=self.ma_content,
                instance=None,
                timestamp=None,
                access=None,
                process=None,
        )

        with patch('audit_tools.tests.audit.models.test_model_action.Foo') as model_mock:
            model_mock.objects.get.side_effect = Exception
            self.assertIsNone(model_action.get_instance())
Exemplo n.º 7
0
    def test_verbose_str(self):
        model_action = ModelAction(
            model=self.ma_model,
            action=self.action,
            content=self.ma_content,
            instance=self.ma_instance,
            timestamp=self.timestamp,
            access=None,
            process=None,
        )

        expected = '{} instance {} of model {} from app {} ({})'.format(
            self.action, self.instance_id, self.model_name, self.model_app,
            self.timestamp)
        self.assertEqual(expected, model_action.verbose_str())
Exemplo n.º 8
0
    def test_get_instance_fail(self):
        model_action = ModelAction(
            model=self.ma_model,
            action=self.action,
            content=self.ma_content,
            instance=None,
            timestamp=None,
            access=None,
            process=None,
        )

        with patch('audit_tools.tests.audit.models.test_model_action.Foo'
                   ) as model_mock:
            model_mock.objects.get.side_effect = Exception
            self.assertIsNone(model_action.get_instance())
Exemplo n.º 9
0
    def test_get_instance(self):
        model_action = ModelAction(
                model=self.ma_model,
                action=self.action,
                content=self.ma_content,
                instance=self.ma_instance,
                timestamp=None,
                access=None,
                process=None,
        )

        foo = Foo()
        with patch('audit_tools.tests.audit.models.test_model_action.Foo') as model_mock:
            model_mock.objects.get.return_value = foo
            self.assertEqual(foo, model_action.get_instance())
Exemplo n.º 10
0
    def test_get_instance(self):
        model_action = ModelAction(
            model=self.ma_model,
            action=self.action,
            content=self.ma_content,
            instance=self.ma_instance,
            timestamp=None,
            access=None,
            process=None,
        )

        foo = Foo()
        with patch('audit_tools.tests.audit.models.test_model_action.Foo'
                   ) as model_mock:
            model_mock.objects.get.return_value = foo
            self.assertEqual(foo, model_action.get_instance())
Exemplo n.º 11
0
    def test_items(self):
        items = [('foo', 'bar')]
        model_action = ModelAction(
                model=None,
                action=self.action,
                content=self.ma_content,
                instance=None,
                timestamp=None,
                access=None,
                process=None,
        )

        items_mock = MagicMock()
        items_mock.items.return_value = items
        with patch.object(ModelAction, 'to_mongo', return_value=items_mock) as mongo_mock:
            result = model_action.items()

            self.assertEqual(items, result)
            self.assertEqual(mongo_mock.call_count, 1)
            self.assertEqual(items_mock.items.call_count, 1)
Exemplo n.º 12
0
    def test_items(self):
        items = [('foo', 'bar')]
        model_action = ModelAction(
            model=None,
            action=self.action,
            content=self.ma_content,
            instance=None,
            timestamp=None,
            access=None,
            process=None,
        )

        items_mock = MagicMock()
        items_mock.items.return_value = items
        with patch.object(ModelAction, 'to_mongo',
                          return_value=items_mock) as mongo_mock:
            result = model_action.items()

            self.assertEqual(items, result)
            self.assertEqual(mongo_mock.call_count, 1)
            self.assertEqual(items_mock.items.call_count, 1)
Exemplo n.º 13
0
    def test_get_model_fail(self, dynamic_import_mock):
        model_action = ModelAction(
            model=self.ma_model,
            action=self.action,
            content=self.ma_content,
            instance=None,
            timestamp=None,
            access=None,
            process=None,
        )

        # Test getter
        self.assertRaises(TypeError, model_action.get_model)
Exemplo n.º 14
0
    def test_str(self):
        model_action = ModelAction(
            model=self.ma_model,
            action=self.action,
            content=self.ma_content,
            instance=self.ma_instance,
            timestamp=self.timestamp,
            access=None,
            process=None,
        )

        expected = 'ModelAction{{{}({}), action:{}, time:{}}}'.format(
            self.model_full_name, self.instance_id, self.action,
            self.timestamp)
        self.assertEqual(expected, str(model_action))
Exemplo n.º 15
0
def _model_action_factory(model,
                          action,
                          content,
                          instance,
                          timestamp=datetime.datetime.now(),
                          process=None,
                          access=None):
    """
    Factory to create :class:`audit_tools.ModelAction` in a flexible way. The object is not stored.

    :param model: Objective model field values.
    :type model: dict
    :param action: Action performed.
    :type action: str
    :param content: Object content changes.
    :type content: dict
    :param instance: Objective instance field values.
    :type instance: dict
    :param timestamp: Timestamp.
    :type timestamp: :class:`datetime.datetime`
    :param process: Process linked.
    :type process: :class:`audit_tools.Process`
    :param access: Access linked.
    :type access: :class:`audit_tools.Access`
    :return: Model created.
    :rtype: :class:`audit_tools.ModelAction`
    """
    from audit_tools.audit.models.model_action import ModelActionModel, ModelActionContent, ModelActionInstance, \
        ModelAction

    model_document = ModelActionModel(**model)
    content_document = ModelActionContent(**content)
    instance_document = ModelActionInstance(**instance)

    model_action = ModelAction(
        model=model_document,
        action=action,
        content=content_document,
        instance=instance_document,
        timestamp=timestamp,
        access=access,
        process=process,
    )

    return model_action