def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.request = self.layer['request']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        properties = {
            'email': '*****@*****.**',
            'username': '******',
            'fullname': 'Noam Avram Chomsky',
            'home_page': 'web.mit.edu/chomsky',
            'description': 'Professor of Linguistics',
            'location': 'Cambridge, MA'
        }
        self.user = api.user.create(email='*****@*****.**',
                                    username='******',
                                    properties=properties)

        self.gtool = api.portal.get_tool('portal_groups')
        properties = {
            'title': 'Plone Team',
            'description': 'We are Plone',
            'email': '*****@*****.**',
        }
        self.gtool.addGroup('ploneteam', (), (),
                            properties=properties,
                            title=properties['title'],
                            description=properties['description'])
        transaction.commit()
    def setUp(self):
        self.app = self.layer['app']
        self.request = self.layer['request']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        # Register custom UUID generator to produce stable UUIDs during tests
        pushGlobalRegistry(getSite())
        register_static_uuid_utility(prefix='SomeUUID')

        self.time_freezer = freeze_time("2016-10-21 19:00:00")
        self.frozen_time = self.time_freezer.start()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.document = self.create_document()

        transaction.commit()
        self.browser = Browser(self.app)
        self.browser.handleErrors = False
        self.browser.addHeader(
            'Authorization',
            'Basic %s:%s' % (SITE_OWNER_NAME, SITE_OWNER_PASSWORD,)
        )
    def test_list_users_without_being_manager(self):
        noam_api_session = RelativeSession(self.portal_url)
        noam_api_session.headers.update({'Accept': 'application/json'})
        noam_api_session.auth = ('noam', 'password')

        response = noam_api_session.get('/@users')
        self.assertEqual(response.status_code, 401)
Пример #4
0
    def setUp(self):
        if not HAS_AT:
            raise unittest.SkipTest("Skip tests if Archetypes is not present")
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        self.request = self.portal.REQUEST
        self.catalog = getToolByName(self.portal, "portal_catalog")

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        # /plone/folder
        with api.env.adopt_roles(["Manager"]):
            self.folder = api.content.create(
                type=u"ATTestFolder",
                id=u"folder",
                title=u"Some Folder",
                container=self.portal,
            )

            # /plone/folder/doc
            self.doc = api.content.create(
                container=self.folder,
                type=u"ATTestDocument",
                id="doc",
                title=u"Lorem Ipsum",
                start=DateTime(1950, 1, 1, 0, 0),
                effective=DateTime(1995, 1, 1, 0, 0),
                expires=DateTime(1999, 1, 1, 0, 0),
                testIntegerField=42,
                testLinesField=["Keyword1", "Keyword2", "Keyword3"],
                testBooleanField=True,
                testTextField=u"<p>Some Text</p>",
            )

            # /plone/folder/other-document
            self.doc2 = api.content.create(
                container=self.folder,
                type=u"ATTestDocument",
                id="other-document",
                title=u"Other Document",
                description=u"\xdcbersicht",
                start=DateTime(1975, 1, 1, 0, 0),
                effective=DateTime(2015, 1, 1, 0, 0),
                expires=DateTime(2020, 1, 1, 0, 0),
                testLinesField=["Keyword2", "Keyword3"],
                testBooleanField=False,
            )

            # /plone/doc-outside-folder
            api.content.create(
                container=self.portal,
                type=u"ATTestDocument",
                id="doc-outside-folder",
                title=u"Doc outside folder",
            )

        transaction.commit()
Пример #5
0
class TestContentTypesSchema(unittest.TestCase):
    layer = REDTURTLE_VOLTO_API_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

    def tearDown(self):
        self.api_session.close()

    def test_link_remoteUrl_return_proper_widget(self):
        response = self.api_session.get("/@types/Link")
        res = response.json()

        self.assertEqual(res["properties"]["remoteUrl"]["widget"], "url")

    def test_link_remoteUrl_return_proper_description(self):
        response = self.api_session.get("/@types/Link")
        res = response.json()

        self.assertEqual(
            res["properties"]["remoteUrl"]["description"],
            "Insert an external link directly into the field,or select an internal link clicking on the icon.",  # noqa
        )
Пример #6
0
    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        self.mailhost = getUtility(IMailHost)

        self.registry = getUtility(IRegistry)
        self.registry["plone.email_from_address"] = "*****@*****.**"
        self.registry["plone.email_from_name"] = "Plone test site"

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
        self.anon_api_session = RelativeSession(self.portal_url)
        self.anon_api_session.headers.update({"Accept": "application/json"})

        self.document = api.content.create(
            type="Document",
            title="Example context",
            container=self.portal,
        )
        self.document.blocks = {
            "text-id": {
                "@type": "text"
            },
            "form-id": {
                "@type": "form"
            },
        }
        self.document_url = self.document.absolute_url()
        transaction.commit()
    def setUp(self):
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        self.request = self.layer["request"]

        self.api_session = RelativeSession(self.portal_url, test=self)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        alsoProvides(self.layer["request"], IPloneAppMultilingualInstalled)
        login(self.portal, SITE_OWNER_NAME)
        self.en_content = createContentInContainer(self.portal["en"],
                                                   "Document",
                                                   title="Test document")
        self.es_content = createContentInContainer(self.portal["es"],
                                                   "Document",
                                                   title="Test document")
        ITranslationManager(self.en_content).register_translation(
            "es", self.es_content)
        self.folder = createContentInContainer(self.portal["es"],
                                               "Folder",
                                               id="folder",
                                               title="Some Folder")
        createContentInContainer(self.folder,
                                 "Document",
                                 id="doc1",
                                 title="A document")
        transaction.commit()
    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
    def setUp(self):
        tz = "Europe/Rome"
        os.environ["TZ"] = tz
        time.tzset()

        # Patch DateTime's timezone for deterministic behavior.
        self.DT_orig_localZone = DateTime.localZone
        DateTime.localZone = lambda cls=None, ltm=None: tz

        from plone.dexterity import content

        content.FLOOR_DATE = DateTime(1970, 0)
        content.CEILING_DATE = DateTime(2500, 0)
        self._orig_content_zone = content._zone
        content._zone = "GMT+2"

        registry = getUtility(IRegistry)
        registry["plone.portal_timezone"] = tz
        registry["plone.available_timezones"] = [tz]
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        commit()
Пример #10
0
class SocialSettingsControlpanelTest(unittest.TestCase):

    layer = VOLTO_SOCIALSETTINGS_API_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        setRoles(self.portal, TEST_USER_ID, ["Manager"])

    def test_controlpanel_exists(self):
        response = self.api_session.get("/@controlpanels/social-settings")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.headers.get("Content-Type"),
                         "application/json")

    def test_controlpanel_listed(self):
        response = self.api_session.get("/@controlpanels")

        titles = [x.get("title") for x in response.json()]
        self.assertIn("Social settings", titles)
