From 6175ff659c2523974072b404636911ebb89c2d42 Mon Sep 17 00:00:00 2001 From: Steve Piercy Date: Wed, 1 Jan 2020 20:15:20 -0800 Subject: Resync wiki/*.rst and related files after moving tests directory --- docs/tutorials/wiki/installation.rst | 4 +- docs/tutorials/wiki/src/authorization/MANIFEST.in | 3 + docs/tutorials/wiki/src/authorization/pytest.ini | 7 +- .../wiki/src/authorization/tests/__init__.py | 0 .../wiki/src/authorization/tests/test_it.py | 24 +++ .../wiki/src/authorization/tutorial/tests.py | 24 --- docs/tutorials/wiki/src/basiclayout/MANIFEST.in | 3 + docs/tutorials/wiki/src/basiclayout/pytest.ini | 7 +- .../wiki/src/basiclayout/tests/__init__.py | 0 .../wiki/src/basiclayout/tests/test_it.py | 24 +++ .../wiki/src/basiclayout/tutorial/tests.py | 24 --- docs/tutorials/wiki/src/installation/MANIFEST.in | 3 + docs/tutorials/wiki/src/installation/pytest.ini | 7 +- .../wiki/src/installation/tests/__init__.py | 0 .../wiki/src/installation/tests/test_it.py | 24 +++ .../wiki/src/installation/tutorial/tests.py | 24 --- docs/tutorials/wiki/src/models/MANIFEST.in | 3 + docs/tutorials/wiki/src/models/pytest.ini | 7 +- docs/tutorials/wiki/src/models/tests/__init__.py | 0 docs/tutorials/wiki/src/models/tests/test_it.py | 24 +++ docs/tutorials/wiki/src/models/tutorial/tests.py | 24 --- docs/tutorials/wiki/src/tests/MANIFEST.in | 3 + docs/tutorials/wiki/src/tests/pytest.ini | 7 +- docs/tutorials/wiki/src/tests/tests/__init__.py | 0 docs/tutorials/wiki/src/tests/tests/test_it.py | 232 +++++++++++++++++++++ docs/tutorials/wiki/src/tests/tutorial/tests.py | 232 --------------------- docs/tutorials/wiki/src/views/MANIFEST.in | 3 + docs/tutorials/wiki/src/views/pytest.ini | 7 +- docs/tutorials/wiki/src/views/tests/__init__.py | 0 docs/tutorials/wiki/src/views/tests/test_it.py | 24 +++ docs/tutorials/wiki/src/views/tutorial/tests.py | 24 --- docs/tutorials/wiki/tests.rst | 16 +- 32 files changed, 410 insertions(+), 374 deletions(-) create mode 100644 docs/tutorials/wiki/src/authorization/tests/__init__.py create mode 100644 docs/tutorials/wiki/src/authorization/tests/test_it.py delete mode 100644 docs/tutorials/wiki/src/authorization/tutorial/tests.py create mode 100644 docs/tutorials/wiki/src/basiclayout/tests/__init__.py create mode 100644 docs/tutorials/wiki/src/basiclayout/tests/test_it.py delete mode 100644 docs/tutorials/wiki/src/basiclayout/tutorial/tests.py create mode 100644 docs/tutorials/wiki/src/installation/tests/__init__.py create mode 100644 docs/tutorials/wiki/src/installation/tests/test_it.py delete mode 100644 docs/tutorials/wiki/src/installation/tutorial/tests.py create mode 100644 docs/tutorials/wiki/src/models/tests/__init__.py create mode 100644 docs/tutorials/wiki/src/models/tests/test_it.py delete mode 100644 docs/tutorials/wiki/src/models/tutorial/tests.py create mode 100644 docs/tutorials/wiki/src/tests/tests/__init__.py create mode 100644 docs/tutorials/wiki/src/tests/tests/test_it.py delete mode 100644 docs/tutorials/wiki/src/tests/tutorial/tests.py create mode 100644 docs/tutorials/wiki/src/views/tests/__init__.py create mode 100644 docs/tutorials/wiki/src/views/tests/test_it.py delete mode 100644 docs/tutorials/wiki/src/views/tutorial/tests.py diff --git a/docs/tutorials/wiki/installation.rst b/docs/tutorials/wiki/installation.rst index 7c21ec02c..6088f577d 100644 --- a/docs/tutorials/wiki/installation.rst +++ b/docs/tutorials/wiki/installation.rst @@ -317,14 +317,14 @@ On Unix .. code-block:: bash - $VENV/bin/pytest --cov=tutorial tutorial/tests.py -q + $VENV/bin/pytest --cov=tutorial tests.py -q On Windows ^^^^^^^^^^ .. code-block:: doscon - %VENV%\Scripts\pytest --cov=tutorial tutorial\tests.py -q + %VENV%\Scripts\pytest --cov=tutorial tests -q ``pytest`` follows :ref:`conventions for Python test discovery `. diff --git a/docs/tutorials/wiki/src/authorization/MANIFEST.in b/docs/tutorials/wiki/src/authorization/MANIFEST.in index 05cc195d9..b4624fd1c 100644 --- a/docs/tutorials/wiki/src/authorization/MANIFEST.in +++ b/docs/tutorials/wiki/src/authorization/MANIFEST.in @@ -1,2 +1,5 @@ include *.txt *.ini *.cfg *.rst recursive-include tutorial *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] diff --git a/docs/tutorials/wiki/src/authorization/pytest.ini b/docs/tutorials/wiki/src/authorization/pytest.ini index a3489cdf8..42c3259f9 100644 --- a/docs/tutorials/wiki/src/authorization/pytest.ini +++ b/docs/tutorials/wiki/src/authorization/pytest.ini @@ -1,3 +1,6 @@ [pytest] -testpaths = tutorial -python_files = test*.py +addopts = --strict + +testpaths = + tutorial + tests diff --git a/docs/tutorials/wiki/src/authorization/tests/__init__.py b/docs/tutorials/wiki/src/authorization/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/docs/tutorials/wiki/src/authorization/tests/test_it.py b/docs/tutorials/wiki/src/authorization/tests/test_it.py new file mode 100644 index 000000000..6c72bcc62 --- /dev/null +++ b/docs/tutorials/wiki/src/authorization/tests/test_it.py @@ -0,0 +1,24 @@ +import unittest + +from pyramid import testing + + +class ViewTests(unittest.TestCase): + def setUp(self): + self.config = testing.setUp() + + def tearDown(self): + testing.tearDown() + + def test_my_view(self): + from tutorial.views.default import my_view + request = testing.DummyRequest() + info = my_view(request) + self.assertEqual(info['project'], 'myproj') + + def test_notfound_view(self): + from tutorial.views.notfound import notfound_view + request = testing.DummyRequest() + info = notfound_view(request) + self.assertEqual(info, {}) + diff --git a/docs/tutorials/wiki/src/authorization/tutorial/tests.py b/docs/tutorials/wiki/src/authorization/tutorial/tests.py deleted file mode 100644 index aa5641cdd..000000000 --- a/docs/tutorials/wiki/src/authorization/tutorial/tests.py +++ /dev/null @@ -1,24 +0,0 @@ -import unittest - -from pyramid import testing - - -class ViewTests(unittest.TestCase): - def setUp(self): - self.config = testing.setUp() - - def tearDown(self): - testing.tearDown() - - def test_my_view(self): - from .views.default import my_view - request = testing.DummyRequest() - info = my_view(request) - self.assertEqual(info['project'], 'myproj') - - def test_notfound_view(self): - from .views.notfound import notfound_view - request = testing.DummyRequest() - info = notfound_view(request) - self.assertEqual(info, {}) - diff --git a/docs/tutorials/wiki/src/basiclayout/MANIFEST.in b/docs/tutorials/wiki/src/basiclayout/MANIFEST.in index 05cc195d9..b4624fd1c 100644 --- a/docs/tutorials/wiki/src/basiclayout/MANIFEST.in +++ b/docs/tutorials/wiki/src/basiclayout/MANIFEST.in @@ -1,2 +1,5 @@ include *.txt *.ini *.cfg *.rst recursive-include tutorial *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] diff --git a/docs/tutorials/wiki/src/basiclayout/pytest.ini b/docs/tutorials/wiki/src/basiclayout/pytest.ini index a3489cdf8..42c3259f9 100644 --- a/docs/tutorials/wiki/src/basiclayout/pytest.ini +++ b/docs/tutorials/wiki/src/basiclayout/pytest.ini @@ -1,3 +1,6 @@ [pytest] -testpaths = tutorial -python_files = test*.py +addopts = --strict + +testpaths = + tutorial + tests diff --git a/docs/tutorials/wiki/src/basiclayout/tests/__init__.py b/docs/tutorials/wiki/src/basiclayout/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/docs/tutorials/wiki/src/basiclayout/tests/test_it.py b/docs/tutorials/wiki/src/basiclayout/tests/test_it.py new file mode 100644 index 000000000..6c72bcc62 --- /dev/null +++ b/docs/tutorials/wiki/src/basiclayout/tests/test_it.py @@ -0,0 +1,24 @@ +import unittest + +from pyramid import testing + + +class ViewTests(unittest.TestCase): + def setUp(self): + self.config = testing.setUp() + + def tearDown(self): + testing.tearDown() + + def test_my_view(self): + from tutorial.views.default import my_view + request = testing.DummyRequest() + info = my_view(request) + self.assertEqual(info['project'], 'myproj') + + def test_notfound_view(self): + from tutorial.views.notfound import notfound_view + request = testing.DummyRequest() + info = notfound_view(request) + self.assertEqual(info, {}) + diff --git a/docs/tutorials/wiki/src/basiclayout/tutorial/tests.py b/docs/tutorials/wiki/src/basiclayout/tutorial/tests.py deleted file mode 100644 index aa5641cdd..000000000 --- a/docs/tutorials/wiki/src/basiclayout/tutorial/tests.py +++ /dev/null @@ -1,24 +0,0 @@ -import unittest - -from pyramid import testing - - -class ViewTests(unittest.TestCase): - def setUp(self): - self.config = testing.setUp() - - def tearDown(self): - testing.tearDown() - - def test_my_view(self): - from .views.default import my_view - request = testing.DummyRequest() - info = my_view(request) - self.assertEqual(info['project'], 'myproj') - - def test_notfound_view(self): - from .views.notfound import notfound_view - request = testing.DummyRequest() - info = notfound_view(request) - self.assertEqual(info, {}) - diff --git a/docs/tutorials/wiki/src/installation/MANIFEST.in b/docs/tutorials/wiki/src/installation/MANIFEST.in index 05cc195d9..b4624fd1c 100644 --- a/docs/tutorials/wiki/src/installation/MANIFEST.in +++ b/docs/tutorials/wiki/src/installation/MANIFEST.in @@ -1,2 +1,5 @@ include *.txt *.ini *.cfg *.rst recursive-include tutorial *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] diff --git a/docs/tutorials/wiki/src/installation/pytest.ini b/docs/tutorials/wiki/src/installation/pytest.ini index a3489cdf8..42c3259f9 100644 --- a/docs/tutorials/wiki/src/installation/pytest.ini +++ b/docs/tutorials/wiki/src/installation/pytest.ini @@ -1,3 +1,6 @@ [pytest] -testpaths = tutorial -python_files = test*.py +addopts = --strict + +testpaths = + tutorial + tests diff --git a/docs/tutorials/wiki/src/installation/tests/__init__.py b/docs/tutorials/wiki/src/installation/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/docs/tutorials/wiki/src/installation/tests/test_it.py b/docs/tutorials/wiki/src/installation/tests/test_it.py new file mode 100644 index 000000000..6c72bcc62 --- /dev/null +++ b/docs/tutorials/wiki/src/installation/tests/test_it.py @@ -0,0 +1,24 @@ +import unittest + +from pyramid import testing + + +class ViewTests(unittest.TestCase): + def setUp(self): + self.config = testing.setUp() + + def tearDown(self): + testing.tearDown() + + def test_my_view(self): + from tutorial.views.default import my_view + request = testing.DummyRequest() + info = my_view(request) + self.assertEqual(info['project'], 'myproj') + + def test_notfound_view(self): + from tutorial.views.notfound import notfound_view + request = testing.DummyRequest() + info = notfound_view(request) + self.assertEqual(info, {}) + diff --git a/docs/tutorials/wiki/src/installation/tutorial/tests.py b/docs/tutorials/wiki/src/installation/tutorial/tests.py deleted file mode 100644 index aa5641cdd..000000000 --- a/docs/tutorials/wiki/src/installation/tutorial/tests.py +++ /dev/null @@ -1,24 +0,0 @@ -import unittest - -from pyramid import testing - - -class ViewTests(unittest.TestCase): - def setUp(self): - self.config = testing.setUp() - - def tearDown(self): - testing.tearDown() - - def test_my_view(self): - from .views.default import my_view - request = testing.DummyRequest() - info = my_view(request) - self.assertEqual(info['project'], 'myproj') - - def test_notfound_view(self): - from .views.notfound import notfound_view - request = testing.DummyRequest() - info = notfound_view(request) - self.assertEqual(info, {}) - diff --git a/docs/tutorials/wiki/src/models/MANIFEST.in b/docs/tutorials/wiki/src/models/MANIFEST.in index 05cc195d9..b4624fd1c 100644 --- a/docs/tutorials/wiki/src/models/MANIFEST.in +++ b/docs/tutorials/wiki/src/models/MANIFEST.in @@ -1,2 +1,5 @@ include *.txt *.ini *.cfg *.rst recursive-include tutorial *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] diff --git a/docs/tutorials/wiki/src/models/pytest.ini b/docs/tutorials/wiki/src/models/pytest.ini index a3489cdf8..42c3259f9 100644 --- a/docs/tutorials/wiki/src/models/pytest.ini +++ b/docs/tutorials/wiki/src/models/pytest.ini @@ -1,3 +1,6 @@ [pytest] -testpaths = tutorial -python_files = test*.py +addopts = --strict + +testpaths = + tutorial + tests diff --git a/docs/tutorials/wiki/src/models/tests/__init__.py b/docs/tutorials/wiki/src/models/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/docs/tutorials/wiki/src/models/tests/test_it.py b/docs/tutorials/wiki/src/models/tests/test_it.py new file mode 100644 index 000000000..6c72bcc62 --- /dev/null +++ b/docs/tutorials/wiki/src/models/tests/test_it.py @@ -0,0 +1,24 @@ +import unittest + +from pyramid import testing + + +class ViewTests(unittest.TestCase): + def setUp(self): + self.config = testing.setUp() + + def tearDown(self): + testing.tearDown() + + def test_my_view(self): + from tutorial.views.default import my_view + request = testing.DummyRequest() + info = my_view(request) + self.assertEqual(info['project'], 'myproj') + + def test_notfound_view(self): + from tutorial.views.notfound import notfound_view + request = testing.DummyRequest() + info = notfound_view(request) + self.assertEqual(info, {}) + diff --git a/docs/tutorials/wiki/src/models/tutorial/tests.py b/docs/tutorials/wiki/src/models/tutorial/tests.py deleted file mode 100644 index aa5641cdd..000000000 --- a/docs/tutorials/wiki/src/models/tutorial/tests.py +++ /dev/null @@ -1,24 +0,0 @@ -import unittest - -from pyramid import testing - - -class ViewTests(unittest.TestCase): - def setUp(self): - self.config = testing.setUp() - - def tearDown(self): - testing.tearDown() - - def test_my_view(self): - from .views.default import my_view - request = testing.DummyRequest() - info = my_view(request) - self.assertEqual(info['project'], 'myproj') - - def test_notfound_view(self): - from .views.notfound import notfound_view - request = testing.DummyRequest() - info = notfound_view(request) - self.assertEqual(info, {}) - diff --git a/docs/tutorials/wiki/src/tests/MANIFEST.in b/docs/tutorials/wiki/src/tests/MANIFEST.in index 05cc195d9..b4624fd1c 100644 --- a/docs/tutorials/wiki/src/tests/MANIFEST.in +++ b/docs/tutorials/wiki/src/tests/MANIFEST.in @@ -1,2 +1,5 @@ include *.txt *.ini *.cfg *.rst recursive-include tutorial *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] diff --git a/docs/tutorials/wiki/src/tests/pytest.ini b/docs/tutorials/wiki/src/tests/pytest.ini index a3489cdf8..42c3259f9 100644 --- a/docs/tutorials/wiki/src/tests/pytest.ini +++ b/docs/tutorials/wiki/src/tests/pytest.ini @@ -1,3 +1,6 @@ [pytest] -testpaths = tutorial -python_files = test*.py +addopts = --strict + +testpaths = + tutorial + tests diff --git a/docs/tutorials/wiki/src/tests/tests/__init__.py b/docs/tutorials/wiki/src/tests/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/docs/tutorials/wiki/src/tests/tests/test_it.py b/docs/tutorials/wiki/src/tests/tests/test_it.py new file mode 100644 index 000000000..e45380e6f --- /dev/null +++ b/docs/tutorials/wiki/src/tests/tests/test_it.py @@ -0,0 +1,232 @@ +import unittest + +from pyramid import testing + +class PageModelTests(unittest.TestCase): + + def _getTargetClass(self): + from tutorial.models import Page + return Page + + def _makeOne(self, data='some data'): + return self._getTargetClass()(data=data) + + def test_constructor(self): + instance = self._makeOne() + self.assertEqual(instance.data, 'some data') + +class WikiModelTests(unittest.TestCase): + + def _getTargetClass(self): + from tutorial.models import Wiki + return Wiki + + def _makeOne(self): + return self._getTargetClass()() + + def test_it(self): + wiki = self._makeOne() + self.assertEqual(wiki.__parent__, None) + self.assertEqual(wiki.__name__, None) + +class AppmakerTests(unittest.TestCase): + + def _callFUT(self, zodb_root): + from tutorial.models import appmaker + return appmaker(zodb_root) + + def test_it(self): + root = {} + self._callFUT(root) + self.assertEqual(root['app_root']['FrontPage'].data, + 'This is the front page') + +class ViewWikiTests(unittest.TestCase): + def test_it(self): + from tutorial.views.default import view_wiki + context = testing.DummyResource() + request = testing.DummyRequest() + response = view_wiki(context, request) + self.assertEqual(response.location, 'http://example.com/FrontPage') + +class ViewPageTests(unittest.TestCase): + def _callFUT(self, context, request): + from tutorial.views.default import view_page + return view_page(context, request) + + def test_it(self): + wiki = testing.DummyResource() + wiki['IDoExist'] = testing.DummyResource() + context = testing.DummyResource(data='Hello CruelWorld IDoExist') + context.__parent__ = wiki + context.__name__ = 'thepage' + request = testing.DummyRequest() + info = self._callFUT(context, request) + self.assertEqual(info['page'], context) + self.assertEqual( + info['page_text'], + '
\n' + '

Hello ' + 'CruelWorld ' + '' + 'IDoExist' + '

\n
\n') + self.assertEqual(info['edit_url'], + 'http://example.com/thepage/edit_page') + + +class AddPageTests(unittest.TestCase): + def _callFUT(self, context, request): + from tutorial.views.default import add_page + return add_page(context, request) + + def test_it_notsubmitted(self): + context = testing.DummyResource() + request = testing.DummyRequest() + request.subpath = ['AnotherPage'] + info = self._callFUT(context, request) + self.assertEqual(info['page'].data,'') + self.assertEqual( + info['save_url'], + request.resource_url(context, 'add_page', 'AnotherPage')) + + def test_it_submitted(self): + context = testing.DummyResource() + request = testing.DummyRequest({'form.submitted':True, + 'body':'Hello yo!'}) + request.subpath = ['AnotherPage'] + self._callFUT(context, request) + page = context['AnotherPage'] + self.assertEqual(page.data, 'Hello yo!') + self.assertEqual(page.__name__, 'AnotherPage') + self.assertEqual(page.__parent__, context) + +class EditPageTests(unittest.TestCase): + def _callFUT(self, context, request): + from tutorial.views.default import edit_page + return edit_page(context, request) + + def test_it_notsubmitted(self): + context = testing.DummyResource() + request = testing.DummyRequest() + info = self._callFUT(context, request) + self.assertEqual(info['page'], context) + self.assertEqual(info['save_url'], + request.resource_url(context, 'edit_page')) + + def test_it_submitted(self): + context = testing.DummyResource() + request = testing.DummyRequest({'form.submitted':True, + 'body':'Hello yo!'}) + response = self._callFUT(context, request) + self.assertEqual(response.location, 'http://example.com/') + self.assertEqual(context.data, 'Hello yo!') + +class SecurityTests(unittest.TestCase): + def test_hashing(self): + from tutorial.security import hash_password, check_password + password = 'secretpassword' + hashed_password = hash_password(password) + self.assertTrue(check_password(hashed_password, password)) + + self.assertFalse(check_password(hashed_password, 'attackerpassword')) + + self.assertFalse(check_password(None, password)) + +class FunctionalTests(unittest.TestCase): + + viewer_login = '/login?login=viewer&password=viewer' \ + '&came_from=FrontPage&form.submitted=Login' + viewer_wrong_login = '/login?login=viewer&password=incorrect' \ + '&came_from=FrontPage&form.submitted=Login' + editor_login = '/login?login=editor&password=editor' \ + '&came_from=FrontPage&form.submitted=Login' + + def setUp(self): + import tempfile + import os.path + from tutorial import main + self.tmpdir = tempfile.mkdtemp() + + dbpath = os.path.join( self.tmpdir, 'test.db') + uri = 'file://' + dbpath + settings = { 'zodbconn.uri' : uri , + 'pyramid.includes': ['pyramid_zodbconn', 'pyramid_tm'] } + + app = main({}, **settings) + self.db = app.registry._zodb_databases[''] + from webtest import TestApp + self.testapp = TestApp(app) + + def tearDown(self): + import shutil + self.db.close() + shutil.rmtree( self.tmpdir ) + + def test_root(self): + res = self.testapp.get('/', status=302) + self.assertEqual(res.location, 'http://localhost/FrontPage') + + def test_FrontPage(self): + res = self.testapp.get('/FrontPage', status=200) + self.assertTrue(b'FrontPage' in res.body) + + def test_unexisting_page(self): + res = self.testapp.get('/SomePage', status=404) + self.assertTrue(b'Not Found' in res.body) + + def test_referrer_is_login(self): + res = self.testapp.get('/login', status=200) + self.assertTrue(b'name="came_from" value="/"' in res.body) + + def test_successful_log_in(self): + res = self.testapp.get( self.viewer_login, status=302) + self.assertEqual(res.location, 'http://localhost/FrontPage') + + def test_failed_log_in(self): + res = self.testapp.get( self.viewer_wrong_login, status=200) + self.assertTrue(b'login' in res.body) + + def test_logout_link_present_when_logged_in(self): + res = self.testapp.get( self.viewer_login, status=302) + res = self.testapp.get('/FrontPage', status=200) + self.assertTrue(b'Logout' in res.body) + + def test_logout_link_not_present_after_logged_out(self): + res = self.testapp.get( self.viewer_login, status=302) + res = self.testapp.get('/FrontPage', status=200) + res = self.testapp.get('/logout', status=302) + self.assertTrue(b'Logout' not in res.body) + + def test_anonymous_user_cannot_edit(self): + res = self.testapp.get('/FrontPage/edit_page', status=200) + self.assertTrue(b'Login' in res.body) + + def test_anonymous_user_cannot_add(self): + res = self.testapp.get('/add_page/NewPage', status=200) + self.assertTrue(b'Login' in res.body) + + def test_viewer_user_cannot_edit(self): + res = self.testapp.get( self.viewer_login, status=302) + res = self.testapp.get('/FrontPage/edit_page', status=200) + self.assertTrue(b'Login' in res.body) + + def test_viewer_user_cannot_add(self): + res = self.testapp.get( self.viewer_login, status=302) + res = self.testapp.get('/add_page/NewPage', status=200) + self.assertTrue(b'Login' in res.body) + + def test_editors_member_user_can_edit(self): + res = self.testapp.get( self.editor_login, status=302) + res = self.testapp.get('/FrontPage/edit_page', status=200) + self.assertTrue(b'Editing' in res.body) + + def test_editors_member_user_can_add(self): + res = self.testapp.get( self.editor_login, status=302) + res = self.testapp.get('/add_page/NewPage', status=200) + self.assertTrue(b'Editing' in res.body) + + def test_editors_member_user_can_view(self): + res = self.testapp.get( self.editor_login, status=302) + res = self.testapp.get('/FrontPage', status=200) + self.assertTrue(b'FrontPage' in res.body) diff --git a/docs/tutorials/wiki/src/tests/tutorial/tests.py b/docs/tutorials/wiki/src/tests/tutorial/tests.py deleted file mode 100644 index ff1c07b7c..000000000 --- a/docs/tutorials/wiki/src/tests/tutorial/tests.py +++ /dev/null @@ -1,232 +0,0 @@ -import unittest - -from pyramid import testing - -class PageModelTests(unittest.TestCase): - - def _getTargetClass(self): - from .models import Page - return Page - - def _makeOne(self, data='some data'): - return self._getTargetClass()(data=data) - - def test_constructor(self): - instance = self._makeOne() - self.assertEqual(instance.data, 'some data') - -class WikiModelTests(unittest.TestCase): - - def _getTargetClass(self): - from .models import Wiki - return Wiki - - def _makeOne(self): - return self._getTargetClass()() - - def test_it(self): - wiki = self._makeOne() - self.assertEqual(wiki.__parent__, None) - self.assertEqual(wiki.__name__, None) - -class AppmakerTests(unittest.TestCase): - - def _callFUT(self, zodb_root): - from .models import appmaker - return appmaker(zodb_root) - - def test_it(self): - root = {} - self._callFUT(root) - self.assertEqual(root['app_root']['FrontPage'].data, - 'This is the front page') - -class ViewWikiTests(unittest.TestCase): - def test_it(self): - from .views.default import view_wiki - context = testing.DummyResource() - request = testing.DummyRequest() - response = view_wiki(context, request) - self.assertEqual(response.location, 'http://example.com/FrontPage') - -class ViewPageTests(unittest.TestCase): - def _callFUT(self, context, request): - from .views.default import view_page - return view_page(context, request) - - def test_it(self): - wiki = testing.DummyResource() - wiki['IDoExist'] = testing.DummyResource() - context = testing.DummyResource(data='Hello CruelWorld IDoExist') - context.__parent__ = wiki - context.__name__ = 'thepage' - request = testing.DummyRequest() - info = self._callFUT(context, request) - self.assertEqual(info['page'], context) - self.assertEqual( - info['page_text'], - '
\n' - '

Hello ' - 'CruelWorld ' - '' - 'IDoExist' - '

\n
\n') - self.assertEqual(info['edit_url'], - 'http://example.com/thepage/edit_page') - - -class AddPageTests(unittest.TestCase): - def _callFUT(self, context, request): - from .views.default import add_page - return add_page(context, request) - - def test_it_notsubmitted(self): - context = testing.DummyResource() - request = testing.DummyRequest() - request.subpath = ['AnotherPage'] - info = self._callFUT(context, request) - self.assertEqual(info['page'].data,'') - self.assertEqual( - info['save_url'], - request.resource_url(context, 'add_page', 'AnotherPage')) - - def test_it_submitted(self): - context = testing.DummyResource() - request = testing.DummyRequest({'form.submitted':True, - 'body':'Hello yo!'}) - request.subpath = ['AnotherPage'] - self._callFUT(context, request) - page = context['AnotherPage'] - self.assertEqual(page.data, 'Hello yo!') - self.assertEqual(page.__name__, 'AnotherPage') - self.assertEqual(page.__parent__, context) - -class EditPageTests(unittest.TestCase): - def _callFUT(self, context, request): - from .views.default import edit_page - return edit_page(context, request) - - def test_it_notsubmitted(self): - context = testing.DummyResource() - request = testing.DummyRequest() - info = self._callFUT(context, request) - self.assertEqual(info['page'], context) - self.assertEqual(info['save_url'], - request.resource_url(context, 'edit_page')) - - def test_it_submitted(self): - context = testing.DummyResource() - request = testing.DummyRequest({'form.submitted':True, - 'body':'Hello yo!'}) - response = self._callFUT(context, request) - self.assertEqual(response.location, 'http://example.com/') - self.assertEqual(context.data, 'Hello yo!') - -class SecurityTests(unittest.TestCase): - def test_hashing(self): - from .security import hash_password, check_password - password = 'secretpassword' - hashed_password = hash_password(password) - self.assertTrue(check_password(hashed_password, password)) - - self.assertFalse(check_password(hashed_password, 'attackerpassword')) - - self.assertFalse(check_password(None, password)) - -class FunctionalTests(unittest.TestCase): - - viewer_login = '/login?login=viewer&password=viewer' \ - '&came_from=FrontPage&form.submitted=Login' - viewer_wrong_login = '/login?login=viewer&password=incorrect' \ - '&came_from=FrontPage&form.submitted=Login' - editor_login = '/login?login=editor&password=editor' \ - '&came_from=FrontPage&form.submitted=Login' - - def setUp(self): - import tempfile - import os.path - from . import main - self.tmpdir = tempfile.mkdtemp() - - dbpath = os.path.join( self.tmpdir, 'test.db') - uri = 'file://' + dbpath - settings = { 'zodbconn.uri' : uri , - 'pyramid.includes': ['pyramid_zodbconn', 'pyramid_tm'] } - - app = main({}, **settings) - self.db = app.registry._zodb_databases[''] - from webtest import TestApp - self.testapp = TestApp(app) - - def tearDown(self): - import shutil - self.db.close() - shutil.rmtree( self.tmpdir ) - - def test_root(self): - res = self.testapp.get('/', status=302) - self.assertEqual(res.location, 'http://localhost/FrontPage') - - def test_FrontPage(self): - res = self.testapp.get('/FrontPage', status=200) - self.assertTrue(b'FrontPage' in res.body) - - def test_unexisting_page(self): - res = self.testapp.get('/SomePage', status=404) - self.assertTrue(b'Not Found' in res.body) - - def test_referrer_is_login(self): - res = self.testapp.get('/login', status=200) - self.assertTrue(b'name="came_from" value="/"' in res.body) - - def test_successful_log_in(self): - res = self.testapp.get( self.viewer_login, status=302) - self.assertEqual(res.location, 'http://localhost/FrontPage') - - def test_failed_log_in(self): - res = self.testapp.get( self.viewer_wrong_login, status=200) - self.assertTrue(b'login' in res.body) - - def test_logout_link_present_when_logged_in(self): - res = self.testapp.get( self.viewer_login, status=302) - res = self.testapp.get('/FrontPage', status=200) - self.assertTrue(b'Logout' in res.body) - - def test_logout_link_not_present_after_logged_out(self): - res = self.testapp.get( self.viewer_login, status=302) - res = self.testapp.get('/FrontPage', status=200) - res = self.testapp.get('/logout', status=302) - self.assertTrue(b'Logout' not in res.body) - - def test_anonymous_user_cannot_edit(self): - res = self.testapp.get('/FrontPage/edit_page', status=200) - self.assertTrue(b'Login' in res.body) - - def test_anonymous_user_cannot_add(self): - res = self.testapp.get('/add_page/NewPage', status=200) - self.assertTrue(b'Login' in res.body) - - def test_viewer_user_cannot_edit(self): - res = self.testapp.get( self.viewer_login, status=302) - res = self.testapp.get('/FrontPage/edit_page', status=200) - self.assertTrue(b'Login' in res.body) - - def test_viewer_user_cannot_add(self): - res = self.testapp.get( self.viewer_login, status=302) - res = self.testapp.get('/add_page/NewPage', status=200) - self.assertTrue(b'Login' in res.body) - - def test_editors_member_user_can_edit(self): - res = self.testapp.get( self.editor_login, status=302) - res = self.testapp.get('/FrontPage/edit_page', status=200) - self.assertTrue(b'Editing' in res.body) - - def test_editors_member_user_can_add(self): - res = self.testapp.get( self.editor_login, status=302) - res = self.testapp.get('/add_page/NewPage', status=200) - self.assertTrue(b'Editing' in res.body) - - def test_editors_member_user_can_view(self): - res = self.testapp.get( self.editor_login, status=302) - res = self.testapp.get('/FrontPage', status=200) - self.assertTrue(b'FrontPage' in res.body) diff --git a/docs/tutorials/wiki/src/views/MANIFEST.in b/docs/tutorials/wiki/src/views/MANIFEST.in index 05cc195d9..b4624fd1c 100644 --- a/docs/tutorials/wiki/src/views/MANIFEST.in +++ b/docs/tutorials/wiki/src/views/MANIFEST.in @@ -1,2 +1,5 @@ include *.txt *.ini *.cfg *.rst recursive-include tutorial *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] diff --git a/docs/tutorials/wiki/src/views/pytest.ini b/docs/tutorials/wiki/src/views/pytest.ini index a3489cdf8..42c3259f9 100644 --- a/docs/tutorials/wiki/src/views/pytest.ini +++ b/docs/tutorials/wiki/src/views/pytest.ini @@ -1,3 +1,6 @@ [pytest] -testpaths = tutorial -python_files = test*.py +addopts = --strict + +testpaths = + tutorial + tests diff --git a/docs/tutorials/wiki/src/views/tests/__init__.py b/docs/tutorials/wiki/src/views/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/docs/tutorials/wiki/src/views/tests/test_it.py b/docs/tutorials/wiki/src/views/tests/test_it.py new file mode 100644 index 000000000..6c72bcc62 --- /dev/null +++ b/docs/tutorials/wiki/src/views/tests/test_it.py @@ -0,0 +1,24 @@ +import unittest + +from pyramid import testing + + +class ViewTests(unittest.TestCase): + def setUp(self): + self.config = testing.setUp() + + def tearDown(self): + testing.tearDown() + + def test_my_view(self): + from tutorial.views.default import my_view + request = testing.DummyRequest() + info = my_view(request) + self.assertEqual(info['project'], 'myproj') + + def test_notfound_view(self): + from tutorial.views.notfound import notfound_view + request = testing.DummyRequest() + info = notfound_view(request) + self.assertEqual(info, {}) + diff --git a/docs/tutorials/wiki/src/views/tutorial/tests.py b/docs/tutorials/wiki/src/views/tutorial/tests.py deleted file mode 100644 index aa5641cdd..000000000 --- a/docs/tutorials/wiki/src/views/tutorial/tests.py +++ /dev/null @@ -1,24 +0,0 @@ -import unittest - -from pyramid import testing - - -class ViewTests(unittest.TestCase): - def setUp(self): - self.config = testing.setUp() - - def tearDown(self): - testing.tearDown() - - def test_my_view(self): - from .views.default import my_view - request = testing.DummyRequest() - info = my_view(request) - self.assertEqual(info['project'], 'myproj') - - def test_notfound_view(self): - from .views.notfound import notfound_view - request = testing.DummyRequest() - info = notfound_view(request) - self.assertEqual(info, {}) - diff --git a/docs/tutorials/wiki/tests.rst b/docs/tutorials/wiki/tests.rst index 9dacc5f96..f710b3b10 100644 --- a/docs/tutorials/wiki/tests.rst +++ b/docs/tutorials/wiki/tests.rst @@ -4,7 +4,7 @@ Adding Tests ============ -We will now add tests for the models and the views and a few functional tests in ``tests.py``. +We will now add tests for the models and the views and a few functional tests in ``tests/test_it.py``. Tests ensure that an application works, and that it continues to work when changes are made in the future. @@ -12,10 +12,9 @@ Test the models =============== We write tests for the ``model`` classes and the ``appmaker``. -Changing ``tests.py``, we will write a separate test class for each ``model`` class +We will modify our ``test_it.py`` file, writing a separate test class for each ``model`` class. We will also write a test class for the ``appmaker``. -To do so, we will retain the ``tutorial.tests.ViewTests`` class that was generated from choosing the ``zodb`` backend option. We will add three test classes, one for each of the following: - the ``Page`` model named ``PageModelTests`` @@ -26,7 +25,7 @@ We will add three test classes, one for each of the following: Test the views ============== -We will modify our ``tests.py`` file, adding tests for each view function that we added previously. +We will modify our ``test_it.py`` file, adding tests for each view function that we added previously. As a result, we will delete the ``ViewTests`` class that the ``zodb`` backend option provided, and add four other test classes: ``ViewWikiTests``, ``ViewPageTests``, ``AddPageTests``, and ``EditPageTests``. These test the ``view_wiki``, ``view_page``, ``add_page``, and ``edit_page`` views. @@ -35,14 +34,15 @@ Functional tests ================ We will test the whole application, covering security aspects that are not tested in the unit tests, such as logging in, logging out, checking that the ``viewer`` user cannot add or edit pages, but the ``editor`` user can, and so on. +As a result we will add two test classes, ``SecurityTests`` and ``FunctionalTests``. -View the results of all our edits to ``tests.py`` -================================================= +View the results of all our edits to ``tests/test_it.py`` +========================================================= -Open the ``tutorial/tests.py`` module, and edit it such that it appears as follows: +Open the ``tests/test_it.py`` module, and edit it such that it appears as follows: -.. literalinclude:: src/tests/tutorial/tests.py +.. literalinclude:: src/tests/tests/test_it.py :linenos: :language: python -- cgit v1.2.3