Example #1
0
 def test_check_permissions_app_label_single(self):
     perm = 'testapp.add_book'
     book = BookFixture(Book).create_one()
     self.assertEqual(utils.check_permissions_app_label(perm),
                      (utils.get_content_type(book), {'add_book'}))
     self.assertEqual(utils.check_permissions_app_label(perm),
                      (utils.get_content_type(Book), {'add_book'}))
Example #2
0
    def test_with_superuser_false(self):
        BookFixture(Book, follow_fk=True, generate_m2m={
            'authors': (1, 1)
        }).create(count=2)

        user = User.objects.latest('pk')
        user.is_superuser = True

        # `with_superuser=False` requires defined access rules - should yield no results!
        self.assertEqual(
            set(Book.objects.none()),
            set(
                utils.get_objects_for_user(user, ['testapp.change_book'],
                                           Book.objects.all(),
                                           with_superuser=False)))

        access_rule = AccessRule.objects.create(
            ctype_source=utils.get_content_type(user),
            ctype_target=utils.get_content_type(Book),
            relation_types=[{
                'AUTHOR': None
            }, {
                'BOOK': None
            }])
        perm = Permission.objects.get(content_type__app_label='testapp',
                                      codename='change_book')
        access_rule.permissions.add(perm)
        user.user_permissions.add(perm)

        objects = utils.get_objects_for_user(user, ['testapp.change_book'],
                                             Book.objects.all(),
                                             with_superuser=False)
        self.assertEqual(set(user.author.book_set.all()), set(objects))
Example #3
0
    def test_relation_types_definition_index_variable(self):
        book = BookFixture(Book, generate_m2m={'authors': (2, 2)}).create_one()
        get_nodeset_for_queryset(Store.objects.filter(pk=book.pk), sync=True)

        user = User.objects.filter(
            pk__in=book.authors.values('user')).latest('pk')
        perm = Permission.objects.get(content_type__app_label='auth',
                                      codename='change_user')

        access_rule = AccessRule.objects.create(
            ctype_source=utils.get_content_type(User),
            ctype_target=utils.get_content_type(User),
            relation_types=[{
                'AUTHOR': None
            }, {
                'BOOK': None
            }, {
                '{0:AUTHORS}': None
            }, {
                'USER': None
            }])
        access_rule.permissions.add(perm)
        user.user_permissions.add(perm)

        objects = utils.get_objects_for_user(user, 'auth.change_user')
        self.assertEqual({user}, set(objects))
        self.assertNotEqual(User.objects.count(), objects.count())
Example #4
0
    def test_check_permissions_app_label_sequence(self):
        perms = ['testapp.add_book', 'testapp.change_book']
        book = BookFixture(Book).create_one()

        ctype, codenames = utils.check_permissions_app_label(perms)
        self.assertEqual(ctype, utils.get_content_type(book))
        self.assertEqual(sorted(codenames), ['add_book', 'change_book'])
 def test_delete_object_is_deleted(self):
     pre_delete.disconnect(pre_delete_handler, dispatch_uid='chemtrails.signals.handlers.pre_delete_handler')
     pre_delete.connect(pre_delete_handler, dispatch_uid='pre_delete_handler.test')
     try:
         book = BookFixture(Book).create_one()
         klass = get_node_class_for_model(Book)
         pk = book.pk
         try:
             book.delete()
             klass.nodes.get(pk=pk)
             self.fail('Did not raise when trying to get non-existent book node.')
         except klass.DoesNotExist as e:
             self.assertEqual(str(e), "{'pk': %d}" % pk)
     finally:
         pre_delete.connect(pre_delete_handler, dispatch_uid='chemtrails.signals.handlers.pre_delete_handler')
         pre_delete.disconnect(pre_delete_handler, dispatch_uid='pre_delete_handler.test')
Example #6
0
    def setUp(self):
        clear_neo4j_model_nodes()

        BookFixture(Book, generate_m2m={'authors': (2, 2)}).create_one()
        self.user1, self.user2 = User.objects.earliest(
            'pk'), User.objects.latest('pk')
        self.group = Group.objects.create(name='group')
