[CalendarServer-changes] [12575] CalendarServer/trunk/txdav/dps

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:18:52 PDT 2014


Revision: 12575
          http://trac.calendarserver.org//changeset/12575
Author:   sagen at apple.com
Date:     2014-02-05 14:10:31 -0800 (Wed, 05 Feb 2014)
Log Message:
-----------
Added DPS AMP test

Modified Paths:
--------------
    CalendarServer/trunk/txdav/dps/client.py
    CalendarServer/trunk/txdav/dps/server.py

Added Paths:
-----------
    CalendarServer/trunk/txdav/dps/test/
    CalendarServer/trunk/txdav/dps/test/__init__.py
    CalendarServer/trunk/txdav/dps/test/test.xml
    CalendarServer/trunk/txdav/dps/test/test_client.py

Modified: CalendarServer/trunk/txdav/dps/client.py
===================================================================
--- CalendarServer/trunk/txdav/dps/client.py	2014-02-05 19:45:57 UTC (rev 12574)
+++ CalendarServer/trunk/txdav/dps/client.py	2014-02-05 22:10:31 UTC (rev 12575)
@@ -14,27 +14,26 @@
 # limitations under the License.
 ##
 
-from twext.who.idirectory import RecordType
+import cPickle as pickle
+
 from twext.python.log import Logger
-from twisted.internet import reactor
-from twext.who.directory import DirectoryService as BaseDirectoryService
 from twext.who.directory import DirectoryRecord as BaseDirectoryRecord
-from twext.who.util import ConstantsContainer
+from twext.who.directory import DirectoryService as BaseDirectoryService
+from twext.who.idirectory import RecordType
 import twext.who.idirectory
-import txdav.who.idirectory
-
-from zope.interface import implementer
-
+from twext.who.util import ConstantsContainer
+from twisted.internet import reactor
+from twisted.internet.defer import inlineCallbacks, returnValue
+from twisted.internet.protocol import ClientCreator
+from twisted.protocols import amp
 from txdav.dps.commands import (
     RecordWithShortNameCommand, RecordWithUIDCommand,
     RecordWithGUIDCommand, RecordsWithRecordTypeCommand,
     RecordsWithEmailAddressCommand
 )
+import txdav.who.idirectory
+from zope.interface import implementer
 
-from twisted.internet.defer import inlineCallbacks, returnValue
-from twisted.internet.protocol import ClientCreator
-from twisted.protocols import amp
-import cPickle as pickle
 
 log = Logger()
 
@@ -93,6 +92,9 @@
 
     @inlineCallbacks
     def _getConnection(self):
+        # TODO: make socket patch configurable
+        # TODO: reconnect if needed
+
         # path = config.DirectoryProxy.SocketPath
         path = "data/Logs/state/directory-proxy.sock"
         if getattr(self, "_connection", None) is None:
@@ -104,77 +106,55 @@
         returnValue(self._connection)
 
 
-    def recordWithShortName(self, recordType, shortName):
+    @inlineCallbacks
+    def _call(self, command, postProcess, **kwds):
+        ampProto = (yield self._getConnection())
+        results = (yield ampProto.callRemote(command, **kwds))
+        returnValue(postProcess(results))
 
-        def _call(ampProto):
-            return ampProto.callRemote(
-                RecordWithShortNameCommand,
-                recordType=recordType.description.encode("utf-8"),
-                shortName=shortName.encode("utf-8")
-            )
 
-        d = self._getConnection()
-        d.addCallback(_call)
-        d.addCallback(self._processSingleRecord)
-        return d
+    def recordWithShortName(self, recordType, shortName):
+        return self._call(
+            RecordWithShortNameCommand,
+            self._processSingleRecord,
+            recordType=recordType.description.encode("utf-8"),
+            shortName=shortName.encode("utf-8")
+        )
 
 
     def recordWithUID(self, uid):
+        return self._call(
+            RecordWithUIDCommand,
+            self._processSingleRecord,
+            uid=uid.encode("utf-8")
+        )
 
-        def _call(ampProto):
-            return ampProto.callRemote(
-                RecordWithUIDCommand,
-                uid=uid.encode("utf-8")
-            )
 
