This file is indexed.

/usr/lib/python2.7/dist-packages/launchpadlib/testing/tests/test_launchpad.py is in python-launchpadlib 1.10.2+ds-2.

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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# Copyright 2008 Canonical Ltd.

# This file is part of launchpadlib.
#
# launchpadlib is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# launchpadlib is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with launchpadlib.  If not, see
# <http://www.gnu.org/licenses/>.

from datetime import datetime

from testresources import ResourcedTestCase

from launchpadlib.testing.launchpad import (
    FakeLaunchpad,
    FakeResource,
    FakeRoot,
    IntegrityError,
    )
from launchpadlib.testing.resources import (
    FakeLaunchpadResource, get_application)


class FakeRootTest(ResourcedTestCase):

    def test_create_root_resource(self):
        root_resource = FakeRoot(get_application())
        self.assertTrue(isinstance(root_resource, FakeResource))


class FakeResourceTest(ResourcedTestCase):

    resources = [("launchpad", FakeLaunchpadResource())]

    def test_repr(self):
        """A custom C{__repr__} is provided for L{FakeResource}s."""
        branches = dict(total_size="test-branch")
        self.launchpad.me = dict(getBranches=lambda statuses: branches)
        branches = self.launchpad.me.getBranches([])
        obj_id = hex(id(branches))
        self.assertEqual(
            "<FakeResource branch-page-resource object at %s>" % obj_id,
            repr(branches))

    def test_repr_with_name(self):
        """
        If the fake has a C{name} property it's included in the repr string to
        make it easier to figure out what it is.
        """
        self.launchpad.me = dict(name="foo")
        person = self.launchpad.me
        self.assertEqual("<FakeEntry person foo at %s>" % hex(id(person)),
                         repr(person))

    def test_repr_with_id(self):
        """
        If the fake has an C{id} property it's included in the repr string to
        make it easier to figure out what it is.
        """
        bug = dict(id="1", title="Bug #1")
        self.launchpad.bugs = dict(entries=[bug])
        [bug] = list(self.launchpad.bugs)
        self.assertEqual("<FakeResource bug 1 at %s>" % hex(id(bug)), repr(bug))


