PylonsWikiNG / src / authorization / tutorial / tests.py

import unittest

from pylons.configuration import Configurator
from repoze.bfg import testing

class TestWikiHandler(unittest.TestCase):
    def setUp(self):
        self.session = self._initTestingDB()
        self.config = Configurator()
        self.config.begin()
        self.config.add_route('root', '/')
        self.config.add_route('wiki_action', '/wiki/{pagename}/{action}')

    def tearDown(self):
        self.session.remove()
        self.config.end()

    def _initTestingDB(self):
        from tutorial.models import DBSession
        from tutorial.models import Base
        from sqlalchemy import create_engine
        engine = create_engine('sqlite://')
        DBSession.configure(bind=engine)
        Base.metadata.bind = engine
        Base.metadata.create_all(engine)
        return DBSession

    def _makeOne(self, request):
        from tutorial.handlers import WikiHandler
        return WikiHandler(request)
        
    def test___call__(self):
        request = testing.DummyRequest()
        handler = self._makeOne(request)
        response = handler()
        self.assertEqual(response.location,
                         'http://example.com/wiki/FrontPage/view')

    def test_view(self):
        request = testing.DummyRequest()
        handler = self._makeOne(request)
        from tutorial.models import Page
        request.matchdict['pagename'] = 'IDoExist'
        page = Page('IDoExist', 'Hello CruelWorld IDoExist')
        self.session.add(page)
        info = handler.view()
        self.assertEqual(info['page'], page)
        self.assertEqual(
            info['content'], 
            '<div class="document">\n'
            '<p>Hello <a href="http://example.com/wiki/CruelWorld/add">'
            'CruelWorld</a> '
            '<a href="http://example.com/wiki/IDoExist/view">'
            'IDoExist</a>'
            '</p>\n</div>\n')
        self.assertEqual(info['edit_url'],
                         'http://example.com/wiki/IDoExist/edit')
        
    
    def test_add_notsubmitted(self):
        request = testing.DummyRequest()
        request.matchdict = {'pagename':'AnotherPage'}
        handler = self._makeOne(request)
        info = handler.add()
        self.assertEqual(info['page'].data,'')
        self.assertEqual(info['save_url'],
                         'http://example.com/wiki/AnotherPage/add')
        
    def test_add_submitted(self):
        from tutorial.models import Page
        request = testing.DummyRequest({'form.submitted':True,
                                        'body':'Hello yo!'})
        request.matchdict = {'pagename':'AnotherPage'}
        handler = self._makeOne(request)
        handler.add()
        page = self.session.query(Page).filter_by(name='AnotherPage').one()
        self.assertEqual(page.data, 'Hello yo!')

    def test_edit_notsubmitted(self):
        from tutorial.models import Page
        request = testing.DummyRequest()
        request.matchdict = {'pagename':'abc'}
        page = Page('abc', 'hello')
        self.session.add(page)
        handler = self._makeOne(request)
        info = handler.edit()
        self.assertEqual(info['page'], page)
        self.assertEqual(info['save_url'],
                         'http://example.com/wiki/abc/edit')
        
    def test_edit_submitted(self):
        from tutorial.models import Page
        request = testing.DummyRequest({'form.submitted':True,
                                        'body':'Hello yo!'})
        request.matchdict = {'pagename':'abc'}
        page = Page('abc', 'hello')
        self.session.add(page)
        handler = self._makeOne(request)
        response = handler.edit()
        self.assertEqual(response.location, 'http://example.com/wiki/abc/view')
        self.assertEqual(page.data, 'Hello yo!')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.