Пример #11
0
    def setUp(self):
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url, test=self)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
Пример #12
0
class TestBatchingDXBase(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        self.request = self.portal.REQUEST

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

    def tearDown(self):
        self.api_session.close()

    def _create_doc(self, container, number):
        createContentInContainer(
            container,
            u"DXTestDocument",
            id="doc-%s" % str(number + 1),
            title=u"Document %s" % str(number + 1),
            created=DateTime(1975, 1, 1, 0, 0),
            effective=DateTime(2015, 1, 1, 0, 0),
            expires=DateTime(2020, 1, 1, 0, 0),
        )
Пример #13
0
    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        fti = queryUtility(IDexterityFTI, name='Document')
        behavior_list = [a for a in fti.behaviors]
        behavior_list.append('plone.tiles')
        behavior_list.append('plone.leadimage')
        fti.behaviors = tuple(behavior_list)

        self.doc = createContentInContainer(self.portal,
                                            u'Document',
                                            id=u'doc',
                                            title=u'A document')
        transaction.commit()

        sampleTileType = TileType(u'sample.tile',
                                  u'Sample tile',
                                  'cmf.ModifyPortalContent',
                                  'zope.Public',
                                  description=u'A tile used for testing',
                                  schema=ISampleTile,
                                  icon='testicon')
        provideUtility(sampleTileType, name=u'sample.tile')
        provideAdapter(SampleTile, (Interface, Interface),
                       IBasicTile,
                       name=u'sample.tile')
Пример #14
0
    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        api.user.create(
            email="*****@*****.**",
            username="******",
            password="******",
        )

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
        self.anon_api_session = RelativeSession(self.portal_url)
        self.anon_api_session.headers.update({"Accept": "application/json"})

        self.url = "{}/@subscriptions".format(self.portal_url)

        self.tool = getUtility(ISubscriptionsStore)
        self.id = self.tool.add(
            {
                "channels": ["foo"],
                "email": "*****@*****.**",
                "name": "John",
                "surname": "Doe",
                "phone": "123456",
            }, )
        transaction.commit()
Пример #15
0
class TestHistoryVersioning(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        self.portal.invokeFactory("Document",
                                  id="doc_with_history",
                                  title="My Document")
        self.doc = self.portal.doc_with_history
        self.doc.setTitle("Current version")

        transaction.commit()

    def tearDown(self):
        self.api_session.close()

    def test_response(self):
        response = self.api_session.get(self.doc.absolute_url())
        self.assertIn("version", response.json())
    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        self.anon_api_session = RelativeSession(self.portal_url)
        self.anon_api_session.headers.update({'Accept': 'application/json'})

        self.portal_actions = api.portal.get_tool(name='portal_actions')
        existing_ids = self.portal_actions.objectIds()
        self.portal_actions.manage_delObjects(ids=existing_ids)
        self.cat1 = self.add_category('category1')
        self.add_action(self.cat1,
                        'member_action',
                        'Members only',
                        available_expr='python:member is not None')
        self.add_action(self.cat1,
                        'view_action',
                        'Action with view permission',
                        permissions=(permissions.View, ))
        self.add_action(self.cat1,
                        'manage_action',
                        'Action with Manage Portal Content permission',
                        permissions=(permissions.ManagePortal, ))
        self.cat2 = self.add_category('category2')
        self.cat3 = self.add_category('category3')

        transaction.commit()
    def test_get_other_user_info_when_logged_in(self):
        noam_api_session = RelativeSession(self.portal_url)
        noam_api_session.headers.update({'Accept': 'application/json'})
        noam_api_session.auth = ('noam', 'password')

        response = noam_api_session.get('/@users/otheruser')
        self.assertEqual(response.status_code, 401)
Пример #18
0
    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        self.argomento = api.content.create(
            container=self.portal,
            type="Pagina Argomento",
            title="Argomento",
            icona="test-icon",
        )
        self.document = api.content.create(
            container=self.portal, type="Document", title="Document"
        )
        commit()
        intids = getUtility(IIntIds)

        self.document.correlato_in_evidenza = [
            RelationValue(intids.getId(self.argomento))
        ]
        commit()
Пример #19
0
class SerializerFunctionalTest(unittest.TestCase):
    """ """
    layer = PLONE_APP_JSON_FIELD_FUNCTIONAL_TESTING

    def setUp(self):
        """ """
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

    def test_serializer(self):
        """" """
        id_ = 'test-hospital'
        json_body = {
            '@type': 'TestToken',
            'title': 'Test Organization xxx',
            'id': id_
        }
        with open(os.path.join(FHIR_FIXTURE_PATH, 'Organization.json'), 'r') as f:
            json_body['resource'] = json.load(f)

        response = self.api_session.post(
             self.portal_url,
             json=json_body,
        )
        self.assertEqual(201, response.status_code)
        response = self.api_session.get(self.portal_url + '/' + id_)

        self.assertEqual(200, response.status_code)
        self.assertEqual(response.json()['resource']['resourceType'], json_body['resource']['resourceType'])
    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.request = self.layer['request']
        self.portal_url = self.portal.absolute_url()

        # Allow discussion
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IDiscussionSettings, check=False)
        settings.globally_enabled = True
        settings.edit_comment_enabled = True
        settings.delete_own_comment_enabled = True

        # doc with comments
        self.doc = api.content.create(container=self.portal,
                                      type='Document',
                                      id='doc_with_comments',
                                      title='Document with comments',
                                      allow_discussion=True)
        api.content.transition(self.doc, 'publish')

        api.user.create(username='******', password='******', email='*****@*****.**')

        # Admin session
        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        # User session
        self.user_session = RelativeSession(self.portal_url)
        self.user_session.headers.update({'Accept': 'application/json'})
        self.user_session.auth = ('jos', 'jos')

        transaction.commit()
Пример #21
0
class TestDatabaseServiceFunctional(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        self.request = self.portal.REQUEST
        self.catalog = getToolByName(self.portal, "portal_catalog")

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

    def tearDown(self):
        self.api_session.close()

    def test_get_system(self):
        response = self.api_session.get("/@database")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.headers.get("Content-Type"),
                         "application/json")

        results = response.json()
        self.assertEqual(results[u"@id"],
                         self.portal.absolute_url() + "/@database")
        self.assertTrue("cache_length" in results.keys())
        self.assertTrue("cache_length_bytes" in results.keys())
        self.assertTrue("cache_detail_length" in results.keys())
        self.assertTrue("cache_size" in results.keys())
        self.assertTrue("database_size" in results.keys())
        self.assertTrue("db_name" in results.keys())
        self.assertTrue("db_size" in results.keys())
Пример #22
0
    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        self.api_session = RelativeSession(self.portal_url, test=self)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        fti = queryUtility(IDexterityFTI, name="Document")
        behavior_list = [a for a in fti.behaviors]
        behavior_list.append("plone.leadimage")
        fti.behaviors = tuple(behavior_list)

        self.doc = createContentInContainer(self.portal,
                                            "Document",
                                            id="doc",
                                            title="A document")
        transaction.commit()

        sampleTileType = TileType(
            "sample.tile",
            "Sample tile",
            "cmf.ModifyPortalContent",
            "zope.Public",
            description="A tile used for testing",
            schema=ISampleTile,
            icon="testicon",
        )
        provideUtility(sampleTileType, name="sample.tile")
        provideAdapter(SampleTile, (Interface, Interface),
                       IBasicTile,
                       name="sample.tile")
    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        self.news_with_image = api.content.create(
            container=self.portal,
            type="News Item",
            title="News with image",
            description="",
        )

        self.news_without_image = api.content.create(
            container=self.portal,
            type="News Item",
            title="News without image",
            description="",
        )

        filename = os.path.join(os.path.dirname(__file__), u"logo.png")
        self.news_with_image.image = NamedBlobImage(
            data=open(filename, "rb").read(),
            filename="logo.png",
            contentType="image/png",
        )

        self.news_with_image.reindexObject()

        transaction.commit()
Пример #24
0
class TestTUSUploadWithCORS(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        provideAdapter(
            CORSTestPolicy,
            adapts=(Interface, IBrowserRequest),
            provides=ICORSPolicy,
        )
        self.portal = self.layer['portal']
        self.api_session = RelativeSession(self.portal.absolute_url())
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
        self.upload_url = '{}/@tus-upload'.format(self.portal.absolute_url())

    def test_cors_preflight_for_post_contains_tus_headers(self):
        response = self.api_session.options(
            self.upload_url,
            headers={
                'Origin': 'http://myhost.net',
                'Access-Control-Request-Method': 'POST',
                'Access-Control-Request-Headers':
                'Tus-Resumable,Upload-Length',
            })
        self.assertIn('Tus-Resumable', response.headers)
        self.assertIn('Tus-Version', response.headers)
        self.assertIn('Tus-Extension', response.headers)

    def test_cors_preflight_for_patch_contains_tus_headers(self):
        response = self.api_session.options(
            self.upload_url,
            headers={
                'Origin':
                'http://myhost.net',
                'Access-Control-Request-Method':
                'PATCH',
                'Access-Control-Request-Headers':
                'Content-Type,Tus-Resumable,Upload-Offset',
            })
        self.assertIn('Tus-Resumable', response.headers)
        self.assertIn('Tus-Version', response.headers)
        self.assertIn('Tus-Extension', response.headers)

    def test_cors_preflight_for_head_contains_tus_headers(self):
        response = self.api_session.options(
            self.upload_url,
            headers={
                'Origin': 'http://myhost.net',
                'Access-Control-Request-Method': 'HEAD',
                'Access-Control-Request-Headers': 'Tus-Resumable',
            })
        self.assertIn('Tus-Resumable', response.headers)
        self.assertIn('Tus-Version', response.headers)
        self.assertIn('Tus-Extension', response.headers)

    def tearDown(self):
        gsm = getGlobalSiteManager()
        gsm.unregisterAdapter(CORSTestPolicy, (Interface, IBrowserRequest),
                              ICORSPolicy)
Пример #25
0
    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        self.intids = getUtility(IIntIds)
        self.news = api.content.create(
            container=self.portal, type="News Item", title="TestNews"
        )
        self.luogo = api.content.create(
            container=self.portal,
            type="Venue",
            title="Luogo",
        )
        sede = RelationValue(self.intids.getId(self.luogo))

        self.uo = api.content.create(
            container=self.portal,
            type="UnitaOrganizzativa",
            title="TestUO",
            sede=[sede],
            city="Metropolis",
            zip_code="1234",
            street="whatever",
            contact_info={"blocks": {"xxx": {"foo": "bar"}}},
        )

        self.uo_child = api.content.create(
            container=self.uo,
            type="UnitaOrganizzativa",
            title="Child uo",
            city="Gotham City",
            zip_code="5678",
            street="somewhere",
            contact_info={"blocks": {"yyy": {"foo": "bar"}}},
        )

        self.service = api.content.create(
            container=self.portal,
            type="Servizio",
            title="TestService",
            ufficio_responsabile=[RelationValue(self.intids.getId(self.uo))],
        )

        self.bando = api.content.create(
            container=self.portal,
            type="Bando",
            title="TestBando",
            ufficio_responsabile=[RelationValue(self.intids.getId(self.uo))],
        )

        self.news.a_cura_di = [RelationValue(self.intids.getId(self.uo))]
        pcatalog = getToolByName(self.portal, "portal_catalog")
        pcatalog.manage_reindexIndex(ids=["ufficio_responsabile", "news_uo"])
        commit()
Пример #26
0
class TestRolesGet(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

    def test_roles_endpoint_lists_roles(self):
        response = self.api_session.get('/@roles')

        self.assertItemsEqual([{
            u'@id': self.portal_url + u'/@roles/Contributor',
            u'@type': u'role',
            u'id': u'Contributor',
            u'title': u'Contributor'
        }, {
            u'@id': self.portal_url + u'/@roles/Editor',
            u'@type': u'role',
            u'id': u'Editor',
            u'title': u'Editor'
        }, {
            u'@id': self.portal_url + u'/@roles/Member',
            u'@type': u'role',
            u'id': u'Member',
            u'title': u'Member'
        }, {
            u'@id': self.portal_url + u'/@roles/Reader',
            u'@type': u'role',
            u'id': u'Reader',
            u'title': u'Reader'
        }, {
            u'@id': self.portal_url + u'/@roles/Reviewer',
            u'@type': u'role',
            u'id': u'Reviewer',
            u'title': u'Reviewer'
        }, {
            u'@id': self.portal_url + u'/@roles/Site Administrator',
            u'@type': u'role',
            u'id': u'Site Administrator',
            u'title': u'Site Administrator'
        }, {
            u'@id': self.portal_url + u'/@roles/Manager',
            u'@type': u'role',
            u'id': u'Manager',
            u'title': u'Manager'
        }], response.json())

    def test_roles_endpoint_translates_role_titles(self):
        self.api_session.headers.update({'Accept-Language': 'de'})
        response = self.api_session.get('/@roles')

        self.assertItemsEqual([
            u'Hinzuf\xfcgen', u'Bearbeiten', u'Benutzer', u'Ansehen',
            u'Ver\xf6ffentlichen', u'Website-Administrator', u'Verwalten'
        ], [item['title'] for item in response.json()])
Пример #27
0
    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.request = self.layer["request"]
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ["Manager"])

        self.api_session = RelativeSession(self.portal_url, test=self)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        self.gtool = api.portal.get_tool("portal_groups")
        properties = {
            "title": "Plone Team",
            "description": "We are Plone",
            "email": "*****@*****.**",
        }
        self.gtool.addGroup(
            "ploneteam",
            (),
            (),
            properties=properties,
            title=properties["title"],
            description=properties["description"],
        )
        transaction.commit()
Пример #28
0
    def setUp(self):
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
    def setUp(self):
        self.app = self.layer['app']
        self.request = self.layer['request']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.portal.invokeFactory('Document', id='front-page')
        self.document = self.portal['front-page']
        self.document.title = u"Welcome to Plone"
        self.document.description = \
            u"Congratulations! You have successfully installed Plone."
        self.document.text = RichTextValue(
            u"If you're seeing this instead of the web site you were " +
            u"expecting, the owner of this web site has just installed " +
            u"Plone. Do not contact the Plone Team or the Plone mailing " +
            u"lists about this.", 'text/plain', 'text/html')
        self.document.creation_date = DateTime('2016-01-21T01:14:48+00:00')
        self.document.modification_date = DateTime('2016-01-21T01:24:11+00:00')
        IMutableUUID(self.document).set('1f699ffa110e45afb1ba502f75f7ec33')
        import transaction
        transaction.commit()
        self.browser = Browser(self.app)
        self.browser.handleErrors = False
        self.browser.addHeader(
            'Authorization', 'Basic %s:%s' % (
                SITE_OWNER_NAME,
                SITE_OWNER_PASSWORD,
            ))
Пример #30
0
    def setUp(self):
        if not HAS_AT:
            raise unittest.SkipTest('Skip tests if Archetypes is not present')
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        self.request = self.portal.REQUEST
        self.catalog = getToolByName(self.portal, 'portal_catalog')

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        # /plone/folder
        with api.env.adopt_roles(['Manager']):
            self.folder = api.content.create(
                type=u'ATTestFolder',
                id=u'folder',
                title=u'Some Folder',
                container=self.portal,
            )

            # /plone/folder/doc
            self.doc = api.content.create(
                container=self.folder,
                type=u'ATTestDocument',
                id='doc',
                title=u'Lorem Ipsum',
                start=DateTime(1950, 1, 1, 0, 0),
                effective=DateTime(1995, 1, 1, 0, 0),
                expires=DateTime(1999, 1, 1, 0, 0),
                testIntegerField=42,
                testLinesField=['Keyword1', 'Keyword2', 'Keyword3'],
                testBooleanField=True,
                testTextField=u'<p>Some Text</p>',
            )

            # /plone/folder/other-document
            self.doc2 = api.content.create(
                container=self.folder,
                type=u'ATTestDocument',
                id='other-document',
                title=u'Other Document',
                description=u'\xdcbersicht',
                start=DateTime(1975, 1, 1, 0, 0),
                effective=DateTime(2015, 1, 1, 0, 0),
                expires=DateTime(2020, 1, 1, 0, 0),
                testLinesField=['Keyword2', 'Keyword3'],
                testBooleanField=False,
            )

            # /plone/doc-outside-folder
            api.content.create(
                container=self.portal,
                type=u'ATTestDocument',
                id='doc-outside-folder',
                title=u'Doc outside folder',
            )

        transaction.commit()
Пример #31
0
class TestRegistry(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        registry = getUtility(IRegistry)
        record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum")
        registry.records['foo.bar'] = record
        transaction.commit()

    def test_get_registry_record(self):
        response = self.api_session.get('/@registry/foo.bar')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), 'Lorem Ipsum')

    def test_update_registry_record(self):
        registry = getUtility(IRegistry)
        payload = {'foo.bar': 'lorem ipsum'}
        response = self.api_session.patch('/@registry', json=payload)
        transaction.commit()

        self.assertEqual(response.status_code, 204)
        self.assertEqual(registry['foo.bar'], 'lorem ipsum')

    def test_update_several_registry_records(self):
        registry = getUtility(IRegistry)
        record = Record(field.TextLine(title=u"Foo Bar Baz"),
                        u"Lorem Ipsum Dolor")
        registry.records['foo.bar.baz'] = record
        transaction.commit()
        payload = {'foo.bar': 'lorem ipsum',
                   'foo.bar.baz': 'lorem ipsum dolor'}
        response = self.api_session.patch('/@registry', json=payload)
        transaction.commit()

        self.assertEqual(response.status_code, 204)
        self.assertEqual(registry['foo.bar'], 'lorem ipsum')
        self.assertEqual(registry['foo.bar.baz'], 'lorem ipsum dolor')

    def test_update_non_existing_registry_record(self):
        payload = {'foo.bar.baz': 'lorem ipsum'}
        response = self.api_session.patch('/@registry', json=payload)
        self.assertEqual(response.status_code, 500)
Пример #32
0
    def setUp(self):
        self.app = self.layer['app']
        self.request = self.layer['request']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.portal.invokeFactory('Document', id='front-page')
        self.document = self.portal['front-page']
        self.document.title = u"Welcome to Plone"
        self.document.description = \
            u"Congratulations! You have successfully installed Plone."
        self.document.text = RichTextValue(
            u"If you're seeing this instead of the web site you were " +
            u"expecting, the owner of this web site has just installed " +
            u"Plone. Do not contact the Plone Team or the Plone mailing " +
            u"lists about this.",
            'text/plain',
            'text/html'
        )
        import transaction
        transaction.commit()
        self.browser = Browser(self.app)
        self.browser.handleErrors = False
        self.browser.addHeader(
            'Authorization',
            'Basic %s:%s' % (SITE_OWNER_NAME, SITE_OWNER_PASSWORD,)
        )
Пример #33
0
    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (TEST_USER_NAME, TEST_USER_PASSWORD)
Пример #34
0
class TestPermissions(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (TEST_USER_NAME, TEST_USER_PASSWORD)

    def test_anonymous_allowed_to_use_api_by_default(self):
        setRoles(self.portal, TEST_USER_ID, ['Anonymous'])
        transaction.commit()

        response = self.api_session.get(self.portal_url)
        self.assertEqual(response.status_code, 200)

    def test_authenticated_allowed_to_use_api_by_default(self):
        setRoles(self.portal, TEST_USER_ID, ['Authenticated'])
        transaction.commit()

        response = self.api_session.get(self.portal_url)
        self.assertEqual(response.status_code, 200)

    def test_manager_allowed_to_use_api_by_default(self):
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        transaction.commit()

        response = self.api_session.get(self.portal_url)
        self.assertEqual(response.status_code, 200)

    def test_unauthorized_if_missing_permission(self):
        # Unmap the 'plone.restapi: Use REST API'
        # permission from any roles
        self.portal.manage_permission(UseRESTAPI, roles=[])
        transaction.commit()

        response = self.api_session.get(self.portal_url)
        self.assertEqual(response.status_code, 401)
        self.assertDictContainsSubset(
            {u'type': u'Unauthorized',
             u'message': u"Missing 'plone.restapi: Use REST API' permission"},
            response.json())
Пример #35
0
    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        self.request = self.portal.REQUEST
        self.catalog = getToolByName(self.portal, 'portal_catalog')

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        # /plone/folder
        self.folder = createContentInContainer(
            self.portal, u'Folder',
            id=u'folder',
            title=u'Some Folder')

        # /plone/folder/doc
        self.doc = createContentInContainer(
            self.folder, u'DXTestDocument',
            id='doc',
            title=u'Lorem Ipsum',
            created=DateTime(1950, 1, 1, 0, 0),
            effective=DateTime(1995, 1, 1, 0, 0),
            expires=DateTime(1999, 1, 1, 0, 0),
            test_int_field=42,
            test_list_field=['Keyword1', 'Keyword2', 'Keyword3'],
            test_bool_field=True,
        )
        IMutableUUID(self.doc).set('77779ffa110e45afb1ba502f75f77777')
        self.doc.reindexObject()

        # /plone/folder/other-document
        self.doc2 = createContentInContainer(
            self.folder, u'DXTestDocument',
            id='other-document',
            title=u'Other Document',
            description=u'\xdcbersicht',
            created=DateTime(1975, 1, 1, 0, 0),
            effective=DateTime(2015, 1, 1, 0, 0),
            expires=DateTime(2020, 1, 1, 0, 0),
            test_list_field=['Keyword2', 'Keyword3'],
            test_bool_field=False,
        )

        # /plone/doc-outside-folder
        createContentInContainer(
            self.portal, u'DXTestDocument',
            id='doc-outside-folder',
            title=u'Doc outside folder',
        )

        transaction.commit()
Пример #36
0
    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        registry = getUtility(IRegistry)
        record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum")
        registry.records['foo.bar'] = record
        transaction.commit()
Пример #37
0
    def setUp(self):
        self.app = self.layer["app"]
        self.request = self.layer["request"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        setRoles(self.portal, TEST_USER_ID, ["Manager"])
        self.portal.invokeFactory("Document", id="document1")
        self.document = self.portal.document1
        self.document_url = self.document.absolute_url()
        self.portal.invokeFactory("Folder", id="folder1")
        self.folder = self.portal.folder1
        self.folder_url = self.folder.absolute_url()
        transaction.commit()
Пример #38
0
    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        self.folder = createContentInContainer(
            self.portal, u'Folder',
            id=u'folder',
            title=u'Some Folder')
        createContentInContainer(
            self.folder, u'Document',
            id=u'doc1',
            title=u'A document')
        transaction.commit()
Пример #39
0
    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        self.request = self.portal.REQUEST

        setRoles(self.portal, TEST_USER_ID, ['Member', 'Contributor'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        folder = self.portal[self.portal.invokeFactory(
            'Folder',
            id='folder',
            title='Some Folder',
        )]

        for i in range(5):
            self._create_doc(folder, i)
        transaction.commit()
Пример #40
0
    def setUp(self):
        self.app = self.layer['app']
        self.request = self.layer['request']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        self.time_freezer = freeze_time("2016-10-21 19:00:00")
        self.frozen_time = self.time_freezer.start()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.portal.invokeFactory('Document', id='front-page')
        self.document = self.portal['front-page']
        self.document.title = u"Welcome to Plone"
        self.document.description = \
            u"Congratulations! You have successfully installed Plone."
        self.document.text = RichTextValue(
            u"If you're seeing this instead of the web site you were " +
            u"expecting, the owner of this web site has just installed " +
            u"Plone. Do not contact the Plone Team or the Plone mailing " +
            u"lists about this.",
            'text/plain',
            'text/html'
        )
        self.document.creation_date = DateTime('2016-01-21T01:14:48+00:00')
        IMutableUUID(self.document).set('1f699ffa110e45afb1ba502f75f7ec33')
        self.document.reindexObject()
        self.document.modification_date = DateTime('2016-01-21T01:24:11+00:00')
        import transaction
        transaction.commit()
        self.browser = Browser(self.app)
        self.browser.handleErrors = False
        self.browser.addHeader(
            'Authorization',
            'Basic %s:%s' % (SITE_OWNER_NAME, SITE_OWNER_PASSWORD,)
        )
Пример #41
0
    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        properties = {
            'email': '*****@*****.**',
            'username': '******',
            'fullname': 'Noam Avram Chomsky',
            'home_page': 'web.mit.edu/chomsky',
            'description': 'Professor of Linguistics',
            'location': 'Cambridge, MA'
        }
        api.user.create(
            email='*****@*****.**',
            username='******',
            properties=properties
        )
        transaction.commit()
Пример #42
0
class TestComponents(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        self.folder = createContentInContainer(
            self.portal, u'Folder',
            id=u'folder',
            title=u'Some Folder')
        createContentInContainer(
            self.folder, u'Document',
            id=u'doc1',
            title=u'A document')
        transaction.commit()

    def test_breadcrumb(self):
        response = self.api_session.get('/folder/doc1/@components/breadcrumbs')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.json(),
            [{
                u'@id': u'http://*****:*****@components/breadcrumbs',
                u'items': [{
                    u'url': u'http://*****:*****@components/navigation')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.json(),
            [{
                u'@id': u'http://*****:*****@components/'
                'navigation',
                u'items': [{
                    u'title': u'Home',
                    u'url': u'http://localhost:55001/plone'
                }, {
                    u'title': u'Some Folder',
                    u'url': u'http://localhost:55001/plone/folder'
                }],
            }]
        )
Пример #43
0
class TestTraversal(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.request = self.layer['request']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        self.time_freezer = freeze_time("2016-10-21 19:00:00")
        self.frozen_time = self.time_freezer.start()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.portal.invokeFactory('Document', id='front-page')
        self.document = self.portal['front-page']
        self.document.title = u"Welcome to Plone"
        self.document.description = \
            u"Congratulations! You have successfully installed Plone."
        self.document.text = RichTextValue(
            u"If you're seeing this instead of the web site you were " +
            u"expecting, the owner of this web site has just installed " +
            u"Plone. Do not contact the Plone Team or the Plone mailing " +
            u"lists about this.",
            'text/plain',
            'text/html'
        )
        self.document.creation_date = DateTime('2016-01-21T01:14:48+00:00')
        IMutableUUID(self.document).set('1f699ffa110e45afb1ba502f75f7ec33')
        self.document.reindexObject()
        self.document.modification_date = DateTime('2016-01-21T01:24:11+00:00')
        import transaction
        transaction.commit()
        self.browser = Browser(self.app)
        self.browser.handleErrors = False
        self.browser.addHeader(
            'Authorization',
            'Basic %s:%s' % (SITE_OWNER_NAME, SITE_OWNER_PASSWORD,)
        )

    def tearDown(self):
        self.time_freezer.stop()

    def test_documentation_document(self):
        response = self.api_session.get(self.document.absolute_url())
        save_response_for_documentation('document.json', response)

    def test_documentation_news_item(self):
        self.portal.invokeFactory('News Item', id='newsitem')
        self.portal.newsitem.title = 'My News Item'
        self.portal.newsitem.description = u'This is a news item'
        self.portal.newsitem.text = RichTextValue(
            u"Lorem ipsum",
            'text/plain',
            'text/html'
        )
        image_file = os.path.join(os.path.dirname(__file__), u'image.png')
        self.portal.newsitem.image = NamedBlobImage(
            data=open(image_file, 'r').read(),
            contentType='image/png',
            filename=u'image.png'
        )
        self.portal.newsitem.image_caption = u'This is an image caption.'
        self.portal.newsitem.creation_date = DateTime(
            '2016-01-21T02:14:48+00:00')
        self.portal.newsitem.modification_date = DateTime(
            '2016-01-21T02:24:11+00:00')
        IMutableUUID(self.portal.newsitem).set(
            '80c2a074cb4240d08c9a129e3a834c74')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.newsitem.absolute_url())
        save_response_for_documentation('newsitem.json', response)

    def test_documentation_event(self):
        self.portal.invokeFactory('Event', id='event')
        self.portal.event.title = 'Event'
        self.portal.event.description = u'This is an event'
        self.portal.event.start = datetime(2013, 1, 1, 10, 0)
        self.portal.event.end = datetime(2013, 1, 1, 12, 0)
        self.portal.event.creation_date = DateTime('2016-01-21T03:14:48+00:00')
        self.portal.event.modification_date = DateTime(
            '2016-01-21T03:24:11+00:00')
        IMutableUUID(self.portal.event).set('846d632bc0854c5aa6d3dcae171ed2db')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.event.absolute_url())
        save_response_for_documentation('event.json', response)

    def test_documentation_link(self):
        self.portal.invokeFactory('Link', id='link')
        self.portal.link.title = 'My Link'
        self.portal.link.description = u'This is a link'
        self.portal.remoteUrl = 'http://plone.org'
        self.portal.link.creation_date = DateTime('2016-01-21T04:14:48+00:00')
        self.portal.link.modification_date = DateTime(
            '2016-01-21T04:24:11+00:00')
        IMutableUUID(self.portal.link).set('6ff48d27762143a0ae8d63cee73d9fc2')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.link.absolute_url())
        save_response_for_documentation('link.json', response)

    def test_documentation_file(self):
        self.portal.invokeFactory('File', id='file')
        self.portal.file.title = 'My File'
        self.portal.file.description = u'This is a file'
        pdf_file = os.path.join(
            os.path.dirname(__file__), u'file.pdf'
        )
        self.portal.file.file = NamedBlobFile(
            data=open(pdf_file, 'r').read(),
            contentType='application/pdf',
            filename=u'file.pdf'
        )
        self.portal.file.creation_date = DateTime('2016-01-21T05:14:48+00:00')
        self.portal.file.modification_date = DateTime(
            '2016-01-21T05:24:11+00:00')
        IMutableUUID(self.portal.file).set('9b6a4eadb9074dde97d86171bb332ae9')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.file.absolute_url())
        save_response_for_documentation('file.json', response)

    def test_documentation_image(self):
        self.portal.invokeFactory('Image', id='image')
        self.portal.image.title = 'My Image'
        self.portal.image.description = u'This is an image'
        image_file = os.path.join(os.path.dirname(__file__), u'image.png')
        self.portal.image.image = NamedBlobImage(
            data=open(image_file, 'r').read(),
            contentType='image/png',
            filename=u'image.png'
        )
        self.portal.image.creation_date = DateTime('2016-01-21T06:14:48+00:00')
        self.portal.image.modification_date = DateTime(
            '2016-01-21T06:24:11+00:00')
        IMutableUUID(self.portal.image).set('2166e81a0c224fe3b62e197c7fdc9c3e')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.image.absolute_url())
        save_response_for_documentation('image.json', response)

    def test_documentation_folder(self):
        self.portal.invokeFactory('Folder', id='folder')
        self.portal.folder.title = 'My Folder'
        self.portal.folder.description = u'This is a folder with two documents'
        self.portal.folder.invokeFactory(
            'Document',
            id='doc1',
            title='A document within a folder'
        )
        self.portal.folder.invokeFactory(
            'Document',
            id='doc2',
            title='A document within a folder'
        )
        self.portal.folder.creation_date = DateTime(
            '2016-01-21T07:14:48+00:00')
        self.portal.folder.modification_date = DateTime(
            '2016-01-21T07:24:11+00:00')
        IMutableUUID(self.portal.folder).set(
            'fc7881c46d61452db4177bc059d9dcb5')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.folder.absolute_url())
        save_response_for_documentation('folder.json', response)

    def test_documentation_collection(self):
        self.portal.invokeFactory('Collection', id='collection')
        self.portal.collection.title = 'My Collection'
        self.portal.collection.description = \
            u'This is a collection with two documents'
        self.portal.collection.query = [{
            'i': 'portal_type',
            'o': 'plone.app.querystring.operation.string.is',
            'v': 'Document',
        }]
        self.portal.invokeFactory(
            'Document',
            id='doc1',
            title='Document 1'
        )
        self.portal.invokeFactory(
            'Document',
            id='doc2',
            title='Document 2'
        )
        self.portal.collection.creation_date = DateTime(
            '2016-01-21T08:14:48+00:00')
        self.portal.collection.modification_date = DateTime(
            '2016-01-21T08:24:11+00:00')
        IMutableUUID(self.portal.collection).set(
            'd0c89bc77f874ce1aad5720921d875c0')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.collection.absolute_url())
        save_response_for_documentation('collection.json', response)

    def test_documentation_siteroot(self):
        response = self.api_session.get(self.portal.absolute_url())
        save_response_for_documentation('siteroot.json', response)

    def test_documentation_404_not_found(self):
        response = self.api_session.get('non-existing-resource')
        save_response_for_documentation('404_not_found.json', response)

    def test_documentation_search(self):
        query = {'sort_on': 'path'}
        response = self.api_session.get('/@search', params=query)
        save_response_for_documentation('search.json', response)

    def test_documentation_workflow(self):
        response = self.api_session.get(
            '{}/@workflow'.format(self.document.absolute_url()))
        save_response_for_documentation('workflow_get.json', response)

    def test_documentation_workflow_transition(self):
        self.frozen_time.tick(timedelta(minutes=5))
        response = self.api_session.post(
            '{}/@workflow/publish'.format(self.document.absolute_url()))
        save_response_for_documentation('workflow_post.json', response)

    def test_documentation_registry_get(self):
        response = self.api_session.get(
            '/@registry/plone.app.querystring.field.path.title')
        save_response_for_documentation('registry_get.json', response)

    def test_documentation_registry_update(self):
        response = self.api_session.patch(
            '/@registry/',
            json={'plone.app.querystring.field.path.title': 'Value'})
        save_response_for_documentation('registry_update.json', response)

    def test_documentation_types(self):
        response = self.api_session.get('/@types')
        save_response_for_documentation('types.json', response)

    def test_documentation_types_document(self):
        response = self.api_session.get('@types/Document')
        save_response_for_documentation('types_document.json', response)

    def test_documentation_login(self):
        self.portal.acl_users.jwt_auth._secret = 'secret'
        self.portal.acl_users.jwt_auth.use_keyring = False
        self.portal.acl_users.jwt_auth.token_timeout = 0
        import transaction
        transaction.commit()
        self.api_session.auth = None
        response = self.api_session.post(
            '{}/@login'.format(self.portal.absolute_url()),
            json={'login': SITE_OWNER_NAME, 'password': SITE_OWNER_PASSWORD})
        save_response_for_documentation('login.json', response)

    def test_documentation_login_renew(self):
        self.portal.acl_users.jwt_auth._secret = 'secret'
        self.portal.acl_users.jwt_auth.use_keyring = False
        self.portal.acl_users.jwt_auth.token_timeout = 0
        import transaction
        transaction.commit()
        self.api_session.auth = None
        response = self.api_session.post(
            '{}/@login'.format(self.portal.absolute_url()),
            json={'login': SITE_OWNER_NAME, 'password': SITE_OWNER_PASSWORD})
        token = json.loads(response.content)['token']
        response = self.api_session.post(
            '{}/@login-renew'.format(self.portal.absolute_url()),
            headers={'Authorization': 'Bearer {}'.format(token)})
        save_response_for_documentation('login_renew.json', response)

    def test_documentation_logout(self):
        self.portal.acl_users.jwt_auth._secret = 'secret'
        self.portal.acl_users.jwt_auth.use_keyring = False
        self.portal.acl_users.jwt_auth.token_timeout = 0
        self.portal.acl_users.jwt_auth.store_tokens = True
        import transaction
        transaction.commit()
        self.api_session.auth = None
        response = self.api_session.post(
            '{}/@login'.format(self.portal.absolute_url()),
            json={'login': SITE_OWNER_NAME, 'password': SITE_OWNER_PASSWORD})
        token = json.loads(response.content)['token']
        response = self.api_session.post(
            '{}/@logout'.format(self.portal.absolute_url()),
            headers={'Authorization': 'Bearer {}'.format(token)})
        save_response_for_documentation('logout.json', response)

    def test_documentation_batching(self):
        folder = self.portal[self.portal.invokeFactory(
            'Folder',
            id='folder',
            title='Folder'
        )]
        for i in range(7):
            folder.invokeFactory(
                'Document',
                id='doc-%s' % str(i + 1),
                title='Document %s' % str(i + 1)
            )
        transaction.commit()

        query = {'sort_on': 'path'}
        response = self.api_session.get(
            '/folder/@search?b_size=5', params=query)
        save_response_for_documentation('batching.json', response)

    def test_documentation_users(self):
        test_user = api.user.get(username=TEST_USER_ID)
        properties = {
            "description": "This is a test user",
            "email": "*****@*****.**",
            "fullname": "Test User",
            "home_page": "http://www.example.com",
            "location": "Bonn",
            "username": "******"
        }
        test_user.setMemberProperties(mapping=properties)
        admin = api.user.get(username='******')
        properties = {
            "description": "This is an admin user",
            "email": "*****@*****.**",
            "fullname": "Administrator",
            "home_page": "http://www.example.com",
            "location": "Berlin",
            "username": "******"
        }
        admin.setMemberProperties(mapping=properties)
        transaction.commit()
        response = self.api_session.get('/@users')
        save_response_for_documentation('users.json', response)

    def test_documentation_users_get(self):
        properties = {
            'email': '*****@*****.**',
            'username': '******',
            'fullname': 'Noam Avram Chomsky',
            'home_page': 'web.mit.edu/chomsky',
            'description': 'Professor of Linguistics',
            'location': 'Cambridge, MA'
        }
        api.user.create(
            email='*****@*****.**',
            username='******',
            properties=properties
        )
        transaction.commit()
        response = self.api_session.get('@users/noam')
        save_response_for_documentation('users_get.json', response)

    def test_documentation_users_filtered_get(self):
        properties = {
            'email': '*****@*****.**',
            'username': '******',
            'fullname': 'Noam Avram Chomsky',
            'home_page': 'web.mit.edu/chomsky',
            'description': 'Professor of Linguistics',
            'location': 'Cambridge, MA'
        }
        api.user.create(
            email='*****@*****.**',
            username='******',
            properties=properties
        )
        transaction.commit()
        response = self.api_session.get('@users', params={'query': 'noa'})
        save_response_for_documentation('users_filtered_by_username.json', response)  # noqa

    def test_documentation_users_created(self):
        response = self.api_session.post(
            '/@users',
            json={
                'username': '******',
                'email': '*****@*****.**',
                'password': '******',
                'username': '******',
                'fullname': 'Noam Avram Chomsky',
                'home_page': 'web.mit.edu/chomsky',
                'description': 'Professor of Linguistics',
                'location': 'Cambridge, MA'
            },
        )
        save_response_for_documentation('users_created.json', response)

    def test_documentation_users_update(self):
        properties = {
            'email': '*****@*****.**',
            'username': '******',
            'fullname': 'Noam Avram Chomsky',
            'home_page': 'web.mit.edu/chomsky',
            'description': 'Professor of Linguistics',
            'location': 'Cambridge, MA'
        }
        api.user.create(
            email='*****@*****.**',
            username='******',
            properties=properties
        )
        transaction.commit()

        response = self.api_session.patch(
            '/@users/noam',
            json={
                'email': '*****@*****.**',
            },
        )
        save_response_for_documentation('users_update.json', response)

    def test_documentation_users_delete(self):
        properties = {
            'email': '*****@*****.**',
            'username': '******',
            'fullname': 'Noam Avram Chomsky',
            'home_page': 'web.mit.edu/chomsky',
            'description': 'Professor of Linguistics',
            'location': 'Cambridge, MA'
        }
        api.user.create(
            email='*****@*****.**',
            username='******',
            properties=properties
        )
        transaction.commit()

        response = self.api_session.delete(
            '/@users/noam')
        save_response_for_documentation('users_delete.json', response)

    def test_documentation_breadcrumbs(self):
        response = self.api_session.get(
            '{}/@components/breadcrumbs'.format(self.document.absolute_url()))
        save_response_for_documentation('breadcrumbs.json', response)

    def test_documentation_navigation(self):
        response = self.api_session.get(
            '{}/@components/navigation'.format(self.document.absolute_url()))
        save_response_for_documentation('navigation.json', response)
Пример #44
0
class TestTraversal(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.request = self.layer['request']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.portal.invokeFactory('Document', id='front-page')
        self.document = self.portal['front-page']
        self.document.title = u"Welcome to Plone"
        self.document.description = \
            u"Congratulations! You have successfully installed Plone."
        self.document.text = RichTextValue(
            u"If you're seeing this instead of the web site you were " +
            u"expecting, the owner of this web site has just installed " +
            u"Plone. Do not contact the Plone Team or the Plone mailing " +
            u"lists about this.",
            'text/plain',
            'text/html'
        )
        self.document.creation_date = DateTime('2016-01-21T01:14:48+00:00')
        IMutableUUID(self.document).set('1f699ffa110e45afb1ba502f75f7ec33')
        self.document.reindexObject()
        self.document.modification_date = DateTime('2016-01-21T01:24:11+00:00')
        import transaction
        transaction.commit()
        self.browser = Browser(self.app)
        self.browser.handleErrors = False
        self.browser.addHeader(
            'Authorization',
            'Basic %s:%s' % (SITE_OWNER_NAME, SITE_OWNER_PASSWORD,)
        )

    def test_documentation_document(self):
        response = self.api_session.get(self.document.absolute_url())
        save_response_for_documentation('document.json', response)

    def test_documentation_news_item(self):
        self.portal.invokeFactory('News Item', id='newsitem')
        self.portal.newsitem.title = 'My News Item'
        self.portal.newsitem.description = u'This is a news item'
        self.portal.newsitem.text = RichTextValue(
            u"Lorem ipsum",
            'text/plain',
            'text/html'
        )
        image_file = os.path.join(os.path.dirname(__file__), u'image.png')
        self.portal.newsitem.image = NamedBlobImage(
            data=open(image_file, 'r').read(),
            contentType='image/png',
            filename=u'image.png'
        )
        self.portal.newsitem.image_caption = u'This is an image caption.'
        self.portal.newsitem.creation_date = DateTime(
            '2016-01-21T02:14:48+00:00')
        self.portal.newsitem.modification_date = DateTime(
            '2016-01-21T02:24:11+00:00')
        IMutableUUID(self.portal.newsitem).set(
            '80c2a074cb4240d08c9a129e3a834c74')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.newsitem.absolute_url())
        save_response_for_documentation('newsitem.json', response)

    def test_documentation_event(self):
        self.portal.invokeFactory('Event', id='event')
        self.portal.event.title = 'Event'
        self.portal.event.description = u'This is an event'
        self.portal.event.start = datetime(2013, 1, 1, 10, 0)
        self.portal.event.end = datetime(2013, 1, 1, 12, 0)
        self.portal.event.creation_date = DateTime('2016-01-21T03:14:48+00:00')
        self.portal.event.modification_date = DateTime(
            '2016-01-21T03:24:11+00:00')
        IMutableUUID(self.portal.event).set('846d632bc0854c5aa6d3dcae171ed2db')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.event.absolute_url())
        save_response_for_documentation('event.json', response)

    def test_documentation_link(self):
        self.portal.invokeFactory('Link', id='link')
        self.portal.link.title = 'My Link'
        self.portal.link.description = u'This is a link'
        self.portal.remoteUrl = 'http://plone.org'
        self.portal.link.creation_date = DateTime('2016-01-21T04:14:48+00:00')
        self.portal.link.modification_date = DateTime(
            '2016-01-21T04:24:11+00:00')
        IMutableUUID(self.portal.link).set('6ff48d27762143a0ae8d63cee73d9fc2')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.link.absolute_url())
        save_response_for_documentation('link.json', response)

    def test_documentation_file(self):
        self.portal.invokeFactory('File', id='file')
        self.portal.file.title = 'My File'
        self.portal.file.description = u'This is a file'
        pdf_file = os.path.join(
            os.path.dirname(__file__), u'file.pdf'
        )
        self.portal.file.file = NamedBlobFile(
            data=open(pdf_file, 'r').read(),
            contentType='application/pdf',
            filename=u'file.pdf'
        )
        intids = getUtility(IIntIds)
        file_id = intids.getId(self.portal.file)
        self.portal.file.file = RelationValue(file_id)
        self.portal.file.creation_date = DateTime('2016-01-21T05:14:48+00:00')
        self.portal.file.modification_date = DateTime(
            '2016-01-21T05:24:11+00:00')
        IMutableUUID(self.portal.file).set('9b6a4eadb9074dde97d86171bb332ae9')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.file.absolute_url())
        save_response_for_documentation('file.json', response)

    def test_documentation_image(self):
        self.portal.invokeFactory('Image', id='image')
        self.portal.image.title = 'My Image'
        self.portal.image.description = u'This is an image'
        image_file = os.path.join(os.path.dirname(__file__), u'image.png')
        self.portal.image.image = NamedBlobImage(
            data=open(image_file, 'r').read(),
            contentType='image/png',
            filename=u'image.png'
        )
        self.portal.image.creation_date = DateTime('2016-01-21T06:14:48+00:00')
        self.portal.image.modification_date = DateTime(
            '2016-01-21T06:24:11+00:00')
        IMutableUUID(self.portal.image).set('2166e81a0c224fe3b62e197c7fdc9c3e')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.image.absolute_url())
        save_response_for_documentation('image.json', response)

    def test_documentation_folder(self):
        self.portal.invokeFactory('Folder', id='folder')
        self.portal.folder.title = 'My Folder'
        self.portal.folder.description = u'This is a folder with two documents'
        self.portal.folder.invokeFactory(
            'Document',
            id='doc1',
            title='A document within a folder'
        )
        self.portal.folder.invokeFactory(
            'Document',
            id='doc2',
            title='A document within a folder'
        )
        self.portal.folder.creation_date = DateTime(
            '2016-01-21T07:14:48+00:00')
        self.portal.folder.modification_date = DateTime(
            '2016-01-21T07:24:11+00:00')
        IMutableUUID(self.portal.folder).set(
            'fc7881c46d61452db4177bc059d9dcb5')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.folder.absolute_url())
        save_response_for_documentation('folder.json', response)

    def test_documentation_collection(self):
        self.portal.invokeFactory('Collection', id='collection')
        self.portal.collection.title = 'My Collection'
        self.portal.collection.description = \
            u'This is a collection with two documents'
        self.portal.collection.query = [{
            'i': 'portal_type',
            'o': 'plone.app.querystring.operation.string.is',
            'v': 'Document',
        }]
        self.portal.invokeFactory(
            'Document',
            id='doc1',
            title='Document 1'
        )
        self.portal.invokeFactory(
            'Document',
            id='doc2',
            title='Document 2'
        )
        self.portal.collection.creation_date = DateTime(
            '2016-01-21T08:14:48+00:00')
        self.portal.collection.modification_date = DateTime(
            '2016-01-21T08:24:11+00:00')
        IMutableUUID(self.portal.collection).set(
            'd0c89bc77f874ce1aad5720921d875c0')
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.collection.absolute_url())
        save_response_for_documentation('collection.json', response)

    def test_documentation_siteroot(self):
        response = self.api_session.get(self.portal.absolute_url())
        save_response_for_documentation('siteroot.json', response)

    def test_documentation_404_not_found(self):
        response = self.api_session.get('non-existing-resource')
        save_response_for_documentation('404_not_found.json', response)

    def test_documentation_search(self):
        query = {'sort_on': 'path'}
        response = self.api_session.get('/search', params=query)
        save_response_for_documentation('search.json', response)
