Ejemplo n.º 1
0
 def test_get_member_specific_fields(self):
     request = DummyRequest(path='/thing/1.json', params={'$fields': '["id"]'})
     request.matchdict = {'id': 1, 'renderer': 'json'}
     view = RESTfulView(_dummy_context_factory(), request)
     response = view.get_member()
     member = json.loads(response.body)['results'][0]
     self.assert_(member.keys() == ['id'])
Ejemplo n.º 2
0
    def test_decimal_json(self):
        from decimal import Decimal
        from pyramid.testing import DummyRequest
        from c2cgeoportal import DecimalJSON
        renderer = DecimalJSON()(None)
        request = DummyRequest()
        request.user = None
        system = {'request': request}

        self.assertEquals(
            renderer({'a': Decimal('3.3')}, system),
            '{"a": 3.3}'
        )
        self.assertEquals(request.response.content_type, 'application/json')

        request.params = {'callback': 'abc'}
        self.assertEquals(
            renderer({'a': Decimal('3.3')}, system),
            'abc({"a": 3.3});'
        )
        self.assertEquals(request.response.content_type, 'text/javascript')

        renderer = DecimalJSON(jsonp_param_name='cb')(None)
        request.params = {'cb': 'def'}
        self.assertEquals(
            renderer({'a': Decimal('3.3')}, system),
            'def({"a": 3.3});'
        )
        self.assertEquals(request.response.content_type, 'text/javascript')
Ejemplo n.º 3
0
    def test_login(self):
        from pyramid.testing import DummyRequest
        from c2cgeoportal.views.entry import Entry

        request = DummyRequest()
        request.user = None
        entry = Entry(request)

        request.path = '/for_test'
        expected = {
            'lang': 'fr',
            'came_from': '/for_test',
        }
        self.assertEqual(entry.loginform403(), expected)

        request.params = {
            'came_from': '/for_a_second_test',
        }
        entry = Entry(request)
        expected = {
            'lang': 'fr',
            'came_from': '/for_a_second_test',
        }
        self.assertEqual(entry.loginform(), expected)

        entry = Entry(request)
        request.params = {}
        expected = {
            'lang': 'fr',
            'came_from': '/',
        }
        self.assertEqual(entry.loginform(), expected)
Ejemplo n.º 4
0
 def test_PUT_using_header(self):
     app = self._make_app()
     request = DummyRequest(method='POST')
     request.headers['X-HTTP-Method-Override'] = 'PUT'
     self._assert_before(request)
     app.registry.notify(NewRequest(request))
     self._assert_after(request, 'PUT')
Ejemplo n.º 5
0
    def test__get_child_layers_info_without_scalehint(self):
        from pyramid.testing import DummyRequest
        from c2cgeoportal.views.entry import Entry

        request = DummyRequest()
        request.user = None
        entry = Entry(request)

        class Layer(object):
            pass

        child_layer_1 = Layer()
        child_layer_1.name = 'layer_1'
        child_layer_1.scaleHint = None
        child_layer_1.layers = []

        child_layer_2 = Layer()
        child_layer_2.name = 'layer_2'
        child_layer_2.scaleHint = None
        child_layer_2.layers = []

        layer = Layer()
        layer.layers = [child_layer_1, child_layer_2]

        child_layers_info = entry._get_child_layers_info(layer)

        expected = [{
            'name': 'layer_1'
        }, {
            'name': 'layer_2',
        }]
        self.assertEqual(child_layers_info, expected)
Ejemplo n.º 6
0
def req(db_session):
    """
    (Integration Testing) Creates a dummy request

    The request is setup with configuration CSRF values and the expected
    ``db_session`` property, the goal being to be be as close to a real
    database session as possible.

    Note that we must called it "req" as "request" is reserved by pytest.

    :param db_session: The testing database session

    :returns: a configured request object
    """
    import uuid
    import mock
    from pyramid.testing import DummyRequest

    dummy_request = DummyRequest()

    # Configurable csrf token
    csrf_token = str(uuid.uuid4())
    get_csrf_token = mock.Mock(return_value=csrf_token)
    dummy_request.session.get_csrf_token = get_csrf_token
    dummy_request.headers['X-CSRF-Token'] = csrf_token

    # Attach database session for expected behavior
    dummy_request.db_session = db_session
    db_session.info['request'] = dummy_request

    return dummy_request
Ejemplo n.º 7
0
    def test_forbidden(self):
        from ptahcrowd.forbidden import Forbidden

        class Context(object):
            """ """

        request = DummyRequest()
        request.url = 'http://example.com'
        request.application_url = 'http://example.com'
        request.root = Context()

        excview = Forbidden(HTTPForbidden(), request)
        excview.update()

        res = request.response

        self.assertIs(excview.__parent__, request.root)
        self.assertEqual(res.status, '302 Found')
        self.assertEqual(
            text_(res.headers['location']),
            'http://example.com/login.html?came_from=http%3A%2F%2Fexample.com')

        excview = Forbidden(HTTPForbidden(), request)
        res = excview()
        self.assertEqual(res.status, '302 Found')
Ejemplo n.º 8
0
    def test_shortener(self):
        from pyramid.testing import DummyRequest
        from pyramid.httpexceptions import HTTPNotFound, HTTPBadRequest
        from c2cgeoportal.views.shortener import Shortener

        def route_url(name, *elements, **kw):
            return "https://example.com/short/" + kw["ref"]

        request = DummyRequest()
        request.user = None
        request.host = "example.com:443"
        request.server_name = "example.com"
        request.route_url = route_url
        request.registry.settings["shortener"] = {
            "base_url": "https://example.com/s/"
        }
        shortener = Shortener(request)

        request.params = {}
        request.matchdict = {
            "ref": "AAAAAA"
        }
        self.assertRaises(HTTPNotFound, shortener.get)

        request.params = {}
        request.matchdict = {}
        self.assertRaises(HTTPBadRequest, shortener.create)

        request.params = {
            "url": "https://other-site.com/hi"
        }
        self.assertRaises(HTTPBadRequest, shortener.create)
Ejemplo n.º 9
0
 def dummy_request(self):
     request = DummyRequest()
     request.context = DummyResource()
     request.userdb = self.userdb
     request.db = self.db
     request.registry.settings = self.settings
     return request
Ejemplo n.º 10
0
def test_websocket_same_origin(config):
    config.include('h.streamer')
    # example.com is the dummy request default host URL
    req = DummyRequest(headers={'Origin': 'http://example.com'})
    req.get_response = MagicMock()
    res = websocket(req)
    assert res.code != 403
Ejemplo n.º 11
0
    def test_profile_csv(self):
        from pyramid.testing import DummyRequest
        from c2cgeoportal.views.profile import Profile

        request = DummyRequest()
        request.registry.settings = {
            "raster": {
                "dem": {"file": "c2cgeoportal/tests/data/dem.shp", "round": 1},
                "dem2": {"file": "c2cgeoportal/tests/data/dem.shp", "round": 1}
            }
        }
        profile = Profile(request)

        request.params["nbPoints"] = "3"
        request.params["geom"] = '{"type":"LineString",' \
            '"coordinates":[[548009.5,215990],[547990,216009.5]]}'
        response = profile.csv()
        self.assertEqual(response.body, """distance,dem2,dem,x,y
0.0,1166,1166,548009,215990
9.2,1181,1181,548003,215996
18.4,1181,1181,547996,216003""")

        request.params["layers"] = "dem"
        response = profile.csv()
        self.assertEqual(response.body, """distance,dem,x,y
0.0,1166,548009,215990
9.2,1181,548003,215996
18.4,1181,547996,216003""")
Ejemplo n.º 12
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(root_factory=getAppRoot, settings=settings)
    config.add_static_view('static', 'static', cache_max_age=3600)

#    authnPolicy = BasicAuthAuthenticationPolicy(check=checkAuthentication,
#                                                realm='Djosr Login') #, debug=True)
    authnPolicy = AuthTktAuthenticationPolicy(secret='not telling',
                                              callback=getGroups)#, debug=True)
    authzPolicy = ACLAuthorizationPolicy()
    config.set_authentication_policy(authnPolicy)
    config.set_authorization_policy(authzPolicy)

    sessFactory = UnencryptedCookieSessionFactoryConfig('not telling')
    config.set_session_factory(sessFactory)
    config.add_request_method(getUser, 'user', reify=True)
    config.scan()

    # this saves having to do a GET to catch simple bugs
    # TODO remove later when code is bug-free ;-)
    from pyramid.testing import DummyRequest
    dummy = DummyRequest()
    dummy.registry = config.registry
    getAppRoot(dummy)

    return config.make_wsgi_app()
