# -*- coding: utf-8 -*- """ (c) 2015-2016 - Copyright Red Hat Inc Authors: Pierre-Yves Chibon """ __requires__ = ['SQLAlchemy >= 0.8'] import pkg_resources import datetime import json import unittest import shutil import sys import tempfile import os import pygit2 from mock import patch sys.path.insert(0, os.path.join(os.path.dirname( os.path.abspath(__file__)), '..')) import pagure.lib import tests from pagure.lib.repo import PagureRepo class PagureFlaskRepotests(tests.Modeltests): """ Tests for flask app controller of pagure """ def setUp(self): """ Set up the environnment, ran before every tests. """ super(PagureFlaskRepotests, 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['GIT_FOLDER'] = tests.HERE pagure.APP.config['FORK_FOLDER'] = os.path.join( tests.HERE, 'forks') pagure.APP.config['REQUESTS_FOLDER'] = os.path.join( tests.HERE, 'requests') pagure.APP.config['TICKETS_FOLDER'] = os.path.join( tests.HERE, 'tickets') pagure.APP.config['DOCS_FOLDER'] = os.path.join( tests.HERE, 'docs') pagure.APP.config['UPLOAD_FOLDER_PATH'] = os.path.join( tests.HERE, 'releases') self.app = pagure.APP.test_client() @patch('pagure.ui.repo.admin_session_timedout') def test_add_user_when_user_mngt_off(self, ast): """ Test the add_user endpoint when user management is turned off in the pagure instance """ pagure.APP.config['ENABLE_USER_MNGT'] = False ast.return_value = False output = self.app.get('/foo/adduser') self.assertEqual(output.status_code, 302) user = tests.FakeUser(username='pingou') with tests.user_set(pagure.APP, user): tests.create_projects(self.session) output = self.app.get('/test/adduser') self.assertEqual(output.status_code, 404) #just get the csrf token pagure.APP.config['ENABLE_USER_MNGT'] = True output = self.app.get('/test/adduser') csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] pagure.APP.config['ENABLE_USER_MNGT'] = False data = { 'user': 'ralph', } output = self.app.post('/test/adduser', data=data) self.assertEqual(output.status_code, 404) data['csrf_token'] = csrf_token output = self.app.post('/test/adduser', data=data) self.assertEqual(output.status_code, 404) data['user'] = 'foo' tests.create_projects_git(tests.HERE) output = self.app.post( '/test/adduser', data=data, follow_redirects=True) self.assertEqual(output.status_code, 404) pagure.APP.config['ENABLE_USER_MNGT'] = True @patch('pagure.ui.repo.admin_session_timedout') def test_add_user(self, ast): """ Test the add_user endpoint. """ ast.return_value = False output = self.app.get('/foo/adduser') self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(pagure.APP, user): output = self.app.get('/foo/adduser') self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.get('/test/adduser') self.assertEqual(output.status_code, 403) ast.return_value = True output = self.app.get('/test/adduser') self.assertEqual(output.status_code, 302) # Redirect also happens for POST request output = self.app.post('/test/adduser') self.assertEqual(output.status_code, 302) # Need to do this un-authentified since our fake user isn't in the DB # Check the message flashed during the redirect output = self.app.get('/') self.assertEqual(output.status_code, 200) self.assertIn( '\n Action canceled, try it ' 'again',output.data) ast.return_value = False user.username = 'pingou' with tests.user_set(pagure.APP, user): output = self.app.get('/test/adduser') self.assertEqual(output.status_code, 200) self.assertIn('Add user to the', output.data) csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] data = { 'user': 'ralph', } output = self.app.post('/test/adduser', data=data) self.assertEqual(output.status_code, 200) self.assertTrue('Add user to the' in output.data) data['csrf_token'] = csrf_token output = self.app.post('/test/adduser', data=data) self.assertEqual(output.status_code, 200) self.assertIn('Add user to the', output.data) self.assertIn( '\n No user "ralph" ' 'found', output.data) data['user'] = 'foo' tests.create_projects_git(tests.HERE) output = self.app.post( '/test/adduser', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '\n User added', output.data) @patch('pagure.ui.repo.admin_session_timedout') def test_add_group_project_when_user_mngt_off(self, ast): """ Test the add_group_project endpoint when user management is turned off in the pagure instance""" pagure.APP.config['ENABLE_USER_MNGT'] = False ast.return_value = False output = self.app.get('/foo/addgroup') self.assertEqual(output.status_code, 302) msg = pagure.lib.add_group( self.session, group_name='foo', group_type='bar', user='pingou', is_admin=False, blacklist=pagure.APP.config['BLACKLISTED_GROUPS'], ) self.session.commit() self.assertEqual(msg, 'User `pingou` added to the group `foo`.') user = tests.FakeUser(username='pingou') with tests.user_set(pagure.APP, user): output = self.app.get('/test/addgroup') self.assertEqual(output.status_code, 404) #just get the csrf token pagure.APP.config['ENABLE_USER_MNGT'] = True tests.create_projects(self.session) output = self.app.get('/test/addgroup') csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] pagure.APP.config['ENABLE_USER_MNGT'] = False data = { 'group': 'ralph', } output = self.app.post('/test/addgroup', data=data) self.assertEqual(output.status_code, 404) data['csrf_token'] = csrf_token output = self.app.post('/test/addgroup', data=data) self.assertEqual(output.status_code, 404) data['group'] = 'foo' tests.create_projects_git(tests.HERE) output = self.app.post( '/test/addgroup', data=data, follow_redirects=True) self.assertEqual(output.status_code, 404) pagure.APP.config['ENABLE_USER_MNGT'] = True @patch('pagure.ui.repo.admin_session_timedout') def test_add_group_project(self, ast): """ Test the add_group_project endpoint. """ ast.return_value = False output = self.app.get('/foo/addgroup') self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(pagure.APP, user): output = self.app.get('/foo/addgroup') self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.get('/test/addgroup') self.assertEqual(output.status_code, 403) ast.return_value = True output = self.app.get('/test/addgroup') self.assertEqual(output.status_code, 302) # Redirect also happens for POST request output = self.app.post('/test/addgroup') self.assertEqual(output.status_code, 302) # Need to do this un-authentified since our fake user isn't in the DB # Check the message flashed during the redirect output = self.app.get('/') self.assertEqual(output.status_code, 200) self.assertIn( '\n Action canceled, try it ' 'again', output.data) ast.return_value = False msg = pagure.lib.add_group( self.session, group_name='foo', group_type='bar', user='pingou', is_admin=False, blacklist=pagure.APP.config['BLACKLISTED_GROUPS'], ) self.session.commit() self.assertEqual(msg, 'User `pingou` added to the group `foo`.') user.username = 'pingou' with tests.user_set(pagure.APP, user): output = self.app.get('/test/addgroup') self.assertEqual(output.status_code, 200) self.assertTrue('Add group to the' in output.data) csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] data = { 'group': 'ralph', } output = self.app.post('/test/addgroup', data=data) self.assertEqual(output.status_code, 200) self.assertTrue('Add group to the' in output.data) data['csrf_token'] = csrf_token output = self.app.post('/test/addgroup', data=data) self.assertEqual(output.status_code, 200) self.assertTrue('Add group to the' in output.data) self.assertIn( '\n No group ralph found.', output.data) data['group'] = 'foo' tests.create_projects_git(tests.HERE) output = self.app.post( '/test/addgroup', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '\n Group added', output.data) @patch('pagure.ui.repo.admin_session_timedout') def test_remove_user_when_user_mngt_off(self, ast): """ Test the remove_user endpoint when user management is turned off in the pagure instance""" pagure.APP.config['ENABLE_USER_MNGT'] = False ast.return_value = False output = self.app.post('/foo/dropuser/1') self.assertEqual(output.status_code, 302) user = tests.FakeUser(username='pingou') with tests.user_set(pagure.APP, user): tests.create_projects(self.session) tests.create_projects_git(tests.HERE) output = self.app.post('/test/settings') csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] data = {'csrf_token': csrf_token} output = self.app.post( '/test/dropuser/2', data=data, follow_redirects=True) self.assertEqual(output.status_code, 404) # Add an user to a project repo = pagure.lib.get_project(self.session, 'test') msg = pagure.lib.add_user_to_project( session=self.session, project=repo, new_user='foo', user='pingou', ) self.session.commit() self.assertEqual(msg, 'User added') with tests.user_set(pagure.APP, user): output = self.app.post('/test/dropuser/2', follow_redirects=True) self.assertEqual(output.status_code, 404) data = {'csrf_token': csrf_token} output = self.app.post( '/test/dropuser/2', data=data, follow_redirects=True) self.assertEqual(output.status_code, 404) pagure.APP.config['ENABLE_USER_MNGT'] = True @patch('pagure.ui.repo.admin_session_timedout') def test_remove_user(self, ast): """ Test the remove_user endpoint. """ ast.return_value = False output = self.app.post('/foo/dropuser/1') self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(pagure.APP, user): output = self.app.post('/foo/dropuser/1') self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.post('/test/dropuser/1') self.assertEqual(output.status_code, 403) ast.return_value = True output = self.app.post('/test/dropuser/1') self.assertEqual(output.status_code, 302) ast.return_value = False user.username = 'pingou' with tests.user_set(pagure.APP, user): tests.create_projects_git(tests.HERE) output = self.app.post('/test/settings') csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] data = {'csrf_token': csrf_token} output = self.app.post( '/test/dropuser/2', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '\n User does not have commit or cannot ' 'loose it right', output.data) # Add an user to a project repo = pagure.lib.get_project(self.session, 'test') msg = pagure.lib.add_user_to_project( session=self.session, project=repo, new_user='foo', user='pingou', ) self.session.commit() self.assertEqual(msg, 'User added') with tests.user_set(pagure.APP, user): output = self.app.post('/test/dropuser/2', follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertNotIn( '\n User removed', output.data) data = {'csrf_token': csrf_token} output = self.app.post( '/test/dropuser/2', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '\n User removed', output.data) @patch('pagure.ui.repo.admin_session_timedout') def test_remove_group_project_when_user_mngt_off(self, ast): """ Test the remove_group_project endpoint when user management is turned off in the pagure instance""" pagure.APP.config['ENABLE_USER_MNGT'] = False ast.return_value = False output = self.app.post('/foo/dropgroup/1') self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(pagure.APP, user): tests.create_projects(self.session) user.username = 'pingou' with tests.user_set(pagure.APP, user): tests.create_projects_git(tests.HERE) output = self.app.post('/test/settings') csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] data = {'csrf_token': csrf_token} output = self.app.post( '/test/dropgroup/2', data=data, follow_redirects=True) self.assertEqual(output.status_code, 404) # Create the new group msg = pagure.lib.add_group( session=self.session, group_name='testgrp', group_type='user', user='pingou', is_admin=False, blacklist=[], ) self.assertEqual(msg, 'User `pingou` added to the group `testgrp`.') self.session.commit() repo = pagure.lib.get_project(self.session, 'test') # Add the group to a project msg = pagure.lib.add_group_to_project( session=self.session, project=repo, new_group='testgrp', user='pingou', ) self.session.commit() self.assertEqual(msg, 'Group added') with tests.user_set(pagure.APP, user): output = self.app.post('/test/dropgroup/1', follow_redirects=True) self.assertEqual(output.status_code, 404) data = {'csrf_token': csrf_token} output = self.app.post( '/test/dropgroup/1', data=data, follow_redirects=True) self.assertEqual(output.status_code, 404) pagure.APP.config['ENABLE_USER_MNGT'] = True @patch('pagure.ui.repo.admin_session_timedout') def test_remove_group_project(self, ast): """ Test the remove_group_project endpoint. """ ast.return_value = False output = self.app.post('/foo/dropgroup/1') self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(pagure.APP, user): output = self.app.post('/foo/dropgroup/1') self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.post('/test/dropgroup/1') self.assertEqual(output.status_code, 403) ast.return_value = True output = self.app.post('/test/dropgroup/1') self.assertEqual(output.status_code, 302) ast.return_value = False user.username = 'pingou' with tests.user_set(pagure.APP, user): tests.create_projects_git(tests.HERE) output = self.app.post('/test/settings') csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] data = {'csrf_token': csrf_token} output = self.app.post( '/test/dropgroup/2', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '\n ' 'Group does not seem to be part of this project', output.data) # Create the new group msg = pagure.lib.add_group( session=self.session, group_name='testgrp', group_type='user', user='pingou', is_admin=False, blacklist=[], ) self.assertEqual(msg, 'User `pingou` added to the group `testgrp`.') self.session.commit() repo = pagure.lib.get_project(self.session, 'test') # Add the group to a project msg = pagure.lib.add_group_to_project( session=self.session, project=repo, new_group='testgrp', user='pingou', ) self.session.commit() self.assertEqual(msg, 'Group added') with tests.user_set(pagure.APP, user): output = self.app.post('/test/dropgroup/1', follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertNotIn( '\n Group removed', output.data) data = {'csrf_token': csrf_token} output = self.app.post( '/test/dropgroup/1', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '\n Group removed', output.data) @patch('pagure.ui.repo.admin_session_timedout') def test_update_project(self, ast): """ Test the update_project endpoint. """ ast.return_value = True output = self.app.post('/foo/update') self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(pagure.APP, user): output = self.app.post('/foo/update') self.assertEqual(output.status_code, 302) ast.return_value = False output = self.app.post('/foo/update') self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.post('/test/update') self.assertEqual(output.status_code, 403) user.username = 'pingou' with tests.user_set(pagure.APP, user): tests.create_projects_git(tests.HERE) output = self.app.post('/test/update', follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] data = { 'description': 'new description for test project #1', 'csrf_token': csrf_token, } output = self.app.post( '/test/update', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '', output.data) self.assertIn( '\n Project updated', output.data) # Edit the avatar_email data = { 'description': 'new description for test project #1', 'avatar_email': 'pingou@fp.o', 'csrf_token': csrf_token, } output = self.app.post( '/test/update', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '', output.data) self.assertIn( '\n Project updated', output.data) # Reset the avatar_email data = { 'description': 'new description for test project #1', 'avatar_email': '', 'csrf_token': csrf_token, } output = self.app.post( '/test/update', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '', output.data) self.assertIn( '\n Project updated', output.data) @patch('pagure.ui.repo.admin_session_timedout') def test_view_settings(self, ast): """ Test the view_settings endpoint. """ ast.return_value = False output = self.app.get('/foo/settings') self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(pagure.APP, user): output = self.app.get('/foo/settings') self.assertEqual(output.status_code, 404) tests.create_projects(self.session) tests.create_projects_git(tests.HERE) output = self.app.get('/test/settings') self.assertEqual(output.status_code, 403) user.username = 'pingou' with tests.user_set(pagure.APP, user): ast.return_value = True output = self.app.get('/test/settings') self.assertEqual(output.status_code, 302) ast.return_value = False output = self.app.get('/test/settings') self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) # Both checkbox checked before self.assertIn( '', output.data) self.assertIn( '', output.data) csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] data = {} output = self.app.post( '/test/settings', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) # Both checkbox are still checked output = self.app.get('/test/settings', follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '', output.data) self.assertIn( '', output.data) data = {'csrf_token': csrf_token} output = self.app.post( '/test/settings', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Overview - test - Pagure', output.data) self.assertIn( '
\n' 'test project #1
', output.data) self.assertIn( '\n Edited successfully ' 'settings of repo: test', output.data) # Both checkbox are now un-checked output = self.app.get('/test/settings', follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '', output.data) self.assertIn( '', output.data) data = { 'csrf_token': csrf_token, 'pull_requests': 'y', 'issue_tracker': 'y', } output = self.app.post( '/test/settings', data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Overview - test - Pagure', output.data) self.assertIn( '
\n' 'test project #1
', output.data) self.assertIn( '\n Edited successfully ' 'settings of repo: test', output.data) # Both checkbox are again checked output = self.app.get('/test/settings', follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( 'Settings - test - Pagure', output.data) self.assertIn('

Settings for test

', output.data) self.assertIn( '', output.data) self.assertIn( '', output.data) def test_view_forks(self): """ Test the view_forks endpoint. """ output = self.app.get('/foo/forks', follow_redirects=True) self.assertEqual(output.status_code, 404) tests.create_projects(self.session) tests.create_projects_git(tests.HERE, bare=True) output = self.app.get('/test/forks', follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertTrue('This project has not been forked.' in output.data) def test_view_repo(self): """ Test the view_repo endpoint. """ output = self.app.get('/foo') # No project registered in the DB self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.get('/test') # No git repo associated self.assertEqual(output.status_code, 404) tests.create_projects_git(tests.HERE, bare=True) output = self.app.get('/test') self.assertEqual(output.status_code, 200) self.assertTrue('

This repo is brand new!

' in output.data) self.assertIn( '
\n' 'test project #1
', output.data) output = self.app.get('/test/') self.assertEqual(output.status_code, 200) self.assertTrue('

This repo is brand new!

' in output.data) self.assertIn( '
\n' 'test project #1
', output.data) # Add some content to the git repo tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git')) tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git')) output = self.app.get('/test') self.assertEqual(output.status_code, 200) self.assertFalse('

