Merge lp:~bac/launchpad/bug-838957 into lp:launchpad

Proposed by Brad Crittenden
Status: Merged
Approved by: Aaron Bentley
Approved revision: no longer in the source branch.
Merged at revision: 13898
Proposed branch: lp:~bac/launchpad/bug-838957
Merge into: lp:launchpad
Diff against target: 79 lines (+35/-5)
3 files modified
lib/lp/bugs/browser/bugtask.py (+8/-2)
lib/lp/bugs/browser/tests/test_buglisting.py (+23/-0)
lib/lp/bugs/model/bugtask.py (+4/-3)
To merge this branch: bzr merge lp:~bac/launchpad/bug-838957
Reviewer Review Type Date Requested Status
Aaron Bentley (community) Approve
Review via email: mp+74460@code.launchpad.net

Commit message

[r=abentley][bug=838957] Catch exception rather than throw OOPS on bug search with hacked URL.

Description of the change

= Summary =

Only distributions should have 'components' as part of an advanced bug
search. Hacked URLs, however, could allow that condition to happen.
In that event we should display a helpful message to the user rather
than throwing an OOPS.

== Proposed fix ==

Change the assert to raising a ValueError which is caught, subdued,
and turned into said notice.

== Pre-implementation notes ==

Nah.

== Implementation details ==

As above.

== Tests ==

bin/test -vvm -t test_buglisting

== Demo and Q/A ==

Go to https://launchpad.dev/ubuntu/+bugs
Select a component
Search
Now, hack the URL to change 'ubuntu' to 'firefox'.
See the message and a redirect to the main bugs page for firefox.

= Launchpad lint =

Checking for conflicts and issues in changed files.

Linting changed files:
  lib/lp/bugs/browser/bugtask.py
  lib/lp/bugs/model/bugtask.py
  lib/lp/bugs/browser/tests/test_buglisting.py

To post a comment you must log in.
Revision history for this message
Aaron Bentley (abentley) wrote :

You may wish to rewrite the "except" block as "except ValueError as e". I think this syntax is considered clearer. Otherwise, this looks fine.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/bugs/browser/bugtask.py'
2--- lib/lp/bugs/browser/bugtask.py 2011-09-02 16:31:43 +0000
3+++ lib/lp/bugs/browser/bugtask.py 2011-09-07 16:06:46 +0000
4@@ -2726,7 +2726,13 @@
5 search_params = self.buildSearchParams(
6 searchtext=searchtext, extra_params=extra_params)
7 search_params.user = self.user
8- tasks = context.searchTasks(search_params, prejoins=prejoins)
9+ try:
10+ tasks = context.searchTasks(search_params, prejoins=prejoins)
11+ except ValueError as e:
12+ self.request.response.addErrorNotification(str(e))
13+ self.request.response.redirect(canonical_url(
14+ self.context, rootsite='bugs', view_name='+bugs'))
15+ tasks = None
16 return tasks
17
18 def getWidgetValues(
19@@ -3734,7 +3740,7 @@
20 self._redirectToSearchContext()
21
22 def _redirectToSearchContext(self):
23- """Check wether a target was given and redirect to it.
24+ """Check whether a target was given and redirect to it.
25
26 All the URL parameters will be passed on to the target's +bugs
27 page.
28
29=== modified file 'lib/lp/bugs/browser/tests/test_buglisting.py'
30--- lib/lp/bugs/browser/tests/test_buglisting.py 2011-08-03 11:00:11 +0000
31+++ lib/lp/bugs/browser/tests/test_buglisting.py 2011-09-07 16:06:46 +0000
32@@ -156,6 +156,29 @@
33 [bugtask.owner for bugtask in bugtasks]
34 self.assertThat(recorder, HasQueryCount(Equals(2)))
35
36+ def test_search_components_error(self):
37+ # Searching for using components for bug targets that are not a distro
38+ # or distroseries will report an error, but not OOPS. See bug
39+ # 838957.
40+ product = self.factory.makeProduct()
41+ form = {
42+ 'search': 'Search',
43+ 'advanced': 1,
44+ 'field.component': 1,
45+ 'field.component-empty-marker': 1}
46+ with person_logged_in(product.owner):
47+ view = create_initialized_view(product, '+bugs', form=form)
48+ view.searchUnbatched()
49+ response = view.request.response
50+ self.assertEqual(1, len(response.notifications))
51+ expected = (
52+ "Search by component requires a context with "
53+ "a distribution or distroseries.")
54+ self.assertEqual(expected, response.notifications[0].message)
55+ self.assertEqual(
56+ canonical_url(product, rootsite='bugs', view_name='+bugs'),
57+ response.getHeader('Location'))
58+
59
60 class BugTargetTestCase(TestCaseWithFactory):
61 """Test helpers for setting up `IBugTarget` tests."""
62
63=== modified file 'lib/lp/bugs/model/bugtask.py'
64--- lib/lp/bugs/model/bugtask.py 2011-08-22 13:23:35 +0000
65+++ lib/lp/bugs/model/bugtask.py 2011-09-07 16:06:46 +0000
66@@ -2004,9 +2004,10 @@
67 distroseries = params.distribution.currentseries
68 elif params.distroseries:
69 distroseries = params.distroseries
70- assert distroseries, (
71- "Search by component requires a context with a distribution "
72- "or distroseries.")
73+ if distroseries is None:
74+ raise ValueError(
75+ "Search by component requires a context with a "
76+ "distribution or distroseries.")
77
78 if zope_isinstance(params.component, any):
79 component_ids = sqlvalues(*params.component.query_values)