From 12829b2e3ba7120595cd2da7eb6e4dbcb954da45 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 31 2017 09:34:18 +0000 Subject: Do not show the `Fork and edit` button all the time If the project you are viewing is your own fork there is no need to offer to fork and edit a file. Fixes https://pagure.io/pagure/issue/2559 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/templates/file.html b/pagure/templates/file.html index 7130305..b5541c8 100644 --- a/pagure/templates/file.html +++ b/pagure/templates/file.html @@ -109,7 +109,10 @@ branchname=branchname, filename=filename | unicode) }}" title="Edit file">Edit {% endif %} - {% if output_type in ('file','markup') %} + {% if output_type in ('file','markup') and not authenticated + or ( + not repo.is_fork + or (authenticated and repo.user.user != g.fas_user.username)) %}
+ +""" + +import unittest +import sys +import os + +import pygit2 + +sys.path.insert(0, os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +import pagure # noqa +import pagure.lib # noqa +import tests # noqa +from pagure.lib.repo import PagureRepo # noqa + + +class LocalBasetests(tests.Modeltests): + """ Tests for view_file endpoint of the flask pagure app """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(LocalBasetests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.ui.SESSION = self.session + pagure.ui.app.SESSION = self.session + pagure.ui.filters.SESSION = self.session + pagure.ui.repo.SESSION = self.session + + pagure.APP.config['VIRUS_SCAN_ATTACHMENTS'] = False + pagure.APP.config['UPLOAD_FOLDER_URL'] = '/releases/' + pagure.APP.config['UPLOAD_FOLDER_PATH'] = os.path.join( + self.path, 'releases') + + +class PagureFlaskRepoViewFileSimpletests(LocalBasetests): + """ Tests for view_file endpoint of the flask pagure app """ + + def test_view_file_no_project(self): + """ Test the view_file when the project is unknown. """ + output = self.app.get('/foo/blob/foo/f/sources') + # No project registered in the DB + self.assertEqual(output.status_code, 404) + + def test_view_file_no_git(self): + """ Test the view_file when the project has no git repo. """ + tests.create_projects(self.session) + + output = self.app.get('/test/blob/foo/f/sources') + # No git repo associated + self.assertEqual(output.status_code, 404) + + def test_view_file_no_git_content(self): + """ Test the view_file when the file doesn't exist. """ + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True) + + output = self.app.get('/test/blob/foo/f/sources') + self.assertEqual(output.status_code, 404) + + +class PagureFlaskRepoViewFiletests(LocalBasetests): + """ Tests for view_file endpoint of the flask pagure app """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskRepoViewFiletests, self).setUp() + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(self.path, 'repos'), bare=True) + + # Add some content to the git repo + tests.add_content_git_repo( + os.path.join(self.path, 'repos', 'test.git')) + tests.add_readme_git_repo( + os.path.join(self.path, 'repos', 'test.git')) + tests.add_binary_git_repo( + os.path.join(self.path, 'repos', 'test.git'), 'test.jpg') + tests.add_binary_git_repo( + os.path.join(self.path, 'repos', 'test.git'), 'test_binary') + + def test_view_file_invalid_file(self): + """ Test the view_file when the file doesn't exist. """ + + output = self.app.get('/test/blob/master/foofile') + self.assertEqual(output.status_code, 404) + output = self.app.get('/test/blob/sources/f/testfoo.jpg') + self.assertEqual(output.status_code, 404) + output = self.app.get('/test/blob/master/f/folder1/testfoo.jpg') + self.assertEqual(output.status_code, 404) + + def test_view_file_basic_text(self): + """ Test the view_file with a basic text file. """ + output = self.app.get('/test/blob/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertTrue('' in output.data) + self.assertTrue( + '' + in output.data) + self.assertTrue( + '' in output.data) + + def test_view_file_empty_file(self): + """ Test the view_file with an empty file. """ + + # Empty files should also be displayed + tests.add_content_to_git( + os.path.join(self.path, 'repos', 'test.git'), + filename="emptyfile.md", + content="") + output = self.app.get('/test/blob/master/f/emptyfile.md') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Raw', output.data) + self.assertIn( + '
\n' + ' \n
', output.data) + + def test_view_file_binary_file(self): + """ Test the view_file with a binary file. """ + + # View what's supposed to be an image + output = self.app.get('/test/blob/master/f/test.jpg') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Binary files cannot be rendered.
', output.data) + self.assertIn( + 'view the raw version', + output.data) + + def test_view_file_by_commit(self): + """ Test the view_file in a specific commit. """ + + # View by commit id + repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git')) + commit = repo.revparse_single('HEAD') + + output = self.app.get('/test/blob/%s/f/test.jpg' % commit.oid.hex) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Binary files cannot be rendered.
', output.data) + self.assertIn('/f/test.jpg">view the raw version', output.data) + + def test_view_file_by_name(self): + """ Test the view_file via a image name. """ + + # View by image name -- somehow we support this + output = self.app.get('/test/blob/sources/f/test.jpg') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Binary files cannot be rendered.
', output.data) + self.assertIn('/f/test.jpg">view the raw version', output.data) + + def test_view_file_binary_file2(self): + """ Test the view_file with a binary file (2). """ + + # View binary file + output = self.app.get('/test/blob/sources/f/test_binary') + self.assertEqual(output.status_code, 200) + self.assertIn('/f/test_binary">view the raw version', output.data) + self.assertTrue( + 'Binary files cannot be rendered.
' + in output.data) + + def test_view_file_for_folder(self): + """ Test the view_file with a folder. """ + + # View folder + output = self.app.get('/test/blob/master/f/folder1') + self.assertEqual(output.status_code, 200) + self.assertIn( + '', + output.data) + self.assertIn('Tree - test - Pagure', output.data) + self.assertIn( + '
', output.data) + + def test_view_file_nested_file(self): + """ Test the view_file with a nested file. """ + + # Verify the nav links correctly when viewing a nested folder/file. + output = self.app.get('/test/blob/master/f/folder1/folder2/file') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
  • \n' + ' ' + '  folder2\n' + '
  • ', output.data) + + def test_view_file_non_ascii_file(self): + """ Test the view_file with a non-ascii file name. """ + + # View file with a non-ascii name + tests.add_commit_git_repo( + os.path.join(self.path, 'repos', 'test.git'), + ncommits=1, filename='Šource') + output = self.app.get('/test/blob/master/f/Šource') + self.assertEqual(output.status_code, 200) + self.assertEqual(output.headers['Content-Type'].lower(), + 'text/html; charset=utf-8') + self.assertIn('  Šource', output.data) + self.assertIn('
     bar
    ', output.data) + self.assertIn( + '', output.data) + self.assertTrue( + '' + in output.data + or + '' in output.data + ) + + def test_view_file_fork_and_edit_logged_out(self): + """ Test the view_file fork and edit button presence when logged + out. + """ + + # not logged in, no edit button but fork & edit is there + output = self.app.get('/test/blob/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertNotIn( + '' + 'Edit', output.data) + self.assertIn( + 'onclick="fork_project.submit();">\n ' + ' Fork and Edit', output.data) + + def test_view_file_fork_and_edit_logged_out(self): + """ Test the view_file fork and edit button presence when logged + in. + """ + + # logged in, both buttons are there + user = tests.FakeUser(username='pingou') + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/blob/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertIn( + '' + 'Edit', output.data) + self.assertIn( + 'onclick="fork_project.submit();">\n ' + ' Fork and Edit', output.data) + + +class PagureFlaskRepoViewFileForktests(PagureFlaskRepoViewFiletests): + """ Tests for view_file endpoint of the flask pagure app for a fork """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskRepoViewFileForktests, self).setUp() + + # Add a fork of a fork + item = pagure.lib.model.Project( + user_id=1, # pingou + name='test3', + description='test project #3', + is_fork=True, + parent_id=1, + hook_token='aaabbbppp', + ) + self.session.add(item) + self.session.commit() + + tests.add_content_git_repo( + os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git')) + tests.add_readme_git_repo( + os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git')) + tests.add_commit_git_repo( + os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'), + ncommits=10) + + def test_view_file_nested_file_in_fork(self): + """ Test the view_file with a nested file in fork. """ + # Verify the nav links correctly when viewing a file/folder in a fork. + output = self.app.get( + '/fork/pingou/test3/blob/master/f/folder1/folder2/file') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
  • \n' + '   ' + 'folder2\n
  • ', output.data) + + def test_view_file_in_branch_in_fork(self): + """ Test the view_file in a specific branch of a fork. """ + output = self.app.get('/fork/pingou/test3/blob/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertIn('
    Row 0
    Row 0
    ', output.data) + self.assertIn( + '', + output.data) + self.assertIn( + '', output.data) + + def test_view_file_fork_and_edit_on_fork_logged_out(self): + """ Test the view_file on a text file on a fork when logged out. """ + + # not logged in, no edit button but fork & edit is there + output = self.app.get('/fork/pingou/test3/blob/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertNotIn( + '' + 'Edit', output.data) + self.assertIn( + 'onclick="fork_project.submit();">\n ' + ' Fork and Edit', output.data) + + def test_view_file_fork_and_edit_on_your_fork(self): + """ Test the view_file on a text file on your fork when logged in. + """ + + # logged in, but it's your own fork, so just edit button is there + user = tests.FakeUser(username='pingou') + with tests.user_set(pagure.APP, user): + output = self.app.get('/fork/pingou/test3/blob/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertIn( + '' + 'Edit', output.data) + self.assertNotIn( + 'onclick="fork_project.submit();">\n ' + ' Fork and Edit', output.data) + + def test_view_file_fork_and_edit_on_a_fork(self): + """ Test the view_file on a text file on somone else's fork when + logged in. + """ + + # logged in, but it's not your fork, so only fork and edit button + # is there + user = tests.FakeUser(username='foo') + with tests.user_set(pagure.APP, user): + output = self.app.get('/fork/pingou/test3/blob/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertNotIn( + '' + 'Edit', output.data) + self.assertIn( + 'onclick="fork_project.submit();">\n ' + ' Fork and Edit', output.data) + + +if __name__ == '__main__': + unittest.main(verbosity=2)
     barRow 0