Example #7
0
        def create_nodes():
            book = BookFixture(Book).create_one()

            node = get_node_for_object(book)
            assert isinstance(node, StructuredNode)

            klass = get_node_class_for_model(Book)
            assert len(klass.nodes.all()) == 1
Example #8
0
    def test_create_model_node(self):
        book = BookFixture(Book).create_one()

        @six.add_metaclass(ModelNodeMeta)
        class ModelNode(ModelNodeMixin, StructuredNode):
            class Meta:
                model = Book

        self.assertTrue(issubclass(ModelNode, StructuredNode))
        self.assertIsInstance(ModelNode(instance=book), StructuredNode)
Example #9
0
    def test_flush_nodes_context_manager(self):

        with flush_nodes():
            book = BookFixture(Book).create_one()

            node = get_node_for_object(book)
            self.assertIsInstance(node, StructuredNode)

            klass = get_node_class_for_model(Book)
            self.assertEqual(len(klass.nodes.all()), 1)

        self.assertEqual(len(klass.nodes.all()), 0)
    def test_create_new_object_is_synced(self):
        post_save.disconnect(post_save_handler, dispatch_uid='chemtrails.signals.handlers.post_save_handler')
        post_save.connect(post_save_handler, dispatch_uid='post_save_handler.test')
        try:
            book = BookFixture(Book).create_one()
            klass = get_node_class_for_model(Book)

            self.assertEqual(book.pk, klass.nodes.get(pk=book.pk).pk)
            self.assertEqual(1, len(klass.nodes.has(authors=True, publisher=True)))
        finally:
            post_save.connect(post_save_handler, dispatch_uid='chemtrails.signals.handlers.post_save_handler')
            post_save.disconnect(post_save_handler, dispatch_uid='post_save_handler.test')
    def test_m2m_changed_post_add_reverse(self):
        m2m_changed.disconnect(m2m_changed_handler, dispatch_uid='chemtrails.signals.handlers.m2m_changed_handler')
        m2m_changed.connect(m2m_changed_handler, dispatch_uid='m2m_changed_handler.test')
        try:
            author = AuthorFixture(Author).create_one()
            self.assertEqual(0, len(get_node_class_for_model(Author).nodes.has(book_set=True)))

            book = BookFixture(Book, follow_m2m=False, field_values={'authors': []}).create_one()
            author.book_set.add(book)
            self.assertEqual(1, len(get_node_class_for_model(Author).nodes.has(book_set=True)))
        finally:
            m2m_changed.connect(m2m_changed_handler, dispatch_uid='chemtrails.signals.handlers.m2m_changed_handler')
            m2m_changed.disconnect(m2m_changed_handler, dispatch_uid='m2m_changed_handler.test')
    def test_m2m_changed_post_clear(self):
        m2m_changed.disconnect(m2m_changed_handler, dispatch_uid='chemtrails.signals.handlers.m2m_changed_handler')
        m2m_changed.connect(m2m_changed_handler, dispatch_uid='m2m_changed_handler.test')
        try:
            book = BookFixture(Book, generate_m2m={'authors': (1, 1)}).create_one()
            self.assertEqual(1, len(get_node_class_for_model(Book).nodes.has(authors=True)))

            book.authors.clear()
            self.assertEqual(0, len(get_node_class_for_model(Book).nodes.has(authors=True)))
            self.assertEqual(0, len(get_node_class_for_model(Author).nodes.has(book_set=True)))
        finally:
            m2m_changed.connect(m2m_changed_handler, dispatch_uid='chemtrails.signals.handlers.m2m_changed_handler')
            m2m_changed.disconnect(m2m_changed_handler, dispatch_uid='m2m_changed_handler.test')
