Merge lp:~zematynnad/ubuntu-webcatalog/preflight_987822 into lp:ubuntu-webcatalog

Proposed by Danny Tamez
Status: Merged
Approved by: Natalia Bidart
Approved revision: 129
Merged at revision: 128
Proposed branch: lp:~zematynnad/ubuntu-webcatalog/preflight_987822
Merge into: lp:ubuntu-webcatalog
Diff against target: 243 lines (+201/-0)
5 files modified
django_project/urls.py (+2/-0)
src/webcatalog/preflight.py (+90/-0)
src/webcatalog/schema.py (+1/-0)
src/webcatalog/tests/__init__.py (+1/-0)
src/webcatalog/tests/test_preflight.py (+107/-0)
To merge this branch: bzr merge lp:~zematynnad/ubuntu-webcatalog/preflight_987822
Reviewer Review Type Date Requested Status
Natalia Bidart (community) Approve
Review via email: mp+107483@code.launchpad.net

Commit message

Preflight check for webcatalog

Description of the change

Overview
=========
This branch adds some more functionality for preflight check to webcatalog.

Details
========
To view preflight for ubuntu-webcatalog go to /+preflight. Here is a screenshot of the preflight page: http://simplest-image-hosting.net/png-0-sn2383

The setting PREFLIGHT_GROUPS was added so that we can configure which groups are allowed to view the preflight page. Since webcatalog does not currently have a login page the +preflight url redirects to the openid login page and then redirects on success to the preflight page.

To Test
=========
$fab bootstrap test

To post a comment you must log in.
Revision history for this message
Natalia Bidart (nataliabidart) wrote :

While running the test suite I'm getting 3 failures that I already had notices in trunk, those are fixed in:

https://code.launchpad.net/~nataliabidart/ubuntu-webcatalog/law-and-order/+merge/108204

Would you please review and merge that branch in?

Besides that, I'm getting this output from the test run:

Creating test database for alias 'default'...
...................................................................F...F.......................F........................................................................................................................................Generated checkid_setup request to https://login.staging.ubuntu.com/+openid with assocication {HMAC-SHA1}{4fc7c2c1}{RSEj3w==}
................./home/nessita/canonical/webcatalog/review_preflight_987822/src/webcatalog/preflight.py:46:5: E303 too many blank lines (2)
.

Would you fix the pep8 issue? And perhaps check what is printing the "Generated checkid_setup request to..." message being printed?

Thanks!

review: Needs Fixing
127. By Danny Tamez

Merge from trunk.

128. By Danny Tamez

Remove extra blank line.

129. By Danny Tamez