-        d = self._getConnection()
-        d.addCallback(_call)
-        d.addCallback(self._processSingleRecord)
-        return d
-
-
     def recordWithGUID(self, guid):
+        return self._call(
+            RecordWithGUIDCommand,
+            self._processSingleRecord,
+            guid=guid.encode("utf-8")
+        )
 
-        def _call(ampProto):
-            return ampProto.callRemote(
-                RecordWithGUIDCommand,
-                guid=guid.encode("utf-8")
-            )
 
-        d = self._getConnection()
-        d.addCallback(_call)
-        d.addCallback(self._processSingleRecord)
-        return d
-
-
     def recordsWithRecordType(self, recordType):
+        return self._call(
+            RecordsWithRecordTypeCommand,
+            self._processMultipleRecords,
+            recordType=recordType.description.encode("utf-8")
+        )
 
-        def _call(ampProto):
-            return ampProto.callRemote(
-                RecordsWithRecordTypeCommand,
-                recordType=recordType.description.encode("utf-8")
-            )
 
-        d = self._getConnection()
-        d.addCallback(_call)
-        d.addCallback(self._processMultipleRecords)
-        return d
-
-
     def recordsWithEmailAddress(self, emailAddress):
+        return self._call(
+            RecordsWithEmailAddressCommand,
+            self._processMultipleRecords,
+            emailAddress=emailAddress
+        )
 
-        def _call(ampProto):
-            return ampProto.callRemote(
-                RecordsWithEmailAddressCommand,
-                emailAddress=emailAddress
-            )
 
-        d = self._getConnection()
-        d.addCallback(_call)
-        d.addCallback(self._processMultipleRecords)
-        return d
 
-
 class DirectoryRecord(BaseDirectoryRecord):
     pass
 

Modified: CalendarServer/trunk/txdav/dps/server.py
===================================================================
--- CalendarServer/trunk/txdav/dps/server.py	2014-02-05 19:45:57 UTC (rev 12574)
+++ CalendarServer/trunk/txdav/dps/server.py	2014-02-05 22:10:31 UTC (rev 12575)
@@ -14,28 +14,29 @@
 # limitations under the License.
 ##
 
+import cPickle as pickle
+import uuid
+
+from twext.python.log import Logger
 from twext.who.idirectory import RecordType
+from twext.who.opendirectory import DirectoryService as ODDirectoryService
+from twisted.application import service
+from twisted.application.strports import service as strPortsService
+from twisted.internet.defer import inlineCallbacks, returnValue
+from twisted.internet.protocol import Factory
+from twisted.plugin import IPlugin
 from twisted.protocols import amp
-from twisted.internet.defer import inlineCallbacks, returnValue
-from twext.python.log import Logger
-import uuid
-import cPickle as pickle
+from twisted.python.filepath import FilePath
+from twisted.python.usage import Options, UsageError
+from twistedcaldav.config import config
+from twistedcaldav.stdconfig import DEFAULT_CONFIG, DEFAULT_CONFIG_FILE
 from txdav.dps.commands import (
     RecordWithShortNameCommand, RecordWithUIDCommand, RecordWithGUIDCommand,
     RecordsWithRecordTypeCommand, RecordsWithEmailAddressCommand,
     # UpdateRecordsCommand, RemoveRecordsCommand
 )
-from twisted.internet.protocol import Factory
-from twisted.python.usage import Options, UsageError
-from twistedcaldav.config import config
-from twistedcaldav.stdconfig import DEFAULT_CONFIG, DEFAULT_CONFIG_FILE
+from txdav.who.xml import DirectoryService as XMLDirectoryService
 from zope.interface import implementer
-from twisted.plugin import IPlugin
-from twisted.application import service
-from twext.who.opendirectory import DirectoryService as ODDirectoryService
-from txdav.who.xml import DirectoryService as XMLDirectoryService
-from twisted.python.filepath import FilePath
-from twisted.application.strports import service as strPortsService
 
 log = Logger()
 
@@ -285,7 +286,8 @@
         elif directoryType == "LDAP":
             pass
         elif directoryType == "XML":