Пример #45
0
class TestBatchingArchetypes(unittest.TestCase):

    layer = PLONE_RESTAPI_AT_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        self.request = self.portal.REQUEST

        setRoles(self.portal, TEST_USER_ID, ['Member', 'Contributor'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        folder = self.portal[self.portal.invokeFactory(
            'Folder',
            id='folder',
            title='Some Folder',
        )]

        for i in range(5):
            self._create_doc(folder, i)
        transaction.commit()

    def _create_doc(self, container, number):
        container.invokeFactory(
            'Document',
            id='doc-%s' % str(number + 1),
            title='Document %s' % str(number + 1),
        )

    def test_contains_canonical_url(self):
        # Fetch the second page of the batch
        response = self.api_session.get('/folder?b_start=2&b_size=2')

        # Response should contain canonical URL without batching params
        self.assertEqual(
            response.json()['@id'],
            self.portal_url + '/folder')

    def test_contains_batching_links(self):
        # Fetch the second page of the batch
        response = self.api_session.get('/folder?b_start=2&b_size=2')

        # Batch info in response should contain appropriate batching links
        batch_info = response.json()['batching']

        self.assertDictEqual(
            {u'@id': self.portal_url + '/folder?b_start=2&b_size=2',
             u'first': self.portal_url + '/folder?b_size=2&b_start=0',
             u'next': self.portal_url + '/folder?b_size=2&b_start=4',
             u'prev': self.portal_url + '/folder?b_size=2&b_start=0',
             u'last': self.portal_url + '/folder?b_size=2&b_start=4',
             },
            batch_info)

    def test_contains_correct_batch_of_items(self):
        # Fetch the second page of the batch
        response = self.api_session.get('/folder?b_start=2&b_size=2')

        # Response should contain second batch of items
        self.assertEquals([
            u'/plone/folder/doc-3',
            u'/plone/folder/doc-4'],
            result_paths(response.json()))

    def test_total_item_count_is_correct(self):
        # Fetch the second page of the batch
        response = self.api_session.get('/folder?b_start=2&b_size=2')

        # Total count of items should be in items_total
        self.assertEqual(5, response.json()['items_total'])

    def test_batching_links_omitted_if_resulset_fits_in_single_batch(self):
        response = self.api_session.get('/folder?b_size=100')
        self.assertNotIn('batching', response.json().keys())
Пример #46
0
class TestUsersEndpoint(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        properties = {
            'email': '*****@*****.**',
            'username': '******',
            'fullname': 'Noam Avram Chomsky',
            'home_page': 'web.mit.edu/chomsky',
            'description': 'Professor of Linguistics',
            'location': 'Cambridge, MA'
        }
        api.user.create(
            email='*****@*****.**',
            username='******',
            properties=properties
        )
        transaction.commit()

    def test_list_users(self):
        response = self.api_session.get('/@users')

        self.assertEqual(200, response.status_code)
        self.assertEqual(3, len(response.json()))
        user_ids = [user['id'] for user in response.json()]
        self.assertIn('admin', user_ids)
        self.assertIn('test_user_1_', user_ids)
        self.assertIn('noam', user_ids)
        noam = [x for x in response.json() if x.get('username') == 'noam'][0]
        self.assertEqual('noam', noam.get('id'))
        self.assertEqual(
            self.portal.absolute_url() + '/@users/noam',
            noam.get('@id')
        )
        self.assertEqual('*****@*****.**', noam.get('email'))
        self.assertEqual('Noam Avram Chomsky', noam.get('fullname'))
        self.assertEqual('web.mit.edu/chomsky', noam.get('home_page'))  # noqa
        self.assertEqual('Professor of Linguistics', noam.get('description'))  # noqa
        self.assertEqual('Cambridge, MA', noam.get('location'))

    def test_add_user(self):
        response = self.api_session.post(
            '/@users',
            json={
                "username": "******",
                "email": "*****@*****.**",
                "password": "******"
            },
        )
        transaction.commit()

        self.assertEqual(201, response.status_code)
        howard = api.user.get(userid='howard')
        self.assertEqual(
            "*****@*****.**", howard.getProperty('email')
        )

    def test_add_user_username_is_required(self):
        response = self.api_session.post(
            '/@users',
            json={
                "password": "******"
            },
        )
        transaction.commit()

        self.assertEqual(400, response.status_code)
        self.assertTrue('"Property \'username\' is required' in response.text)

    def test_add_user_password_is_required(self):
        response = self.api_session.post(
            '/@users',
            json={
                "username": "******"
            },
        )
        transaction.commit()

        self.assertEqual(400, response.status_code)
        self.assertTrue('"Property \'password\' is required' in response.text)

    def test_add_user_email_is_required_if_email_login_is_enabled(self):
        # enable use_email_as_login
        security_settings = getAdapter(self.portal, ISecuritySchema)
        security_settings.use_email_as_login = True
        transaction.commit()
        response = self.api_session.post(
            '/@users',
            json={
                "username": "******",
                "password": "******"
            },
        )

        self.assertEqual(400, response.status_code)
        self.assertTrue('"Property \'email\' is required' in response.text)

    def test_add_user_email_with_email_login_enabled(self):
        # enable use_email_as_login
        security_settings = getAdapter(self.portal, ISecuritySchema)
        security_settings.use_email_as_login = True
        transaction.commit()
        response = self.api_session.post(
            '/@users',
            json={
                "email": "*****@*****.**",
                "password": "******"
            },
        )
        transaction.commit()

        self.assertEqual(201, response.status_code)
        self.assertTrue(api.user.get(userid='*****@*****.**'))

    def test_add_user_with_email_login_enabled(self):
        # enable use_email_as_login
        security_settings = getAdapter(self.portal, ISecuritySchema)
        security_settings.use_email_as_login = True
        transaction.commit()
        response = self.api_session.post(
            '/@users',
            json={
                "username": "******",
                "email": "*****@*****.**",
                "password": "******"
            },
        )
        transaction.commit()

        self.assertEqual(201, response.status_code)
        user = api.user.get(userid='*****@*****.**')
        self.assertTrue(user)
        self.assertEqual('*****@*****.**', user.getUserName())
        self.assertEqual('*****@*****.**', user.getProperty('email'))

    def test_get_user(self):
        response = self.api_session.get('/@users/noam')

        self.assertEqual(response.status_code, 200)
        self.assertEqual('noam', response.json().get('id'))
        self.assertEqual(
            self.portal.absolute_url() + '/@users/noam',
            response.json().get('@id')
        )
        self.assertEqual(
            '*****@*****.**',
            response.json().get('email')
        )
        self.assertEqual('Noam Avram Chomsky', response.json().get('fullname'))
        self.assertEqual('web.mit.edu/chomsky', response.json().get('home_page'))  # noqa
        self.assertEqual('Professor of Linguistics', response.json().get('description'))  # noqa
        self.assertEqual('Cambridge, MA', response.json().get('location'))

    def test_get_search_user_with_filter(self):
        response = self.api_session.post(
            '/@users',
            json={
                "username": "******",
                "email": "*****@*****.**",
                "password": "******"
            },
        )
        transaction.commit()
        response = self.api_session.get('/@users', params={'query': 'noa'})

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json()), 1)
        self.assertEqual('noam', response.json()[0].get('id'))
        self.assertEqual(
            self.portal.absolute_url() + '/@users/noam',
            response.json()[0].get('@id')
        )
        self.assertEqual(
            '*****@*****.**',
            response.json()[0].get('email')
        )
        self.assertEqual('Noam Avram Chomsky', response.json()[0].get('fullname'))  # noqa

        response = self.api_session.get('/@users', params={'query': 'howa'})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json()), 1)
        self.assertEqual('howard', response.json()[0].get('id'))

    def test_get_non_existing_user(self):
        response = self.api_session.get('/@users/non-existing-user')

        self.assertEqual(response.status_code, 404)

    def test_update_user(self):
        payload = {
            'fullname': 'Noam A. Chomsky',
            'username': '******',
            'email': '*****@*****.**'
        }
        response = self.api_session.patch('/@users/noam', json=payload)
        transaction.commit()

        self.assertEqual(response.status_code, 204)
        noam = api.user.get(userid='noam')
        self.assertEqual('noam', noam.getUserId())  # user id never changes
        self.assertEqual('avram', noam.getUserName())
        self.assertEqual('Noam A. Chomsky', noam.getProperty('fullname'))
        self.assertEqual(
            '*****@*****.**',
            noam.getProperty('email')
        )

    def test_update_user_password(self):
        old_password_hashes = dict(
            self.portal.acl_users.source_users._user_passwords
        )
        payload = {'password': '******'}
        self.api_session.patch('/@users/noam', json=payload)
        transaction.commit()

        new_password_hashes = dict(
            self.portal.acl_users.source_users._user_passwords
        )
        self.assertNotEqual(
            old_password_hashes['noam'], new_password_hashes['noam']
        )

    def test_delete_user(self):
        response = self.api_session.delete('/@users/noam')
        transaction.commit()

        self.assertEqual(response.status_code, 204)
        self.assertEqual(None, api.user.get(userid='noam'))

    def test_delete_non_existing_user(self):
        response = self.api_session.delete('/@users/non-existing-user')
        transaction.commit()

        self.assertEqual(response.status_code, 404)