This repo is brand new!

' in output.data) self.assertFalse('Forked from' in output.data) self.assertIn( '
\n' 'test project #1
', output.data) # Turn that repo into a fork repo = pagure.lib.get_project(self.session, 'test') repo.parent_id = 2 self.session.add(repo) self.session.commit() # View the repo in the UI output = self.app.get('/test') self.assertEqual(output.status_code, 404) # Add some content to the git repo tests.add_content_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test.git')) tests.add_readme_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test.git')) output = self.app.get('/fork/pingou/test') self.assertEqual(output.status_code, 200) self.assertFalse('

This repo is brand new!

' in output.data) self.assertIn( '
\n' 'test project #1
', output.data) self.assertTrue('Forked from' in output.data) # Add a fork of a fork item = pagure.lib.model.Project( user_id=1, # pingou name='test3', description='test project #3', parent_id=1, hook_token='aaabbbmmm', ) self.session.add(item) self.session.commit() tests.add_content_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) tests.add_readme_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) tests.add_commit_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'), ncommits=10) output = self.app.get('/fork/pingou/test3') self.assertEqual(output.status_code, 200) self.assertFalse('

This repo is brand new!

' in output.data) self.assertIn( '
\n' 'test project #3
', output.data) self.assertTrue('Forked from' in output.data) def test_view_repo_empty(self): """ Test the view_repo endpoint on a repo w/o master branch. """ tests.create_projects(self.session) # Create a git repo to play with gitrepo = os.path.join(tests.HERE, 'test.git') pygit2.init_repository(gitrepo, bare=True) # Create a fork of this repo newpath = tempfile.mkdtemp(prefix='pagure-viewrepo-test') new_repo = pygit2.clone_repository(gitrepo, newpath) # Edit the sources file again with open(os.path.join(newpath, 'sources'), 'w') as stream: stream.write('foo\n bar\nbaz\n boose') new_repo.index.add('sources') new_repo.index.write() # Commits the files added tree = new_repo.index.write_tree() author = pygit2.Signature( 'Alice Author', 'alice@authors.tld') committer = pygit2.Signature( 'Cecil Committer', 'cecil@committers.tld') new_repo.create_commit( 'refs/heads/feature', author, committer, 'A commit on branch feature', tree, [] ) refname = 'refs/heads/feature' ori_remote = new_repo.remotes[0] PagureRepo.push(ori_remote, refname) output = self.app.get('/test') self.assertEqual(output.status_code, 200) self.assertFalse('

