Back to index

plone3  3.1.7
test_login_portlet.py
Go to the documentation of this file.
00001 from zope.component import getUtility, getMultiAdapter, queryMultiAdapter
00002 from zope.app.component.hooks import setHooks, setSite
00003 
00004 from Products.GenericSetup.utils import _getDottedName
00005 
00006 from plone.portlets.interfaces import IPortletType
00007 from plone.portlets.interfaces import IPortletManager
00008 from plone.portlets.interfaces import IPortletAssignment
00009 from plone.portlets.interfaces import IPortletDataProvider
00010 from plone.portlets.interfaces import IPortletRenderer
00011 
00012 from plone.app.portlets.portlets import login
00013 
00014 from plone.app.portlets.storage import PortletAssignmentMapping
00015 
00016 from plone.app.portlets.tests.base import PortletsTestCase
00017 
00018 class TestPortlet(PortletsTestCase):
00019 
00020     def afterSetUp(self):
00021         setHooks()
00022         setSite(self.portal)
00023         self.setRoles(('Manager',))
00024 
00025     def testPortletTypeRegistered(self):
00026         portlet = getUtility(IPortletType, name='portlets.Login')
00027         self.assertEquals(portlet.addview, 'portlets.Login')
00028 
00029     def testRegisteredInterfaces(self):
00030         portlet = getUtility(IPortletType, name='portlets.Login')
00031         registered_interfaces = [_getDottedName(i) for i in portlet.for_] 
00032         self.assertEquals(['plone.app.portlets.interfaces.IColumn'],
00033           registered_interfaces)
00034 
00035     def testInterfaces(self):
00036         portlet = login.Assignment()
00037         self.failUnless(IPortletAssignment.providedBy(portlet))
00038         self.failUnless(IPortletDataProvider.providedBy(portlet.data))
00039 
00040     def testInvokeAddview(self):
00041         portlet = getUtility(IPortletType, name='portlets.Login')
00042         mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
00043         for m in mapping.keys():
00044             del mapping[m]
00045         addview = mapping.restrictedTraverse('+/' + portlet.addview)
00046 
00047         # This is a NullAddForm - calling it does the work
00048         addview()
00049 
00050         self.assertEquals(len(mapping), 1)
00051         self.failUnless(isinstance(mapping.values()[0], login.Assignment))
00052 
00053     def testInvokeEditView(self):
00054         mapping = PortletAssignmentMapping()
00055         request = self.folder.REQUEST
00056 
00057         mapping['foo'] = login.Assignment()
00058         editview = queryMultiAdapter((mapping['foo'], request), name='edit', default=None)
00059         self.failUnless(editview is None)
00060 
00061     def testRenderer(self):
00062         context = self.folder
00063         request = self.folder.REQUEST
00064         view = self.folder.restrictedTraverse('@@plone')
00065         manager = getUtility(IPortletManager, name='plone.leftcolumn', context=self.portal)
00066         assignment = login.Assignment()
00067 
00068         renderer = getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
00069         self.failUnless(isinstance(renderer, login.Renderer))
00070 
00071 class TestRenderer(PortletsTestCase):
00072 
00073     def afterSetUp(self):
00074         setHooks()
00075         setSite(self.portal)
00076 
00077     def renderer(self, context=None, request=None, view=None, manager=None, assignment=None):
00078         context = context or self.folder
00079         request = request or self.folder.REQUEST
00080         view = view or self.folder.restrictedTraverse('@@plone')
00081         manager = manager or getUtility(IPortletManager, name='plone.leftcolumn', context=self.portal)
00082         assignment = assignment or login.Assignment()
00083 
00084         return getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
00085 
00086     def testAvailable(self):
00087         request = self.folder.REQUEST
00088         r = self.renderer()
00089         self.assertEquals(False, r.available)
00090         self.logout()
00091         del request.__annotations__
00092         r = self.renderer()
00093         self.assertEquals(True, r.available)
00094         self.portal.acl_users._delObject('credentials_cookie_auth')
00095         r = self.renderer()
00096         del request.__annotations__
00097         self.assertEquals(False, r.available)
00098         
00099 
00100     def testShow(self):
00101         request = self.folder.REQUEST
00102         
00103         r = self.renderer()
00104         self.assertEquals(False, r.show())
00105         
00106         self.logout()
00107         
00108         del request.__annotations__
00109         self.assertEquals(True, r.show())
00110         
00111         del request.__annotations__
00112         request['URL'] = self.portal.absolute_url() + '/login_form'
00113         self.assertEquals(False, self.renderer(request=request).show())
00114 
00115         del request.__annotations__
00116         request['URL'] = self.portal.absolute_url() + '/join_form'
00117         self.assertEquals(False, self.renderer(request=request).show())
00118 
00119     # TODO: Add more detailed tests here
00120 
00121 def test_suite():
00122     from unittest import TestSuite, makeSuite
00123     suite = TestSuite()
00124     suite.addTest(makeSuite(TestPortlet))
00125     suite.addTest(makeSuite(TestRenderer))
00126     return suite