Пример #47
0
class TestTraversal(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

    def test_get_document(self):
        self.portal.invokeFactory(
            'Document',
            id='doc1',
            title='My Document'
        )
        self.portal.doc1.description = u"This is a document"
        self.portal.doc1.text = RichTextValue(
            u"Lorem ipsum",
            'text/plain',
            'text/html'
        )
        transaction.commit()

        response = self.api_session.get(self.portal.doc1.absolute_url())

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'),
            'application/json',
            'When sending a GET request with Accept: application/json ' +
            'the server should respond with sending back application/json: ' +
            '{}'.format(response.headers.get('Content-Type'))
        )
        self.assertEqual(
            'Document',
            response.json().get('@type'),
            "Response should be @type 'Document', not '{}'".format(
                response.json().get('@type')
            )
        )
        self.assertEqual(
            response.json().get('@id'),
            self.portal.doc1.absolute_url(),
            '@id attribute != {}: {}'.format(
                self.portal.doc1.absolute_url(),
                response.json()
            )
        )
        self.assertEqual(
            'My Document',
            response.json().get('title'),
        )
        self.assertEqual(
            'This is a document',
            response.json().get('description')
        )
        self.assertEqual(
            '<p>Lorem ipsum</p>',
            response.json().get('text')
        )

    def test_get_news_item(self):
        self.portal.invokeFactory(
            'News Item',
            id='news1',
            title='News Item 1'
        )
        image_file = os.path.join(os.path.dirname(__file__), u'image.png')
        self.portal.news1.image = NamedBlobImage(
            data=open(image_file, 'r').read(),
            contentType='image/png',
            filename=u'image.png'
        )
        self.portal.news1.image_caption = u'This is an image caption.'
        transaction.commit()

        response = self.api_session.get(self.portal.news1.absolute_url())

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'),
            'application/json',
            'When sending a GET request with Content-Type: application/json ' +
            'the server should respond with sending back application/json.'
        )
        self.assertEqual(
            'News Item',
            response.json().get('@type'),
            "Response should be @type 'News Item', not '{}'".format(
                response.json().get('@type')
            )
        )
        self.assertEqual(
            response.json().get('@id'),
            self.portal.news1.absolute_url()
        )
        self.assertEqual(
            'News Item 1',
            response.json().get('title')
        )
        self.assertEqual(
            u'This is an image caption.',
            response.json()['image_caption']
        )
        self.assertDictContainsSubset(
            {'original': u'http://*****:*****@@images/image'},
            response.json()['image']
        )

    def test_get_folder(self):
        self.portal.invokeFactory(
            'Folder',
            id='folder1',
            title='My Folder'
        )
        transaction.commit()

        response = self.api_session.get(self.portal.folder1.absolute_url())

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'),
            'application/json',
            'When sending a GET request with Content-Type: application/json ' +
            'the server should respond with sending back application/json.'
        )
        self.assertEqual(
            'Folder',
            response.json().get('@type'),
            "Response should be @type 'Folder', not '{}'".format(
                response.json().get('@type')
            )
        )
        self.assertEqual(
            self.portal.folder1.absolute_url(),
            response.json().get('@id')
        )
        self.assertEqual(
            'My Folder',
            response.json().get('title')
        )

    def test_get_site_root(self):
        response = self.api_session.get(self.portal_url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'),
            'application/json',
            'When sending a GET request with Content-Type: application/json ' +
            'the server should respond with sending back application/json.'
        )
        self.assertEqual(
            self.portal_url,
            response.json().get('@id')
        )
        self.assertEqual(
            'SiteRoot',
            response.json().get('@type')
        )

    def test_get_site_root_with_default_page(self):
        self.portal.invokeFactory('Document', id='front-page')
        self.portal.setDefaultPage('front-page')
        transaction.commit()

        response = self.api_session.get(self.portal_url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'),
            'application/json',
            'When sending a GET request with Content-Type: application/json ' +
            'the server should respond with sending back application/json.'
        )
        self.assertEqual(
            response.json().get('@id'),
            self.portal_url
        )
        self.assertEqual(
            'SiteRoot',
            response.json().get('@type')
        )

    @unittest.skip('Not implemented yet.')
    def test_get_file(self):  # pragma: no cover
        self.portal.invokeFactory('File', id='file1')
        self.portal.file1.title = 'File'
        self.portal.file1.description = u'A file'
        pdf_file = os.path.join(
            os.path.dirname(__file__), u'file.pdf'
        )
        self.portal.file1.file = NamedBlobFile(
            data=open(pdf_file, 'r').read(),
            contentType='application/pdf',
            filename=u'file.pdf'
        )
        intids = getUtility(IIntIds)
        file_id = intids.getId(self.portal.file1)
        self.portal.file1.file = RelationValue(file_id)
        transaction.commit()

        response = self.api_session.get(self.portal.file1.absolute_url())

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'),
            'application/json',
            'When sending a GET request with Content-Type: application/json ' +
            'the server should respond with sending back application/json.'
        )
        self.assertEqual(
            response.json()['@id'],
            self.portal.file1.absolute_url()
        )

    @unittest.skip('Not implemented yet.')
    def test_get_image(self):  # pragma: no cover
        self.portal.invokeFactory('Image', id='img1')
        self.portal.img1.title = 'Image'
        self.portal.img1.description = u'An image'
        image_file = os.path.join(os.path.dirname(__file__), u'image.png')
        self.portal.img1.image = NamedBlobImage(
            data=open(image_file, 'r').read(),
            contentType='image/png',
            filename=u'image.png'
        )
        transaction.commit()

        response = self.api_session.get(self.portal.img1.absolute_url())
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'),
            'application/json',
            'When sending a GET request with Content-Type: application/json ' +
            'the server should respond with sending back application/json.'
        )
        self.assertEqual(
            response.json()['@id'],
            self.portal.img1.absolute_url()
        )