This repo is brand new!

' in output.data) self.assertFalse('Forked from' in output.data) self.assertIn( '
\n' 'test project #1
', output.data) self.assertEqual( output.data.count(''), 0) shutil.rmtree(newpath) def test_view_repo_branch(self): """ Test the view_repo_branch endpoint. """ output = self.app.get('/foo/branch/master') # No project registered in the DB self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.get('/test/branch/master') # No git repo associated self.assertEqual(output.status_code, 404) tests.create_projects_git(tests.HERE, bare=True) output = self.app.get('/test/branch/master') self.assertEqual(output.status_code, 404) # Add some content to the git repo tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git')) tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git')) output = self.app.get('/test/branch/master') self.assertEqual(output.status_code, 200) self.assertFalse('

This repo is brand new!

' in output.data) self.assertFalse('Forked from' in output.data) self.assertIn( '
\n' 'test project #1
', output.data) # Turn that repo into a fork repo = pagure.lib.get_project(self.session, 'test') repo.parent_id = 2 self.session.add(repo) self.session.commit() # View the repo in the UI output = self.app.get('/test/branch/master') self.assertEqual(output.status_code, 404) # Add some content to the git repo tests.add_content_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test.git')) tests.add_readme_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test.git')) output = self.app.get('/fork/pingou/test/branch/master') self.assertEqual(output.status_code, 200) self.assertFalse('

