Merge lp:~julian-edwards/launchpad/checkUpload-bug-608891 into lp:launchpad

Proposed by Julian Edwards
Status: Merged
Approved by: Māris Fogels
Approved revision: no longer in the source branch.
Merged at revision: 11238
Proposed branch: lp:~julian-edwards/launchpad/checkUpload-bug-608891
Merge into: lp:launchpad
Diff against target: 83 lines (+68/-0)
2 files modified
lib/lp/soyuz/browser/tests/test_archive_webservice.py (+67/-0)
lib/lp/soyuz/interfaces/archive.py (+1/-0)
To merge this branch: bzr merge lp:~julian-edwards/launchpad/checkUpload-bug-608891
Reviewer Review Type Date Requested Status
Māris Fogels (community) code Approve
Review via email: mp+31041@code.launchpad.net

Description of the change

= Summary =
Fix bug 608891, prevent an OOPS when there should not be one.

== Proposed fix ==
There's a webservice_error() missing from a the CannotUploadToPocket exception
class, so when it gets raised it generates an OOPS instead of returning an
error to the client.

== Tests ==
I added a new unit test file
bin/test -cvv test_archive_webservice

= Launchpad lint =

The lint checker blows on interface files with webservice exports. Ignore
this.

Checking for conflicts and issues in changed files.

Linting changed files:
  lib/lp/soyuz/interfaces/archive.py
  lib/lp/soyuz/browser/tests/test_archive_webservice.py

./lib/lp/soyuz/interfaces/archive.py
     528: E301 expected 1 blank line, found 0
     545: E202 whitespace before ')'
     570: E501 line too long (81 characters)
     572: E501 line too long (80 characters)
     577: E501 line too long (81 characters)
     580: E501 line too long (80 characters)
     655: E301 expected 1 blank line, found 0
     678: E301 expected 1 blank line, found 0
     701: E301 expected 1 blank line, found 0
     801: E301 expected 1 blank line, found 0
    1138: E302 expected 2 blank lines, found 1
    1435: E301 expected 1 blank line, found 2
    1664: E303 too many blank lines (3)
     521: Line exceeds 78 characters.
     570: Line exceeds 78 characters.
     572: Line exceeds 78 characters.
     577: Line exceeds 78 characters.
     580: Line exceeds 78 characters.
    1449: Line exceeds 78 characters.

To post a comment you must log in.
Revision history for this message
Māris Fogels (mars) wrote :

Hi Julian,

This code looks good. I have a few questions about the code, just trivial fixes:

• The copyright needs updating to 2010

• Does it matter if you explicitly call TestCaseWithFactory.setUp(self), or should you use super()?

• An alternative but still elegant way to write _do_check() is to use a partial function to save the parameters: http://docs.python.org/library/functools.html#functools.partial

With those considerations, r=mars

Maris

review: Approve (code)
Revision history for this message
Julian Edwards (julian-edwards) wrote :

On Tuesday 27 July 2010 17:11:01 you wrote:
> • The copyright needs updating to 2010

Woops, fixed, thanks.

> • Does it matter if you explicitly call TestCaseWithFactory.setUp(self), or
> should you use super()?

I was just following the lead of the existing test code. I think I've only
ever used super in __init__ but maybe we should use it everywhere? I'd raise
it at the reviewers' meeting.

> • An alternative but still elegant way to write _do_check() is to use a
> partial function to save the parameters:
> http://docs.python.org/library/functools.html#functools.partial

I'm not entirely convinced that's any more readable! But thanks for the
pointer.

> With those considerations, r=mars

Thanks for the review.

Cheers.

Revision history for this message
Jonathan Lange (jml) wrote :

On Tue, Jul 27, 2010 at 5:31 PM, Julian Edwards
<email address hidden> wrote:
...
>> • Does it matter if you explicitly call TestCaseWithFactory.setUp(self), or
>> should you use super()?
>
> I was just following the lead of the existing test code.  I think I've only
> ever used super in __init__ but maybe we should use it everywhere?  I'd raise
> it at the reviewers' meeting.
>