Пример #48
0
class TestSearchFunctional(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        self.request = self.portal.REQUEST
        self.catalog = getToolByName(self.portal, 'portal_catalog')

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        # /plone/folder
        self.folder = createContentInContainer(
            self.portal, u'Folder',
            id=u'folder',
            title=u'Some Folder')

        # /plone/folder/doc
        self.doc = createContentInContainer(
            self.folder, u'DXTestDocument',
            id='doc',
            title=u'Lorem Ipsum',
            created=DateTime(1950, 1, 1, 0, 0),
            effective=DateTime(1995, 1, 1, 0, 0),
            expires=DateTime(1999, 1, 1, 0, 0),
            test_int_field=42,
            test_list_field=['Keyword1', 'Keyword2', 'Keyword3'],
            test_bool_field=True,
        )
        IMutableUUID(self.doc).set('77779ffa110e45afb1ba502f75f77777')
        self.doc.reindexObject()

        # /plone/folder/other-document
        self.doc2 = createContentInContainer(
            self.folder, u'DXTestDocument',
            id='other-document',
            title=u'Other Document',
            description=u'\xdcbersicht',
            created=DateTime(1975, 1, 1, 0, 0),
            effective=DateTime(2015, 1, 1, 0, 0),
            expires=DateTime(2020, 1, 1, 0, 0),
            test_list_field=['Keyword2', 'Keyword3'],
            test_bool_field=False,
        )

        # /plone/doc-outside-folder
        createContentInContainer(
            self.portal, u'DXTestDocument',
            id='doc-outside-folder',
            title=u'Doc outside folder',
        )

        transaction.commit()

    def test_overall_response_format(self):
        response = self.api_session.get('/search')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'),
            'application/json',
        )

        results = response.json()
        self.assertEqual(
            results[u'items_count'],
            len(results[u'member']),
            'items_count property should match actual item count.'
        )

    def test_search_on_context_constrains_query_by_path(self):
        response = self.api_session.get('/folder/search')

        self.assertSetEqual(
            {u'/plone/folder',
             u'/plone/folder/doc',
             u'/plone/folder/other-document'},
            set(result_paths(response.json())))

    def test_partial_metadata_retrieval(self):
        query = {'SearchableText': 'lorem',
                 'metadata_fields': ['portal_type', 'review_state']}
        response = self.api_session.get('/search', params=query)

        self.assertDictContainsSubset(
            {u'@id': u'http://*****:*****@id': u'http://localhost:55001/plone/folder/doc',
             u'Creator': u'test_user_1_',
             u'Description': u'',
             u'EffectiveDate': u'None',
             u'ExpirationDate': u'None',
             u'Subject': [],
             u'Title': u'Lorem Ipsum',
             u'Type': u'DX Test Document',
             u'UID': u'77779ffa110e45afb1ba502f75f77777',
             u'author_name': None,
             u'cmf_uid': None,
             u'commentators': [],
             u'description': u'',
             u'effective': u'1995-01-01T00:00:00+00:00',
             u'end': None,
             u'exclude_from_nav': False,
             u'expires': u'1999-01-01T00:00:00+00:00',
             u'getId': u'doc',
             u'getObjSize': u'0 KB',
             u'getPath': u'/plone/folder/doc',
             u'getRemoteUrl': None,
             u'getURL': u'http://localhost:55001/plone/folder/doc',
             u'id': u'doc',
             u'in_response_to': None,
             u'is_folderish': False,
             u'last_comment_date': None,
             u'listCreators': [u'test_user_1_'],
             u'location': None,
             u'meta_type': u'Dexterity Item',
             u'portal_type': u'DXTestDocument',
             u'review_state': u'private',
             u'start': None,
             u'sync_uid': None,
             u'title': u'Lorem Ipsum',
             u'total_comments': 0},
            response.json()['member'][0])

    # ZCTextIndex

    def test_fulltext_search(self):
        query = {'SearchableText': 'lorem'}
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder/doc'],
            result_paths(response.json())
        )

    def test_fulltext_search_with_non_ascii_characters(self):
        query = {'SearchableText': u'\xfcbersicht'}
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder/other-document'],
            result_paths(response.json())
        )

    # KeywordIndex

    def test_keyword_index_str_query(self):
        query = {'test_list_field': 'Keyword1'}
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder/doc'],
            result_paths(response.json())
        )

    def test_keyword_index_str_query_or(self):
        query = {'test_list_field': ['Keyword2', 'Keyword3']}
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder/doc',
             u'/plone/folder/other-document'],
            result_paths(response.json())
        )

    def test_keyword_index_str_query_and(self):
        query = {
            'test_list_field.query': ['Keyword1', 'Keyword2'],
            'test_list_field.operator': 'and',
        }
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder/doc'],
            result_paths(response.json())
        )

    def test_keyword_index_int_query(self):
        self.doc.test_list_field = [42, 23]
        self.doc.reindexObject()
        transaction.commit()

        query = {'test_list_field:int': 42}
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder/doc'],
            result_paths(response.json())
        )

    # BooleanIndex

    def test_boolean_index_query(self):
        query = {'test_bool_field': True, 'portal_type': 'DXTestDocument'}
        response = self.api_session.get('/folder/search', params=query)
        self.assertEqual(
            [u'/plone/folder/doc'],
            result_paths(response.json())
        )

        query = {'test_bool_field': False, 'portal_type': 'DXTestDocument'}
        response = self.api_session.get('/folder/search', params=query)
        self.assertEqual(
            [u'/plone/folder/other-document'],
            result_paths(response.json())
        )

    # FieldIndex

    def test_field_index_int_query(self):
        query = {'test_int_field:int': 42}
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder/doc'],
            result_paths(response.json())
        )

    def test_field_index_int_range_query(self):
        query = {
            'test_int_field.query:int': [41, 43],
            'test_int_field.range': 'min:max',
        }
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder/doc'],
            result_paths(response.json())
        )

    # ExtendedPathIndex

    def test_extended_path_index_query(self):
        query = {'path': '/'.join(self.folder.getPhysicalPath())}

        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder',
             u'/plone/folder/doc',
             u'/plone/folder/other-document'],
            result_paths(response.json())
        )

    def test_extended_path_index_depth_limiting(self):
        lvl1 = createContentInContainer(self.portal, u'Folder', id=u'lvl1')
        lvl2 = createContentInContainer(lvl1, u'Folder', id=u'lvl2')
        createContentInContainer(lvl2, u'Folder', id=u'lvl3')
        transaction.commit()

        path = '/plone/lvl1'

        # Depth 0 - only object identified by path
        query = {'path.query': path, 'path.depth': 0}
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/lvl1'],
            result_paths(response.json()))

        # Depth 1 - immediate children
        query = {'path.query': path, 'path.depth': 1}
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/lvl1/lvl2'],
            result_paths(response.json()))

        # No depth - object itself and all children
        query = {'path': path}
        response = self.api_session.get('/search', params=query)

        self.assertSetEqual(
            {u'/plone/lvl1', u'/plone/lvl1/lvl2', u'/plone/lvl1/lvl2/lvl3'},
            set(result_paths(response.json())))

    # DateIndex

    def test_date_index_query(self):
        query = {'created': date(1950, 1, 1).isoformat()}
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder/doc'],
            result_paths(response.json())
        )

    def test_date_index_ranged_query(self):
        query = {
            'created.query': [date(1949, 1, 1).isoformat(),
                              date(1951, 1, 1).isoformat()],
            'created.range': 'min:max',
        }
        response = self.api_session.get('/search', params=query)

        self.assertEqual(
            [u'/plone/folder/doc'],
            result_paths(response.json())
        )

    # DateRangeIndex

    def test_date_range_index_query(self):
        query = {'effectiveRange': date(1997, 1, 1).isoformat()}
        response = self.api_session.get('/folder/search', params=query)

        self.assertEqual(
            [u'/plone/folder',
             u'/plone/folder/doc'],
            result_paths(response.json())
        )

    # DateRecurringIndex

    def test_date_recurring_index_query(self):
        from datetime import datetime
        createContentInContainer(
            self.folder, u'Event', id=u'event',
            title=u'Event',
            start=datetime(2013, 1, 1, 0, 0),
            end=datetime(2013, 1, 1, 23, 59),
            whole_day=True,
            recurrence='FREQ=DAILY;COUNT=10;INTERVAL=2',
            timezone='UTC',
        )
        import transaction
        transaction.commit()

        # First occurrence
        query = {'start': date(2013, 1, 1).isoformat()}
        response = self.api_session.get('/folder/search', params=query)

        self.assertEqual(
            [u'/plone/folder/event'],
            result_paths(response.json())
        )

        # No event that day
        query = {'start': date(2013, 1, 2).isoformat()}
        response = self.api_session.get('/folder/search', params=query)

        self.assertEqual(
            [],
            result_paths(response.json())
        )

        # Second occurrence
        query = {'start': date(2013, 1, 3).isoformat()}
        response = self.api_session.get('/folder/search', params=query)

        self.assertEqual(
            [u'/plone/folder/event'],
            result_paths(response.json())
        )

        # Ranged query
        query = {
            'start.query': [date(2013, 1, 1).isoformat(),
                            date(2013, 1, 5).isoformat()],
            'start.range': 'min:max',
        }
        response = self.api_session.get('/folder/search', params=query)

        self.assertEqual(
            [u'/plone/folder/event'],
            result_paths(response.json())
        )

    # UUIDIndex

    def test_uuid_index_query(self):
        IMutableUUID(self.doc).set('7777a074cb4240d08c9a129e3a837777')
        self.doc.reindexObject()
        transaction.commit()

        query = {'UID': '7777a074cb4240d08c9a129e3a837777'}
        response = self.api_session.get('/search', params=query)
        self.assertEqual(
            [u'/plone/folder/doc'],
            result_paths(response.json())
        )
