Merge lp:~cjwatson/launchpad/snap-export-findByOwner into lp:launchpad

Proposed by Colin Watson
Status: Merged
Merged at revision: 18617
Proposed branch: lp:~cjwatson/launchpad/snap-export-findByOwner
Merge into: lp:launchpad
Diff against target: 87 lines (+66/-0)
2 files modified
lib/lp/snappy/interfaces/snap.py (+5/-0)
lib/lp/snappy/tests/test_snap.py (+61/-0)
To merge this branch: bzr merge lp:~cjwatson/launchpad/snap-export-findByOwner
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+343278@code.launchpad.net

Commit message

Export ISnapSet.findByOwner on the webservice.

Description of the change

It's kind of rubbish to have to use things like `lp.snaps.findByURLPrefix(url_prefix='https://github.com/', owner='/~build.snapcraft.io')` to iterate over BSI's snaps.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/snappy/interfaces/snap.py'
2--- lib/lp/snappy/interfaces/snap.py 2018-04-04 14:14:30 +0000
3+++ lib/lp/snappy/interfaces/snap.py 2018-04-15 16:50:59 +0000
4@@ -660,6 +660,11 @@
5 def getByName(owner, name):
6 """Return the appropriate `ISnap` for the given objects."""
7
8+ @operation_parameters(
9+ owner=Reference(IPerson, title=_("Owner"), required=True))
10+ @operation_returns_collection_of(ISnap)
11+ @export_read_operation()
12+ @operation_for_version("devel")
13 def findByOwner(owner):
14 """Return all snap packages with the given `owner`."""
15
16
17=== modified file 'lib/lp/snappy/tests/test_snap.py'
18--- lib/lp/snappy/tests/test_snap.py 2018-04-04 14:14:30 +0000
19+++ lib/lp/snappy/tests/test_snap.py 2018-04-15 16:50:59 +0000
20@@ -1547,6 +1547,67 @@
21 "No such snap package with this owner: 'nonexistent'.",
22 response.body)
23
24+ def test_findByOwner(self):
25+ # lp.snaps.findByOwner returns all visible Snaps with the given owner.
26+ persons = [self.factory.makePerson(), self.factory.makePerson()]
27+ snaps = []
28+ for person in persons:
29+ for private in (False, True):
30+ snaps.append(self.factory.makeSnap(
31+ registrant=person, owner=person, private=private))
32+ with admin_logged_in():
33+ person_urls = [api_url(person) for person in persons]
34+ ws_snaps = [
35+ self.webservice.getAbsoluteUrl(api_url(snap))
36+ for snap in snaps]
37+ commercial_admin = (
38+ getUtility(ILaunchpadCelebrities).commercial_admin.teamowner)
39+ logout()
40+ # Anonymous requests can only see public snaps.
41+ anon_webservice = LaunchpadWebServiceCaller("test", "")
42+ response = anon_webservice.named_get(
43+ "/+snaps", "findByOwner", owner=person_urls[0],
44+ api_version="devel")
45+ self.assertEqual(200, response.status)
46+ self.assertContentEqual(
47+ [ws_snaps[0]],
48+ [entry["self_link"] for entry in response.jsonBody()["entries"]])
49+ # persons[0] can see their own private snap as well, but not those
50+ # for other people.
51+ webservice = webservice_for_person(
52+ persons[0], permission=OAuthPermission.READ_PRIVATE)
53+ response = webservice.named_get(
54+ "/+snaps", "findByOwner", owner=person_urls[0],
55+ api_version="devel")
56+ self.assertEqual(200, response.status)
57+ self.assertContentEqual(
58+ ws_snaps[:2],
59+ [entry["self_link"] for entry in response.jsonBody()["entries"]])
60+ response = webservice.named_get(
61+ "/+snaps", "findByOwner", owner=person_urls[1],
62+ api_version="devel")
63+ self.assertEqual(200, response.status)
64+ self.assertContentEqual(
65+ [ws_snaps[2]],
66+ [entry["self_link"] for entry in response.jsonBody()["entries"]])
67+ # Admins can see all snaps.
68+ commercial_admin_webservice = webservice_for_person(
69+ commercial_admin, permission=OAuthPermission.READ_PRIVATE)
70+ response = commercial_admin_webservice.named_get(
71+ "/+snaps", "findByOwner", owner=person_urls[0],
72+ api_version="devel")
73+ self.assertEqual(200, response.status)
74+ self.assertContentEqual(
75+ ws_snaps[:2],
76+ [entry["self_link"] for entry in response.jsonBody()["entries"]])
77+ response = commercial_admin_webservice.named_get(
78+ "/+snaps", "findByOwner", owner=person_urls[1],
79+ api_version="devel")
80+ self.assertEqual(200, response.status)
81+ self.assertContentEqual(
82+ ws_snaps[2:],
83+ [entry["self_link"] for entry in response.jsonBody()["entries"]])
84+
85 def test_findByURL(self):
86 # lp.snaps.findByURL returns visible Snaps with the given URL.
87 persons = [self.factory.makePerson(), self.factory.makePerson()]