Ejemplo n.º 13
0
def test_websocket_good_origin(config):
    config.registry.settings.update({'origins': 'http://good'})
    config.include('h.streamer')
    req = DummyRequest(headers={'Origin': 'http://good'})
    req.get_response = MagicMock()
    res = websocket(req)
    assert res.code != 403
Ejemplo n.º 14
0
def test_subscriber_predicate(settings):
    """Test that the ``asset_request`` subscriber predicate.

    It should correctly match asset requests when its value is ``True``,
    and other requests when ``False``.
    """
    mock1 = Mock()
    mock2 = Mock()

    with testConfig(settings=settings) as config:
        config.include(assets)
        config.add_subscriber(mock1, DummyEvent, asset_request=False)
        config.add_subscriber(mock2, DummyEvent, asset_request=True)

        request1 = DummyRequest('/')
        request1.matched_route = None

        pattern = config.get_webassets_env().url + '*subpath'
        request2 = DummyRequest(config.get_webassets_env().url + '/t.png')
        request2.matched_route = Route('__' + pattern, pattern)

        event1 = DummyEvent(request1)
        event2 = DummyEvent(request2)

        config.registry.notify(event1)
        config.registry.notify(event2)

        mock1.assert_called_onceventwith(event1)
        mock2.assert_called_onceventwith(event2)
Ejemplo n.º 15
0
    def _makeRequest(self, db, fs):
        from pyramid.testing import DummyRequest

        request = DummyRequest()
        request.db = db
        request.fs = fs
        return request
Ejemplo n.º 16
0
 def test_leaders(self):
     session = self.db_master_session
     today = datetime.utcnow().date()
     yesterday = today - timedelta(days=1)
     for i in range(3):
         user = User(nickname=unicode(i))
         session.add(user)
         session.flush()
         score1 = Score(userid=user.id, time=today, value=i)
         score1.name = 'location'
         session.add(score1)
         score2 = Score(userid=user.id, time=yesterday, value=i + 1)
         score2.name = 'location'
         session.add(score2)
     session.commit()
     request = DummyRequest()
     request.db_slave_session = self.db_master_session
     inst = self._make_view(request)
     result = inst.leaders_view()
     self.assertEqual(
         result['leaders1'],
         [{'anchor': u'2', 'nickname': u'2', 'num': 5, 'pos': 1},
          {'anchor': u'1', 'nickname': u'1', 'num': 3, 'pos': 2}])
     self.assertEqual(
         result['leaders2'],
         [{'anchor': u'0', 'nickname': u'0', 'num': 1, 'pos': 3}])
Ejemplo n.º 17
0
 def test_leaders_weekly(self):
     session = self.db_master_session
     for i in range(3):
         user = User(nickname=unicode(i))
         session.add(user)
         session.flush()
         score1 = Score(userid=user.id, value=i)
         score1.name = 'new_cell'
         session.add(score1)
         score2 = Score(userid=user.id, value=i)
         score2.name = 'new_wifi'
         session.add(score2)
     session.commit()
     request = DummyRequest()
     request.db_slave_session = self.db_master_session
     inst = self._make_view(request)
     result = inst.leaders_weekly_view()
     for score_name in ('new_cell', 'new_wifi'):
         self.assertEqual(
             result['scores'][score_name]['leaders1'],
             [{'nickname': u'2', 'num': 2, 'pos': 1},
              {'nickname': u'1', 'num': 1, 'pos': 2}])
         self.assertEqual(
             result['scores'][score_name]['leaders2'],
             [{'nickname': u'0', 'num': 0, 'pos': 3}])
Ejemplo n.º 18
0
    def test_login(self):
        from pyramid.testing import DummyRequest
        from c2cgeoportal.views.entry import Entry

        request = DummyRequest()
        request.user = None
        entry = Entry(request)

        request.path = "/for_test"
        expected = {
            "lang": "fr",
            "came_from": "/for_test",
        }
        self.assertEqual(entry.loginform403(), expected)

        request.params = {
            "came_from": "/for_a_second_test",
        }
        entry = Entry(request)
        expected = {
            "lang": "fr",
            "came_from": "/for_a_second_test",
        }
        self.assertEqual(entry.loginform(), expected)

        entry = Entry(request)
        request.params = {}
        expected = {
            "lang": "fr",
            "came_from": "/",
        }
        self.assertEqual(entry.loginform(), expected)
Ejemplo n.º 19
0
    def test_decimal_json(self):
        from decimal import Decimal
        from pyramid.testing import DummyRequest
        from c2cgeoportal import DecimalJSON
        renderer = DecimalJSON()(None)
        request = DummyRequest()
        request.user = None
        system = {"request": request}

        self.assertEquals(
            renderer({"a": Decimal("3.3")}, system),
            '{"a": 3.3}'
        )
        self.assertEquals(request.response.content_type, "application/json")

        request.params = {"callback": "abc"}
        self.assertEquals(
            renderer({"a": Decimal("3.3")}, system),
            'abc({"a": 3.3});'
        )
        self.assertEquals(request.response.content_type, "text/javascript")

        renderer = DecimalJSON(jsonp_param_name="cb")(None)
        request.params = {"cb": "def"}
        self.assertEquals(
            renderer({"a": Decimal("3.3")}, system),
            'def({"a": 3.3});'
        )
        self.assertEquals(request.response.content_type, "text/javascript")
Ejemplo n.º 20
0
    def test_stats(self):
        day = util.utcnow().date() - timedelta(1)
        session = self.db_master_session
        stats = [
            Stat(key=STAT_TYPE['cell'], time=day, value=2000000),
            Stat(key=STAT_TYPE['wifi'], time=day, value=2000000),
            Stat(key=STAT_TYPE['unique_cell'], time=day, value=1000000),
            Stat(key=STAT_TYPE['unique_wifi'], time=day, value=2000000),
        ]
        session.add_all(stats)
        session.commit()
        request = DummyRequest()
        request.db_slave_session = self.db_master_session
        request.registry.redis_client = self.redis_client
        inst = self._make_view(request)
        result = inst.stats_view()
        self.assertEqual(result['page_title'], 'Statistics')
        self.assertEqual(
            result['metrics1'], [
                {'name': 'Unique Cells', 'value': '1.00'},
                {'name': 'Cell Observations', 'value': '2.00'},
            ])
        self.assertEqual(
            result['metrics2'], [
                {'name': 'Unique Wifi Networks', 'value': '2.00'},
                {'name': 'Wifi Observations', 'value': '2.00'},
            ])

        # call the view again, without a working db session, so
        # we can be sure to use the cached result
        inst = self._make_view(request)
        request.db_slave_session = None
        second_result = inst.stats_view()
        self.assertEqual(second_result, result)
    def test_oauth2_step1(self):
        with patch('uuid.uuid4') as fake:
            fake.return_value = 'random-string'

            request = DummyRequest()
            request.params = {'next_url': 'http://localhost/'}
            request.session = {}
            response = oauth2_step1(
                request=request,
                auth_uri='http://example.com/oauth2/auth',
                client_id='1234',
                redirect_url='http://localhost/oauth2/callback',
                scope='scope1 scope2'
                )
            self.assertEqual(response.status, '302 Found')
            url = urlparse.urlparse(response.location)
            self.assertEqual(url.netloc, 'example.com')
            self.assertEqual(url.path, '/oauth2/auth')
            query = urlparse.parse_qs(url.query)
            self.assertEqual(query, {
                    'scope': ['scope1 scope2'],
                    'state': ['random-string'],
                    'redirect_uri': ['http://localhost/oauth2/callback'],
                    'response_type': ['code'],
                    'client_id': ['1234'],
                    })
            self.assertEqual(request.session['next_url'], 'http://localhost/')
