<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[13615] CalendarServer/trunk/contrib/od</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/13615">13615</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-06-06 14:07:05 -0700 (Fri, 06 Jun 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Adding live OD service unit tests</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcontribodsetup_directorypy">CalendarServer/trunk/contrib/od/setup_directory.py</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li>CalendarServer/trunk/contrib/od/test/</li>
<li><a href="#CalendarServertrunkcontribodtest__init__py">CalendarServer/trunk/contrib/od/test/__init__.py</a></li>
<li><a href="#CalendarServertrunkcontribodtesttest_livepy">CalendarServer/trunk/contrib/od/test/test_live.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcontribodsetup_directorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/contrib/od/setup_directory.py (13614 => 13615)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/contrib/od/setup_directory.py        2014-06-06 18:25:15 UTC (rev 13614)
+++ CalendarServer/trunk/contrib/od/setup_directory.py        2014-06-06 21:07:05 UTC (rev 13615)
</span><span class="lines">@@ -115,6 +115,18 @@
</span><span class="cx">             dsattributes.kDS1AttrPrimaryGroupID : [&quot;20&quot;],
</span><span class="cx">         },
</span><span class="cx">     ),
</span><ins>+    (
+        &quot;anotherodtestamanda&quot;,
+        {
+            dsattributes.kDS1AttrFirstName : [&quot;Amanda&quot;],
+            dsattributes.kDS1AttrLastName  : [&quot;Test&quot;],
+            dsattributes.kDS1AttrDistinguishedName : [&quot;Amanda Test&quot;],
+            dsattributes.kDSNAttrEMailAddress : [&quot;anotheramanda@example.com&quot;],
+            dsattributes.kDS1AttrGeneratedUID : [&quot;E7666814-6D92-49EC-8562-8C4C3D64A4B0&quot;],
+            dsattributes.kDS1AttrUniqueID : [&quot;33307&quot;],
+            dsattributes.kDS1AttrPrimaryGroupID : [&quot;20&quot;],
+        },
+    ),
</ins><span class="cx"> ]
</span><span class="cx"> 
</span><span class="cx"> masterGroups = [
</span><span class="lines">@@ -137,6 +149,16 @@
</span><span class="cx">             dsattributes.kDS1AttrPrimaryGroupID : [&quot;33400&quot;],
</span><span class="cx">         },
</span><span class="cx">     ),
</span><ins>+    (
+        &quot;odtestgroupbetty&quot;,
+        {
+            dsattributes.kDS1AttrGeneratedUID : [&quot;2A1F3ED9-D1B3-40F2-8FC4-05E197C1F90C&quot;],
+            dsattributes.kDS1AttrDistinguishedName : [&quot;OD Test Group Betty&quot;],
+            dsattributes.kDSNAttrGroupMembers : [],
+            dsattributes.kDSNAttrNestedGroups : [],
+            dsattributes.kDS1AttrPrimaryGroupID : [&quot;33403&quot;],
+        },
+    ),
</ins><span class="cx"> ]
</span><span class="cx"> 
</span><span class="cx"> localUsers = [
</span><span class="lines">@@ -188,6 +210,18 @@
</span><span class="cx">             dsattributes.kDS1AttrPrimaryGroupID : [&quot;20&quot;],
</span><span class="cx">         },
</span><span class="cx">     ),
</span><ins>+    (
+        &quot;anotherodtestalbert&quot;,
+        {
+            dsattributes.kDS1AttrFirstName : [&quot;Albert&quot;],
+            dsattributes.kDS1AttrLastName  : [&quot;Test&quot;],
+            dsattributes.kDS1AttrDistinguishedName : [&quot;Albert Test&quot;],
+            dsattributes.kDSNAttrEMailAddress : [&quot;anotheralbert@example.com&quot;],
+            dsattributes.kDS1AttrGeneratedUID : [&quot;8F059F1B-1CD0-42B5-BEA2-6A36C9B5620F&quot;],
+            dsattributes.kDS1AttrUniqueID : [&quot;33354&quot;],
+            dsattributes.kDS1AttrPrimaryGroupID : [&quot;20&quot;],
+        },
+    ),
</ins><span class="cx"> ]
</span><span class="cx"> 
</span><span class="cx"> localGroups = [
</span><span class="lines">@@ -200,6 +234,15 @@
</span><span class="cx">             dsattributes.kDS1AttrPrimaryGroupID : [&quot;33402&quot;],
</span><span class="cx">         },
</span><span class="cx">     ),
</span><ins>+    (
+        &quot;odtestgroupalbert&quot;,
+        {
+            dsattributes.kDS1AttrGeneratedUID : [&quot;3F4D01B8-FDFD-4805-A853-DE9879A2D951&quot;],
+            dsattributes.kDS1AttrDistinguishedName : [&quot;OD Test Group Albert&quot;],
+            dsattributes.kDSNAttrGroupMembers : [],
+            dsattributes.kDS1AttrPrimaryGroupID : [&quot;33404&quot;],
+        },
+    ),
</ins><span class="cx"> ]
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunkcontribodtest__init__py"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/contrib/od/test/__init__.py (0 => 13615)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/contrib/od/test/__init__.py                                (rev 0)
+++ CalendarServer/trunk/contrib/od/test/__init__.py        2014-06-06 21:07:05 UTC (rev 13615)
</span><span class="lines">@@ -0,0 +1,15 @@
</span><ins>+##
+# Copyright (c) 2014 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
+# 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 &quot;AS IS&quot; 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.
+##
</ins></span></pre></div>
<a id="CalendarServertrunkcontribodtesttest_livepy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/contrib/od/test/test_live.py (0 => 13615)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/contrib/od/test/test_live.py                                (rev 0)
+++ CalendarServer/trunk/contrib/od/test/test_live.py        2014-06-06 21:07:05 UTC (rev 13615)
</span><span class="lines">@@ -0,0 +1,272 @@
</span><ins>+##
+# Copyright (c) 2014 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
+# 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 &quot;AS IS&quot; 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.
+##
+
+&quot;&quot;&quot;
+OpenDirectory live service tests.
+&quot;&quot;&quot;
+
+from __future__ import print_function
+
+from itertools import chain
+from uuid import UUID
+
+from twisted.trial import unittest
+from twisted.internet.defer import inlineCallbacks, returnValue
+
+
+
+try:
+    from twext.who.opendirectory import DirectoryService
+    moduleImported = True
+except:
+    moduleImported = False
+    print(&quot;Could not import OpenDirectory&quot;)
+
+if moduleImported:
+
+    from twext.who.expression import (
+        CompoundExpression, Operand, MatchExpression, MatchType, MatchFlags
+    )
+
+
+    LOCAL_SHORTNAMES = &quot;odtestalbert odtestbill odtestcarl odtestdavid odtestsubgroupa&quot;.split()
+    NETWORK_SHORTNAMES = &quot;odtestamanda odtestbetty odtestcarlene odtestdenise odtestsubgroupb odtestgrouptop&quot;.split()
+
+
+    def onlyIfPopulated(func):
+        &quot;&quot;&quot;
+        Only run the decorated test method if the &quot;odtestamanda&quot; record exists
+        &quot;&quot;&quot;
+        @inlineCallbacks
+        def checkThenRun(self):
+            record = yield self.service.recordWithShortName(self.service.recordType.user, u&quot;odtestamanda&quot;)
+            if record is not None:
+                result = yield func(self)
+                returnValue(result)
+            else:
+                print(&quot;OD not populated, skipping {}&quot;.format(func.func_name))
+        return checkThenRun
+
+
+
+    class LiveOpenDirectoryServiceTestCase(unittest.TestCase):
+        &quot;&quot;&quot;
+        Live service tests for L{DirectoryService}.
+        &quot;&quot;&quot;
+
+        def setUp(self):
+            self.service = DirectoryService()
+
+
+        def verifyResults(self, records, expected, unexpected):
+            shortNames = []
+            for record in records:
+                for shortName in record.shortNames:
+                    shortNames.append(shortName)
+
+            for name in expected:
+                self.assertTrue(name in shortNames)
+            for name in unexpected:
+                self.assertFalse(name in shortNames)
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_shortNameStartsWith(self):
+            records = yield self.service.recordsFromExpression(
+                MatchExpression(
+                    self.service.fieldName.shortNames, u&quot;odtest&quot;,
+                    matchType=MatchType.startsWith
+                )
+            )
+            self.verifyResults(
+                records,
+                chain(LOCAL_SHORTNAMES, NETWORK_SHORTNAMES),
+                [&quot;anotherodtestamanda&quot;, &quot;anotherodtestalbert&quot;]
+            )
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_uid(self):
+            for uid, name in (
+                (u&quot;9DC04A71-E6DD-11DF-9492-0800200C9A66&quot;, u&quot;odtestbetty&quot;),
+                (u&quot;9DC04A75-E6DD-11DF-9492-0800200C9A66&quot;, u&quot;odtestbill&quot;),
+            ):
+                record = yield self.service.recordWithUID(uid)
+                self.assertTrue(record is not None)
+                self.assertEquals(record.shortNames[0], name)
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_guid(self):
+            for guid, name in (
+                (UUID(&quot;9DC04A71-E6DD-11DF-9492-0800200C9A66&quot;), u&quot;odtestbetty&quot;),
+                (UUID(&quot;9DC04A75-E6DD-11DF-9492-0800200C9A66&quot;), u&quot;odtestbill&quot;),
+            ):
+                record = yield self.service.recordWithGUID(guid)
+                self.assertTrue(record is not None)
+                self.assertEquals(record.shortNames[0], name)
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_compoundWithoutRecordType(self):
+            expression = CompoundExpression(
+                [
+                    CompoundExpression(
+                        [
+                            MatchExpression(
+                                self.service.fieldName.fullNames, u&quot;be&quot;,
+                                matchType=MatchType.contains
+                            ),
+                            MatchExpression(
+                                self.service.fieldName.emailAddresses, u&quot;be&quot;,
+                                matchType=MatchType.startsWith
+                            ),
+                        ],
+                        Operand.OR
+                    ),
+                    CompoundExpression(
+                        [
+                            MatchExpression(
+                                self.service.fieldName.fullNames, u&quot;test&quot;,
+                                matchType=MatchType.contains
+                            ),
+                            MatchExpression(
+                                self.service.fieldName.emailAddresses, u&quot;test&quot;,
+                                matchType=MatchType.startsWith
+                            ),
+                        ],
+                        Operand.OR
+                    ),
+                ],
+                Operand.AND
+            )
+            records = yield self.service.recordsFromExpression(expression)
+
+            # We should get back users and groups:
+            self.verifyResults(
+                records,
+                [&quot;odtestbetty&quot;, &quot;odtestalbert&quot;, &quot;anotherodtestalbert&quot;, &quot;odtestgroupbetty&quot;, &quot;odtestgroupalbert&quot;],
+                [&quot;odtestamanda&quot;, &quot;odtestbill&quot;, &quot;odtestgroupa&quot;, &quot;odtestgroupb&quot;]
+            )
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_compoundWithSingleRecordType(self):
+            expression = CompoundExpression(
+                [
+                    CompoundExpression(
+                        [
+                            CompoundExpression(
+                                [
+                                    MatchExpression(
+                                        self.service.fieldName.fullNames, u&quot;be&quot;,
+                                        matchType=MatchType.contains
+                                    ),
+                                    MatchExpression(
+                                        self.service.fieldName.emailAddresses, u&quot;be&quot;,
+                                        matchType=MatchType.startsWith
+                                    ),
+                                ],
+                                Operand.OR
+                            ),
+                            CompoundExpression(
+                                [
+                                    MatchExpression(
+                                        self.service.fieldName.fullNames, u&quot;test&quot;,
+                                        matchType=MatchType.contains
+                                    ),
+                                    MatchExpression(
+                                        self.service.fieldName.emailAddresses, u&quot;test&quot;,
+                                        matchType=MatchType.startsWith
+                                    ),
+                                ],
+                                Operand.OR
+                            ),
+                        ],
+                        Operand.AND
+                    ),
+                    MatchExpression(
+                        self.service.fieldName.recordType, self.service.recordType.user,
+                    ),
+                ],
+                Operand.AND
+            )
+            records = yield self.service.recordsFromExpression(expression)
+
+            # We should only get users back, not groups:
+            self.verifyResults(
+                records,
+                [&quot;odtestbetty&quot;, &quot;odtestalbert&quot;, &quot;anotherodtestalbert&quot;],
+                [&quot;odtestamanda&quot;, &quot;odtestbill&quot;, &quot;odtestgroupa&quot;, &quot;odtestgroupb&quot;, &quot;odtestgroupbetty&quot;, &quot;odtestgroupalbert&quot;]
+            )
+
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_compoundWithSingleRecordType(self):
+            expression = CompoundExpression(
+                [
+                    CompoundExpression(
+                        [
+                            CompoundExpression(
+                                [
+                                    MatchExpression(
+                                        self.service.fieldName.fullNames, u&quot;be&quot;,
+                                        matchType=MatchType.contains
+                                    ),
+                                    MatchExpression(
+                                        self.service.fieldName.emailAddresses, u&quot;be&quot;,
+                                        matchType=MatchType.startsWith
+                                    ),
+                                ],
+                                Operand.OR
+                            ),
+                            CompoundExpression(
+                                [
+                                    MatchExpression(
+                                        self.service.fieldName.fullNames, u&quot;test&quot;,
+                                        matchType=MatchType.contains
+                                    ),
+                                    MatchExpression(
+                                        self.service.fieldName.emailAddresses, u&quot;test&quot;,
+                                        matchType=MatchType.startsWith
+                                    ),
+                                ],
+                                Operand.OR
+                            ),
+                        ],
+                        Operand.AND
+                    ),
+                    MatchExpression(
+                        self.service.fieldName.recordType, self.service.recordType.user,
+                    ),
+                ],
+                Operand.AND
+            )
+            records = yield self.service.recordsFromExpression(expression)
+
+            # We should only get users back, not groups:
+            self.verifyResults(
+                records,
+                [&quot;odtestbetty&quot;, &quot;odtestalbert&quot;, &quot;anotherodtestalbert&quot;],
+                [&quot;odtestamanda&quot;, &quot;odtestbill&quot;, &quot;odtestgroupa&quot;, &quot;odtestgroupb&quot;, &quot;odtestgroupbetty&quot;, &quot;odtestgroupalbert&quot;]
+            )
</ins></span></pre>
</div>
</div>

</body>
</html>