[Zope3-checkins] CVS: Products3/NewsSite/tests - test_register.py:1.3

Tres Seaver tseaver@zope.com
Thu, 27 Mar 2003 09:23:21 -0500


Update of /cvs-repository/Products3/NewsSite/tests
In directory cvs.zope.org:/tmp/cvs-serv19209/tests

Modified Files:
	test_register.py 
Log Message:
 - Clean up tests to recognize new reality.

=== Products3/NewsSite/tests/test_register.py 1.2 => 1.3 ===
--- Products3/NewsSite/tests/test_register.py:1.2	Tue Mar 25 12:45:51 2003
+++ Products3/NewsSite/tests/test_register.py	Thu Mar 27 09:23:21 2003
@@ -18,17 +18,22 @@
 
 import unittest
 
-from zope.publisher.browser import TestRequest
-
-from zope.app.interfaces.security import IPrincipal
+from zope.exceptions import DuplicationError
 from zope.component import getAdapter
 from zope.component.interfaces import IFactory
+from zope.component.service import serviceManager
+from zope.component.service import defineService
 from zope.component.factory import provideFactory
 from zope.component.adapter import provideAdapter
+from zope.publisher.browser import TestRequest
+
+from zope.app.interfaces.security import IPrincipal
+from zope.app.interfaces.security import IAuthenticationService
+from zope.app.services.servicenames import Authentication
+from zope.app.services.tests.placefulsetup import PlacefulSetup
 from zope.app.form.widget import CustomWidget
 from zope.app.browser.form.widget import TextWidget
 from zope.app.browser.form.add import AddView
-from zope.app.tests.placelesssetup import PlacelessSetup
 
 from zopeproducts.NewsSite.interfaces import IMember
 
@@ -44,39 +49,18 @@
 from zope.testing.cleanup import CleanUp # Base class w registry cleanup
 
 class DummyUser:
-    __implements__ = ( IPrincipal, )
-
-    def __init__(self, id, title, description, login, pw):
+    __implements__ = IPrincipal
+    def __init__(self, id):
         self._id = id
-        self._title = title
-        self._description = description
-        self._login = login
-        self._pw = pw
-        self.roles = []
-
     def getId(self):
         return self._id
 
-    def setRoles(self, roles):
-        self.roles = roles
-
-class DUFactory:    # XXX
-    """Sacrifice dead chickens to appease factory framework.
-    """
-    __implements__ = ( IFactory, )
-
-    def __call__( self, *args, **kw ):
-        return DummyUser( *args, **kw )
-
-    def getInterfaces( self ):
-        raise "D'oh!"
-
 class DummyUserFolderAdding:
 
     def __init__(self):
-        self._things = {}
+        self._things = []
     def add(self,content):
-        self._things[content._id] = content
+        self._things.append(content)
 
 class DummyMemberAdapter( object ):
 
@@ -86,38 +70,59 @@
         self.context = context
 
     def _setLogin(self, login):
-        self.context._login = login
+        self.context.setId(login)
 
-    login = property( lambda self: self.context._login,
-                      _setLogin )
+    login = property( lambda self: self.context.getId(), _setLogin )
 
     def _setPassword(self, password):
-        self.context._pw = password
+        self.context.setPassword(password)
 
-    password = property( lambda self: self.context._pw,
-                      _setPassword )
+    password = property( None, _setPassword ) # Write-only!
 
     def _setEmail(self, email): # XXX
         self.context._email = email
 
-    email = property( lambda self: self.context._email,
-                      _setEmail )
+    email = property( lambda self: self.context._email, _setEmail )
 
     def _setRealName(self, realname): # XXX
         self.context._realname = realname
 
-    realname = property( lambda self: self.context._realname,
-                      _setRealName )
+    realname = property( lambda self: self.context._realname, _setRealName )
+
+class DummyAuthenticatonService:
+
+    __implements__ = IAuthenticationService
+
+    def __init__(self):
+        self._objects = {}
+
+    def setObject(self, key, value):
+        self._objects[key] = value
+
+    def getObject(self, key):
+        return self._objects[key]
+
 
 #############################################################################
 
-class TestMemberAddView(PlacelessSetup, unittest.TestCase, CleanUp):
+class TestMemberAddView(PlacefulSetup, unittest.TestCase, CleanUp):
 
     def setUp(self):
 
-        PlacelessSetup.setUp(self)
-        provideFactory('User', DUFactory() )
-        provideAdapter( IPrincipal, IMember, DummyMemberAdapter )
+        PlacefulSetup.setUp(self)
+        provideAdapter(IPrincipal, IMember, DummyMemberAdapter)
+        self._authsvc = DummyAuthenticatonService()
+        try:
+            serviceManager.defineService(Authentication,
+                                         IAuthenticationService)
+        except DuplicationError:
+            pass
+        serviceManager.provideService(Authentication,
+                                      self._authsvc)
+
+    def tearDown(self):
+        PlacefulSetup.tearDown(self)
+        CleanUp.tearDown(self)
 
     def _getTargetClass(self):
 
@@ -148,35 +153,42 @@
                            password='xyzzy'
                            )
         self.failUnless(IPrincipal.isImplementedBy(user))
-        self.assertEqual(user._id, 'login')
-        self.assertEqual(user._title, '')
-        self.assertEqual(user._description, '')
-        self.assertEqual(user._login, 'login')
-        self.assertEqual(user._pw, 'xyzzy')
+        self.assertEqual(user.getId(), 'login')
+        self.assertEqual(user.getTitle(), '')
+        self.assertEqual(user.getDescription(), '')
+        self.assertEqual(user.getRoles(), ['Member'])
 
     def test_createAndAdd(self):
 
-        ufa = DummyUserFolderAdding()
-        view = self._makeOne(ufa)
+        view = self._makeOne()
 
         view.createAndAdd({'login' : 'login',
                            'realname' : 'Real Name',
                            'email' : 'login@example.com',
                            'password' : 'xyzzy' })
-        user = ufa._things['login']
+
+        user = self._authsvc.getObject('login')
 
         self.failUnless(IPrincipal.isImplementedBy(user))
-        self.assertEqual(user._id, 'login')
-        self.assertEqual(user._title, '')
-        self.assertEqual(user._description, '')
-        self.assertEqual(user._login, 'login')
-        self.assertEqual(user._pw, 'xyzzy')
+        self.assertEqual(user.getId(), 'login')
+        self.assertEqual(user.getTitle(), '')
+        self.assertEqual(user.getDescription(), '')
+        self.assertEqual(user.getRoles(), ['Member'])
+        self.failUnless(user.validate('xyzzy'))
+
         member = getAdapter( user, IMember )
         self.assertEqual(member.login, 'login')
         self.assertEqual(member.realname, 'Real Name')
         self.assertEqual(member.email, 'login@example.com')
-        self.assertEqual(member.password, 'xyzzy')
 
+    def test_add(self):
+
+        ufa = DummyUserFolderAdding()
+        view = self._makeOne(ufa)
+        user = DummyUser('testing')
+        view.add(user)
+        self.failIf(ufa._things)  # user is added to authsvc, not ctx.
+        self.failUnless(self._authsvc.getObject('testing') is user)
 
 
 def test_suite():