Ejemplo n.º 22
0
    def test_leaders(self):
        session = self.db_master_session
        today = util.utcnow().date()
        yesterday = today - timedelta(days=1)
        for i in range(3):
            user = User(nickname=unicode(i))
            session.add(user)
            session.flush()
            score1 = Score(userid=user.id, time=today, value=i)
            score1.name = 'location'
            session.add(score1)
            score2 = Score(userid=user.id, time=yesterday, value=i + 1)
            score2.name = 'location'
            session.add(score2)
        session.commit()
        request = DummyRequest()
        request.db_slave_session = self.db_master_session
        request.registry.redis_client = self.redis_client
        inst = self._make_view(request)
        result = inst.leaders_view()
        self.assertEqual(
            result['leaders1'],
            [{'anchor': u'2', 'nickname': u'2', 'num': 5, 'pos': 1},
             {'anchor': u'1', 'nickname': u'1', 'num': 3, 'pos': 2}])
        self.assertEqual(
            result['leaders2'],
            [{'anchor': u'0', 'nickname': u'0', 'num': 1, 'pos': 3}])

        # call the view again, without a working db session, so
        # we can be sure to use the cached result
        inst = self._make_view(request)
        request.db_slave_session = None
        second_result = inst.leaders_view()
        self.assertEqual(second_result, result)
Ejemplo n.º 23
0
    def test_remember_me_with_bad_endpoint(self):
        """Test the post-login hook with a bad openid endpoint"""
        req = DummyRequest(params={
            'openid.op_endpoint': 'bad_endpoint',
        })
        req.db = self.db
        def flash(msg):
            pass
        req.session.flash = flash
        info = {
            'identity_url': 'http://lmacken.id.fedoraproject.org',
            'groups': [u'releng'],
        }
        req.registry.settings = self.app_settings

        try:
            resp = remember_me(None, req, info)
            assert False, 'remember_me should have thrown an exception'
        except Exception:
            # A ComponentLookupError is thrown because we're doing this outside
            # of the webapp
            pass

        # The user should not exist
        self.assertIsNone(User.get(u'lmacken', self.db))
Ejemplo n.º 24
0
    def test_call(self):
        config = testing.setUp()
        config.add_route('some_route', '/')

        pagination_request_writer = PaginationRequestWriterMock()
        param_property_naming = PropertyNamingMock(
            'page-number',
            'page-size',
            'sort-property',
            'sort-direction')
        url_creator_factory = UrlCreatorFactoryMock('http://example.com')
        request = DummyRequest()
        request.registry.pagination = {
            'some_route': {
                'pagination_request_writer': pagination_request_writer,
                'param_property_naming': param_property_naming,
                'url_creator_factory': url_creator_factory,
            }
        }
        request.pagination = Pagination(
            Paging(
                100,
                PagingRequest(3, 12)),
            Sorting('some_property', 'desc'))
        request.matched_route = RouteMock('some_route')
        event = BeforeRenderEventMock()
        event['request'] = request
        subscriber = PaginationBeforeRenderSubscriber()
        subscriber(event)
        self.assertEqual(pagination_request_writer.call_count, 1)

        pagination = event.rendering_val['pagination']
        self.assertEqual('http://example.com', str(pagination.url))
Ejemplo n.º 25
0
 def test_build_url_forward_headers(self):
     request = DummyRequest()
     request.environ = {
         "SERVER_NAME": "example.com"
     }
     request.registry.settings = {
         "checker": {
             "forward_headers": ["Cookie"]
         }
     }
     request.headers["Cookie"] = "test"
     self.assertEqual(
         build_url(
             "Test",
             "https://camptocamp.com/toto?titi#tutu",
             request
         ),
         (
             "https://camptocamp.com/toto?titi#tutu",
             {
                 "Cache-Control": "no-cache",
                 "Cookie": "test",
             }
         )
     )
Ejemplo n.º 26
0
    def _undo_delete_column(self, base, old_name, path, results):
        for doc in results:
            if doc.get(old_name, None):
                id_doc=doc['_metadata']['id_doc']

                url='/%s/doc/%d/%s' % (
                    base.metadata.name, 
                    id_doc, "/".join(path)
                )
                params={
                    'value': doc[old_name], 
                    'alter_files': False
                }
                request=DummyRequest(path=url, params=params)
                request.method='PUT'
                request.matchdict={
                    'base': base.metadata.name,
                    'id': str(id_doc),
                    'path': "/".join(path)
                }
                doc_view=DocumentCustomView(
                    DocumentContextFactory(request), 
                    request
                )
                response=doc_view.put_path()
Ejemplo n.º 27
0
    def test_call_exception(self):

        class ExceptionPaginationReader(object):
            # pylint: disable=too-few-public-methods

            def __init__(self, exception):
                self._exception = exception

            def __call__(self, request, content_size_provider):
                raise self._exception


        content_size_provider_mock = ContentSizeProviderMock(100)

        request = DummyRequest()
        request.registry.pagination = {
            'some_route': {
                'content_size_provider': content_size_provider_mock,
                'pagination_reader': ExceptionPaginationReader(
                    PageNotFoundException()),
            }
        }
        request.matched_route = RouteMock('some_route')

        event = ContextFoundEventMock(request)
        subscriber = PaginationContextFoundSubscriber()
        with self.assertRaises(ParameterValidationException):
            subscriber(event)
Ejemplo n.º 28
0
 def test_get_current_page(self):
     request = DummyRequest()
     page = get_current_page(request)
     self.assertEqual(page, 1)
     request.GET['page'] = 5
     page = get_current_page(request)
     self.assertEqual(page, 5)
Ejemplo n.º 29
0
def test_websocket_view_same_origin(config):
    # example.com is the dummy request default host URL
    config.registry.settings.update({'origins': []})
    req = DummyRequest(headers={'Origin': 'http://example.com'})
    req.get_response = lambda _: mock.sentinel.good_response
    res = views.websocket_view(req)
    assert res == mock.sentinel.good_response
Ejemplo n.º 30
0
    def test_leaders_weekly(self):
        session = self.db_master_session
        for i in range(3):
            user = User(nickname=unicode(i))
            session.add(user)
            session.flush()
            score1 = Score(userid=user.id, value=i)
            score1.name = 'new_cell'
            session.add(score1)
            score2 = Score(userid=user.id, value=i)
            score2.name = 'new_wifi'
            session.add(score2)
        session.commit()
        request = DummyRequest()
        request.db_slave_session = self.db_master_session
        request.registry.redis_client = self.redis_client
        inst = self._make_view(request)
        result = inst.leaders_weekly_view()
        for score_name in ('new_cell', 'new_wifi'):
            self.assertEqual(
                result['scores'][score_name]['leaders1'],
                [{'nickname': u'2', 'num': 2, 'pos': 1},
                 {'nickname': u'1', 'num': 1, 'pos': 2}])
            self.assertEqual(
                result['scores'][score_name]['leaders2'],
                [{'nickname': u'0', 'num': 0, 'pos': 3}])

        # call the view again, without a working db session, so
        # we can be sure to use the cached result
        inst = self._make_view(request)
        request.db_slave_session = None
        second_result = inst.leaders_weekly_view()
        self.assertEqual(second_result, result)
Ejemplo n.º 31
0
def test_response_content_type():
    renderer = renderers.CSV({})
    req = DummyRequest()
    renderer({}, {'request': req})
    assert req.response.content_type == 'text/csv'
Ejemplo n.º 32
0
 def setUp(self):
     super(TestFunctionalContentViews, self).setUp()
     request = DummyRequest()
     self.config = testing.setUp(request=request)
Ejemplo n.º 33
0
def test_list_view_unit(loaded_db_item):
    """Test if the list_view returns the expected dict."""
    from learning_journal.views import list_view
    response = list_view(DummyRequest())
    assert response['articles'][0].title == "jill"
Ejemplo n.º 34
0
 def setUp(self):
     self.__request = DummyRequest()
     # Must set hook_zca to false to work with uniittest_with_sqlite
     self.__config = testing.setUp(request=self.__request, hook_zca=False)
Ejemplo n.º 35
0
    def test_shortener(self):
        from pyramid.testing import DummyRequest
        from pyramid.httpexceptions import HTTPNotFound, HTTPBadRequest
        from c2cgeoportal_geoportal.views.shortener import Shortener

        def route_url(name, *elements, **kw):
            return "https://example.com/short/" + kw["ref"]

        request = DummyRequest()
        request.user = None
        request.host = "example.com:443"
        request.server_name = "example.com"
        request.route_url = route_url
        request.registry.settings["shortener"] = {
            "base_url": "https://example.com/s/"
        }
        shortener = Shortener(request)

        request.params = {}
        request.matchdict = {"ref": "AAAAAA"}
        self.assertRaises(HTTPNotFound, shortener.get)

        request.params = {}
        request.matchdict = {}
        self.assertRaises(HTTPBadRequest, shortener.create)

        request.params = {"url": "https://other-site.com/hi"}
        self.assertRaises(HTTPBadRequest, shortener.create)
