1
*******************************
2
Using launchpadlib in pagetests
3
*******************************
5
As an alternative to crafting HTTP requests with the 'webservice'
6
object, you can write pagetests using launchpadlib.
8
Two helper functions make it easy to set up Launchpad objects that
9
can access the web service. With launchpadlib_for() you can set up a
10
Launchpad object for a given user with a single call.
12
>>> launchpad = launchpadlib_for(
13
... 'launchpadlib test', 'salgado', 'WRITE_PUBLIC')
14
>>> print launchpad.me.name
17
# XXX leonardr 2010-03-31 bug=552732
18
# launchpadlib doesn't work with a credential scoped to a context
19
# like 'firefox', because the service root resource is considered
20
# out of scope. This test should pass, but it doesn't.
22
# When you fix this, be sure to show that an attempt to access
23
# something that really is out of scope (like launchpad.me.name)
26
#>>> launchpad = launchpadlib_for(
27
#... 'launchpadlib test', 'no-priv', 'READ_PRIVATE', 'firefox',
29
#>>> print launchpad.projects['firefox'].name
32
With launchpadlib_credentials_for() you can get a launchpadlib
35
>>> from lp.testing import launchpadlib_credentials_for
36
>>> credentials = launchpadlib_credentials_for(
37
... 'launchpadlib test', 'no-priv', 'READ_PUBLIC')
39
<launchpadlib.credentials.Credentials object ...>
41
>>> print credentials.consumer.key
43
>>> print credentials.access_token
44
oauth_token_secret=...&oauth_token=...
46
This can be used to create your own Launchpad object. Note you cannot
47
use launchpadlib.uris.DEV_SERVICE_ROOT as the URL as it uses the https
48
scheme which does not work in the test environment.
50
>>> from launchpadlib.launchpad import Launchpad
51
>>> launchpad = Launchpad(credentials, 'http://api.launchpad.dev/')
52
>>> print launchpad.me.name
58
>>> lp_anon = Launchpad.login_anonymously('launchpadlib test',
59
... 'http://api.launchpad.dev/')
61
The Launchpad object for the anonymous user can be used to access
64
>>> apache_results = lp_anon.project_groups.search(text="Apache")
65
>>> print apache_results[0].name
68
But trying to access information that requires a logged in user
71
>>> print lp_anon.me.name
72
Traceback (most recent call last):
74
HTTPError: HTTP Error 401: Unauthorized...
79
Let's make sure Launchpad serves caching-related headers that make
80
launchpadlib work correctly. First, we set up a temporary directory to
84
>>> cache = tempfile.mkdtemp()
86
Then we make it possible to view the HTTP traffic between launchpadlib
90
>>> old_debug_level = httplib2.debuglevel
91
>>> httplib2.debuglevel = 1
93
Now create a Launchpad object and observe how it populates the cache.
95
>>> launchpad = Launchpad(
96
... credentials, 'http://api.launchpad.dev/', cache=cache)
97
send: 'GET /1.0/ ...accept: application/vnd.sun.wadl+xml...'
98
reply: 'HTTP/1.0 200 Ok\n'
100
send: 'GET /1.0/ ...accept: application/json...'
101
reply: 'HTTP/1.0 200 Ok\n'
104
Create a second Launchpad object, and the cached documents will be
105
used instead of new HTTP requests being used.
107
>>> launchpad = Launchpad(
108
... credentials, 'http://api.launchpad.dev/', cache=cache)
113
>>> shutil.rmtree(cache)
114
>>> httplib2.debuglevel = old_debug_level