/usr/share/doc/python-mockldap-doc/html/overview.html is in python-mockldap-doc 0.2.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Overview — mockldap 0.2.5 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.2.5',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="mockldap 0.2.5 documentation" href="index.html" />
<link rel="next" title="Mock Directories" href="directories.html" />
<link rel="prev" title="mockldap" href="index.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="directories.html" title="Mock Directories"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="index.html" title="mockldap"
accesskey="P">previous</a> |</li>
<li><a href="index.html">mockldap 0.2.5 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="overview">
<h1>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h1>
<p>The goal of mockldap is to provide a mock instance of LDAPObject in response to
any call to ldap.initialize. In the general case, you would register return
values for all LDAPObject calls that you expect the code under test to make.
Your assertions would then verify that the tested code behaved correctly given
this set of return values from the LDAP APIs.</p>
<p>As a convenience, the mock LDAPObject isn’t just a dumb mock object. The typical
way to use mockldap is to provide some static directory content and then let
<a class="reference internal" href="operations.html#mockldap.LDAPObject" title="mockldap.LDAPObject"><tt class="xref py py-class docutils literal"><span class="pre">LDAPObject</span></tt></a> generate real return values. This will only work
for simple LDAP operations–this obviously isn’t a complete Python LDAP server
implementation–but those simple operations tend to cover a lot of cases.</p>
<div class="section" id="example">
<span id="id1"></span><h2>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">unittest</span>
<span class="kn">import</span> <span class="nn">ldap</span>
<span class="kn">from</span> <span class="nn">mockldap</span> <span class="kn">import</span> <span class="n">MockLdap</span>
<span class="k">class</span> <span class="nc">MyTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> A simple test case showing off some of the basic features of mockldap.</span>
<span class="sd"> """</span>
<span class="n">top</span> <span class="o">=</span> <span class="p">(</span><span class="s">'o=test'</span><span class="p">,</span> <span class="p">{</span><span class="s">'o'</span><span class="p">:</span> <span class="s">'test'</span><span class="p">})</span>
<span class="n">example</span> <span class="o">=</span> <span class="p">(</span><span class="s">'ou=example,o=test'</span><span class="p">,</span> <span class="p">{</span><span class="s">'ou'</span><span class="p">:</span> <span class="s">'example'</span><span class="p">})</span>
<span class="n">other</span> <span class="o">=</span> <span class="p">(</span><span class="s">'ou=other,o=test'</span><span class="p">,</span> <span class="p">{</span><span class="s">'ou'</span><span class="p">:</span> <span class="s">'other'</span><span class="p">})</span>
<span class="n">manager</span> <span class="o">=</span> <span class="p">(</span><span class="s">'cn=manager,ou=example,o=test'</span><span class="p">,</span> <span class="p">{</span><span class="s">'cn'</span><span class="p">:</span> <span class="s">'manager'</span><span class="p">,</span> <span class="s">'userPassword'</span><span class="p">:</span> <span class="p">[</span><span class="s">'ldaptest'</span><span class="p">]})</span>
<span class="n">alice</span> <span class="o">=</span> <span class="p">(</span><span class="s">'cn=alice,ou=example,o=test'</span><span class="p">,</span> <span class="p">{</span><span class="s">'cn'</span><span class="p">:</span> <span class="s">'alice'</span><span class="p">,</span> <span class="s">'userPassword'</span><span class="p">:</span> <span class="p">[</span><span class="s">'alicepw'</span><span class="p">]})</span>
<span class="n">bob</span> <span class="o">=</span> <span class="p">(</span><span class="s">'cn=bob,ou=other,o=test'</span><span class="p">,</span> <span class="p">{</span><span class="s">'cn'</span><span class="p">:</span> <span class="s">'bob'</span><span class="p">,</span> <span class="s">'userPassword'</span><span class="p">:</span> <span class="p">[</span><span class="s">'bobpw'</span><span class="p">]})</span>
<span class="c"># This is the content of our mock LDAP directory. It takes the form</span>
<span class="c"># {dn: {attr: [value, ...], ...}, ...}.</span>
<span class="n">directory</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([</span><span class="n">top</span><span class="p">,</span> <span class="n">example</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">manager</span><span class="p">,</span> <span class="n">alice</span><span class="p">,</span> <span class="n">bob</span><span class="p">])</span>
<span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">setUpClass</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
<span class="c"># We only need to create the MockLdap instance once. The content we</span>
<span class="c"># pass in will be used for all LDAP connections.</span>
<span class="n">cls</span><span class="o">.</span><span class="n">mockldap</span> <span class="o">=</span> <span class="n">MockLdap</span><span class="p">(</span><span class="n">cls</span><span class="o">.</span><span class="n">directory</span><span class="p">)</span>
<span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">tearDownClass</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
<span class="k">del</span> <span class="n">cls</span><span class="o">.</span><span class="n">mockldap</span>
<span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c"># Patch ldap.initialize</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mockldap</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">ldapobj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">mockldap</span><span class="p">[</span><span class="s">'ldap://localhost/'</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c"># Stop patching ldap.initialize and reset state.</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mockldap</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
<span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldapobj</span>
<span class="k">def</span> <span class="nf">test_some_ldap</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Some LDAP operations, including binds and simple searches, can be</span>
<span class="sd"> mimicked.</span>
<span class="sd"> """</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">_do_simple_ldap_search</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEquals</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ldapobj</span><span class="o">.</span><span class="n">methods_called</span><span class="p">(),</span> <span class="p">[</span><span class="s">'initialize'</span><span class="p">,</span> <span class="s">'simple_bind_s'</span><span class="p">,</span> <span class="s">'search_s'</span><span class="p">])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEquals</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">results</span><span class="p">),</span> <span class="nb">sorted</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">manager</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">alice</span><span class="p">]))</span>
<span class="k">def</span> <span class="nf">test_complex_search</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Some LDAP operations, such as complex searches, are not implemented.</span>
<span class="sd"> If you're doing anything nontrivial, you have to set an explicit</span>
<span class="sd"> return value for a set of parameters.</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">ldapobj</span><span class="o">.</span><span class="n">search_s</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="s">'o=test'</span><span class="p">,</span> <span class="n">ldap</span><span class="o">.</span><span class="n">SCOPE_SUBTREE</span><span class="p">,</span> <span class="s">'(|(cn=b*b)(cn=a*e))'</span><span class="p">)([</span><span class="bp">self</span><span class="o">.</span><span class="n">alice</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">bob</span><span class="p">])</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">_do_complex_ldap_search</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEquals</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ldapobj</span><span class="o">.</span><span class="n">methods_called</span><span class="p">(),</span> <span class="p">[</span><span class="s">'initialize'</span><span class="p">,</span> <span class="s">'simple_bind_s'</span><span class="p">,</span> <span class="s">'search_s'</span><span class="p">])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEquals</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">results</span><span class="p">),</span> <span class="nb">sorted</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">alice</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">bob</span><span class="p">]))</span>
<span class="k">def</span> <span class="nf">_do_simple_ldap_search</span><span class="p">():</span>
<span class="n">conn</span> <span class="o">=</span> <span class="n">ldap</span><span class="o">.</span><span class="n">initialize</span><span class="p">(</span><span class="s">'ldap://localhost/'</span><span class="p">)</span>
<span class="n">conn</span><span class="o">.</span><span class="n">simple_bind_s</span><span class="p">(</span><span class="s">'cn=alice,ou=example,o=test'</span><span class="p">,</span> <span class="s">'alicepw'</span><span class="p">)</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">search_s</span><span class="p">(</span><span class="s">'ou=example,o=test'</span><span class="p">,</span> <span class="n">ldap</span><span class="o">.</span><span class="n">SCOPE_ONELEVEL</span><span class="p">,</span> <span class="s">'(cn=*)'</span><span class="p">)</span>
<span class="k">return</span> <span class="n">results</span>
<span class="k">def</span> <span class="nf">_do_complex_ldap_search</span><span class="p">():</span>
<span class="n">conn</span> <span class="o">=</span> <span class="n">ldap</span><span class="o">.</span><span class="n">initialize</span><span class="p">(</span><span class="s">'ldap://localhost/'</span><span class="p">)</span>
<span class="n">conn</span><span class="o">.</span><span class="n">simple_bind_s</span><span class="p">(</span><span class="s">'cn=alice,ou=example,o=test'</span><span class="p">,</span> <span class="s">'alicepw'</span><span class="p">)</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">search_s</span><span class="p">(</span><span class="s">'o=test'</span><span class="p">,</span> <span class="n">ldap</span><span class="o">.</span><span class="n">SCOPE_SUBTREE</span><span class="p">,</span> <span class="s">'(|(cn=b*b)(cn=a*e))'</span><span class="p">)</span>
<span class="k">return</span> <span class="n">results</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Overview</a><ul>
<li><a class="reference internal" href="#example">Example</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="index.html"
title="previous chapter">mockldap</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="directories.html"
title="next chapter">Mock Directories</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/overview.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="directories.html" title="Mock Directories"
>next</a> |</li>
<li class="right" >
<a href="index.html" title="mockldap"
>previous</a> |</li>
<li><a href="index.html">mockldap 0.2.5 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2013, Peter Sagerson.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
</div>
</body>
</html>
|