Beispiel #1
0
class TestPublishViews(TestDatabaseFixture):
    def setUp(self):
        super(TestPublishViews, self).setUp()

        self.registry = NodeRegistry()
        self.registry.register_node(Node, view=MyNodeView, child_nodetypes=['*'])
        self.registry.register_node(TestType, view=MyNodeView, child_nodetypes=['*'], parent_nodetypes=['*'])

        self.registry.register_view('node', ExpandedNodeView)
        self.registry.register_view(TestType, ExpandedNodeView)

        # Make some nodes
        self.root = Node(name=u'root', title=u'Root Node')
        if not hasattr(self, 'nodetype'):
            self.nodetype = Node
        self.node1 = self.nodetype(name=u'node1', title=u'Node 1', parent=self.root)
        self.node2 = self.nodetype(name=u'node2', title=u'Node 2', parent=self.root)
        self.node3 = self.nodetype(name=u'node3', title=u'Node 3', parent=self.node2)
        self.node4 = self.nodetype(name=u'node4', title=u'Node 4', parent=self.node3)
        self.node5 = self.nodetype(name=u'node5', title=u'Node 5', parent=self.root)
        db.session.add_all([self.root, self.node1, self.node2, self.node3, self.node4, self.node5])
        db.session.commit()

        self.rootpub = NodePublisher(self.root, self.registry, u'/')
        self.nodepub = NodePublisher(self.root, self.registry, u'/node2', u'/')
        self.nodepub_differenturl = NodePublisher(self.root, self.registry, u'/node2', u'/newnode2')
        self.nodepub_defaulturl = NodePublisher(self.root, self.registry, u'/node2')

    def test_init_root(self):
        deferpub = NodePublisher(None, self.registry, u'/')
        self.assertEqual(deferpub.root, None)
        self.assertEqual(deferpub.root_id, None)
        deferpub.root = self.node1
        self.assertEqual(deferpub.root, self.node1)
        self.assertEqual(deferpub.root_id, self.node1.id)

    def test_publishview(self):
        """Publish a default view."""
        with self.app.test_request_context():
            response = self.rootpub.publish(u'/node2')
        self.assertEqual(response, 'node-index')
        with self.app.test_request_context():
            response = self.nodepub.publish(u'/')
        self.assertEqual(response, 'node-index')
        with self.app.test_request_context():
            response = self.nodepub_defaulturl.publish(u'/node2')
        self.assertEqual(response, 'node-index')
        with self.app.test_request_context():
            response = self.nodepub_differenturl.publish(u'/newnode2')
        self.assertEqual(response, 'node-index')

    def test_methods(self):
        """Publish views with different methods."""
        with self.app.test_request_context(method='GET'):
            response = self.rootpub.publish(u'/node2/edit')
        self.assertEqual(response, 'edit-GET')

        with self.app.test_request_context(method='GET'):
            response = self.nodepub.publish(u'/edit')
        self.assertEqual(response, 'edit-GET')

        with self.app.test_request_context(method='POST'):
            response = self.rootpub.publish(u'/node2/edit')
        self.assertEqual(response, 'edit-POST')

        with self.app.test_request_context(method='POST'):
            response = self.nodepub.publish(u'/edit')
        self.assertEqual(response, 'edit-POST')

        with self.app.test_request_context(method='GET'):
            response = self.rootpub.publish(u'/node2/multimethod')
        self.assertEqual(response, 'multimethod-GET')

        with self.app.test_request_context(method='GET'):
            response = self.nodepub.publish(u'/multimethod')
        self.assertEqual(response, 'multimethod-GET')

        with self.app.test_request_context(method='POST'):
            response = self.rootpub.publish(u'/node2/multimethod')
        self.assertEqual(response, 'multimethod-POST')

        with self.app.test_request_context(method='POST'):
            response = self.nodepub.publish(u'/multimethod')
        self.assertEqual(response, 'multimethod-POST')

        with self.app.test_request_context(method='PUT'):
            response = self.rootpub.publish(u'/node2/multimethod')
        self.assertEqual(response, 'multimethod-PUT')

        with self.app.test_request_context(method='PUT'):
            response = self.nodepub.publish(u'/multimethod')
        self.assertEqual(response, 'multimethod-PUT')

        with self.app.test_request_context(method='GET'):
            self.assertRaises(NotFound, self.rootpub.publish, u'/node2/random')

        with self.app.test_request_context(method='GET'):
            self.assertRaises(NotFound, self.nodepub.publish, u'/random')

    def test_redirect_gone(self):
        """
        Test the publisher's 30x and 410 responses.
        """
        self.node2.name = u'nodeX'
        db.session.commit()
        with self.app.test_request_context(method='GET'):
            response = self.rootpub.publish(u'/node2/edit')
        self.assertTrue(response.status[:3] in ['301', '302'])
        self.assertEqual(response.headers['Location'], '/nodeX/edit')

        db.session.delete(self.node2)
        db.session.commit()
        with self.app.test_request_context(method='GET'):
            self.assertRaises(Gone, self.rootpub.publish, u'/node2/edit')

    def test_noroot(self):
        """
        Test the publisher's NOROOT 404 response.
        """
        newpub = NodePublisher(self.root, self.registry, u'/no-node')
        with self.app.test_request_context(method='GET'):
            self.assertRaises(NotFound, newpub.publish, '/')

    def test_urlfor(self):
        """Test the publisher's url making functionality"""
        with self.app.test_request_context(method='GET'):
            pub = self.rootpub
            self.assertEqual(pub.url_for(self.node2), '/node2/')
            self.assertEqual(pub.url_for(self.node2, 'editget'), '/node2/edit')
            self.assertEqual(pub.url_for(self.node2, 'editget', _external=True), 'http://localhost/node2/edit')
            self.assertEqual(pub.url_for(self.node2, 'editget', js=False), '/node2/edit?js=False')

            self.assertEqual(pub.url_for(self.node3, 'editget'), '/node2/node3/edit')
            self.assertRaises(ViewNotFound, pub.url_for, self.node3, 'random')

            pub = self.nodepub
            self.assertEqual(pub.url_for(self.node2, 'editget'), '/edit')
            self.assertEqual(pub.url_for(self.node3, 'editget'), '/node3/edit')
            self.assertRaises(ViewNotFound, pub.url_for, self.node3, 'random')

            pub = self.nodepub_defaulturl
            self.assertEqual(pub.url_for(self.node2, 'editget'), '/node2/edit')
            self.assertEqual(pub.url_for(self.node3, 'editget'), '/node2/node3/edit')
            self.assertRaises(ViewNotFound, pub.url_for, self.node3, 'random')

            pub = self.nodepub_differenturl
            self.assertEqual(pub.url_for(self.node2, 'editget'), '/newnode2/edit')
            self.assertEqual(pub.url_for(self.node3, 'editget'), '/newnode2/node3/edit')
            self.assertRaises(ViewNotFound, pub.url_for, self.node3, 'random')