This repo is brand new!

' in output.data) self.assertIn( '
\n' 'test project #1
', output.data) self.assertTrue('Forked from' in output.data) # Add a fork of a fork item = pagure.lib.model.Project( user_id=1, # pingou name='test3', description='test project #3', parent_id=1, hook_token='aaabbbnnn', ) self.session.add(item) self.session.commit() tests.add_content_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) tests.add_readme_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) tests.add_commit_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'), ncommits=10) output = self.app.get('/fork/pingou/test3/branch/master') self.assertEqual(output.status_code, 200) self.assertFalse('

This repo is brand new!

' in output.data) self.assertIn( '
\n' 'test project #3
', output.data) self.assertTrue('Forked from' in output.data) def test_view_commits(self): """ Test the view_commits endpoint. """ output = self.app.get('/foo/commits') # No project registered in the DB self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.get('/test/commits') # No git repo associated self.assertEqual(output.status_code, 404) tests.create_projects_git(tests.HERE, bare=True) output = self.app.get('/test/commits') self.assertEqual(output.status_code, 200) self.assertIn('

This repo is brand new!

', output.data) self.assertIn( '
\n' 'test project #1
', output.data) # Add some content to the git repo tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git')) tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git')) output = self.app.get('/test/commits') self.assertEqual(output.status_code, 200) self.assertNotIn('