class FakeLaunchpadTest(ResourcedTestCase):

    resources = [("launchpad", FakeLaunchpadResource())]

    def test_wb_instantiate_without_application(self):
        """
        The builtin WADL definition is used if the C{application} is not
        provided during instantiation.
        """
        credentials = object()
        launchpad = FakeLaunchpad(credentials)
        self.assertEqual(credentials, launchpad.credentials)
        self.assertEqual(get_application(), launchpad._application)

    def test_instantiate_with_everything(self):
        """
        L{FakeLaunchpad} takes the same parameters as L{Launchpad} during
        instantiation, with the addition of an C{application} parameter.  The
        optional parameters are discarded when the object is instantiated.
        """
        credentials = object()
        launchpad = FakeLaunchpad(credentials, service_root=None, cache=None,
                                  timeout=None, proxy_info=None,
                                  application=get_application())
        self.assertEqual(credentials, launchpad.credentials)

    def test_instantiate_with_credentials(self):
        """A L{FakeLaunchpad} can be instantiated with credentials."""
        credentials = object()
        launchpad = FakeLaunchpad(credentials, application=get_application())
        self.assertEqual(credentials, launchpad.credentials)

    def test_instantiate_without_credentials(self):
        """
        A L{FakeLaunchpad} instantiated without credentials has its
        C{credentials} attribute set to C{None}.
        """
        self.assertEqual(None, self.launchpad.credentials)

    def test_set_undefined_property(self):
        """
        An L{IntegrityError} is raised if an attribute is set on a
        L{FakeLaunchpad} instance that isn't present in the WADL definition.
        """
        self.assertRaises(IntegrityError, setattr, self.launchpad, "foo", "bar")

    def test_get_undefined_resource(self):
        """
        An L{AttributeError} is raised if an attribute is accessed on a
        L{FakeLaunchpad} instance that doesn't exist.
        """
        self.launchpad.me = dict(display_name="Foo")
        self.assertRaises(AttributeError, getattr, self.launchpad.me, "name")

    def test_string_property(self):
        """
        Sample data can be created by setting L{FakeLaunchpad} attributes with
        dicts that represent objects.  Plain string values can be represented
        as C{str} values.
        """
        self.launchpad.me = dict(name="foo")
        self.assertEqual("foo", self.launchpad.me.name)

    def test_unicode_property(self):
        """
        Sample data can be created by setting L{FakeLaunchpad} attributes with
        dicts that represent objects.  Plain string values can be represented
        as C{unicode} strings.
        """
        self.launchpad.me = dict(name=u"foo")
        self.assertEqual(u"foo", self.launchpad.me.name)

    def test_datetime_property(self):
        """
        Attributes that represent dates are set with C{datetime} instances.
        """
        now = datetime.utcnow()
        self.launchpad.me = dict(date_created=now)
        self.assertEqual(now, self.launchpad.me.date_created)

    def test_invalid_datetime_property(self):
        """
        Only C{datetime} values can be set on L{FakeLaunchpad} instances for
        attributes that represent dates.
        """
        self.assertRaises(IntegrityError, setattr, self.launchpad, "me",
                          dict(date_created="now"))

    def test_multiple_string_properties(self):
        """
        Sample data can be created by setting L{FakeLaunchpad} attributes with
        dicts that represent objects.
        """
        self.launchpad.me = dict(name="foo", display_name="Foo")
        self.assertEqual("foo", self.launchpad.me.name)
        self.assertEqual("Foo", self.launchpad.me.display_name)

    def test_invalid_property_name(self):
        """
        Sample data set on a L{FakeLaunchpad} instance is validated against
        the WADL definition.  If a key is defined on a resource that doesn't
        match a related parameter, an L{IntegrityError} is raised.
        """
        self.assertRaises(IntegrityError, setattr, self.launchpad, "me",
                          dict(foo="bar"))

    def test_invalid_property_value(self):
        """
        The types of sample data values set on L{FakeLaunchpad} instances are
        validated against types defined in the WADL definition.
        """
        self.assertRaises(IntegrityError, setattr, self.launchpad, "me",
                          dict(name=102))

    def test_callable(self):
        """
        A callable set on a L{FakeLaunchpad} instance is validated against the
        WADL definition, to make sure a matching method exists.
        """
        branches = dict(total_size="test-branch")
        self.launchpad.me = dict(getBranches=lambda statuses: branches)
        self.assertNotEqual(None, self.launchpad.me.getBranches([]))

    def test_invalid_callable_name(self):
        """
        An L{IntegrityError} is raised if a method is defined on a resource
        that doesn't match a method defined in the WADL definition.
        """
        self.assertRaises(IntegrityError, setattr, self.launchpad, "me",
                          dict(foo=lambda: None))

    def test_callable_object_return_type(self):
        """
        The result of a fake method is a L{FakeResource}, automatically
        created from the object used to define the return object.
        """
        branches = dict(total_size="8")
        self.launchpad.me = dict(getBranches=lambda statuses: branches)
        branches = self.launchpad.me.getBranches([])
        self.assertTrue(isinstance(branches, FakeResource))
        self.assertEqual("8", branches.total_size)

    def test_invalid_callable_object_return_type(self):
        """
        An L{IntegrityError} is raised if a method returns an invalid result.
        """
        branches = dict(total_size=8)
        self.launchpad.me = dict(getBranches=lambda statuses: branches)
        self.assertRaises(IntegrityError, self.launchpad.me.getBranches, [])

    def test_collection_property(self):
        """
        Sample collections can be set on L{FakeLaunchpad} instances.  They are
        validated the same way other sample data is validated.
        """
        branch = dict(name="foo")
        self.launchpad.branches = dict(getByUniqueName=lambda name: branch)
        branch = self.launchpad.branches.getByUniqueName("foo")
        self.assertEqual("foo", branch.name)

    def test_iterate_collection(self):
        """
        Data for a sample collection set on a L{FakeLaunchpad} instance can be
        iterated over if an C{entries} key is defined.
        """
        bug = dict(id="1", title="Bug #1")
        self.launchpad.bugs = dict(entries=[bug])
        bugs = [bug for bug in  self.launchpad.bugs]
        self.assertEqual(1, len(bugs))
        bug = bugs[0]
        self.assertEqual("1", bug.id)
        self.assertEqual("Bug #1", bug.title)

    def test_collection_with_invalid_entries(self):
        """
        Sample data for each entry in a collection is validated when it's set
        on a L{FakeLaunchpad} instance.
        """
        bug = dict(foo="bar")
        self.assertRaises(IntegrityError, setattr, self.launchpad, "bugs",
                          dict(entries=[bug]))

    def test_slice_collection(self):
        """
        Data for a sample collection set on a L{FakeLaunchpad} instance can be
        sliced if an C{entries} key is defined.
        """
        bug1 = dict(id="1", title="Bug #1")
        bug2 = dict(id="2", title="Bug #2")
        bug3 = dict(id="3", title="Bug #3")
        self.launchpad.bugs = dict(entries=[bug1, bug2, bug3])
        bugs = self.launchpad.bugs[1:3]
        self.assertEqual(2, len(bugs))
        self.assertEqual("2", bugs[0].id)
        self.assertEqual("3", bugs[1].id)

    def test_slice_collection_with_negative_start(self):
        """
        A C{ValueError} is raised if a negative start value is used when
        slicing a sample collection set on a L{FakeLaunchpad} instance.
        """
        bug1 = dict(id="1", title="Bug #1")
        bug2 = dict(id="2", title="Bug #2")
        self.launchpad.bugs = dict(entries=[bug1, bug2])
        self.assertRaises(ValueError, lambda: self.launchpad.bugs[-1:])
        self.assertRaises(ValueError, lambda: self.launchpad.bugs[-1:2])

    def test_slice_collection_with_negative_stop(self):
        """
        A C{ValueError} is raised if a negative stop value is used when
        slicing a sample collection set on a L{FakeLaunchpad} instance.
        """
        bug1 = dict(id="1", title="Bug #1")
        bug2 = dict(id="2", title="Bug #2")
        self.launchpad.bugs = dict(entries=[bug1, bug2])
        self.assertRaises(ValueError, lambda: self.launchpad.bugs[:-1])
        self.assertRaises(ValueError, lambda: self.launchpad.bugs[0:-1])

    def test_subscript_operator_out_of_range(self):
        """
        An C{IndexError} is raised if an invalid index is used when retrieving
        data from a sample collection.
        """
        bug1 = dict(id="1", title="Bug #1")
        self.launchpad.bugs = dict(entries=[bug1])
        self.assertRaises(IndexError, lambda: self.launchpad.bugs[2])

    def test_replace_property(self):
        """Values already set on fake resource objects can be replaced."""
        self.launchpad.me = dict(name="foo")
        person = self.launchpad.me
        self.assertEqual("foo", person.name)
        person.name = "bar"
        self.assertEqual("bar", person.name)
        self.assertEqual("bar", self.launchpad.me.name)

    def test_replace_method(self):
        """Methods already set on fake resource objects can be replaced."""
        branch1 = dict(name="foo", bzr_identity="lp:~user/project/branch1")
        branch2 = dict(name="foo", bzr_identity="lp:~user/project/branch2")
        self.launchpad.branches = dict(getByUniqueName=lambda name: branch1)
        self.launchpad.branches.getByUniqueName = lambda name: branch2
        branch = self.launchpad.branches.getByUniqueName("foo")
        self.assertEqual("lp:~user/project/branch2", branch.bzr_identity)

    def test_replace_property_with_invalid_value(self):
        """Values set on fake resource objects are validated."""
        self.launchpad.me = dict(name="foo")
        person = self.launchpad.me
        self.assertRaises(IntegrityError, setattr, person, "name", 1)

    def test_replace_resource(self):
        """Resources already set on L{FakeLaunchpad} can be replaced."""
        self.launchpad.me = dict(name="foo")
        self.assertEqual("foo", self.launchpad.me.name)
        self.launchpad.me = dict(name="bar")
        self.assertEqual("bar", self.launchpad.me.name)

    def test_add_property(self):
        """Sample data set on a L{FakeLaunchpad} instance can be added to."""
        self.launchpad.me = dict(name="foo")
        person = self.launchpad.me
        person.display_name = "Foo"
        self.assertEqual("foo", person.name)
        self.assertEqual("Foo", person.display_name)
        self.assertEqual("foo", self.launchpad.me.name)
        self.assertEqual("Foo", self.launchpad.me.display_name)

    def test_add_property_to_empty_object(self):
        """An empty object can be used when creating sample data."""
        self.launchpad.me = dict()
        self.assertRaises(AttributeError, getattr, self.launchpad.me, "name")
        self.launchpad.me.name = "foo"
        self.assertEqual("foo", self.launchpad.me.name)

    def test_login(self):
        """
        L{FakeLaunchpad.login} ignores all parameters and returns a new
        instance using the builtin WADL definition.
        """
        launchpad = FakeLaunchpad.login("name", "token", "secret")
        self.assertTrue(isinstance(launchpad, FakeLaunchpad))

    def test_get_token_and_login(self):
        """
        L{FakeLaunchpad.get_token_and_login} ignores all parameters and
        returns a new instance using the builtin WADL definition.
        """
        launchpad = FakeLaunchpad.get_token_and_login("name")
        self.assertTrue(isinstance(launchpad, FakeLaunchpad))

    def test_login_with(self):
        """
        L{FakeLaunchpad.login_with} ignores all parameters and returns a new
        instance using the builtin WADL definition.
        """
        launchpad = FakeLaunchpad.login_with("name")
        self.assertTrue(isinstance(launchpad, FakeLaunchpad))

    def test_lp_save(self):
        """
        Sample object have an C{lp_save} method that is a no-op by default.
        """
        self.launchpad.me = dict(name="foo")
        self.assertTrue(self.launchpad.me.lp_save())

    def test_custom_lp_save(self):
        """A custom C{lp_save} method can be set on a L{FakeResource}."""
        self.launchpad.me = dict(name="foo", lp_save=lambda: "custom")
        self.assertEqual("custom", self.launchpad.me.lp_save())

    def test_set_custom_lp_save(self):
        """
        A custom C{lp_save} method can be set on a L{FakeResource} after its
        been created.
        """
        self.launchpad.me = dict(name="foo")
        self.launchpad.me.lp_save = lambda: "custom"
        self.assertEqual("custom", self.launchpad.me.lp_save())