Ejemplo n.º 36
0
def csrf_request(config):
    request = DummyRequest(registry=config.registry)
    request.headers['X-CSRF-Token'] = request.session.get_csrf_token()
    return request
Ejemplo n.º 37
0
def dummy_openapi_request():
    from pyramid_openapi3.wrappers import PyramidOpenAPIRequest
    dummy_request = DummyRequest()
    return PyramidOpenAPIRequest(dummy_request)
Ejemplo n.º 38
0
 def client(self):
     return features.Client(DummyRequest(db=db.Session))
Ejemplo n.º 39
0
    def test_mailtmpl_ctor(self):
        tmpl = mail.MailTemplate(Content(),
                                 DummyRequest(),
                                 testattr='testattr')

        self.assertEqual(tmpl.testattr, 'testattr')
Ejemplo n.º 40
0
 def setUp(self):
     request = DummyRequest()
     self.config = setUp(request=request)
     self.config.include('pyramid_chameleon')
Ejemplo n.º 41
0
def dummy_request(registry):
    from pyramid.testing import DummyRequest
    _embed = {}
    request = DummyRequest(registry=registry, _embed=_embed, embed=lambda path: _embed[path])
    return request
Ejemplo n.º 42
0
def test_adapter_factory_call_owssecurity_factory():
    settings = {'twitcher.adapter': DummyAdapter({}).name}
    adapter = get_adapter_factory(settings)
    security = adapter.owssecurity_factory(DummyRequest())
    assert isinstance(security, OWSSecurityInterface)
    assert security.verify_request(DummyRequest()) is True, "Requested adapter should have been called."
Ejemplo n.º 43
0
 def _set_state(self, name: str):
     """Do transition to state `name`, don`t check user permissions."""
     workflow = self.get()['workflow']
     request = get_current_request() or DummyRequest()  # ease testing
     workflow.transition_to_state(self.context, request, to_state=name)
def parse_authz_value(authz):
    environ = {"HTTP_AUTHORIZATION": authz}
    req = DummyRequest(environ=environ)
    return parse_authz_header(req)
Ejemplo n.º 45
0
    def test_route_setup(self):
        from .testing import integration_test_settings
        settings = integration_test_settings()

        from .. import main
        app = main({}, **settings)

        tests = {
            # controller name: (path, routing args)
            'content': (('/contents/abcd-1234.html', {
                'ident_hash': 'abcd-1234',
                'page_ident_hash': '',
                'separator': '',
                'ignore': '',
                'ext': '.html',
            }), ('/contents/abcd-1234/title.html', {
                'ident_hash': 'abcd-1234',
                'page_ident_hash': '',
                'separator': '',
                'ignore': '/title',
                'ext': '.html',
            }), ('/contents/[email protected]:[email protected]', {
                'ident_hash': '[email protected]',
                'page_ident_hash': 'efgh-5678@3',
                'separator': ':',
                'ignore': '',
                'ext': '.html',
            }), ('/contents/[email protected]:efgh-5678@3/ignore.html', {
                'ident_hash': '[email protected]',
                'page_ident_hash': 'efgh-5678@3',
                'separator': ':',
                'ignore': '/ignore',
                'ext': '.html',
            }), ('/contents/abcd-1234.json', {
                'ident_hash': 'abcd-1234',
                'page_ident_hash': '',
                'separator': '',
                'ignore': '',
                'ext': '.json',
            }), ('/contents/abcd-1234/title.json', {
                'ident_hash': 'abcd-1234',
                'page_ident_hash': '',
                'separator': '',
                'ignore': '/title',
                'ext': '.json',
            }), ('/contents/[email protected]:[email protected]', {
                'ident_hash': '[email protected]',
                'page_ident_hash': 'efgh-5678@3',
                'separator': ':',
                'ignore': '',
                'ext': '.json',
            }), ('/contents/[email protected]:efgh-5678@3/ignore.json', {
                'ident_hash': '[email protected]',
                'page_ident_hash': 'efgh-5678@3',
                'separator': ':',
                'ignore': '/ignore',
                'ext': '.json',
            }), ('/contents/abcd-1234', {
                'ident_hash': 'abcd-1234',
                'page_ident_hash': '',
                'separator': '',
                'ignore': '',
                'ext': '',
            }), ('/contents/abcd-1234/', {
                'ident_hash': 'abcd-1234',
                'page_ident_hash': '',
                'separator': '',
                'ignore': '/',
                'ext': '',
            }), ('/contents/abcd-1234/title', {
                'ident_hash': 'abcd-1234',
                'page_ident_hash': '',
                'separator': '',
                'ignore': '/title',
                'ext': '',
            }), ('/contents/abcd-1234/title/', {
                'ident_hash': 'abcd-1234',
                'page_ident_hash': '',
                'separator': '',
                'ignore': '/title/',
                'ext': '',
            }), ('/contents/abcd-1234:efgh-5678@3/ignore', {
                'ident_hash': 'abcd-1234',
                'page_ident_hash': 'efgh-5678@3',
                'separator': ':',
                'ignore': '/ignore',
                'ext': '',
            }), ('/contents/[email protected]:efgh-5678', {
                'ident_hash': '[email protected]',
                'page_ident_hash': 'efgh-5678',
                'separator': ':',
                'ignore': '',
                'ext': '',
            }), ('/contents/[email protected]:efgh-5678/', {
                'ident_hash': '[email protected]',
                'page_ident_hash': 'efgh-5678',
                'separator': ':',
                'ignore': '/',
                'ext': '',
            }), ('/contents/[email protected]:efgh-5678@3/ignore', {
                'ident_hash': '[email protected]',
                'page_ident_hash': 'efgh-5678@3',
                'separator': ':',
                'ignore': '/ignore',
                'ext': '',
            })),
            'resource': (
                ('/resources/abcd1234', {
                    'hash': 'abcd1234',
                    'ignore': '',
                }),
                ('/resources/abcd1234/', {
                    'hash': 'abcd1234',
                    'ignore': '/',
                }),
                ('/resources/abcd1234/picture.jpg', {
                    'hash': 'abcd1234',
                    'ignore': '/picture.jpg',
                }),
            ),
            'export': (
                ('/exports/abcd-1234.pdf', {
                    'ident_hash': 'abcd-1234',
                    'type': 'pdf',
                    'ignore': '',
                }),
                ('/exports/abcd-1234.pdf/title.pdf', {
                    'ident_hash': 'abcd-1234',
                    'type': 'pdf',
                    'ignore': '/title.pdf',
                }),
            ),
            'extras': (
                ('/extras', {
                    'key': ''
                }),
                ('/extras/featured', {
                    'key': '/featured'
                }),
                ('/extras/messages', {
                    'key': '/messages'
                }),
                ('/extras/licenses', {
                    'key': '/licenses'
                }),
                ('/extras/subjects', {
                    'key': '/subjects'
                }),
                ('/extras/languages', {
                    'key': '/languages'
                }),
            ),
            'content-extras': (('/extras/abcd@1234', {
                'ident_hash': 'abcd@1234',
                'page_ident_hash': '',
                'separator': ''
            }), ),
            'content-extras': (('/extras/abcd@1234:efgh@5678', {
                'ident_hash': 'abcd@1234',
                'page_ident_hash': 'efgh@5678',
                'separator': ':'
            }), ),
            'search': (('/search', {}), ),
            'sitemap': (('/sitemap-1.xml', {
                'from_id': '1',
            }), ),
            'sitemap-index': (('/sitemap_index.xml', {}), ),
            'legacy-redirect': (
                ('/content/m12345', {
                    'objid': 'm12345',
                    'ignore': '',
                }),
                ('/content/m12345/', {
                    'objid': 'm12345',
                    'ignore': '/',
                }),
            ),
            'legacy-redirect-w-version': (
                ('/content/m12345/1.2', {
                    'objid': 'm12345',
                    'objver': '1.2',
                    'ignore': '',
                    'filename': '',
                }),
                ('/content/m12345/1.2/', {
                    'objid': 'm12345',
                    'objver': '1.2',
                    'ignore': '/',
                    'filename': '',
                }),
                ('/content/m12345/1.2/picture.jpg', {
                    'objid': 'm12345',
                    'objver': '1.2',
                    'ignore': '/',
                    'filename': 'picture.jpg',
                }),
            ),
            'legacy-redirect-latest': (
                ('/content/m12345/latest', {
                    'objid': 'm12345',
                    'ignore': '',
                    'filename': '',
                }),
                ('/content/m12345/latest/', {
                    'objid': 'm12345',
                    'ignore': '/',
                    'filename': '',
                }),
            ),
            None: (
                ('/extras/', None),
                ('/contents', None),
                ('/contents/', None),
            ),
        }

        for controller_name, args in tests.items():
            for path, routing_args in args:
                req = DummyRequest(environ={'PATH_INFO': path})
                routemap = app.routes_mapper(req)
                route = routemap['route']
                routename = getattr(route, 'name', None)
                self.assertEqual(routename, controller_name)
                self.assertEqual(routemap['match'], routing_args)