This repo is brand new!

', output.data) self.assertNotIn('Forked from', output.data) self.assertIn( '
\n' 'test project #1
', output.data) self.assertIn('Commits - test - Pagure', output.data) output = self.app.get('/test/commits/master') self.assertEqual(output.status_code, 200) self.assertNotIn('

This repo is brand new!

', output.data) self.assertNotIn('Forked from', output.data) self.assertIn( '
\n' 'test project #1
', output.data) # Turn that repo into a fork repo = pagure.lib.get_project(self.session, 'test') repo.parent_id = 2 self.session.add(repo) self.session.commit() # View the repo in the UI output = self.app.get('/test/commits') self.assertEqual(output.status_code, 404) # Add some content to the git repo tests.add_content_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test.git')) tests.add_readme_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test.git')) output = self.app.get('/fork/pingou/test/commits?page=abc') self.assertEqual(output.status_code, 200) self.assertNotIn('

This repo is brand new!

', output.data) self.assertIn( '
\n' 'test project #1
', output.data) self.assertIn('Forked from', output.data) # Add a fork of a fork item = pagure.lib.model.Project( user_id=1, # pingou name='test3', description='test project #3', parent_id=1, hook_token='aaabbbooo', ) self.session.add(item) self.session.commit() tests.add_content_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) tests.add_readme_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) tests.add_commit_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'), ncommits=10) output = self.app.get('/fork/pingou/test3/commits/fobranch') self.assertEqual(output.status_code, 404) output = self.app.get('/fork/pingou/test3/commits') self.assertEqual(output.status_code, 200) self.assertNotIn('