Пример #49
0
class TestTraversal(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.request = self.layer['request']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.portal.invokeFactory('Document', id='front-page')
        self.document = self.portal['front-page']
        self.document.title = u"Welcome to Plone"
        self.document.description = \
            u"Congratulations! You have successfully installed Plone."
        self.document.text = RichTextValue(
            u"If you're seeing this instead of the web site you were " +
            u"expecting, the owner of this web site has just installed " +
            u"Plone. Do not contact the Plone Team or the Plone mailing " +
            u"lists about this.",
            'text/plain',
            'text/html'
        )
        import transaction
        transaction.commit()
        self.browser = Browser(self.app)
        self.browser.handleErrors = False
        self.browser.addHeader(
            'Authorization',
            'Basic %s:%s' % (SITE_OWNER_NAME, SITE_OWNER_PASSWORD,)
        )

    def test_documentation_document(self):
        response = self.api_session.get(self.document.absolute_url())
        save_response_for_documentation('document.json', response)

    def test_documentation_news_item(self):
        self.portal.invokeFactory('News Item', id='newsitem')
        self.portal.newsitem.title = 'My News Item'
        self.portal.newsitem.description = u'This is a news item'
        self.portal.newsitem.text = RichTextValue(
            u"Lorem ipsum",
            'text/plain',
            'text/html'
        )
        image_file = os.path.join(os.path.dirname(__file__), u'image.png')
        self.portal.newsitem.image = NamedBlobImage(
            data=open(image_file, 'r').read(),
            contentType='image/png',
            filename=u'image.png'
        )
        self.portal.newsitem.image_caption = u'This is an image caption.'
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.newsitem.absolute_url())
        save_response_for_documentation('newsitem.json', response)

    def test_documentation_event(self):
        self.portal.invokeFactory('Event', id='event')
        self.portal.event.title = 'Event'
        self.portal.event.description = u'This is an event'
        self.portal.event.start = datetime(2013, 1, 1, 10, 0)
        self.portal.event.end = datetime(2013, 1, 1, 12, 0)
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.event.absolute_url())
        save_response_for_documentation('event.json', response)

    def test_documentation_link(self):
        self.portal.invokeFactory('Link', id='link')
        self.portal.link.title = 'My Link'
        self.portal.link.description = u'This is a link'
        self.portal.remoteUrl = 'http://plone.org'
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.link.absolute_url())
        save_response_for_documentation('link.json', response)

    def test_documentation_file(self):
        self.portal.invokeFactory('File', id='file')
        self.portal.file.title = 'My File'
        self.portal.file.description = u'This is a file'
        pdf_file = os.path.join(
            os.path.dirname(__file__), u'file.pdf'
        )
        self.portal.file.file = NamedBlobFile(
            data=open(pdf_file, 'r').read(),
            contentType='application/pdf',
            filename=u'file.pdf'
        )
        intids = getUtility(IIntIds)
        file_id = intids.getId(self.portal.file)
        self.portal.file.file = RelationValue(file_id)
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.file.absolute_url())
        save_response_for_documentation('file.json', response)

    def test_documentation_image(self):
        self.portal.invokeFactory('Image', id='image')
        self.portal.image.title = 'My Image'
        self.portal.image.description = u'This is an image'
        image_file = os.path.join(os.path.dirname(__file__), u'image.png')
        self.portal.image.image = NamedBlobImage(
            data=open(image_file, 'r').read(),
            contentType='image/png',
            filename=u'image.png'
        )
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.image.absolute_url())
        save_response_for_documentation('image.json', response)

    def test_documentation_folder(self):
        self.portal.invokeFactory('Folder', id='folder')
        self.portal.folder.title = 'My Folder'
        self.portal.folder.description = u'This is a folder with two documents'
        self.portal.folder.invokeFactory(
            'Document',
            id='doc1',
            title='A document within a folder'
        )
        self.portal.folder.invokeFactory(
            'Document',
            id='doc2',
            title='A document within a folder'
        )
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.folder.absolute_url())
        save_response_for_documentation('folder.json', response)

    def test_documentation_collection(self):
        self.portal.invokeFactory('Collection', id='collection')
        self.portal.collection.title = 'My Collection'
        self.portal.collection.description = \
            u'This is a collection with two documents'
        self.portal.collection.query = [{
            'i': 'portal_type',
            'o': 'plone.app.querystring.operation.string.is',
            'v': 'Document',
        }]
        self.portal.invokeFactory(
            'Document',
            id='doc1',
            title='Document 1'
        )
        self.portal.invokeFactory(
            'Document',
            id='doc2',
            title='Document 2'
        )
        import transaction
        transaction.commit()
        response = self.api_session.get(self.portal.collection.absolute_url())
        save_response_for_documentation('collection.json', response)

    def test_documentation_siteroot(self):
        response = self.api_session.get(self.portal.absolute_url())
        save_response_for_documentation('siteroot.json', response)

    def test_documentation_404_not_found(self):
        response = self.api_session.get('non-existing-resource')
        save_response_for_documentation('404_not_found.json', response)
