From d284e053fa2ae8b2bb6fefe068ea7e440a720185 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 20 2017 09:05:15 +0000 Subject: [PATCH 1/2] Fix wrong variable used in jenkins_ci_notification Basically, this test is meant to check that we found the proper project in the database but instead we were checking if one of the URL variable was set, which is anyway going to be the case as otherwise the URL mapping would not work. --- diff --git a/pagure/api/ci/jenkins.py b/pagure/api/ci/jenkins.py index 6c2bb82..5cc0c87 100644 --- a/pagure/api/ci/jenkins.py +++ b/pagure/api/ci/jenkins.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ - (c) 2016 - Copyright Red Hat Inc + (c) 2016-2017 - Copyright Red Hat Inc Authors: Pierre-Yves Chibon @@ -46,7 +46,7 @@ def jenkins_ci_notification( project = pagure.lib.get_project( SESSION, repo, user=username, namespace=namespace) - if repo is None: + if not project: raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT) if not constant_time.bytes_eq( From e81494781ecde0bb8907080e8f7b0c77c76ad76b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 20 2017 09:05:15 +0000 Subject: [PATCH 2/2] Handle the situation where file_content is not set There is at least one situation where file_content was not being set. This was reported to us by email and we're fixing it here. Add unit-tests for this change --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 44ef6d8..c51e195 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -535,6 +535,7 @@ def view_file(repo, identifier, filename, username=None, namespace=None): content, safe = pagure.doc_utils.convert_readme(content.data, ext) output_type = 'markup' elif not is_binary_string(content.data): + file_content = None try: file_content = encoding_utils.decode(ktc.to_bytes(content.data)) except pagure.exceptions.PagureException: @@ -542,28 +543,29 @@ def view_file(repo, identifier, filename, username=None, namespace=None): # file and let the user download it instead of displaying # it. output_type = 'binary' - try: - lexer = guess_lexer_for_filename( - filename, - file_content + if file_content: + try: + lexer = guess_lexer_for_filename( + filename, + file_content + ) + except (ClassNotFound, TypeError): + lexer = TextLexer() + + style = "tango" + + if ext in ('.diff', '.patch'): + lexer.add_filter(VisibleWhitespaceFilter( + wstokentype=False, tabs=True)) + style = "diffstyle" + content = highlight( + file_content, + lexer, + HtmlFormatter( + noclasses=True, + style=style,) ) - except (ClassNotFound, TypeError): - lexer = TextLexer() - - style = "tango" - - if ext in ('.diff', '.patch'): - lexer.add_filter(VisibleWhitespaceFilter( - wstokentype=False, tabs=True)) - style = "diffstyle" - content = highlight( - file_content, - lexer, - HtmlFormatter( - noclasses=True, - style=style,) - ) - output_type = 'file' + output_type = 'file' else: output_type = 'binary' else: diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index bca04f5..97dc566 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -20,7 +20,7 @@ import tempfile import os import pygit2 -from mock import patch +from mock import patch, MagicMock sys.path.insert(0, os.path.join(os.path.dirname( os.path.abspath(__file__)), '..')) @@ -1894,6 +1894,28 @@ class PagureFlaskRepotests(tests.Modeltests): self.assertIn( '
 barRow 0
', output.data) + @patch( + 'pagure.lib.encoding_utils.decode', + MagicMock(side_effect=pagure.exceptions.PagureException)) + def test_view_file_with_wrong_encoding(self): + """ Test the view_file endpoint. """ + + tests.create_projects(self.session) + tests.create_projects_git(self.path, bare=True) + + # Add some content to the git repo + tests.add_content_git_repo(os.path.join(self.path, 'test.git')) + tests.add_readme_git_repo(os.path.join(self.path, 'test.git')) + tests.add_binary_git_repo( + os.path.join(self.path, 'test.git'), 'test.jpg') + tests.add_binary_git_repo( + os.path.join(self.path, 'test.git'), 'test_binary') + + # View file + output = self.app.get('/test/blob/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertIn('Binary files cannot be rendered.
', output.data) + def test_view_raw_file(self): """ Test the view_raw_file endpoint. """ output = self.app.get('/foo/raw/foo/sources')