The guideline is to prefer it, except in cases where it's completely
broken due to old-style base classes.

jml

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'lib/lp/soyuz/browser/tests/test_archive_webservice.py'
2--- lib/lp/soyuz/browser/tests/test_archive_webservice.py 1970-01-01 00:00:00 +0000
3+++ lib/lp/soyuz/browser/tests/test_archive_webservice.py 2010-07-27 16:31:09 +0000
4@@ -0,0 +1,67 @@
5+# Copyright 2010 Canonical Ltd. This software is licensed under the
6+# GNU Affero General Public License version 3 (see the file LICENSE).
7+
8+__metaclass__ = type
9+
10+import unittest
11+from lazr.restfulclient.errors import HTTPError
12+
13+from lp.testing import (
14+ celebrity_logged_in, launchpadlib_for, TestCaseWithFactory)
15+from canonical.launchpad.testing.pages import LaunchpadWebServiceCaller
16+from canonical.testing import DatabaseFunctionalLayer
17+from lp.soyuz.interfaces.archive import ArchivePurpose
18+
19+
20+class TestArchiveWebservice(TestCaseWithFactory):
21+ layer = DatabaseFunctionalLayer
22+
23+ def setUp(self):
24+ TestCaseWithFactory.setUp(self)
25+ with celebrity_logged_in('admin'):
26+ archive = self.factory.makeArchive(
27+ purpose=ArchivePurpose.PRIMARY)
28+ distroseries = self.factory.makeDistroSeries(
29+ distribution=archive.distribution)
30+ person = self.factory.makePerson()
31+ distro_name = archive.distribution.name
32+ distroseries_name = distroseries.name
33+ person_name = person.name
34+
35+ self.webservice = LaunchpadWebServiceCaller(
36+ 'launchpad-library', 'salgado-change-anything')
37+
38+ self.launchpad = launchpadlib_for(
39+ "archive test", "salgado", "WRITE_PUBLIC")
40+ self.distribution = self.launchpad.distributions[distro_name]
41+ self.distroseries = self.distribution.getSeries(
42+ name_or_version=distroseries_name)
43+ self.main_archive = self.distribution.main_archive
44+ self.person = self.launchpad.people[person_name]
45+
46+ def test_checkUpload_bad_pocket(self):
47+ # Make sure a 403 error and not an OOPS is returned when
48+ # CannotUploadToPocket is raised when calling checkUpload.
49+
50+ # When we're on Python 2.7, this code will be much nicer as
51+ # assertRaises is a context manager so you can do something like
52+ # with self.assertRaises(HTTPError) as cm; do_something
53+ # .... then you have cm.exception available.
54+ def _do_check():
55+ self.main_archive.checkUpload(
56+ distroseries=self.distroseries,
57+ sourcepackagename="mozilla-firefox",
58+ pocket="Updates",
59+ component="restricted",
60+ person=self.person)
61+
62+ e = self.assertRaises(HTTPError, _do_check)
63+
64+ self.assertEqual(403, e.response.status)
65+ self.assertIn(
66+ "Not permitted to upload to the UPDATES pocket in a series "
67+ "in the 'DEVELOPMENT' state.", e.content)
68+
69+
70+def test_suite():
71+ return unittest.TestLoader().loadTestsFromName(__name__)
72
73=== modified file 'lib/lp/soyuz/interfaces/archive.py'
74--- lib/lp/soyuz/interfaces/archive.py 2010-07-18 00:56:16 +0000
75+++ lib/lp/soyuz/interfaces/archive.py 2010-07-27 16:31:09 +0000
76@@ -166,6 +166,7 @@
77
78 class CannotUploadToPocket(Exception):
79 """Returned when a pocket is closed for uploads."""
80+ webservice_error(403) # Forbidden.
81
82 def __init__(self, distroseries, pocket):
83 Exception.__init__(self,