Ejemplo n.º 46
0
def test_staff_add_calls_make_staff(make_staff):
    request = DummyRequest(params={"add": "seanh"})

    views.staff_add(request)

    make_staff.assert_called_once_with("seanh")
Ejemplo n.º 47
0
class TestSQLiteCache(unittest.TestCase):
    """ Tests for the SQLAlchemy cache """

    DB_URL = "sqlite://"

    @classmethod
    def setUpClass(cls):
        super(TestSQLiteCache, cls).setUpClass()
        settings = {"pypi.storage": "tests.DummyStorage", "db.url": cls.DB_URL}
        try:
            cls.kwargs = SQLCache.configure(settings)
        except OperationalError:
            raise unittest.SkipTest("Couldn't connect to database")

    def setUp(self):
        super(TestSQLiteCache, self).setUp()
        transaction.begin()
        self.request = DummyRequest()
        self.request.tm = transaction.manager
        self.db = SQLCache(self.request, **self.kwargs)
        self.sql = self.db.db
        self.storage = self.db.storage = MagicMock(spec=IStorage)

    def tearDown(self):
        super(TestSQLiteCache, self).tearDown()
        transaction.abort()
        self.sql.query(SQLPackage).delete()
        transaction.commit()
        self.request._process_finished_callbacks()

    def test_upload(self):
        """ upload() saves package and uploads to storage """
        pkg = make_package(factory=SQLPackage)
        self.db.upload(pkg.filename, None, pkg.name, pkg.version)
        count = self.sql.query(SQLPackage).count()
        self.assertEqual(count, 1)
        saved_pkg = self.sql.query(SQLPackage).first()
        self.assertEqual(saved_pkg, pkg)
        self.storage.upload.assert_called_with(pkg, None)

    def test_upload_overwrite(self):
        """ Uploading a preexisting packages overwrites current package """
        self.db.allow_overwrite = True
        name, filename = "a", "a-1.tar.gz"
        self.db.upload(filename, "old", name)
        self.db.upload(filename, "new", name)

        all_versions = self.db.all(name)
        self.assertEqual(len(all_versions), 1)

    def test_save(self):
        """ save() puts object into database """
        pkg = make_package(factory=SQLPackage)
        self.db.save(pkg)
        count = self.sql.query(SQLPackage).count()
        self.assertEqual(count, 1)
        saved_pkg = self.sql.query(SQLPackage).first()
        self.assertEqual(saved_pkg, pkg)

    def test_save_unicode(self):
        """ save() can store packages with unicode in the names """
        pkg = make_package("mypackage™", factory=SQLPackage)
        self.db.save(pkg)
        count = self.sql.query(SQLPackage).count()
        self.assertEqual(count, 1)
        saved_pkg = self.sql.query(SQLPackage).first()
        self.assertEqual(saved_pkg, pkg)

    def test_delete(self):
        """ delete() removes object from database and deletes from storage """
        pkg = make_package(factory=SQLPackage)
        self.sql.add(pkg)
        transaction.commit()
        self.sql.add(pkg)
        self.db.delete(pkg)
        count = self.sql.query(SQLPackage).count()
        self.assertEqual(count, 0)
        self.storage.delete.assert_called_with(pkg)

    def test_clear(self):
        """ clear() removes object from database """
        pkg = make_package(factory=SQLPackage)
        self.sql.add(pkg)
        transaction.commit()
        self.sql.add(pkg)
        self.db.delete(pkg)
        count = self.sql.query(SQLPackage).count()
        self.assertEqual(count, 0)

    def test_reload(self):
        """ reload_from_storage() inserts packages into the database """
        keys = [
            make_package(factory=SQLPackage),
            make_package("mypkg2",
                         "1.3.4",
                         "my/other/path",
                         factory=SQLPackage),
        ]
        self.storage.list.return_value = keys
        self.db.reload_from_storage()
        all_pkgs = self.sql.query(SQLPackage).all()
        self.assertItemsEqual(all_pkgs, keys)

    def test_fetch(self):
        """ fetch() retrieves a package from the database """
        pkg = make_package(factory=SQLPackage)
        self.sql.add(pkg)
        saved_pkg = self.db.fetch(pkg.filename)
        self.assertEqual(saved_pkg, pkg)

    def test_fetch_missing(self):
        """ fetch() returns None if no package exists """
        saved_pkg = self.db.fetch("missing_pkg-1.2.tar.gz")
        self.assertIsNone(saved_pkg)

    def test_all_versions(self):
        """ all() returns all versions of a package """
        pkgs = [
            make_package(factory=SQLPackage),
            make_package(version="1.3", filename="mypath3",
                         factory=SQLPackage),
            make_package("mypkg2",
                         "1.3.4",
                         "my/other/path",
                         factory=SQLPackage),
        ]
        self.sql.add_all(pkgs)
        saved_pkgs = self.db.all("mypkg")
        self.assertItemsEqual(saved_pkgs, pkgs[:2])

    def test_distinct(self):
        """ distinct() returns all unique package names """
        pkgs = [
            make_package(factory=SQLPackage),
            make_package(version="1.3", filename="mypath3",
                         factory=SQLPackage),
            make_package("mypkg2",
                         "1.3.4",
                         "my/other/path",
                         factory=SQLPackage),
        ]
        self.sql.add_all(pkgs)
        saved_pkgs = self.db.distinct()
        self.assertItemsEqual(saved_pkgs, set([p.name for p in pkgs]))

    def test_search_or(self):
        """ search() returns packages that match the query """
        pkgs = [
            make_package(factory=SQLPackage),
            make_package(
                "somepackage",
                version="1.3",
                filename="mypath3",
                summary="this is mypkg",
                factory=SQLPackage,
            ),
            make_package("mypkg2",
                         "1.3.4",
                         "my/other/path",
                         factory=SQLPackage),
            make_package("package", factory=SQLPackage),
        ]
        self.sql.add_all(pkgs)
        criteria = {"name": ["mypkg"], "summary": ["mypkg"]}
        packages = self.db.search(criteria, "or")
        self.assertItemsEqual(packages, pkgs[:-1])

    def test_search_and(self):
        """ search() returns packages that match the query """
        pkgs = [
            make_package(factory=SQLPackage),
            make_package(
                "somepackage",
                version="1.3",
                filename="mypath3",
                summary="this is mypkg",
                factory=SQLPackage,
            ),
            make_package("mypkg2",
                         "1.3.4",
                         "my/other/path",
                         factory=SQLPackage),
            make_package("package", factory=SQLPackage),
        ]
        self.sql.add_all(pkgs)
        criteria = {"name": ["my", "pkg"], "summary": ["this", "mypkg"]}
        packages = self.db.search(criteria, "and")
        self.assertItemsEqual(packages, pkgs[:-1])

    def test_summary(self):
        """ summary constructs per-package metadata summary """
        self.db.upload("pkg1-0.3.tar.gz", None, "pkg1", "0.3")
        self.db.upload("pkg1-1.1.tar.gz", None, "pkg1", "1.1")
        p1 = self.db.upload("pkg1a2.tar.gz", None, "pkg1", "1.1.1a2")
        p2 = self.db.upload("pkg2.tar.gz", None, "pkg2", "0.1dev2")
        s1, s2 = self.db.summary()  # pylint: disable=E0632
        # Order them correctly. assertItemsEqual isn't playing nice in py2.6
        if s1["name"] == "pkg2":
            s1, s2 = s2, s1
        # last_modified may be rounded when stored in MySQL,
        # so the best we can do is make sure they're close.
        self.assertTrue(
            calendar.timegm(s1["last_modified"].timetuple()) -
            calendar.timegm(p1.last_modified.timetuple()) <= 1)
        self.assertTrue(
            calendar.timegm(s2["last_modified"].timetuple()) -
            calendar.timegm(p2.last_modified.timetuple()) <= 1)

    def test_multiple_packages_same_version(self):
        """ Can upload multiple packages that have the same version """
        with patch.object(self.db, "allow_overwrite", False):
            name, version = "a", "1"
            path1 = "old_package_path-1.tar.gz"
            self.db.upload(path1, None, name, version)
            path2 = "new_path-1.whl"
            self.db.upload(path2, None, name, version)

            all_versions = self.db.all(name)
            self.assertEqual(len(all_versions), 2)

    def test_reload_if_needed(self):
        """ Reload the cache if it's empty """
        self.db.storage = MagicMock()
        self.db.storage.list.return_value = [make_package(factory=SQLPackage)]
        self.db.reload_if_needed()
        count = self.sql.query(SQLPackage).count()
        self.assertEqual(count, 1)

    def test_check_health_success(self):
        """ check_health returns True for good connection """
        ok, msg = self.db.check_health()
        self.assertTrue(ok)

    def test_check_health_fail(self):
        """ check_health returns False for bad connection """
        dbmock = self.db.db = MagicMock()

        def throw(*_, **__):
            """ Throw an exception """
            raise SQLAlchemyError("DB exception")

        dbmock.query.side_effect = throw
        ok, msg = self.db.check_health()
        self.assertFalse(ok)