Fix docstrings and move imports to the top.

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Looks good! Thanks

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'django_project/urls.py'
2--- django_project/urls.py 2012-03-19 17:01:05 +0000
3+++ django_project/urls.py 2012-05-31 20:32:19 +0000
4@@ -29,6 +29,8 @@
5 url(r'^cat/', include('webcatalog.urls')),
6 url(r'^admin/', include(admin.site.urls)),
7 (r'^preflight/$', include('preflight.urls')),
8+ url(r'^\+preflight/$', redirect_to,
9+ {'url': '/openid/login?next=/preflight/'}),
10 url(r'^$', redirect_to, {'url': '/cat/'}),
11
12 # OpenID views
13
14=== added file 'src/webcatalog/preflight.py'
15--- src/webcatalog/preflight.py 1970-01-01 00:00:00 +0000
16+++ src/webcatalog/preflight.py 2012-05-31 20:32:19 +0000
17@@ -0,0 +1,90 @@
18+# -*- coding: utf-8 -*-
19+# This file is part of the Apps Directory
20+# Copyright (C) 2011 Canonical Ltd.
21+#
22+# This program is free software: you can redistribute it and/or modify
23+# it under the terms of the GNU Affero General Public License as
24+# published by the Free Software Foundation, either version 3 of the
25+# License, or (at your option) any later version.
26+#
27+# This program is distributed in the hope that it will be useful,
28+# but WITHOUT ANY WARRANTY; without even the implied warranty of
29+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30+# GNU Affero General Public License for more details.
31+#
32+# You should have received a copy of the GNU Affero General Public License
33+# along with this program. If not, see <http://www.gnu.org/licenses/>.
34+
35+"""Preflight check for webcatalog."""
36+
37+from __future__ import (
38+ absolute_import,
39+ with_statement,
40+ )
41+
42+from django import db
43+from django.conf import settings
44+from preflight import Preflight, register
45+
46+import openid
47+import piston.utils
48+
49+from webcatalog.utilities import WebServices
50+
51+__metaclass__ = type
52+__all__ = [
53+ 'WebcatalogPreflight'
54+ ]
55+
56+
57+class WebcatalogPreflight(Preflight):
58+
59+ def authenticate(self, request):
60+ if not request.user.is_authenticated():
61+ return False
62+ allowed_teams = getattr(settings, 'PREFLIGHT_GROUPS', [])
63+ groups = request.user.groups.filter(name__in=allowed_teams)
64+ return groups.count() > 0
65+
66+ def versions(self):
67+ return [
68+ {'name': 'openid', 'version': openid.__version__},
69+ {'name': 'piston', 'version': piston.utils.get_version()},
70+ ]
71+
72+ def check_validate_config(self):
73+ """Make sure that our config files validate correctly. """
74+ parser = settings.__CONFIGGLUE_PARSER__
75+ return parser.is_valid()
76+
77+ def check_rnr_available(self):
78+ """Make sure that rnr can be reached. """
79+ rnr = WebServices().rnr_api
80+ return rnr.server_status() == 'ok'
81+
82+ def check_recommender_available(self):
83+ """Make sure that the recommender can be reached. """
84+ recommender = WebServices().recommender_api
85+ return recommender.server_status() == 'ok'
86+
87+ def check_screenshots_available(self):
88+ """Make sure that screenshots are available. """
89+ screenshots = WebServices().get_screenshots_for_package('apt')
90+ return len(screenshots) > 0
91+
92+ def check_identity_provider_available(self):
93+ """Make sure SSO can be reached."""
94+ sso = WebServices().identity_provider
95+ return not sso.validate_token(
96+ consumer_key='invalid', token="token")
97+
98+ def check_database(self):
99+ """Make sure database connections are accepted."""
100+ cursor = db.connection.cursor()
101+ cursor.execute('SELECT 42')
102+ cursor.fetchone()
103+
104+ return True
105+
106+
107+register(WebcatalogPreflight)
108
109=== modified file 'src/webcatalog/schema.py'
110--- src/webcatalog/schema.py 2012-04-19 23:05:19 +0000
111+++ src/webcatalog/schema.py 2012-05-31 20:32:19 +0000
112@@ -72,6 +72,7 @@
113 class preflight(schema.Section):
114 preflight_base_template = schema.StringOption(
115 default="webcatalog/base.html")
116+ preflight_groups = schema.ListOption(item=schema.StringOption())
117
118 class rnr(schema.Section):
119 rnr_service_root = schema.StringOption(
120
121=== modified file 'src/webcatalog/tests/__init__.py'
122--- src/webcatalog/tests/__init__.py 2012-03-08 18:00:09 +0000
123+++ src/webcatalog/tests/__init__.py 2012-05-31 20:32:19 +0000
124@@ -25,6 +25,7 @@
125 from .test_models import *
126 from .test_managers import *
127 from .test_pep8 import *
128+from .test_preflight import *
129 from .test_templatetags import *
130 from .test_utilities import *
131 from .test_views import *
132
133=== added file 'src/webcatalog/tests/test_preflight.py'
134--- src/webcatalog/tests/test_preflight.py 1970-01-01 00:00:00 +0000
135+++ src/webcatalog/tests/test_preflight.py 2012-05-31 20:32:19 +0000
136@@ -0,0 +1,107 @@
137+# -*- coding: utf-8 -*-
138+# This file is part of the Ubuntu Web Catalog
139+# Copyright (C) 2011 Canonical Ltd.
140+#
141+# This program is free software: you can redistribute it and/or modify
142+# it under the terms of the GNU Affero General Public License as
143+# published by the Free Software Foundation, either version 3 of the
144+# License, or (at your option) any later version.
145+#
146+# This program is distributed in the hope that it will be useful,
147+# but WITHOUT ANY WARRANTY; without even the implied warranty of
148+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
149+# GNU Affero General Public License for more details.
150+#
151+# You should have received a copy of the GNU Affero General Public License
152+# along with this program. If not, see <http://www.gnu.org/licenses/>.
153+
154+from django.contrib.auth.models import (
155+ Group,
156+ User,
157+ )
158+from mock import patch
159+
160+from webcatalog.tests.factory import TestCaseWithFactory
161+from webcatalog.tests.helpers import patch_settings
162+
163+__metaclass__ = type
164+
165+
166+class TestPreflight(TestCaseWithFactory):
167+
168+ def login(self, add_to_group=True, user=None):
169+ if not user:
170+ user = User.objects.create_user(username='test',
171+ email='test@test.com',
172+ password='test')
173+ if add_to_group:
174+ group = Group.objects.create(name='preflight')
175+ user.groups.add(group)
176+ user.save()
177+ self.client.login(username='test', password='test')
178+
179+ def test_public_url_redirects_to_login(self):
180+ response = self.client.get('/+preflight', follow=True)
181+
182+ with patch_settings(PREFLIGHT_GROUPS=['preflight']):
183+ self.assertContains(response, 'OpenID transaction in progress')
184+
185+ def test_login_needed_for_preflight(self):
186+ self.login()
187+
188+ with patch_settings(PREFLIGHT_GROUPS=['preflight']):
189+ response = self.client.get('/preflight/')
190+
191+ self.assertEqual(200, response.status_code)
192+
193+ def test_no_login_means_preflight_not_found(self):
194+ with patch_settings(PREFLIGHT_GROUPS=['preflight']):
195+ response = self.client.get('/preflight/')
196+
197+ self.assertEqual(404, response.status_code)
198+
199+ def test_login_but_wrong_group_means_preflight_not_found(self):
200+ with patch_settings(PREFLIGHT_GROUPS=['preflight']):
201+ self.login(add_to_group=False)
202+
203+ response = self.client.get('/preflight/')
204+
205+ self.assertEqual(404, response.status_code)
206+
207+ @patch('webcatalog.utilities.WebServices.identity_provider')
208+ @patch('webcatalog.utilities.WebServices.get_screenshots_for_package')
209+ @patch('webcatalog.utilities.WebServices.recommender_api')
210+ @patch('webcatalog.utilities.WebServices.rnr_api')
211+ def test_success(self, mock_rnr, mock_recommender, mock_screenshots,
212+ mock_sso):
213+ mock_rnr.server_status.return_value = 'ok'
214+ mock_recommender.server_status.return_value = 'ok'
215+ mock_screenshots.return_value = ['some_screenshot.jpg']
216+ mock_sso.validate_token.return_value = False
217+ self.login()
218+
219+ with patch_settings(PREFLIGHT_GROUPS=['preflight']):
220+ response = self.client.get('/preflight/')
221+
222+ self.assertEqual(200, response.status_code)
223+ libs = set([x['name'] for x in response.context[0]['versions']])
224+ apps = response.context['applications']
225+ checks = set((x.name, x.passed) for x in apps[0].checks)
226+ expected_libs = set([
227+ 'Python',
228+ 'openid',
229+ 'piston',
230+ 'Django',
231+ 'preflight',
232+ ])
233+ expected_checks = set([
234+ ('validate_config', True),
235+ ('database', True),
236+ ('rnr_available', True),
237+ ('identity_provider_available', True),
238+ ('recommender_available', True),
239+ ('rnr_available', True),
240+ ('screenshots_available', True),
241+ ])
242+ self.assertEqual(expected_libs, libs)
243+ self.assertEqual(expected_checks, checks)

Subscribers

People subscribed via source and target branches