Пример #1
0
 def test_permits_parent_no_inherit(self, target):
     from pyramid.security import Allow, Deny
     from pyramid.testing import DummyResource
     parent_context = DummyResource(
         __parent__=None,
         __acl__=[
             (Allow, ['p1', 'p2'], ['perm1', 'perm2']),
             (Allow, ['p3'], ['perm1']),
             (Deny, ['p5', 'p6'], ['perm2']),
             (Allow, ['p4', 'p5'], ['perm2']),
             (Allow, ['p6'], ['perm3', 'perm4']),
             ]
         )
     context = DummyResource(
         __parent__=parent_context,
         __acl_inherit__=False,
         __acl__=[
             (Deny, ['p1', 'p2'], ['perm1', 'perm2']),
             (Deny, ['p3'], ['perm1']),
             (Allow, ['p5', 'p6'], ['perm2']),
             (Deny, ['p4', 'p5'], ['perm2']),
             (Allow, ['p6'], ['perm5']),
             ]
         )
     assert not target.permits(context, ['p1', 'p2'], 'perm1')
     assert not target.permits(context, ['p1', 'p2'], 'perm2')
     assert not target.permits(context, ['p1', 'p2'], 'perm3')
     assert not target.permits(context, ['p1', 'p2'], 'perm4')
     assert not target.permits(context, ['p1', 'p2'], 'perm5')
     assert target.permits(context, ['p5', 'p6'], 'perm2')
     assert not target.permits(context, ['p6'], 'perm1')
     assert not target.permits(context, ['p6'], 'perm2')
     assert not target.permits(context, ['p6'], 'perm3')
     assert not target.permits(context, ['p6'], 'perm4')
     assert target.permits(context, ['p6'], 'perm5')
Пример #2
0
 def test_queue_and_send_messages(self, websocket_client, changelog_meta):
     from pyramid.testing import DummyResource
     context = DummyResource()
     child = DummyResource()
     context['child'] = child
     metadata = [changelog_meta._replace(resource=child)]
     websocket_client._send_messages(metadata)
     assert websocket_client.changelog_metadata_messages_to_send == set()
Пример #3
0
 def test_send_messages_resource_is_blocked(self, changelog_meta):
     """If a resource is blocked, no event should be sent."""
     client = self.make_one(None)
     client._is_running = True
     resource = DummyResource()
     resource.hidden = True
     metadata = [changelog_meta._replace(modified=True, resource=resource)]
     client.send_messages(metadata)
     assert self._dummy_connection.nothing_sent is True
     assert len(self._dummy_connection.queue) == 0
Пример #4
0
 def test_send_messages_resource_is_blocked(self, changelog_meta):
     """If a resource is blocked, no event should be sent."""
     client = self.make_one(None)
     client._is_running = True
     resource = DummyResource()
     resource.hidden = True
     metadata = [changelog_meta._replace(modified=True, resource=resource)]
     client.send_messages(metadata)
     assert self._dummy_connection.nothing_sent is True
     assert len(self._dummy_connection.queue) == 0
Пример #5
0
    def test_view(self, by_resource_id):
        from pyramid.httpexceptions import HTTPFound
        AccomodationsView._get_title = lambda x, y=None: 'test'

        view = AccomodationsView(DummyResource(),
                                 DummyRequest(params=MultiDict({'rid': 1})))
        self.assertIsInstance(view.view(), HTTPFound)

        AccomodationsView.edit = lambda x=None: {}
        view = AccomodationsView(DummyResource(), DummyRequest())
        self.assertSetEqual({'title', 'readonly'}, set(view.view().keys()))
Пример #6
0
 def test_put(self):
     from pyramid.testing import DummyRequest
     from pyramid.testing import DummyResource
     context = DummyResource()
     fp = 'fp'
     def upload(_fp):
         self.assertEqual(_fp, fp)
     context.upload = upload
     request = DummyRequest()
     inst = self._makeOne(context, request)
     inst.put(fp)
Пример #7
0
    def test_put(self):
        from pyramid.testing import DummyRequest
        from pyramid.testing import DummyResource
        context = DummyResource()
        fp = 'fp'

        def upload(_fp):
            self.assertEqual(_fp, fp)

        context.upload = upload
        request = DummyRequest()
        inst = self._makeOne(context, request)
        inst.put(fp)
Пример #8
0
    def test_get(self):
        fake_response = {'project': 'localapi'}
        request = DummyRequest()
        request.context = DummyResource()

        response = HomeViews(request).get()
        self.assertEqual(response, fake_response)