Ejemplo n.º 48
0
 def _makeRequest(self, **kw):
     from pyramid.testing import DummyRequest
     return DummyRequest(**kw)
Ejemplo n.º 49
0
def dummy_request():
    """Instantiate a fake HTTP Request, complete with a database session."""
    return DummyRequest()
Ejemplo n.º 50
0
 def setUp(self):
     super(TestDynamoCache, self).setUp()
     self.db = DynamoCache(DummyRequest(), **self.kwargs)
     self.storage = self.db.storage = MagicMock(spec=IStorage)
Ejemplo n.º 51
0
 def call(self, context):
     from kotti_tinymce.settings import get_settings_json
     request = DummyRequest()
     request.context = context
     return json.loads(get_settings_json(request))
Ejemplo n.º 52
0
class TestSQLiteCache(unittest.TestCase):

    """ Tests for the SQLCache """

    DB_URL = 'sqlite://'

    @classmethod
    def setUpClass(cls):
        super(TestSQLiteCache, cls).setUpClass()
        settings = {
            'pypi.storage': 'tests.DummyStorage',
            'db.url': cls.DB_URL,
            'db.graceful_reload': True,
        }
        try:
            cls.kwargs = SQLCache.configure(settings)
        except OperationalError:
            raise unittest.SkipTest("Couldn't connect to database")

    def setUp(self):
        super(TestSQLiteCache, self).setUp()
        transaction.begin()
        self.request = DummyRequest()
        self.request.tm = transaction.manager
        self.db = SQLCache(self.request, **self.kwargs)
        self.sql = self.db.db
        self.storage = self.db.storage = MagicMock(spec=IStorage)

    def tearDown(self):
        super(TestSQLiteCache, self).tearDown()
        transaction.abort()
        self.sql.query(SQLPackage).delete()
        transaction.commit()
        self.request._process_finished_callbacks()

    def _make_package(self, *args, **kwargs):
        """ Wrapper around make_package """
        # Some SQL dbs are rounding the timestamps (looking at you MySQL >:|
        # which is a problem if they round UP to the future, as our
        # calculations depend on the timestamps being monotonically increasing.
        now = datetime.utcnow() - timedelta(seconds=1)
        kwargs.setdefault('last_modified', now)
        kwargs.setdefault('factory', SQLPackage)
        return make_package(*args, **kwargs)

    def test_add_missing(self):
        """ Add missing packages to cache """
        keys = [
            self._make_package(),
        ]
        self.storage.list.return_value = keys
        self.db.reload_from_storage()
        all_pkgs = self.sql.query(SQLPackage).all()
        self.assertItemsEqual(all_pkgs, keys)

    def test_remove_extra(self):
        """ Remove extra packages from cache """
        keys = [
            self._make_package(),
            self._make_package('mypkg2', '1.3.4'),
        ]
        self.db.save(keys[0])
        self.db.save(keys[1])
        self.storage.list.return_value = keys[:1]
        self.db.reload_from_storage()
        all_pkgs = self.sql.query(SQLPackage).all()
        self.assertItemsEqual(all_pkgs, keys[:1])

    def test_remove_extra_leave_concurrent(self):
        """ Removing extra packages will leave packages that were uploaded concurrently """
        pkgs = [
            self._make_package(),
            self._make_package('mypkg2'),
        ]
        self.db.save(pkgs[0])
        self.db.save(pkgs[1])

        # Return first pkgs[1], then pkgs[1:] because the second time we list
        # we will have "uploaded" pkgs[2]
        return_values = [lambda: pkgs[1:2], lambda: pkgs[1:]]

        def list_storage(package_class):
            """ mocked method for listing storage packages """
            # The first time we list from storage, concurrently "upload"
            # pkgs[2]
            if len(return_values) == 2:
                nowish = datetime.utcnow() + timedelta(seconds=1)
                pkg = self._make_package('mypkg3', last_modified=nowish)
                pkgs.append(pkg)
                self.db.save(pkg)
            return return_values.pop(0)()
        self.storage.list.side_effect = list_storage

        self.db.reload_from_storage()
        all_pkgs = self.sql.query(SQLPackage).all()
        self.assertItemsEqual(all_pkgs, pkgs[1:])

    def test_remove_extra_concurrent_deletes(self):
        """ Remove packages from cache that were concurrently deleted """
        pkgs = [
            self._make_package(),
            self._make_package('mypkg2'),
        ]
        self.db.save(pkgs[0])

        # Return first pkgs[:], then pkgs[:1] because the second time we list
        # we will have "deleted" pkgs[1]
        return_values = [pkgs[:], pkgs[:1]]
        self.storage.list.side_effect = lambda _: return_values.pop(0)

        self.db.reload_from_storage()
        all_pkgs = self.sql.query(SQLPackage).all()
        self.assertItemsEqual(all_pkgs, pkgs[:1])

    def test_add_missing_more_recent(self):
        """ If we sync a more recent package, update the summary """
        pkgs = [
            self._make_package(last_modified=datetime.utcnow() - timedelta(hours=1)),
            self._make_package(version='1.5'),
        ]
        self.db.save(pkgs[0])
        self.storage.list.return_value = pkgs
        self.db.reload_from_storage()
        all_pkgs = self.sql.query(SQLPackage).all()
        self.assertItemsEqual(all_pkgs, pkgs)
Ejemplo n.º 53
0
def dummy_request(request, config):
    config.manager.get()['request'] = req = DummyRequest()
    return req
Ejemplo n.º 54
0
 def call(self, context):
     from kotti_tinymce import jsondetails
     request = DummyRequest()
     return jsondetails(context, request)
Ejemplo n.º 55
0
    def test_serve_gfi(self, l):
        server.pyramid_server = None
        request = DummyRequest()
        request.registry.settings = {
            'tilegeneration_configfile': 'tilegeneration/test-serve.yaml',
        }
        request.params = {
            'Service': 'WMTS',
            'Version': '1.0.0',
            'Request': 'GetFeatureInfo',
            'Format': 'image/png',
            'Info_Format': 'application/vnd.ogc.gml',
            'Layer': 'point_hash',
            'Query_Layer': 'point_hash',
            'Style': 'default',
            'TileMatrixSet': 'swissgrid_5',
            'TileMatrix': '1',
            'TileRow': '11',
            'TileCol': '14',
            'I': '114',
            'J': '111',
        }
        serve = PyramidView(request)
        serve()
        self.assert_result_equals(
            request.response.body.decode('utf-8') if PY3 else
            request.response.body, u"""<?xml version="1.0" encoding="UTF-8"?>

<msGMLOutput
    xmlns:gml="http://www.opengis.net/gml"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</msGMLOutput>
""")

        server.pyramid_server = None
        request = DummyRequest()
        request.registry.settings = {
            'tilegeneration_configfile': 'tilegeneration/test-serve.yaml',
        }
        request.matchdict = {
            'path': [
                'wmts', '1.0.0', 'point_hash', 'default', '2012',
                'swissgrid_5', '1', '11', '14', '114', '111.xml'
            ]
        }
        request.params = {
            'Service': 'WMTS',
            'Version': '1.0.0',
            'Request': 'GetFeatureInfo',
            'Format': 'image/png',
            'Info_Format': 'application/vnd.ogc.gml',
            'Layer': 'point_hash',
            'Query_Layer': 'point_hash',
            'Style': 'default',
            'TileMatrixSet': 'swissgrid_5',
            'TileMatrix': '1',
            'TileRow': '14',
            'TileCol': '11',
            'I': '114',
            'J': '111',
        }
        serve = PyramidView(request)
        serve()
        self.assert_result_equals(
            request.response.body.decode('utf-8') if PY3 else
            request.response.body, u"""<?xml version="1.0" encoding="UTF-8"?>

<msGMLOutput
    xmlns:gml="http://www.opengis.net/gml"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</msGMLOutput>
""")
Ejemplo n.º 56
0
def as_dict(content, **kw):
    return dict(loads(render('json', content, DummyRequest())), **kw)