Пример #50
0
class TestServicesTypes(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer['app']
        self.portal = self.layer['portal']
        self.portal_url = self.portal.absolute_url()
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({'Accept': 'application/json'})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

    def test_get_types(self):
        response = self.api_session.get(
            '{}/@types'.format(self.portal.absolute_url())
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'),
            'application/json',
            'Sending a GET request to @types endpoint should respond with ' +
            'Content-Type: "application/json", not ' +
            '"{}"'.format(response.headers.get('Content-Type'))
        )
        self.assertTrue(
            {
                u'@id': u'http://*****:*****@types/Collection',
                u'title': u'Collection'
            } in response.json()
        )
        self.assertTrue(
            {
                u'@id': u'http://*****:*****@types/Discussion Item',
                u'title': u'Discussion Item'
            } in response.json()
        )
        self.assertTrue(
            {
                u'@id': u'http://*****:*****@types/Event',
                u'title': u'Event'
            } in response.json()
        )
        self.assertTrue(
            {
                u'@id': u'http://*****:*****@types/File',
                u'title': u'File'
            } in response.json()
        )
        self.assertTrue(
            {
                u'@id': u'http://*****:*****@types/Folder',
                u'title': u'Folder'
            } in response.json()
        )
        self.assertTrue(
            {
                u'@id': u'http://*****:*****@types/Image',
                u'title': u'Image'
            } in response.json()
        )

    def test_get_types_document(self):
        response = self.api_session.get(
            '{}/@types/Document'.format(self.portal.absolute_url())
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.headers.get('Content-Type'),
            'application/json+schema',
            'Sending a GET request to @types endpoint should respond with ' +
            'Content-Type: "application/json+schema", not ' +
            '"{}"'.format(response.headers.get('Content-Type'))
        )

    def test_get_types_with_unknown_type(self):
        response = self.api_session.get(
            '{}/@types/UnknownType'.format(self.portal.absolute_url())
        )

        self.assertEqual(response.status_code, 404)
        self.assertEqual(
            'application/json',
            response.headers.get('Content-Type'),
            'Sending a GET request to @types endpoint should respond with ' +
            'Content-Type: "application/json", not ' +
            '"{}"'.format(response.headers.get('Content-Type'))
        )
Пример #51
0
class TestErrorHandling(unittest.TestCase):

    layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer["app"]
        self.request = self.layer["request"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()

        self.api_session = RelativeSession(self.portal_url)
        self.api_session.headers.update({"Accept": "application/json"})
        self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

        setRoles(self.portal, TEST_USER_ID, ["Manager"])
        self.portal.invokeFactory("Document", id="document1")
        self.document = self.portal.document1
        self.document_url = self.document.absolute_url()
        self.portal.invokeFactory("Folder", id="folder1")
        self.folder = self.portal.folder1
        self.folder_url = self.folder.absolute_url()
        transaction.commit()

    @unittest.skip("Not working since we moved to plone.rest")
    def test_404_not_found(self):
        response = self.api_session.get("non-existing-resource")
        self.assertEqual(response.status_code, 404)
        self.assertEqual(
            response.headers.get("Content-Type"),
            "application/json",
            "When sending a GET request with Accept: application/json "
            + "the server should respond with sending back application/json.",
        )
        self.assertTrue(json.loads(response.content))
        self.assertEqual("NotFound", response.json()["type"])

    @unittest.skip("Not working since we moved to plone.rest")
    def test_401_unauthorized(self):
        response = self.api_session.get(self.document_url)
        self.assertEqual(response.status_code, 401)
        self.assertEqual(
            response.headers.get("Content-Type"),
            "application/json",
            "When sending a GET request with Accept: application/json "
            + "the server should respond with sending back application/json.",
        )
        self.assertTrue(json.loads(response.content))
        self.assertEqual("Unauthorized", response.json()["type"])

    @unittest.skip("Not working since we moved to plone.rest")
    def test_500_internal_server_error(self):
        provideAdapter(
            InternalServerErrorView,
            adapts=(Interface, IBrowserRequest),
            provides=Interface,
            name="internal_server_error",
        )
        import transaction

        transaction.commit()

        response = self.api_session.get("internal_server_error")

        self.assertEqual(response.status_code, 500)
        self.assertEqual(
            response.headers.get("Content-Type"),
            "application/json",
            "When sending a GET request with Accept: application/json "
            + "the server should respond with sending back application/json.",
        )
        self.assertTrue(json.loads(response.content))
        self.assertEqual("HTTPError", response.json()["type"])