From bb137b6b83e6268fef75982c6ea844e18ea655fb Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Apr 10 2014 15:22:22 +0000 Subject: Improve bodhi error handling Summary: Fixes T118 Test Plan: Tests included Reviewers: tflink CC: tflink Maniphest Tasks: T118 Differential Revision: https://phab.qadevel.cloud.fedoraproject.org/D49 --- diff --git a/fake_fedorainfra.spec b/fake_fedorainfra.spec index fd18120..fb36365 100644 --- a/fake_fedorainfra.spec +++ b/fake_fedorainfra.spec @@ -23,6 +23,7 @@ Requires: python-flask Requires: python-flask-sqlalchemy Requires: python-flask-wtf Requires: python-flask-login +Requires: python-fedora BuildRequires: python2-devel BuildRequires: python-setuptools diff --git a/fake_fedorainfra/controllers/main.py b/fake_fedorainfra/controllers/main.py index 5dfe9d8..16deb69 100644 --- a/fake_fedorainfra/controllers/main.py +++ b/fake_fedorainfra/controllers/main.py @@ -18,9 +18,11 @@ # Josef Skladanka from flask import Blueprint -from flask import request, url_for, render_template, flash, redirect +from flask import request, url_for, render_template, flash, redirect, abort from fake_fedorainfra import app, db +import fedora.client + main = Blueprint('main', __name__) @@ -53,29 +55,37 @@ def bodhi_list(): user= release= package= bodhirequest= status= update_type= bugs= mine = None limit = 10 - if 'username' in request.form.keys(): - user = str(request.form['username']) - if 'release' in request.form.keys(): - release = str(request.form['release']) - if 'package' in request.form.keys(): - package = str(request.form['package']) - if 'request' in request.form.keys(): - bodhirequest = str(request.form['request']) - if 'status' in request.form.keys(): - status = str(request.form['status']) - if 'type_' in request.form.keys(): - update_type = str(request.form['type_']) - if 'bugs' in request.form.keys(): - bugs = request.form['bugs'] - if 'mine' in request.form.keys(): - mine = str(request.form['mine']) - if 'tg_paginate_limit' in request.form.keys(): - limit = int(request.form['tg_paginate_limit']) - - bodhi = get_bodhi_connection() - result = bodhi.query(package=package, limit=limit, username=user, - release=release, request=bodhirequest, status=status, - type_=update_type, bugs=bugs, mine=mine).toDict() + if 'username' in request.values.keys(): + user = str(request.values['username']) + if 'release' in request.values.keys(): + release = str(request.values['release']) + if 'package' in request.values.keys(): + package = str(request.values['package']) + if 'request' in request.values.keys(): + bodhirequest = str(request.values['request']) + if 'status' in request.values.keys(): + status = str(request.values['status']) + if 'type_' in request.values.keys(): + update_type = str(request.values['type_']) + if 'bugs' in request.values.keys(): + bugs = request.values['bugs'] + if 'mine' in request.values.keys(): + mine = str(request.values['mine']) + if 'tg_paginate_limit' in request.values.keys(): + limit = int(request.values['tg_paginate_limit']) + + try: + bodhi = get_bodhi_connection() + result = bodhi.query(package=package, limit=limit, username=user, + release=release, request=bodhirequest, status=status, + type_=update_type, bugs=bugs, mine=mine) + except fedora.client.AuthError, e: + app.logger.exception('bodhi AuthError') + abort(401) + except fedora.client.ServerError, e: + app.logger.exception('bodhi ServerError') + abort(int(e.code)) + for update in result['updates']: raw_comments = search_comments(update['title']) comments = [dict(timestamp=row['date'], update=row['update'] ,text=row['text'], author=row['user'], diff --git a/requirements.txt b/requirements.txt index 25c2785..df422a9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ Flask-SQLAlchemy==0.16 WTForms>1.0 Flask-WTF==0.8 Flask-Login>=0.1.3 +python-fedora>=0.3.33 diff --git a/setup.py b/setup.py index 7551de4..afa3706 100644 --- a/setup.py +++ b/setup.py @@ -48,6 +48,7 @@ setup(name='fake_fedorainfra', 'WTForms>1.0', 'Flask-WTF==0.8', 'Flask-Login>=0.1.3', + 'python-fedora>=0.3.33', ] ) diff --git a/testing/testfunc_bodhi.py b/testing/testfunc_bodhi.py index bd30064..2213cab 100644 --- a/testing/testfunc_bodhi.py +++ b/testing/testfunc_bodhi.py @@ -1,7 +1,7 @@ # # testfunc_bodhi.py - functional tests for the bodhi portion of boji # -# Copyright 2011, Red Hat, Inc. +# Copyright 2011-2014, Red Hat, Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -17,52 +17,64 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # -# Author: Tim Flink +# Authors: +# Tim Flink +# Martin Krizek import pytest import bunch import json +import tempfile + +from fedora.client import AuthError, ServerError + import fake_fedorainfra import fake_fedorainfra.cli import fake_fedorainfra.controllers.main -import tempfile -from fake_fedorainfra import util +from fake_fedorainfra.lib import util -class MockBodhi(): - def __init__(self, update): - self.update = update +def query(*args, **kwargs): + return bunch.Bunch(util.make_default_update()) - def query(self, package, limit): - return bunch.Bunch(self.update) -def get_mock_bodhi(): - return MockBodhi(util.make_default_update()) +def query_raise_server_error(*args, **kwargs): + raise ServerError('some_url', '404', 'Not Found') -@classmethod -def setup_class(cls): - cls.setup() -class TestFuncBodhi(): +def query_raise_auth_error(*args, **kwargs): + raise AuthError('some_url') + - def setup(self): +class TestFuncBodhi(): + def setup_method(self, method): self.db_fd, fake_fedorainfra.app.config['DATABASE'] = tempfile.mkstemp() print "tempfiledb: %s" % fake_fedorainfra.app.config['DATABASE'] fake_fedorainfra.db.db_url = 'file://%s' % self.db_fd self.app = fake_fedorainfra.app.test_client() - fake_fedorainfra.cli.init_db() + fake_fedorainfra.cli.initialize_db() + + def test_bodhi_server_error(self, monkeypatch): + monkeypatch.setattr(fake_fedorainfra.controllers.main.fedora.client.bodhi.BodhiClient, 'query', query_raise_server_error) + monkeypatch.setattr(fake_fedorainfra.db, 'db_url', 'file://%s' % self.db_fd) - def mock_bodhi_query(self, package, limit): - print self.ref_query - return json.dumps(self.ref_query) + response = self.app.post('/bodhi/list', data={'package':'foo', 'tg_paginate_limit': 10}) - def test_get_comment(self, monkeypatch): - self.ref_query = util.make_default_update() + assert response.status_code == 404 + + def test_bodhi_auth_error(self, monkeypatch): + monkeypatch.setattr(fake_fedorainfra.controllers.main.fedora.client.bodhi.BodhiClient, 'query', query_raise_auth_error) + monkeypatch.setattr(fake_fedorainfra.db, 'db_url', 'file://%s' % self.db_fd) - monkeypatch.setattr(fake_fedorainfra.controllers.main, 'get_bodhi_connection', get_mock_bodhi) + response = self.app.post('/bodhi/list', data={'package':'foo', 'tg_paginate_limit': 10}) + + assert response.status_code == 401 + + def test_get_comment(self, monkeypatch): + monkeypatch.setattr(fake_fedorainfra.controllers.main.fedora.client.bodhi.BodhiClient, 'query', query) monkeypatch.setattr(fake_fedorainfra.db, 'db_url', 'file://%s' % self.db_fd) - test_result = self.app.post('/bodhi/list', data={'package':'foo', 'limit':10}) + test_result = self.app.post('/bodhi/list', data={'package':'foo', 'tg_paginate_limit': 10}) test_comments = json.loads(test_result.data)['updates'][0]['comments'] assert len(test_comments) == 0 @@ -72,12 +84,12 @@ class TestFuncBodhi(): ref_title = 'nothing-1.2.3-4.fc99' ref_comment = 'this is a random comment' - monkeypatch.setattr(fake_fedorainfra.controllers.main, 'get_bodhi_connection', get_mock_bodhi) - monkeypatch.setattr(fake_fedorainfra.ddb, 'db_url', 'file://%s' % self.db_fd) + monkeypatch.setattr(fake_fedorainfra.controllers.main.fedora.client.bodhi.BodhiClient, 'query', query) + monkeypatch.setattr(fake_fedorainfra.db, 'db_url', 'file://%s' % self.db_fd) - test_result = self.app.post('/bodhi/comment', data={'title':ref_title, 'text': ref_comment, 'user_name': ref_user, 'karma':0, 'email': 'False', 'password': 'password' }) + test_result = self.app.post('/bodhi/comment', data={'title':ref_title, 'text': ref_comment, 'user_name': ref_user, 'karma':0, 'email': 'False', 'password': 'password'}) - test_comments = json.loads(self.app.post('/bodhi/list', data={'package': ref_title, 'limit':10}).data)['updates'] + test_comments = json.loads(self.app.post('/bodhi/list', data={'package': ref_title, 'tg_paginate_limit': 10}).data)['updates'] assert len(test_comments) == 1 assert len(test_comments[0]['comments']) == 1