Ejemplo n.º 57
0
    def test_serve_kvp(self, l):
        self.assert_tiles_generated(
            cmd=
            '.build/venv/bin/generate_tiles -d -c tilegeneration/test-nosns.yaml '
            '-l point_hash --zoom 1',
            main_func=generate.main,
            directory="/tmp/tiles/",
            tiles_pattern='1.0.0/%s',
            tiles=[
                ('point_hash/default/2012/swissgrid_5/1/11/14.png'),
                ('point_hash/default/2012/swissgrid_5/1/15/8.png'),
            ],
            regex=True,
            expected=
            """The tile generation of layer 'point_hash \(DATE=2012\)' is finish
Nb generated metatiles: 1
Nb metatiles dropped: 0
Nb generated tiles: 64
Nb tiles dropped: 62
Nb tiles stored: 2
Nb tiles in error: 0
Total time: [0-9]+:[0-9][0-9]:[0-9][0-9]
Total size: [89][0-9][0-9] o
Time per tile: [0-9]+ ms
Size per tile: 4[0-9][0-9] o

""",
        )
        # use delete to don't delete the repository
        self.assert_tiles_generated_deleted(
            cmd=
            '.build/venv/bin/generate_controller --capabilities -c tilegeneration/test-nosns.yaml',
            main_func=controller.main,
            directory="/tmp/tiles/",
            tiles_pattern='1.0.0/%s',
            tiles=[
                ('WMTSCapabilities.xml'),
                ('point_hash/default/2012/swissgrid_5/1/11/14.png'),
                ('point_hash/default/2012/swissgrid_5/1/15/8.png'),
            ],
        )

        server.pyramid_server = None
        request = DummyRequest()
        request.registry.settings = {
            'tilegeneration_configfile': 'tilegeneration/test-nosns.yaml',
        }
        request.params = {
            'Service': 'WMTS',
            'Version': '1.0.0',
            'Request': 'GetTile',
            'Format': 'image/png',
            'Layer': 'point_hash',
            'Style': 'default',
            'TileMatrixSet': 'swissgrid_5',
            'TileMatrix': '1',
            'TileRow': '11',
            'TileCol': '14',
        }
        serve = PyramidView(request)
        serve()
        self.assertEqual(request.response.headers['Content-Type'], 'image/png')
        self.assertEqual(request.response.headers['Cache-Control'],
                         'max-age=28800')

        request.params['TileRow'] = '12'
        self.assertRaises(HTTPNoContent, serve)

        request.params['TileRow'] = '11'
        request.params['Service'] = 'test'
        self.assertRaises(HTTPBadRequest, serve)

        request.params['Service'] = 'WMTS'
        request.params['Request'] = 'test'
        self.assertRaises(HTTPBadRequest, serve)

        request.params['Request'] = 'GetTile'
        request.params['Version'] = '0.9'
        self.assertRaises(HTTPBadRequest, serve)

        request.params['Version'] = '1.0.0'
        request.params['Format'] = 'image/jpeg'
        self.assertRaises(HTTPBadRequest, serve)

        request.params['Format'] = 'image/png'
        request.params['Layer'] = 'test'
        self.assertRaises(HTTPBadRequest, serve)

        request.params['Layer'] = 'point_hash'
        request.params['Style'] = 'test'
        self.assertRaises(HTTPBadRequest, serve)

        request.params['Style'] = 'default'
        request.params['TileMatrixSet'] = 'test'
        self.assertRaises(HTTPBadRequest, serve)

        request.params['TileMatrixSet'] = 'swissgrid_5'
        del request.params['Service']
        self.assertRaises(HTTPBadRequest, serve)

        request.params = {
            'Service': 'WMTS',
            'Version': '1.0.0',
            'Request': 'GetCapabilities',
        }
        PyramidView(request)()
        self.assertEqual(request.response.headers['Content-Type'],
                         'application/xml')
        self.assert_result_equals(
            request.response.body.decode('utf-8')
            if PY3 else request.response.body,
            regex=True,
            expected=u"""<\?xml version="1.0" encoding="UTF-8"\?>
<Capabilities version="1.0.0" xmlns="http://www.opengis.net/wmts/1.0" xmlns:ows="http://www.opengis.net/ows/1.1"
              xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:gml="http://www.opengis.net/gml"
              xsi:schemaLocation="http://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd">
  <ows:ServiceIdentification> </ows:ServiceIdentification>
  <ows:ServiceProvider> </ows:ServiceProvider>
  <ows:OperationsMetadata>
    <ows:Operation name="GetCapabilities">
      <ows:DCP>
        <ows:HTTP>
          <ows:Get xlink:href="http://wmts1/tiles/wmts/1.0.0/WMTSCapabilities.xml">
            <ows:Constraint name="GetEncoding">
              <ows:AllowedValues>
                <ows:Value>REST</ows:Value>
              </ows:AllowedValues>
            </ows:Constraint>
          </ows:Get>
          <ows:Get xlink:href="http://wmts1/tiles/wmts/">
            <ows:Constraint name="GetEncoding">
              <ows:AllowedValues>
                <ows:Value>KVP</ows:Value>
              </ows:AllowedValues>
            </ows:Constraint>
          </ows:Get>
        </ows:HTTP>
      </ows:DCP>
    </ows:Operation>
    <ows:Operation name="GetTile">
      <ows:DCP>
        <ows:HTTP>
          <ows:Get xlink:href="http://wmts1/tiles/wmts/">
            <ows:Constraint name="GetEncoding">
              <ows:AllowedValues>
                <ows:Value>REST</ows:Value>
                <ows:Value>KVP</ows:Value>
              </ows:AllowedValues>
            </ows:Constraint>
          </ows:Get>
        </ows:HTTP>
      </ows:DCP>
    </ows:Operation>
  </ows:OperationsMetadata>
  <!-- <ServiceMetadataURL xlink:href="" /> -->
  <Contents>

    <Layer>
      <ows:Title>all</ows:Title>
      <ows:Identifier>all</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>image/png</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="image/png" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/all/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>line</ows:Title>
      <ows:Identifier>line</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>image/png</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="image/png" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/line/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>mapnik</ows:Title>
      <ows:Identifier>mapnik</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>image/png</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="image/png" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/mapnik/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>mapnik_grid</ows:Title>
      <ows:Identifier>mapnik_grid</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>application/utfgrid</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="application/utfgrid" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/mapnik_grid/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.json" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>mapnik_grid_drop</ows:Title>
      <ows:Identifier>mapnik_grid_drop</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>application/utfgrid</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="application/utfgrid" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/mapnik_grid_drop/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.json" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>point</ows:Title>
      <ows:Identifier>point</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>image/png</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="image/png" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/point/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>point_error</ows:Title>
      <ows:Identifier>point_error</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>image/png</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="image/png" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/point_error/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>point_hash</ows:Title>
      <ows:Identifier>point_hash</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>image/png</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="image/png" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/point_hash/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>point_hash_no_meta</ows:Title>
      <ows:Identifier>point_hash_no_meta</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>image/png</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="image/png" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/point_hash_no_meta/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>point_px_buffer</ows:Title>
      <ows:Identifier>point_px_buffer</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>image/png</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="image/png" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/point_px_buffer/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>polygon</ows:Title>
      <ows:Identifier>polygon</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>image/png</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="image/png" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/polygon/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_5</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>

    <Layer>
      <ows:Title>polygon2</ows:Title>
      <ows:Identifier>polygon2</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
      </Style>
      <Format>image/png</Format>
      <Dimension>
        <ows:Identifier>DATE</ows:Identifier>
        <Default>2012</Default>
        <Value>2005</Value>
        <Value>2010</Value>
        <Value>2012</Value>
      </Dimension>
      <ResourceURL format="image/png" resourceType="tile"
                   template="http://wmts1/tiles/wmts/1.0.0/polygon2/default/"""
            """{DATE}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
      <TileMatrixSetLink>
        <TileMatrixSet>swissgrid_01</TileMatrixSet>
      </TileMatrixSetLink>
    </Layer>



    <TileMatrixSet>
      <ows:Identifier>swissgrid_01</ows:Identifier>
      <ows:SupportedCRS>urn:ogc:def:crs:EPSG::21781</ows:SupportedCRS>
      <TileMatrix>
        <ows:Identifier>1</ows:Identifier>
        <ScaleDenominator>3571.4285714[0-9]*</ScaleDenominator>
        <TopLeftCorner>420000 350000</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>1875</MatrixWidth>
        <MatrixHeight>1250</MatrixHeight>
      </TileMatrix>

      <TileMatrix>
        <ows:Identifier>0_2</ows:Identifier>
        <ScaleDenominator>714.28571428[0-9]*</ScaleDenominator>
        <TopLeftCorner>420000 350000</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>9375</MatrixWidth>
        <MatrixHeight>6250</MatrixHeight>
      </TileMatrix>

      <TileMatrix>
        <ows:Identifier>0_1</ows:Identifier>
        <ScaleDenominator>357.14285714[0-9]*</ScaleDenominator>
        <TopLeftCorner>420000 350000</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>18750</MatrixWidth>
        <MatrixHeight>12500</MatrixHeight>
      </TileMatrix>

    </TileMatrixSet>

    <TileMatrixSet>
      <ows:Identifier>swissgrid_025</ows:Identifier>
      <ows:SupportedCRS>urn:ogc:def:crs:EPSG::21781</ows:SupportedCRS>
      <TileMatrix>
        <ows:Identifier>0_25</ows:Identifier>
        <ScaleDenominator>892.85714285[0-9]*</ScaleDenominator>
        <TopLeftCorner>420000 350000</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>7500</MatrixWidth>
        <MatrixHeight>5000</MatrixHeight>
      </TileMatrix>

    </TileMatrixSet>

    <TileMatrixSet>
      <ows:Identifier>swissgrid_2_5</ows:Identifier>
      <ows:SupportedCRS>urn:ogc:def:crs:EPSG::21781</ows:SupportedCRS>
      <TileMatrix>
        <ows:Identifier>2_5</ows:Identifier>
        <ScaleDenominator>8928.5714285[0-9]*</ScaleDenominator>
        <TopLeftCorner>420000 350000</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>750</MatrixWidth>
        <MatrixHeight>500</MatrixHeight>
      </TileMatrix>

    </TileMatrixSet>

    <TileMatrixSet>
      <ows:Identifier>swissgrid_5</ows:Identifier>
      <ows:SupportedCRS>urn:ogc:def:crs:EPSG::21781</ows:SupportedCRS>
      <TileMatrix>
        <ows:Identifier>0</ows:Identifier>
        <ScaleDenominator>357142.85714[0-9]*</ScaleDenominator>
        <TopLeftCorner>420000 350000</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>19</MatrixWidth>
        <MatrixHeight>13</MatrixHeight>
      </TileMatrix>

      <TileMatrix>
        <ows:Identifier>1</ows:Identifier>
        <ScaleDenominator>178571.42857[0-9]*</ScaleDenominator>
        <TopLeftCorner>420000 350000</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>38</MatrixWidth>
        <MatrixHeight>25</MatrixHeight>
      </TileMatrix>

      <TileMatrix>
        <ows:Identifier>2</ows:Identifier>
        <ScaleDenominator>71428.571428[0-9]*</ScaleDenominator>
        <TopLeftCorner>420000 350000</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>94</MatrixWidth>
        <MatrixHeight>63</MatrixHeight>
      </TileMatrix>

      <TileMatrix>
        <ows:Identifier>3</ows:Identifier>
        <ScaleDenominator>35714.285714[0-9]*</ScaleDenominator>
        <TopLeftCorner>420000 350000</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>188</MatrixWidth>
        <MatrixHeight>125</MatrixHeight>
      </TileMatrix>

      <TileMatrix>
        <ows:Identifier>4</ows:Identifier>
        <ScaleDenominator>17857.142857[0-9]*</ScaleDenominator>
        <TopLeftCorner>420000 350000</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>375</MatrixWidth>
        <MatrixHeight>250</MatrixHeight>
      </TileMatrix>

    </TileMatrixSet>

  </Contents>
</Capabilities>""")

        l.check()