-            directory = XMLDirectoryService(FilePath("foo.xml"))
+            path = "txdav/dps/test/test.xml"
+            directory = XMLDirectoryService(FilePath(path))
         else:
             log.error("Invalid DirectoryType: {dt}", dt=directoryType)
 

Added: CalendarServer/trunk/txdav/dps/test/__init__.py
===================================================================
--- CalendarServer/trunk/txdav/dps/test/__init__.py	                        (rev 0)
+++ CalendarServer/trunk/txdav/dps/test/__init__.py	2014-02-05 22:10:31 UTC (rev 12575)
@@ -0,0 +1,15 @@
+##
+# Copyright (c) 2013-2014 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##

Added: CalendarServer/trunk/txdav/dps/test/test.xml
===================================================================
--- CalendarServer/trunk/txdav/dps/test/test.xml	                        (rev 0)
+++ CalendarServer/trunk/txdav/dps/test/test.xml	2014-02-05 22:10:31 UTC (rev 12575)
@@ -0,0 +1,116 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<directory realm="xyzzy">
+
+  <record type="user">
+    <uid>__wsanchez__</uid>
+    <short-name>wsanchez</short-name>
+    <short-name>wilfredo_sanchez</short-name>
+    <full-name>Wilfredo Sanchez</full-name>
+    <password>zehcnasw</password>
+    <email>wsanchez at bitbucket.calendarserver.org</email>
+    <email>wsanchez at devnull.twistedmatrix.com</email>
+  </record>
+
+  <record type="user">
+    <uid>__glyph__</uid>
+    <short-name>glyph</short-name>
+    <full-name>Glyph Lefkowitz</full-name>
+    <password>hpylg</password>
+    <email>glyph at bitbucket.calendarserver.org</email>
+    <email>glyph at devnull.twistedmatrix.com</email>
+  </record>
+
+  <record type="user">
+    <uid>__sagen__</uid>
+    <short-name>sagen</short-name>
+    <full-name>Morgen Sagen</full-name>
+    <password>negas</password>
+    <email>sagen at bitbucket.calendarserver.org</email>
+    <email>shared at example.com</email>
+  </record>
+
+  <record type="user">
+    <uid>__cdaboo__</uid>
+    <short-name>cdaboo</short-name>
+    <full-name>Cyrus Daboo</full-name>
+    <password>suryc</password>
+    <email>cdaboo at bitbucket.calendarserver.org</email>
+  </record>
+
+  <record type="user">
+    <uid>__dre__</uid>
+    <guid>A3B1158F-0564-4F5B-81E4-A89EA5FF81B0</guid>
+    <short-name>dre</short-name>
+    <full-name>Andre LaBranche</full-name>
+    <password>erd</password>
+    <email>dre at bitbucket.calendarserver.org</email>
+    <email>shared at example.com</email>
+  </record>
+
+  <record type="user">
+    <uid>__exarkun__</uid>
+    <short-name>exarkun</short-name>
+    <full-name>Jean-Paul Calderone</full-name>
+    <password>nucraxe</password>
+    <email>exarkun at devnull.twistedmatrix.com</email>
+  </record>
+
+  <record type="user">
+    <uid>__dreid__</uid>
+    <short-name>dreid</short-name>
+    <full-name>David Reid</full-name>
+    <password>dierd</password>
+    <email>dreid at devnull.twistedmatrix.com</email>
+  </record>
+
+  <record> <!-- type defaults to "user" -->
+    <uid>__joe__</uid>
+    <short-name>joe</short-name>
+    <full-name>Joe Schmoe</full-name>
+    <password>eoj</password>
+    <email>joe at example.com</email>
+  </record>
+
+  <record> <!-- type defaults to "user" -->
+    <uid>__alyssa__</uid>
+    <short-name>alyssa</short-name>
+    <full-name>Alyssa P. Hacker</full-name>
+    <password>assyla</password>
+    <email>alyssa at example.com</email>
+  </record>
+
+  <record type="group">
+    <uid>__calendar-dev__</uid>
+    <short-name>calendar-dev</short-name>
+    <full-name>Calendar Server developers</full-name>
+    <email>dev at bitbucket.calendarserver.org</email>
+    <member-uid>__wsanchez__</member-uid>
+    <member-uid>__glyph__</member-uid>
+    <member-uid>__sagen__</member-uid>
+    <member-uid>__cdaboo__</member-uid>
+    <member-uid>__dre__</member-uid>
+  </record>
+
+  <record type="group">
+    <uid>__twisted__</uid>
+    <short-name>twisted</short-name>
+    <full-name>Twisted Matrix Laboratories</full-name>
+    <email>hack at devnull.twistedmatrix.com</email>
+    <member-uid>__wsanchez__</member-uid>
+    <member-uid>__glyph__</member-uid>
+    <member-uid>__exarkun__</member-uid>
+    <member-uid>__dreid__</member-uid>
+    <member-uid>__dre__</member-uid>
+  </record>
+
+  <record type="group">
+    <uid>__developers__</uid>
+    <short-name>developers</short-name>
+    <full-name>All Developers</full-name>
+    <member-uid>__calendar-dev__</member-uid>
+    <member-uid>__twisted__</member-uid>
+    <member-uid>__alyssa__</member-uid>
+  </record>
+
+</directory>

