launchpad.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. # Copyright 2008 Canonical Ltd.
  2. # This file is part of launchpadlib.
  3. #
  4. # launchpadlib is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Lesser General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # launchpadlib is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with launchpadlib. If not, see
  16. # <http://www.gnu.org/licenses/>.
  17. """Testing API allows fake data to be used in unit tests.
  18. Testing launchpadlib code is tricky, because it depends so heavily on a
  19. remote, unique webservice: Launchpad. This module helps you write tests for
  20. your launchpadlib application that can be run locally and quickly.
  21. Say you were writing some code that needed to call out to Launchpad and get
  22. the branches owned by the logged-in person, and then do something to them. For
  23. example, something like this::
  24. def collect_unique_names(lp):
  25. names = []
  26. for branch in lp.me.getBranches():
  27. names.append(branch.unique_name)
  28. return names
  29. To test it, you would first prepare a L{FakeLaunchpad} object, and give it
  30. some sample data of your own devising::
  31. lp = FakeLaunchpad()
  32. my_branches = [dict(unique_name='~foo/bar/baz')]
  33. lp.me = dict(getBranches: lambda status: my_branches)
  34. Then, in the test, call your own code and assert that it behaves correctly
  35. given the data.
  36. names = collect_unique_names(lp)
  37. self.assertEqual(['~foo/bar/baz'], names)
  38. And that's it.
  39. The L{FakeLaunchpad} code uses a WADL file to type-check any objects created
  40. or returned. This means you can be sure that you won't accidentally store
  41. sample data with misspelled attribute names.
  42. The WADL file that we use by default is for version 1.0 of the Launchpad API.
  43. If you want to work against a more recent version of the API, download the
  44. WADL yourself (see <https://help.launchpad.net/API/Hacking>) and construct
  45. your C{FakeLaunchpad} like this::
  46. from wadllib.application import Application
  47. lp = FakeLaunchpad(
  48. Application('https://api.launchpad.net/devel/',
  49. '/path/to/wadl.xml'))
  50. Where 'https://api.launchpad.net/devel/' is the URL for the WADL file, found
  51. also in the WADL file itelf.
  52. """
  53. from datetime import datetime
  54. try:
  55. from collections.abc import Callable
  56. except ImportError:
  57. from collections import Callable
  58. import sys
  59. if sys.version_info[0] >= 3:
  60. basestring = str
  61. class IntegrityError(Exception):
  62. """Raised when bad sample data is used with a L{FakeLaunchpad} instance."""
  63. class FakeLaunchpad(object):
  64. """A fake Launchpad API class for unit tests that depend on L{Launchpad}.
  65. @param application: A C{wadllib.application.Application} instance for a
  66. Launchpad WADL definition file.
  67. """
  68. def __init__(
  69. self,
  70. credentials=None,
  71. service_root=None,
  72. cache=None,
  73. timeout=None,
  74. proxy_info=None,
  75. application=None,
  76. ):
  77. if application is None:
  78. from launchpadlib.testing.resources import get_application
  79. application = get_application()
  80. root_resource = FakeRoot(application)
  81. self.__dict__.update(
  82. {
  83. "credentials": credentials,
  84. "_application": application,
  85. "_service_root": root_resource,
  86. }
  87. )
  88. def __setattr__(self, name, values):
  89. """Set sample data.
  90. @param name: The name of the attribute.
  91. @param values: A dict representing an object matching a resource
  92. defined in Launchpad's WADL definition.
  93. """
  94. service_root = self._service_root
  95. setattr(service_root, name, values)
  96. def __getattr__(self, name):
  97. """Get sample data.
  98. @param name: The name of the attribute.
  99. """
  100. return getattr(self._service_root, name)
  101. @classmethod
  102. def login(
  103. cls,
  104. consumer_name,
  105. token_string,
  106. access_secret,
  107. service_root=None,
  108. cache=None,
  109. timeout=None,
  110. proxy_info=None,
  111. ):
  112. """Convenience for setting up access credentials."""
  113. from launchpadlib.testing.resources import get_application
  114. return cls(object(), application=get_application())
  115. @classmethod
  116. def get_token_and_login(
  117. cls,
  118. consumer_name,
  119. service_root=None,
  120. cache=None,
  121. timeout=None,
  122. proxy_info=None,
  123. ):
  124. """Get credentials from Launchpad and log into the service root."""
  125. from launchpadlib.testing.resources import get_application
  126. return cls(object(), application=get_application())
  127. @classmethod
  128. def login_with(
  129. cls,
  130. consumer_name,
  131. service_root=None,
  132. launchpadlib_dir=None,
  133. timeout=None,
  134. proxy_info=None,
  135. ):
  136. """Log in to Launchpad with possibly cached credentials."""
  137. from launchpadlib.testing.resources import get_application
  138. return cls(object(), application=get_application())
  139. def find_by_attribute(element, name, value):
  140. """Find children of element where attribute name is equal to value."""
  141. return [child for child in element if child.get(name) == value]
  142. def strip_suffix(string, suffix):
  143. if string.endswith(suffix):
  144. return string[: -len(suffix)]
  145. return string
  146. def wadl_tag(tag_name):
  147. """Scope a tag name with the WADL namespace."""
  148. return "{http://research.sun.com/wadl/2006/10}" + tag_name
  149. class FakeResource(object):
  150. """
  151. Represents valid sample data on L{FakeLaunchpad} instances.
  152. @ivar _children: A dictionary of child resources, each of type
  153. C{FakeResource}.
  154. @ivar _values: A dictionary of values associated with this resource. e.g.
  155. "display_name" or "date_created". The values of this dictionary will
  156. never be C{FakeResource}s.
  157. Note that if C{_children} has a key, then C{_values} will not, and vice
  158. versa. That is, they are distinct dicts.
  159. """
  160. special_methods = ["lp_save"]
  161. def __init__(self, application, resource_type, values=None):
  162. """Construct a FakeResource.
  163. @param application: A C{waddlib.application.Application} instance.
  164. @param resource_type: A C{wadllib.application.ResourceType} instance
  165. for this resource.
  166. @param values: Optionally, a dict representing attribute key/value
  167. pairs for this resource.
  168. """
  169. if values is None:
  170. values = {}
  171. self.__dict__.update(
  172. {
  173. "_application": application,
  174. "_resource_type": resource_type,
  175. "_children": {},
  176. "_values": values,
  177. }
  178. )
  179. def __setattr__(self, name, value):
  180. """Set sample data.
  181. C{value} can be a dict representing an object matching a resource
  182. defined in the WADL definition. Alternatively, C{value} could be a
  183. resource itself. Either way, it is checked for type correctness
  184. against the WADL definition.
  185. """
  186. if isinstance(value, dict):
  187. self._children[name] = self._create_child_resource(name, value)
  188. else:
  189. values = {}
  190. values.update(self._values)
  191. values[name] = value
  192. # Confirm that the new 'values' dict is a partial type match for
  193. # this resource.
  194. self._check_resource_type(self._resource_type, values)
  195. self.__dict__["_values"] = values
  196. def __getattr__(self, name, _marker=object()):
  197. """Get sample data.
  198. @param name: The name of the attribute.
  199. """
  200. result = self._children.get(name, _marker)
  201. if result is _marker:
  202. result = self._values.get(name, _marker)
  203. if isinstance(result, Callable):
  204. return self._wrap_method(name, result)
  205. if name in self.special_methods:
  206. return lambda: True
  207. if result is _marker:
  208. raise AttributeError("%r has no attribute '%s'" % (self, name))
  209. return result
  210. def _wrap_method(self, name, method):
  211. """Wrapper around methods validates results when it's run.
  212. @param name: The name of the method.
  213. @param method: The callable to run when the method is called.
  214. """
  215. def wrapper(*args, **kwargs):
  216. return self._run_method(name, method, *args, **kwargs)
  217. return wrapper
  218. def _create_child_resource(self, name, values):
  219. """
  220. Ensure that C{values} is a valid object for the C{name} attribute and
  221. return a resource object to represent it as API data.
  222. @param name: The name of the attribute to check the C{values} object
  223. against.
  224. @param values: A dict with key/value pairs representing attributes and
  225. methods of an object matching the C{name} resource's definition.
  226. @return: A L{FakeEntry} for an ordinary resource or a
  227. L{FakeCollection} for a resource that represents a collection.
  228. @raises IntegrityError: Raised if C{name} isn't a valid attribute for
  229. this resource or if C{values} isn't a valid object for the C{name}
  230. attribute.
  231. """
  232. xml_id = self._find_representation_id(self._resource_type, "get")
  233. representation = self._application.representation_definitions[xml_id]
  234. params = {
  235. child.name: child
  236. for child in representation.params(representation)
  237. }
  238. is_link = False
  239. param = params.get(name + "_collection_link")
  240. if param is None:
  241. is_link = True
  242. param = params.get(name + "_link")
  243. if param is None:
  244. raise IntegrityError("%s isn't a valid property." % (name,))
  245. resource_type = self._get_resource_type(param)
  246. if is_link:
  247. self._check_resource_type(resource_type, values)
  248. return FakeEntry(self._application, resource_type, values)
  249. else:
  250. name, child_resource_type = self._check_collection_type(
  251. resource_type, values
  252. )
  253. return FakeCollection(
  254. self._application,
  255. resource_type,
  256. values,
  257. name,
  258. child_resource_type,
  259. )
  260. def _get_resource_type(self, param):
  261. """Get the resource type for C{param}.
  262. @param param: An object representing a C{_link} or C{_collection_link}
  263. parameter.
  264. @return: The resource type for the parameter, or None if one isn't
  265. available.
  266. """
  267. link = param.tag.find(wadl_tag("link"))
  268. name = link.get("resource_type")
  269. return self._application.get_resource_type(name)
  270. def _check_resource_type(self, resource_type, partial_object):
  271. """
  272. Ensure that attributes and methods defined for C{partial_object} match
  273. attributes and methods defined for C{resource_type}.
  274. @param resource_type: The resource type to check the attributes and
  275. methods against.
  276. @param partial_object: A dict with key/value pairs representing
  277. attributes and methods.
  278. """
  279. for name, value in partial_object.items():
  280. if isinstance(value, Callable):
  281. # Performs an integrity check.
  282. self._get_method(resource_type, name)
  283. else:
  284. self._check_attribute(resource_type, name, value)
  285. def _check_collection_type(self, resource_type, partial_object):
  286. """
  287. Ensure that attributes and methods defined for C{partial_object} match
  288. attributes and methods defined for C{resource_type}. Collection
  289. entries are treated specially.
  290. @param resource_type: The resource type to check the attributes and
  291. methods against.
  292. @param partial_object: A dict with key/value pairs representing
  293. attributes and methods.
  294. @return: (name, resource_type), where 'name' is the name of the child
  295. resource type and 'resource_type' is the corresponding resource
  296. type.
  297. """
  298. name = None
  299. child_resource_type = None
  300. for name, value in partial_object.items():
  301. if name == "entries":
  302. name, child_resource_type = self._check_entries(
  303. resource_type, value
  304. )
  305. elif isinstance(value, Callable):
  306. # Performs an integrity check.
  307. self._get_method(resource_type, name)
  308. else:
  309. self._check_attribute(resource_type, name, value)
  310. return name, child_resource_type
  311. def _find_representation_id(self, resource_type, name):
  312. """Find the WADL XML id for the representation of C{resource_type}.
  313. Looks in the WADL for the first representiation associated with the
  314. method for a resource type.
  315. :return: An XML id (a string).
  316. """
  317. get_method = self._get_method(resource_type, name)
  318. for response in get_method:
  319. for representation in response:
  320. representation_url = representation.get("href")
  321. if representation_url is not None:
  322. return self._application.lookup_xml_id(representation_url)
  323. def _check_attribute(self, resource_type, name, value):
  324. """
  325. Ensure that C{value} is a valid C{name} attribute on C{resource_type}.
  326. Does this by finding the representation for the default, canonical GET
  327. method (as opposed to the many "named" GET methods that exist.)
  328. @param resource_type: The resource type to check the attribute
  329. against.
  330. @param name: The name of the attribute.
  331. @param value: The value to check.
  332. """
  333. xml_id = self._find_representation_id(resource_type, "get")
  334. self._check_attribute_representation(xml_id, name, value)
  335. def _check_attribute_representation(self, xml_id, name, value):
  336. """
  337. Ensure that C{value} is a valid value for C{name} with the
  338. representation definition matching C{xml_id}.
  339. @param xml_id: The XML ID for the representation to check the
  340. attribute against.
  341. @param name: The name of the attribute.
  342. @param value: The value to check.
  343. @raises IntegrityError: Raised if C{name} is not a valid attribute
  344. name or if C{value}'s type is not valid for the attribute.
  345. """
  346. representation = self._application.representation_definitions[xml_id]
  347. params = {
  348. child.name: child
  349. for child in representation.params(representation)
  350. }
  351. if name + "_collection_link" in params:
  352. resource_type = self._get_resource_type(
  353. params[name + "_collection_link"]
  354. )
  355. child_name, child_resource_type = self._check_collection_type(
  356. resource_type, value
  357. )
  358. elif name + "_link" in params:
  359. resource_type = self._get_resource_type(params[name + "_link"])
  360. self._check_resource_type(resource_type, value)
  361. else:
  362. param = params.get(name)
  363. if param is None:
  364. raise IntegrityError("%s not found" % name)
  365. if param.type is None:
  366. if not isinstance(value, basestring):
  367. raise IntegrityError(
  368. "%s is not a str or unicode for %s" % (value, name)
  369. )
  370. elif param.type == "xsd:dateTime":
  371. if not isinstance(value, datetime):
  372. raise IntegrityError(
  373. "%s is not a datetime for %s" % (value, name)
  374. )
  375. def _get_method(self, resource_type, name):
  376. """Get the C{name} method on C{resource_type}.
  377. @param resource_type: The method's resource type.
  378. @param name: The name of the method.
  379. @raises IntegrityError: Raised if a method called C{name} is not
  380. available on C{resource_type}.
  381. @return: The XML element for the method from the WADL.
  382. """
  383. if name in self.special_methods:
  384. return
  385. resource_name = resource_type.tag.get("id")
  386. xml_id = "%s-%s" % (resource_name, name)
  387. try:
  388. [get_method] = find_by_attribute(resource_type.tag, "id", xml_id)
  389. except ValueError:
  390. raise IntegrityError(
  391. "%s is not a method of %s" % (name, resource_name)
  392. )
  393. return get_method
  394. def _run_method(self, name, method, *args, **kwargs):
  395. """Run a method and convert its result into a L{FakeResource}.
  396. If the result represents an object it is validated against the WADL
  397. definition before being returned.
  398. @param name: The name of the method.
  399. @param method: A callable.
  400. @param args: Arguments to pass to the callable.
  401. @param kwargs: Keyword arguments to pass to the callable.
  402. @return: A L{FakeResource} representing the result if it's an object.
  403. @raises IntegrityError: Raised if the return value from the method
  404. isn't valid.
  405. """
  406. result = method(*args, **kwargs)
  407. if name in self.special_methods or result is None:
  408. return result
  409. else:
  410. return self._create_resource(self._resource_type, name, result)
  411. def _create_resource(self, resource_type, name, result):
  412. """Create new L{FakeResource} for C{resource_type} method call result.
  413. @param resource_type: The resource type of the method.
  414. @param name: The name of the method on C{resource_type}.
  415. @param result: The result of calling the method.
  416. @raises IntegrityError: Raised if C{result} is an invalid return value
  417. for the method.
  418. @return: A L{FakeResource} for C{result}, or just C{result} if no
  419. response representation is defined for the method.
  420. """
  421. resource_name = resource_type.tag.get("id")
  422. if resource_name == name:
  423. name = "get"
  424. xml_id = self._find_representation_id(resource_type, name)
  425. if xml_id is None:
  426. return result
  427. xml_id = strip_suffix(xml_id, "-full")
  428. if xml_id not in self._application.resource_types:
  429. xml_id += "-resource"
  430. result_resource_type = self._application.resource_types[xml_id]
  431. if xml_id.endswith("-page-resource"):
  432. name, child_resource_type = self._check_collection_type(
  433. result_resource_type, result
  434. )
  435. return FakeCollection(
  436. self._application,
  437. result_resource_type,
  438. result,
  439. name,
  440. child_resource_type,
  441. )
  442. else:
  443. self._check_resource_type(result_resource_type, result)
  444. resource = FakeEntry(self._application, result_resource_type)
  445. for child_name, child_value in result.items():
  446. setattr(resource, child_name, child_value)
  447. return resource
  448. def _get_child_resource_type(self, resource_type):
  449. """Get the name and resource type for the entries in a collection.
  450. @param resource_type: The resource type for a collection.
  451. @return: (name, resource_type), where 'name' is the name of the child
  452. resource type and 'resource_type' is the corresponding resource
  453. type.
  454. """
  455. xml_id = self._find_representation_id(resource_type, "get")
  456. representation_definition = (
  457. self._application.representation_definitions[xml_id]
  458. )
  459. [entry_links] = find_by_attribute(
  460. representation_definition.tag, "name", "entry_links"
  461. )
  462. [resource_type] = list(entry_links)
  463. resource_type_url = resource_type.get("resource_type")
  464. resource_type_name = resource_type_url.split("#")[1]
  465. return (
  466. resource_type_name,
  467. self._application.get_resource_type(resource_type_url),
  468. )
  469. def _check_entries(self, resource_type, entries):
  470. """Ensure that C{entries} are valid for a C{resource_type} collection.
  471. @param resource_type: The resource type of the collection the entries
  472. are in.
  473. @param entries: A list of dicts representing objects in the
  474. collection.
  475. @return: (name, resource_type), where 'name' is the name of the child
  476. resource type and 'resource_type' is the corresponding resource
  477. type.
  478. """
  479. name, child_resource_type = self._get_child_resource_type(
  480. resource_type
  481. )
  482. for entry in entries:
  483. self._check_resource_type(child_resource_type, entry)
  484. return name, child_resource_type
  485. def __repr__(self):
  486. """
  487. The resource type, identifier if available, and memory address are
  488. used to generate a representation of this fake resource.
  489. """
  490. name = self._resource_type.tag.get("id")
  491. key = "object"
  492. key = self._values.get("id", key)
  493. key = self._values.get("name", key)
  494. return "<%s %s %s at %s>" % (
  495. self.__class__.__name__,
  496. name,
  497. key,
  498. hex(id(self)),
  499. )
  500. class FakeRoot(FakeResource):
  501. """Fake root object for an application."""
  502. def __init__(self, application):
  503. """Create a L{FakeResource} for the service root of C{application}.
  504. @param application: A C{wadllib.application.Application} instance.
  505. """
  506. resource_type = application.get_resource_type(
  507. application.markup_url + "#service-root"
  508. )
  509. super(FakeRoot, self).__init__(application, resource_type)
  510. class FakeEntry(FakeResource):
  511. """A fake resource for an entry."""
  512. class FakeCollection(FakeResource):
  513. """A fake resource for a collection."""
  514. def __init__(
  515. self,
  516. application,
  517. resource_type,
  518. values=None,
  519. name=None,
  520. child_resource_type=None,
  521. ):
  522. super(FakeCollection, self).__init__(
  523. application, resource_type, values
  524. )
  525. self.__dict__.update(
  526. {"_name": name, "_child_resource_type": child_resource_type}
  527. )
  528. def __iter__(self):
  529. """Iterate items if this resource has an C{entries} attribute."""
  530. entries = self._values.get("entries", ())
  531. for entry in entries:
  532. yield self._create_resource(
  533. self._child_resource_type, self._name, entry
  534. )
  535. def __getitem__(self, key):
  536. """Look up a slice, or a subordinate resource by index.
  537. @param key: An individual object key or a C{slice}.
  538. @raises IndexError: Raised if an invalid key is provided.
  539. @return: A L{FakeResource} instance for the entry matching C{key}.
  540. """
  541. entries = list(self)
  542. if isinstance(key, slice):
  543. start = key.start or 0
  544. stop = key.stop
  545. if start < 0:
  546. raise ValueError(
  547. "Collection slices must have a nonnegative " "start point."
  548. )
  549. if stop < 0:
  550. raise ValueError(
  551. "Collection slices must have a definite, "
  552. "nonnegative end point."
  553. )
  554. return entries.__getitem__(key)
  555. elif isinstance(key, int):
  556. return entries.__getitem__(key)
  557. else:
  558. raise IndexError("Do not support index lookups yet.")