source: OpenWorkouts-current/ow/tests/views/test_error.py @ 5ec3a0b

currentfeature/docs
Last change on this file since 5ec3a0b was 5ec3a0b, checked in by borja <borja@…>, 5 years ago

Imported sources from the old python2-only repository:

  • Modified the code so it is python 3.6 compatible
  • Fixed deprecation warnings, pyramid 1.10.x supported now
  • Fixed deprecation warnings about some libraries, like pyramid-simpleform
  • Added pytest-pycodestyle and pytest-flakes for automatic checks on the source code files when running tests.
  • Added default pytest.ini setup to enforce some default parameters when running tests.
  • Cleaned up the code a bit, catched up with tests coverage.
  • Property mode set to 100644
File size: 1.9 KB
Line 
1from unittest.mock import patch, Mock
2
3import pytest
4from pyramid.testing import DummyRequest
5from pyramid.httpexceptions import HTTPFound
6
7from ow.models.root import OpenWorkouts
8from ow.views.error import not_found, forbidden, exceptions
9
10
11class TestErrorViews(object):
12
13    @pytest.fixture
14    def root(self):
15        root = OpenWorkouts()
16        return root
17
18    @pytest.fixture
19    def req(self, root):
20        request = DummyRequest()
21        request.root = root
22        # Our error views will override the status_int for request.response,
23        # but pyramid DummyRequest objects do not have that attribute
24        request.response = Mock()
25        return request
26
27    def test_not_found(self, req):
28        request = req
29        response = not_found(request)
30        assert response['url'] == request.resource_url(request.root)
31        assert request.response.status_int == 404
32
33    def test_forbidden_not_logged_in(self):
34        # use a mocked request, as we cannot override authenticated_userid
35        # easily on a dummyrequest
36        request = Mock()
37        request.authenticated_userid = None
38        request.resource_url.return_value = '/login'
39        response = forbidden(request)
40        assert isinstance(response, HTTPFound)
41        assert 'login' in response.location
42
43    def test_forbidden_logged_in(self):
44        request = Mock()
45        request.authenticated_userid = 'john'
46        request.resource_url.return_value = '/'
47        response = forbidden(request)
48        assert response['url'] == request.resource_url(request.root)
49        assert request.response.status_int == 403
50
51    @patch('ow.views.error.log')
52    def test_exceptions(self, log, req):
53        request = req
54        response = exceptions(request.root, request)
55        assert log.error.called
56        assert response['url'] == request.resource_url(request.root)
57        assert request.response.status_int == 500
Note: See TracBrowser for help on using the repository browser.