Пример #9
0
 def test_permits_no_parent(self, target):
     from pyramid.security import Allow, Deny
     from pyramid.testing import DummyResource
     context = DummyResource(
         __parent__=None,
         __acl__=[
             (Allow, ['p1', 'p2'], ['perm1', 'perm2']),
             (Allow, ['p3'], ['perm1']),
             (Deny, ['p5', 'p6'], ['perm2']),
             (Allow, ['p4', 'p5'], ['perm2']),
             (Allow, ['p6'], ['perm3', 'perm4']),
             ]
         )
     for principal in ['p1', 'p2', 'p4', 'p5']:
         for permission in ['perm1', 'perm2', 'perm3', 'perm4']:
             assert not target.permits(context, [principal], permission)
     assert target.permits(context, ['p3'], 'perm1')
     assert not target.permits(context, ['p3'], 'perm2')
     assert not target.permits(context, ['p3'], 'perm3')
     assert not target.permits(context, ['p3'], 'perm4')
     assert not target.permits(context, ['p6'], 'perm1')
     assert not target.permits(context, ['p6'], 'perm2')
     assert target.permits(context, ['p6'], 'perm3')
     assert target.permits(context, ['p6'], 'perm4')
     assert target.permits(context, ['p6'], 'perm3+perm4')
     assert target.permits(context, ['p4', 'p5'], 'perm2')
     assert not target.permits(context, ['p4', 'p5', 'p6'], 'perm2')
     assert target.permits(context, ['p4', 'p5', 'p6'], 'perm3')
     assert target.permits(context, ['p4', 'p5', 'p6'], 'perm4')
Пример #10
0
    def test_allowed(self):
        from pyramid.httpexceptions import HTTPFound
        from ...views.home import HomeView

        self.config.testing_securitypolicy(userid='user', permissive=True)
        view = HomeView(DummyResource(), DummyRequest())
        self.assertIsInstance(view.auth(), HTTPFound)
Пример #11
0
def test_read():
    annotation = DummyResource()

    result = views.read(annotation, DummyRequest())

    _assert_event_published('read')
    assert result == annotation, "Annotation should have been returned"
Пример #12
0
    def test_get(self):
        fake_response = {'project': 'globalapi'}
        request = DummyRequest()
        request.context = DummyResource()

        response = home_views.HomeViews(request).get()
        self.assertEqual(response.json_body, fake_response)
Пример #13
0
def test_read():
    annotation = DummyResource()

    result = api.read(annotation, DummyRequest())

    _assert_event_triggered('read')
    assert result == annotation, "Annotation should have been returned"
Пример #14
0
    def test_install_without_package_name(self):
        request = DummyRequest()
        request.context = DummyResource()
        request.params['package_name'] = None

        response = CatalogItemViews(request).install()
        self.assertEqual(response.status_int, 500)
Пример #15
0
 def test_get_context_has_no_mimetype(self):
     from pyramid.testing import DummyRequest
     from pyramid.testing import DummyResource
     context = DummyResource()
     context.mimetype = None
     blob = DummyResource()
     here = __file__
     def committed():
         return here
     blob.committed = committed
     context.blob = blob
     request = DummyRequest()
     inst = self._makeOne(context, request)
     iterable, mimetype = inst.get()
     self.assertEqual(mimetype, 'application/octet-stream')
     self.assertEqual(type(next(iterable)), bytes)
Пример #16
0
 def setUp(self):
     from pyramid.testing import DummyResource
     from adhocracy_core.interfaces import changelog_meta
     self._client = DummyClient()
     self._registry = DummyResource()
     self._registry.ws_client = self._client
     self._registry.changelog = {}
     self._changelog_metadata = changelog_meta
Пример #17
0
    def test_upload_no_product_id(self):
        request = DummyRequest()
        request.context = DummyResource()
        view = package_views.PackagesViews(request)

        response = view.upload()
        self.assertTrue(response.json_body is not None)
        result = response.json_body
        self.assertTrue(result[u'errors'])
Пример #18
0
    def test_home(self):
        from .views import TutorialViews

        request = DummyRequest()
        title = 'Dummy Context'
        context = DummyResource(title=title)
        inst = TutorialViews(context, request)
        result = inst.home()
        self.assertIn('Home', result['page_title'])
Пример #19
0
    def test_logout(self):
        from ...views.home import HomeView
        from pyramid.httpexceptions import HTTPFound
        view = HomeView(DummyResource(), DummyRequest())
        response = view.logout()
        self.assertEqual({}, response)

        response = view._logout()
        self.assertIsInstance(response, HTTPFound)
Пример #20
0
 def test_send_messages_after_commit_hook_success_and_non_empty_changelog(
         self):
     from pyramid.testing import DummyResource
     from adhocracy_core.websockets.client import send_messages_after_commit_hook
     self._registry.changelog['/'] = \
         self._changelog_metadata._replace(modified=True,
                                           resource=DummyResource())
     send_messages_after_commit_hook(success=True, registry=self._registry)
     assert self._client.messages_sent is True
Пример #21
0
    def test_home_view(self):
        from .views import TutorialViews

        request = DummyRequest()
        title = "Dummy Context"
        context = DummyResource(title=title, __name__='dummy')
        inst = TutorialViews(context, request)
        result = inst.home()
        self.assertIn('Home', result['page_title'])
