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
|
= XMLRPC self-test API =
The Launchpad root object has a simple XMLRPC API to show that XMLRPC works.
>>> from lp.xmlrpc.application import SelfTest, ISelfTest
>>> from canonical.launchpad.webapp.testing import verifyObject
>>> selftestview = SelfTest('somecontext', 'somerequest')
>>> verifyObject(ISelfTest, selftestview)
True
>>> selftestview.concatenate('foo', 'bar')
u'foo bar'
>>> selftestview.make_fault()
<Fault 666: 'Yoghurt and spanners.'>
We can test our XMLRPC APIs using xmlrpclib, using a custom Transport
which talks with the publisher directly.
>>> import xmlrpclib
>>> from lp.testing.xmlrpc import XMLRPCTestTransport
>>> selftest = xmlrpclib.ServerProxy(
... 'http://xmlrpc.launchpad.dev/', transport=XMLRPCTestTransport())
>>> selftest.concatenate('foo', 'bar')
'foo bar'
>>> selftest.make_fault()
Traceback (most recent call last):
...
Fault: <Fault 666: 'Yoghurt and spanners.'>
== Unexpected Exceptions ==
Sometimes an XML-RPC method will be buggy, and raise an exception
other than xmlrpclib.Fault. We have such a method on the self test
view:
>>> selftestview.raise_exception()
Traceback (most recent call last):
...
RuntimeError: selftest exception
As with normal browser requests, we don't want to expose these error
messages to the user since they could contain confidential
information. Such exceptions get converted to a fault listing the
OOPS ID (assuming one was generated):
>>> selftest.raise_exception()
Traceback (most recent call last):
...
Fault: <Fault -1: 'OOPS-...'>
== Authentication ==
>>> selftest.hello()
'Hello Anonymous.'
The last call returned 'Anonymous', since we didn't provided a username
and a password. If we do that, hello() will print the name of the
logged in user:
>>> selftest = xmlrpclib.ServerProxy(
... 'http://test@canonical.com:test@xmlrpc.launchpad.dev/',
... transport=XMLRPCTestTransport())
>>> selftest.hello()
'Hello Sample Person.'
The interactions in this test, and the interaction in the XMLRPC
methods are different, so we still have an anonymous interaction in
this test.
>>> getUtility(ILaunchBag).user is None
True
Even if we log in as Foo Bar here, the XMLRPC method will see Sample
Person as the logged in user.
>>> login('foo.bar@canonical.com')
>>> selftest.hello()
'Hello Sample Person.'
>>> print getUtility(ILaunchBag).user.displayname
Foo Bar
|