crosswiki / crosswiki / tests.py

import unittest
import mock
import webtest

from pyramid import testing


class UpdatePageTests(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()

    def _callFUT(self, *args, **kwargs):
        from .views import update_page
        return update_page(*args, **kwargs)

    def test_it(self):
        self.config.add_route('page', '/{page_name}')
        mockPage = mock.Mock()
        mockPage.page_name = 'FrontPage'
        context = mock.Mock()
        context.get_page.return_value = mockPage
        request = testing.DummyRequest(context=context, 
            matchdict=dict(page_name=mockPage.page_name),
            params=dict(content='updated'))
        result = self._callFUT(request)
        context.get_page.assert_called_with(mockPage.page_name)
        self.assertEqual(result.location, 'http://example.com/FrontPage')
        self.assertEqual(mockPage.content, 'updated')

    def test_not_found(self):
        context = mock.Mock()
        context.get_page.return_value = None
        request = testing.DummyRequest(context=context, 
            location='not_found_location',
            matchdict=dict(page_name='NoPage'),
            params=dict(content='updated'))

        from pyramid.httpexceptions import HTTPNotFound
        self.assertRaises(HTTPNotFound, self._callFUT, request)


class ShowPageTests(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()

    def _callFUT(self, *args, **kwargs):
        from .views import show_page
        return show_page(*args, **kwargs)

    def test_it(self):
        mockPage = mock.Mock()
        mockPage.page_name = u'this is page name'
        mockPage.html_content = u'this is content'
        context = mock.Mock()
        context.get_page.return_value = mockPage
        request = testing.DummyRequest(context=context, matchdict=dict(page_name='FrontPage'))
        result = self._callFUT(request)
        context.get_page.assert_called_with('FrontPage')

        self.assertEqual(result['page_name'], 'this is page name')
        self.assertEqual(result['html_content'], 'this is content')

class CreatePageTests(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()

    def _callFUT(self, *args, **kwargs):
        from .views import create_page
        return create_page(*args, **kwargs)

    def test_it(self):
        self.config.add_route('page', '/{page_name}')
        context = mock.Mock()
        request = testing.DummyRequest(context=context, matchdict=dict(page_name='FrontPage'),
            params=dict(content='this is content'))
        result = self._callFUT(request)

        self.assertEqual(result.location, 'http://example.com/FrontPage')
        created = context.add.call_args[0][0]
        self.assertEqual(created.page_name, 'FrontPage')
        self.assertEqual(created.content, 'this is content')

class WikiResourceTests(unittest.TestCase):
    def _getTarget(self):
        from .models import WikiResource
        return WikiResource

    def _makeOne(self, *args, **kwargs):
        return self._getTarget()(*args, **kwargs)

    @mock.patch('crosswiki.models.Page')
    def test_get_page(self, MockPage):
        page = object()
        mockQuery = mock.Mock()
        mockQuery.one.return_value = page
        MockPage.query.filter_by.return_value = mockQuery
        request = testing.DummyRequest(matchdict=dict(page_name='FrontPage'))
        target = self._makeOne(request)
        result = target.get_page('FrontPage')
        MockPage.query.filter_by.assert_called_with(page_name='FrontPage')
        self.assertEqual(result, page)

    @mock.patch('crosswiki.models.Page')
    def test_get_page_no_result(self, MockPage):
        mockQuery = mock.Mock()
        from sqlalchemy.orm.exc import NoResultFound
        mockQuery.one.side_effect = NoResultFound()
        MockPage.query.filter_by.return_value = mockQuery
        request = testing.DummyRequest(matchdict=dict(page_name='FrontPage'))
        target = self._makeOne(request)
        result = target.get_page('FrontPage')
        self.assertIsNone(result)

class PageUrlTests(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()

    def _callFUT(self, *args, **kwargs):
        from .helpers import page_url
        return page_url(*args, **kwargs)

    def test_it(self):
        self.config.add_route('page', '/{page_name}')
        request = testing.DummyRequest()
        page = mock.Mock()
        page.page_name = 'FrontPage'
        result = self._callFUT(request, page)
        self.assertEqual(result, 'http://example.com/FrontPage')


class PageTests(unittest.TestCase):
    def _getTarget(self):
        from .models import Page
        return Page

    def _makeOne(self, *args, **kwargs):
        return self._getTarget()(*args, **kwargs)

    def test_html_content(self):
        target = self._makeOne(page_name=u'FrontPage',
            content=u"""\
FrontPage
---------------------

this is FrontPage
""")

        self.assertEqual(target.html_content, '<div class="document" id="frontpage">\n<h1 class="title">FrontPage</h1>\n<p>this is FrontPage</p>\n</div>\n')

class FunctionalTests(unittest.TestCase):
    settings = {
        'sqlalchemy.url': 'sqlite:///',
        'mako.directories': 'crosswiki:templates',
    }

    def _makeAUT(self):
        from . import main
        app = main({}, **self.settings)
        return webtest.TestApp(app)

    def test_show(self):
        app = self._makeAUT()
        res = app.get('/FrontPage')
        self.assertIn('FrontPage', res)
        a = res.lxml.xpath("//*[@id='page-edit-url']")
        self.assertEqual(a[0].attrib['href'], 'http://localhost/FrontPage/edit')

    def test_create(self):
        app = self._makeAUT()
        res = app.get('/NewPage')
        self.assertEqual(res.location, 'http://localhost/NewPage/create')
        res = app.get(res.location)
        res.form['content'].value = 'NewPage'
        res = res.form.submit()
        self.assertEqual(res.location, 'http://localhost/NewPage')
        

    def test_edit(self):
        app = self._makeAUT()
        res = app.get('/FrontPage/edit')
        res.form['content'] = 'updated'
        res = res.form.submit()
        self.assertEqual(res.location, 'http://localhost/FrontPage')

        res = app.get(res.location)
        self.assertIn('updated', res)
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.