Added: CalendarServer/trunk/txdav/dps/test/test_client.py
===================================================================
--- CalendarServer/trunk/txdav/dps/test/test_client.py	                        (rev 0)
+++ CalendarServer/trunk/txdav/dps/test/test_client.py	2014-02-05 22:10:31 UTC (rev 12575)
@@ -0,0 +1,91 @@
+#
+# Copyright (c) 2013-2014 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+import os
+
+from twext.who.idirectory import RecordType
+from twisted.internet.defer import inlineCallbacks, succeed
+from twisted.protocols.amp import AMP
+from twisted.python.filepath import FilePath
+from twisted.test.testutils import returnConnected
+from twisted.trial import unittest
+from txdav.dps.client import DirectoryService
+from txdav.dps.server import DirectoryProxyAMPProtocol
+from txdav.who.xml import DirectoryService as XMLDirectoryService
+
+
+
+
+class DPSClientTest(unittest.TestCase):
+
+    def setUp(self):
+        path = os.path.join(os.path.dirname(__file__), "test.xml")
+        remoteDirectory = XMLDirectoryService(FilePath(path))
+        client = AMP()
+        server = DirectoryProxyAMPProtocol(remoteDirectory)
+        pump = returnConnected(server, client)
+        self.directory = DirectoryService(None)
+
+        self.patch(self.directory, "_getConnection", lambda: succeed(client))
+
+        origCall = self.directory._call
+
+        def newCall(*args, **kwds):
+            d = origCall(*args, **kwds)
+            pump.flush()
+            return d
+
+        self.patch(self.directory, "_call", newCall)
+
+
+    @inlineCallbacks
+    def test_uid(self):
+        record = (yield self.directory.recordWithUID("__dre__"))
+        self.assertEquals(record.shortNames, [u"dre"])
+
+
+    @inlineCallbacks
+    def test_shortName(self):
+        record = (yield self.directory.recordWithShortName(
+            RecordType.user,
+            "wsanchez"
+        ))
+        self.assertEquals(record.shortNames, [u'wsanchez', u'wilfredo_sanchez'])
+
+
+    @inlineCallbacks
+    def test_guid(self):
+        record = (yield self.directory.recordWithGUID(
+            "A3B1158F-0564-4F5B-81E4-A89EA5FF81B0"
+        ))
+        self.assertEquals(record.shortNames, [u'dre'])
+
+
+    @inlineCallbacks
+    def test_recordType(self):
+        records = (yield self.directory.recordsWithRecordType(
+            RecordType.user
+        ))
+        self.assertEquals(len(records), 9)
+
+
+    @inlineCallbacks
+    def test_emailAddress(self):
+        records = (yield self.directory.recordsWithEmailAddress(
+            "cdaboo at bitbucket.calendarserver.org"
+        ))
+        self.assertEquals(len(records), 1)
+        self.assertEquals(records[0].shortNames, [u"cdaboo"])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/8535c912/attachment.html>


More information about the calendarserver-changes mailing list