This repo is brand new!

', output.data) self.assertIn( '
\n' 'test project #3
', output.data) self.assertIn('Forked from', output.data) def test_compare_commits(self): """ Test the compare_commits endpoint. """ # First two commits comparison def compare_first_two(c1, c2): # View commits comparison output = self.app.get('/test/c/%s..%s' % (c2.oid.hex, c1.oid.hex)) self.assertEqual(output.status_code, 200) self.assertIn( 'Diff from %s to %s - test - Pagure' % (c2.oid.hex, c1.oid.hex), output.data) self.assertIn( '
%s .. %s
' % (c2.oid.hex, c1.oid.hex), output.data) self.assertIn( 'Commits \n ' + '' + '\n 2\n ', output.data) self.assertIn( '' + '@@ -1,2 +1,1 @@', output.data) self.assertIn( '- Row 0', output.data) # View inverse commits comparison output = self.app.get('/test/c/%s..%s' % (c1.oid.hex, c2.oid.hex)) self.assertEqual(output.status_code, 200) self.assertIn( 'Diff from %s to %s - test - Pagure' % (c1.oid.hex, c2.oid.hex), output.data) self.assertIn( 'Commits \n ' + '' + '\n 2\n ', output.data) self.assertIn( '
%s .. %s
' % (c1.oid.hex, c2.oid.hex), output.data) self.assertIn( '' + '@@ -1,1 +1,2 @@', output.data) self.assertIn( '+ Row 0', output.data) def compare_all(c1, c3): # View commits comparison output = self.app.get('/test/c/%s..%s' % (c1.oid.hex, c3.oid.hex)) self.assertEqual(output.status_code, 200) self.assertIn( 'Diff from %s to %s - test - Pagure' % (c1.oid.hex, c3.oid.hex), output.data) self.assertIn( '
%s .. %s
' % (c1.oid.hex, c3.oid.hex), output.data) self.assertIn( '' + '@@ -1,1 +1,2 @@', output.data) self.assertIn( '+ Row 0', output.data) self.assertEqual( output.data.count( '+ Row 0'), 2) self.assertIn( 'Commits \n ' + '' + '\n 3\n ', output.data) self.assertIn( 'title="View file as of 4829cf">Šource', output.data) self.assertIn( '
file added
', output.data) # View inverse commits comparison output = self.app.get( '/test/c/%s..%s' % (c3.oid.hex, c1.oid.hex)) self.assertEqual(output.status_code, 200) self.assertIn( 'Diff from %s to %s - test - Pagure' % (c3.oid.hex, c1.oid.hex), output.data) self.assertIn( '
%s .. %s
' % (c3.oid.hex, c1.oid.hex), output.data) self.assertIn( '' + '@@ -1,2 +1,1 @@', output.data) self.assertIn( '- Row 0', output.data) self.assertEqual( output.data.count( '- Row 0'), 1) self.assertIn( 'Commits \n ' + '' + '\n 3\n ', output.data) self.assertIn( 'title="View file as of 000000">Šource', output.data) self.assertIn( '
file removed
', output.data) output = self.app.get('/foo/bar') # No project registered in the DB self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.get('/test/bar') # No git repo associated self.assertEqual(output.status_code, 404) tests.create_projects_git(tests.HERE, bare=True) output = self.app.get('/test/bar') self.assertEqual(output.status_code, 404) repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git')) # Add one commit to git repo tests.add_commit_git_repo( os.path.join(tests.HERE, 'test.git'), ncommits=1) c1 = repo.revparse_single('HEAD') # Add another commit to git repo tests.add_commit_git_repo( os.path.join(tests.HERE, 'test.git'), ncommits=1) c2 = repo.revparse_single('HEAD') # Add one more commit to git repo tests.add_commit_git_repo( os.path.join(tests.HERE, 'test.git'), ncommits=1, filename='Šource') c3 = repo.revparse_single('HEAD') compare_first_two(c1, c2) compare_all(c1, c3) user = tests.FakeUser() # Set user logged in with tests.user_set(pagure.APP, user): compare_first_two(c1, c2) compare_all(c1, c3) def test_view_file(self): """ Test the view_file endpoint. """ output = self.app.get('/foo/blob/foo/f/sources') # No project registered in the DB self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.get('/test/blob/foo/f/sources') # No git repo associated self.assertEqual(output.status_code, 404) tests.create_projects_git(tests.HERE, bare=True) output = self.app.get('/test/blob/foo/f/sources') self.assertEqual(output.status_code, 404) # Add some content to the git repo tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git')) tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git')) tests.add_binary_git_repo( os.path.join(tests.HERE, 'test.git'), 'test.jpg') tests.add_binary_git_repo( os.path.join(tests.HERE, 'test.git'), 'test_binary') output = self.app.get('/test/blob/master/foofile') self.assertEqual(output.status_code, 404) # View in a branch 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) # 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) # View by commit id repo = pygit2.Repository(os.path.join(tests.HERE, '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) # 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) # 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) # 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) # View by image name -- with a non-existant file 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) # View file with a non-ascii name tests.add_commit_git_repo( os.path.join(tests.HERE, 'test.git'), ncommits=1, filename='Šource') output = self.app.get('/test/blob/master/f/Šource') self.assertEqual(output.status_code, 200) self.assertIn('  Šource', output.data) self.assertIn('
 bar