Ejemplo n.º 58
0
 def _route_url(name, **kwargs):
     return DummyRequest().route_url(name, **kwargs)
Ejemplo n.º 59
0
 def test_viewContact(self):
     request = DummyRequest()
     contact = DummyResource(name = 'Spike')
     info = viewContact(contact, request)
     self.assertEqual(info['currentUser'], None)
Ejemplo n.º 60
0
    def test_bsddb_rest(self, l):
        self.assert_tiles_generated(
            cmd=
            '.build/venv/bin/generate_tiles -d -c tilegeneration/test-bsddb.yaml'
            ' -l point_hash --zoom 1',
            main_func=generate.main,
            directory="/tmp/tiles/bsddb/",
            tiles_pattern='1.0.0/%s',
            tiles=[('point_hash/default/2012/swissgrid_5.png.bsddb')],
            regex=True,
            expected=
            """The tile generation of layer 'point_hash \(DATE=2012\)' is finish
Nb generated metatiles: 1
Nb metatiles dropped: 0
Nb generated tiles: 64
Nb tiles dropped: 62
Nb tiles stored: 2
Nb tiles in error: 0
Total time: [0-9]+:[0-9][0-9]:[0-9][0-9]
Total size: [89][0-9][0-9] o
Time per tile: [0-9]+ ms
Size per tile: 4[0-9][0-9] o

""",
        )
        # use delete to don't delete the repository
        self.assert_tiles_generated_deleted(
            cmd=
            '.build/venv/bin/generate_controller --capabilities -c tilegeneration/test-bsddb.yaml',
            main_func=controller.main,
            directory="/tmp/tiles/bsddb/",
            tiles_pattern='1.0.0/%s',
            tiles=[('WMTSCapabilities.xml'),
                   ('point_hash/default/2012/swissgrid_5.png.bsddb')],
        )

        server.pyramid_server = None
        request = DummyRequest()
        request.registry.settings = {
            'tilegeneration_configfile': 'tilegeneration/test-bsddb.yaml',
        }
        request.matchdict = {
            'path': [
                'wmts', '1.0.0', 'point_hash', 'default', '2012',
                'swissgrid_5', '1', '11', '14.png'
            ]
        }
        serve = PyramidView(request)
        serve()
        self.assertEqual(request.response.headers['Content-Type'], 'image/png')
        self.assertEqual(request.response.headers['Cache-Control'],
                         'max-age=28800')

        request.matchdict['path'][7] = '12'
        self.assertRaises(HTTPNoContent, serve)

        request.matchdict['path'][7] = '11'
        request.matchdict['path'][1] = '0.9'
        self.assertRaises(HTTPBadRequest, serve)

        request.matchdict['path'][1] = '1.0.0'
        request.matchdict['path'][8] = '14.jpeg'
        self.assertRaises(HTTPBadRequest, serve)

        request.matchdict['path'][8] = '14.png'
        request.matchdict['path'][2] = 'test'
        self.assertRaises(HTTPBadRequest, serve)

        request.matchdict['path'][2] = 'point_hash'
        request.matchdict['path'][3] = 'test'
        self.assertRaises(HTTPBadRequest, serve)

        request.matchdict['path'][3] = 'default'
        request.matchdict['path'][5] = 'test'
        self.assertRaises(HTTPBadRequest, serve)

        request.matchdict['path'] = [
            'wmts', 'point_hash', 'default', 'swissgrid_5', '1', '14', '11.png'
        ]
        self.assertRaises(HTTPBadRequest, serve)

        request.matchdict['path'] = ['wmts', '1.0.0', 'WMTSCapabilities.xml']
        PyramidView(request)()
        self.assertEqual(request.response.headers['Content-Type'],
                         'application/xml')
        self.assert_result_equals(
            request.response.body.decode('utf-8')
            if PY3 else request.response.body,
            CAPABILITIES,
            regex=True,
        )

        request.matchdict['path'] = ['static', '1.0.0', 'WMTSCapabilities.xml']
        PyramidView(request)()
        self.assertEqual(request.response.headers['Content-Type'],
                         'application/xml')
        self.assert_result_equals(
            request.response.body.decode('utf-8')
            if PY3 else request.response.body,
            CAPABILITIES,
            regex=True,
        )

        l.check()