Пример #22
0
    def test_index_exception(self, mock_getlist):
        error_message = 'GET PACKAGE LIST EXCEPTION'
        mock_getlist.side_effect = Exception(error_message)
        request = DummyRequest()
        request.context = DummyResource()

        response = CatalogViews(request).index()
        self.assertEqual(response['status'], 500)
        self.assertTrue(error_message in response['errors'])
Пример #23
0
    def test_index_returns_404(self, mock_package_list):
        fake_response = {'status': 404, 'packages': [{}]}
        mock_package_list.return_value = fake_response
        request = DummyRequest()
        request.context = DummyResource()

        response = CatalogViews(request).index()
        self.assertEqual(type(response), dict)
        self.assertEqual(type(response['packages']), list)
        self.assertEqual(response['status'], 404)
Пример #24
0
    def test_default_view(self):
        from views import ProjectorViews

        request = DummyRequest()
        title = "Dummy Context"
        context = DummyResource(title=title)
        inst = ProjectorViews(context, request)
        result = inst.default_view()
        self.failUnless('SiteFolder' in result.body)
        self.failUnless(title in result.body)
Пример #25
0
    def test_index_404(self, mock_getlist):
        mock_package_list = {'status': 404}
        mock_getlist.return_value = mock_package_list
        request = DummyRequest()
        request.context = DummyResource()
        request.matchdict['product_id'] = 'dummy'
        view = package_views.PackagesViews(request)

        response = view.index()
        self.assertIs(type(response), httpexceptions.HTTPNotFound)
Пример #26
0
def test_blob(filename, content_type):
    from pyragit.views import blob

    data = b"some binary data"
    context = DummyResource(__name__=filename, data=data)
    request = DummyRequest()
    result = blob(context, request)
    assert result.headers["Content-Type"] == content_type
    assert result.headers["Accept-Ranges"] == "bite"
    assert list(result.app_iter) == [b"some binary data"]
Пример #27
0
    def test_install(self, mock_install_package):
        fake_response = HTTPOk
        mock_install_package.return_value = fake_response

        request = DummyRequest()
        request.context = DummyResource()
        request.params['package_name'] = 'dummy'

        response = CatalogItemViews(request).install()
        self.assertEqual(response, fake_response)
Пример #28
0
    def test_default_view(self):
        from views import ProjectorViews

        request = DummyRequest()
        title = "Dummy Context"
        context = DummyResource(title=title, __name__='dummy')
        inst = ProjectorViews(context, request)
        result = inst.default_view()
        self.assertEqual(result['page_title'], 'Dummy Context')
        self.assertEqual(result['parent_title'], 'None')
        self.assertEqual(result['name'], 'dummy')
Пример #29
0
 def test_addContact(self):
     context = DummyResource('contacts',
                             maxKey  = lambda: 1)
     newContact = MultiDict(name     = 'Tucker Clark',
                            address1 = '5/49a Bickley St',
                            address2 = 'Tooting',
                            city     = 'London',
                            postCode = 'SW17 9NF',
                            okBtn    = 'OK')
     request = DummyRequest(post=newContact)
     info = addContact(context, request)
     from pyramid.httpexceptions import HTTPFound
     self.assertIsInstance(info, HTTPFound)
Пример #30
0
    def test_get_404(self, mock_get_package):
        request = DummyRequest()
        request.context = DummyResource()
        request.matchdict['product_id'] = 'dummy'
        request.matchdict['package_id'] = 'test'
        mock_get_package_result = {
            'status': 404,
        }
        mock_get_package.return_value = mock_get_package_result
        view = package_views.PackageViews(request)

        response = view.get()
        self.assertIs(type(response), httpexceptions.HTTPNotFound)
Пример #31
0
    def test_forgot(self, submit, validate):
        from ...views.home import HomeView
        view = HomeView(DummyResource(), DummyRequest())
        response = view.forgot()
        self.assertIn('auth_url', response)

        submit.return_value = True
        validate.return_value = True
        response = view._forgot()
        self.assertIn('success_message', response)

        validate.return_value = False
        response = view._forgot()
        self.assertIn('error_message', response)
Пример #32
0
def test_get_sheet_interfaces_multiple_provided():
    from . import get_isheets
    from adhocracy_core.interfaces import ISheet
    from adhocracy_core.interfaces import IResource
    from pyramid.testing import DummyResource

    class IA(ISheet):
        pass

    class IB(ISheet):
        pass

    context = DummyResource(__provides__=(IResource, IA, IB))
    assert get_isheets(context) == [IA, IB]
Пример #33
0
    def test_index(self, mock_getlist):
        mock_package_list = {'status': 200, 'packages': [{'pkg1': 'test'}]}
        mock_getlist.return_value = mock_package_list
        request = DummyRequest()
        request.context = DummyResource()
        request.matchdict['product_id'] = 'dummy'
        request.params['filters'] = 'keyword'
        view = package_views.PackagesViews(request)

        response = view.index()
        self.assertEqual(type(response), dict)
        self.assertEqual(type(response['packages']), list)
        self.assertEqual(response['status'], 200)
        self.assertEqual(response, mock_package_list)