', output.data) self.assertIn( '', output.data) self.assertTrue( '' in output.data or '' in output.data ) # Add a fork of a fork item = pagure.lib.model.Project( user_id=1, # pingou name='test3', description='test project #3', parent_id=1, hook_token='aaabbbppp', ) self.session.add(item) self.session.commit() tests.add_content_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) tests.add_readme_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) tests.add_commit_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'), ncommits=10) 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_raw_file(self): """ Test the view_raw_file endpoint. """ output = self.app.get('/foo/raw/foo/sources') # No project registered in the DB self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.get('/test/raw/foo/sources') # No git repo associated self.assertEqual(output.status_code, 404) tests.create_projects_git(tests.HERE, bare=True) output = self.app.get('/test/raw/foo/sources') self.assertEqual(output.status_code, 404) # Add some content to the git repo tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git')) # View first commit output = self.app.get('/test/raw/master') self.assertEqual(output.status_code, 200) self.assertTrue(':Author: Pierre-Yves Chibon' in output.data) # Add some more content to the repo tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git')) tests.add_binary_git_repo( os.path.join(tests.HERE, 'test.git'), 'test.jpg') tests.add_binary_git_repo( os.path.join(tests.HERE, 'test.git'), 'test_binary') output = self.app.get('/test/raw/master/f/foofile') self.assertEqual(output.status_code, 404) # View in a branch output = self.app.get('/test/raw/master/f/sources') self.assertEqual(output.status_code, 200) self.assertTrue('foo\n bar' in output.data) # View what's supposed to be an image output = self.app.get('/test/raw/master/f/test.jpg') self.assertEqual(output.status_code, 200) self.assertTrue(output.data.startswith('\x00\x00\x01\x00')) # View by commit id repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git')) commit = repo.revparse_single('HEAD') output = self.app.get('/test/raw/%s/f/test.jpg' % commit.oid.hex) self.assertEqual(output.status_code, 200) self.assertTrue(output.data.startswith('\x00\x00\x01\x00')) # View by image name -- somehow we support this output = self.app.get('/test/raw/sources/f/test.jpg') self.assertEqual(output.status_code, 200) self.assertTrue(output.data.startswith('\x00\x00\x01\x00')) # View binary file output = self.app.get('/test/raw/sources/f/test_binary') self.assertEqual(output.status_code, 200) self.assertTrue(output.data.startswith('\x00\x00\x01\x00')) # View folder output = self.app.get('/test/raw/master/f/folder1') self.assertEqual(output.status_code, 404) # View by image name -- with a non-existant file output = self.app.get('/test/raw/sources/f/testfoo.jpg') self.assertEqual(output.status_code, 404) output = self.app.get('/test/raw/master/f/folder1/testfoo.jpg') self.assertEqual(output.status_code, 404) output = self.app.get('/test/raw/master/f/') self.assertEqual(output.status_code, 404) output = self.app.get('/test/raw/master') self.assertEqual(output.status_code, 200) self.assertTrue(output.data.startswith( 'diff --git a/test_binary b/test_binary\n')) output = self.app.get('/test/raw/%s' % commit.oid.hex) self.assertEqual(output.status_code, 200) self.assertTrue(output.data.startswith( 'diff --git a/test_binary b/test_binary\n')) # Add a fork of a fork item = pagure.lib.model.Project( user_id=1, # pingou name='test3', description='test project #3', parent_id=1, hook_token='aaabbbqqq', ) self.session.add(item) self.session.commit() tests.add_content_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) tests.add_readme_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) tests.add_commit_git_repo( os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'), ncommits=10) output = self.app.get('/fork/pingou/test3/raw/master/f/sources') self.assertEqual(output.status_code, 200) self.assertTrue('foo\n bar' in output.data) def test_view_commit(self): """ Test the view_commit endpoint. """ output = self.app.get('/foo/c/bar') # No project registered in the DB self.assertEqual(output.status_code, 404) tests.create_projects(self.session) output = self.app.get('/test/c/bar') # No git repo associated self.assertEqual(output.status_code, 404) tests.create_projects_git(tests.HERE, bare=True) output = self.app.get('/test/c/bar') self.assertEqual(output.status_code, 404) # Add a README to the git repo - First commit tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git')) repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git')) commit = repo.revparse_single('HEAD') # View first commit output = self.app.get('/test/c/%s' % commit.oid.hex) self.assertEqual(output.status_code, 200) self.assertTrue( '
 barRow 0