def testRandomImageWithEmptyFolder(self):
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     make_test_image(portal)
     make_test_folder(portal, register=True)
     view = portal.restrictedTraverse('randomimage')
     result = view()
     # There are no images, so we only get an empty string.
     self.assertFalse(result)
     response = self.layer['request'].response
     self.assertEqual(response.status, 200)
     self.assertEqual(response.headers.get('location'), None)
 def testRandomness(self):
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     for i in range(5):
         make_test_image(portal)
     view = portal.restrictedTraverse('randomsiteimage')
     locations = set()
     for i in range(10):
         result = view()
         self.assertFalse(result)
         response = self.layer['request'].response
         self.assertEqual(response.status, 302)
         location = response.headers.get('location')
         self.assertTrue(location)
         locations.add(location)
     # Assure that not the same image is returned each time.  Note:
     # this is expected to fail once every 10 million times.
     self.assertTrue(len(locations) > 1)
 def testRandomImageNoFolder(self):
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     image = make_test_image(portal)
     view = portal.restrictedTraverse('randomimage')
     result = view()
     self.assertFalse(result)
     response = self.layer['request'].response
     self.assertEqual(response.status, 302)
     self.assertEqual(response.headers.get('location'),
                      image.absolute_url())
 def testRandomImageNoDoc(self):
     # When searching for images, we should never find documents.
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     make_test_image(portal)
     make_test_doc(portal)
     folder = make_test_folder(portal, register=True)
     folder_image = make_test_image(folder)
     make_test_doc(folder)
     view = portal.restrictedTraverse('randomimage')
     locations = set()
     for i in range(10):
         result = view()
         self.assertFalse(result)
         response = self.layer['request'].response
         self.assertEqual(response.status, 302)
         location = response.headers.get('location')
         self.assertTrue(location)
         locations.add(location)
     self.assertEqual(len(locations), 1)
     self.assertEqual(locations.pop(),
                      folder_image.absolute_url())
 def testRandomSiteImageOtherFolder(self):
     # Check that we can use the views (or one of them anyway) on a
     # regular folder.
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     image = make_test_image(portal)
     folder = make_test_folder(portal)
     view = folder.restrictedTraverse('randomsiteimage')
     result = view()
     self.assertFalse(result)
     response = self.layer['request'].response
     self.assertEqual(response.status, 302)
     self.assertEqual(response.headers.get('location'),
                      image.absolute_url())
 def testRandomSiteImageNavigationRoot(self):
     # Searching for an image in the entire site still takes the
     # navigation root into account.
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     make_test_image(portal)
     folder = make_test_folder(portal)
     from plone.app.layout.navigation.interfaces import INavigationRoot
     from zope.interface import alsoProvides
     alsoProvides(folder, INavigationRoot)
     folder_image = make_test_image(folder)
     view = folder.restrictedTraverse('randomsiteimage')
     locations = set()
     for i in range(10):
         result = view()
         self.assertFalse(result)
         response = self.layer['request'].response
         self.assertEqual(response.status, 302)
         location = response.headers.get('location')
         self.assertTrue(location)
         locations.add(location)
     self.assertEqual(len(locations), 1)
     self.assertEqual(locations.pop(),
                      folder_image.absolute_url())
 def testRandomImage(self):
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     make_test_image(portal)
     folder = make_test_folder(portal, register=True)
     folder_image = make_test_image(folder)
     view = portal.restrictedTraverse('randomimage')
     result = view()
     self.assertFalse(result)
     response = self.layer['request'].response
     self.assertEqual(response.status, 302)
     self.assertEqual(response.headers.get('location'),
                      folder_image.absolute_url())
     # Let's see if that is not a lucky break.
     locations = set()
     for i in range(10):
         result = view()
         self.assertFalse(result)
         response = self.layer['request'].response
         self.assertEqual(response.status, 302)
         location = response.headers.get('location')
         self.assertTrue(location)
         locations.add(location)
     self.assertEqual(len(locations), 1)