Example #13
0
    def test_recursive_connect(self):
        post_save.disconnect(
            post_save_handler,
            dispatch_uid='chemtrails.signals.handlers.post_save_handler')
        m2m_changed.disconnect(
            m2m_changed_handler,
            dispatch_uid='chemtrails.signals.handlers.m2m_changed_handler')
        try:
            book = BookFixture(Book, generate_m2m={
                'authors': (1, 1)
            }).create_one()
            for depth in range(3):
                db.cypher_query(
                    'MATCH (n)-[r]-() WHERE n.type = "ModelNode" DELETE r'
                )  # Delete all relationships
                book_node = get_node_for_object(book).save()
                book_node.recursive_connect(depth)

                if depth == 0:
                    # Max depth 0 means that no recursion should occur, and no connections
                    # can be made, because the connected objects might not exist.
                    for prop in book_node.defined_properties(
                            aliases=False, properties=False).keys():
                        relation = getattr(book_node, prop)
                        try:
                            self.assertEqual(len(relation.all()), 0)
                        except CardinalityViolation:
                            # Will raise CardinalityViolation for nodes which has a single
                            # required relationship
                            continue

                elif depth == 1:
                    self.assertEqual(
                        0,
                        len(
                            get_node_class_for_model(Book).nodes.has(
                                store_set=True)))
                    self.assertEqual(
                        0,
                        len(
                            get_node_class_for_model(Store).nodes.has(
                                books=True)))

                    self.assertEqual(
                        0,
                        len(
                            get_node_class_for_model(Book).nodes.has(
                                bestseller_stores=True)))
                    self.assertEqual(
                        0,
                        len(
                            get_node_class_for_model(Store).nodes.has(
                                bestseller=True)))

                    self.assertEqual(
                        1,
                        len(
                            get_node_class_for_model(Book).nodes.has(
                                publisher=True)))
                    self.assertEqual(
                        1,
                        len(
                            get_node_class_for_model(Publisher).nodes.has(
                                book_set=True)))

                    self.assertEqual(
                        1,
                        len(
                            get_node_class_for_model(Book).nodes.has(
                                authors=True)))
                    self.assertEqual(
                        1,
                        len(
                            get_node_class_for_model(Author).nodes.has(
                                book_set=True)))

                    self.assertEqual(
                        0,
                        len(
                            get_node_class_for_model(Author).nodes.has(
                                user=True)))
                    self.assertEqual(
                        0,
                        len(
                            get_node_class_for_model(User).nodes.has(
                                author=True)))

                    self.assertEqual(
                        1,
                        len(
                            get_node_class_for_model(Book).nodes.has(
                                tags=True)))
                    self.assertEqual(
                        0,
                        len(
                            get_node_class_for_model(Tag).nodes.has(
                                content_type=True)))

                elif depth == 2:
                    self.assertEqual(
                        1,
                        len(
                            get_node_class_for_model(Author).nodes.has(
                                user=True)))
                    self.assertEqual(
                        1,
                        len(
                            get_node_class_for_model(User).nodes.has(
                                author=True)))
                    self.assertEqual(
                        1,
                        len(
                            get_node_class_for_model(Tag).nodes.has(
                                content_type=True)))
                    self.assertEqual(
                        1,
                        len(
                            get_node_class_for_model(ContentType).nodes.has(
                                content_type_set_for_tag=True)))
        finally:
            post_save.connect(
                post_save_handler,
                dispatch_uid='chemtrails.signals.handlers.post_save_handler')
            m2m_changed.connect(
                m2m_changed_handler,
                dispatch_uid='chemtrails.signals.handlers.m2m_changed_handler')
Example #14
0
 def test_get_node_class_for_model(self):
     book = BookFixture(Book).create_one()
     klass = get_node_class_for_model(book, for_concrete_model=True)
     self.assertTrue(issubclass(klass, StructuredNode))
Example #15
0
 def test_connected_meta_node(self):
     book = BookFixture(Book).create_one()
     meta = get_meta_node_for_model(Book).sync()
Example #16
0
 def setUp(self):
     self.node = get_node_for_object(BookFixture(Book).create_one())
Example #17
0
 def test_path_manager_build_query_with_invalid_relationship(self):
     node = get_node_for_object(BookFixture(Book).create_one())
     self.assertRaisesMessage(
         AttributeError,
         '<BookNode: {id}> has no relation type FOOBAR'.format(id=node.id),
         node.paths.add, 'FOOBAR')