Blame tests/test_pagure_flask_ui_repo_delete_project.py

Pierre-Yves Chibon 3f19a5
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
"""
Pierre-Yves Chibon 3f19a5
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
 Authors:
Pierre-Yves Chibon 3f19a5
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
"""
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 3f19a5
import pkg_resources
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
import sys
Pierre-Yves Chibon 3f19a5
import os
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
from mock import patch, MagicMock
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 3f19a5
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
import pagure
Pierre-Yves Chibon 3f19a5
import pagure.lib
Pierre-Yves Chibon 3f19a5
import tests
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
class PagureFlaskDeleteRepotests(tests.Modeltests):
Pierre-Yves Chibon 3f19a5
    """ Tests for deleting a project in pagure """
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
    def setUp(self):
Pierre-Yves Chibon 3f19a5
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 3f19a5
        super(PagureFlaskDeleteRepotests, self).setUp()
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
        # Create some projects
Pierre-Yves Chibon 3f19a5
        tests.create_projects(self.session)
Pierre-Yves Chibon 3f19a5
        tests.create_projects_git(os.path.join(self.path, 'repos'))
Pierre-Yves Chibon 3f19a5
        self.session.commit()
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
        # Create all the git repos
Pierre-Yves Chibon 3f19a5
        tests.create_projects_git(os.path.join(self.path, 'repos'))
Pierre-Yves Chibon 3f19a5
        tests.create_projects_git(os.path.join(self.path, 'docs'))
Pierre-Yves Chibon 3f19a5
        tests.create_projects_git(
Pierre-Yves Chibon 3f19a5
            os.path.join(self.path, 'tickets'), bare=True)
Pierre-Yves Chibon 3f19a5
        tests.create_projects_git(
Pierre-Yves Chibon 3f19a5
            os.path.join(self.path, 'requests'), bare=True)
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(
Pierre-Yves Chibon 3f19a5
            self.session, project_name='test')
Pierre-Yves Chibon 3f19a5
        self.assertIsNotNone(project)
Pierre-Yves Chibon 1dc86f
        # Ensure the project isn't read-only
Pierre-Yves Chibon 1dc86f
        project.read_only = False
Pierre-Yves Chibon 1dc86f
        self.session.add(project)
Pierre-Yves Chibon 1dc86f
        self.session.commit()
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
        # Create a fork
Pierre-Yves Chibon 3f19a5
        task_id = pagure.lib.fork_project(
Pierre-Yves Chibon 3f19a5
            session=self.session,
Pierre-Yves Chibon 3f19a5
            user='pingou',
Pierre-Yves Chibon 3f19a5
            repo=project,
Pierre-Yves Chibon 3f19a5
            gitfolder=os.path.join(self.path, 'repos'),
Pierre-Yves Chibon 3f19a5
            docfolder=os.path.join(self.path, 'docs'),
Pierre-Yves Chibon 3f19a5
            ticketfolder=os.path.join(self.path, 'tickets'),
Pierre-Yves Chibon 3f19a5
            requestfolder=os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 3f19a5
        )
Pierre-Yves Chibon 3f19a5
        pagure.lib.tasks.get_result(task_id).get()
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
        # Ensure everything was correctly created
Pierre-Yves Chibon 3f19a5
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon 3f19a5
        self.assertEqual(len(projects), 4)
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
Pierre-Yves Chibon 3f19a5
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Clement Verna afe475
    @patch('pagure.decorators.admin_session_timedout',
Pierre-Yves Chibon 3f19a5
           MagicMock(return_value=False))
Pierre-Yves Chibon 3f19a5
    def test_delete_repo_when_turned_off(self):
Pierre-Yves Chibon 3f19a5
        """ Test the delete_repo endpoint for a fork when only deleting main
Pierre-Yves Chibon 3f19a5
        project is forbidden.
Pierre-Yves Chibon 3f19a5
        """
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 3f19a5
            output = self.app.post('/test/delete', follow_redirects=True)
Pierre-Yves Chibon 3f19a5
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon 3f19a5
        self.assertEqual(len(projects), 4)
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon d490c7
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Clement Verna afe475
    @patch('pagure.decorators.admin_session_timedout',
Pierre-Yves Chibon d490c7
           MagicMock(return_value=False))
Pierre-Yves Chibon d490c7
    def test_delete_button_present(self):
Pierre-Yves Chibon d490c7
        """ Test that the delete button is present when deletions are
Pierre-Yves Chibon d490c7
        allowed.
Pierre-Yves Chibon d490c7
        """
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon d490c7
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon d490c7
            output = self.app.get('/test/settings')
Pierre-Yves Chibon d490c7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon d490c7
            self.assertIn('
Pierre-Yves Chibon d490c7
            self.assertIn('  Delete the test project', output.data)
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon d490c7
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon d490c7
        self.assertEqual(len(projects), 4)
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
Pierre-Yves Chibon d490c7
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Clement Verna afe475
    @patch('pagure.decorators.admin_session_timedout',
Pierre-Yves Chibon d490c7
           MagicMock(return_value=False))
Pierre-Yves Chibon d490c7
    def test_delete_button_absent(self):
Pierre-Yves Chibon d490c7
        """ Test that the delete button is absent when deletions are not
Pierre-Yves Chibon d490c7
        allowed.
Pierre-Yves Chibon d490c7
        """
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon d490c7
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon d490c7
            output = self.app.get('/test/settings')
Pierre-Yves Chibon d490c7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon d490c7
            self.assertNotIn('
Pierre-Yves Chibon d490c7
            self.assertNotIn('  Delete the test project', output.data)
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon d490c7
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon d490c7
        self.assertEqual(len(projects), 4)
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': True})
Pierre-Yves Chibon 3f19a5
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Clement Verna afe475
    @patch('pagure.decorators.admin_session_timedout',
Pierre-Yves Chibon 3f19a5
           MagicMock(return_value=False))
Pierre-Yves Chibon 282d02
    def test_delete_fork_when_project_off_refreshing(self):
Pierre-Yves Chibon 282d02
        """ Test the delete_repo endpoint for a fork when only deleting main
Pierre-Yves Chibon 282d02
        project is forbidden but the fork is being refreshed in the backend
Pierre-Yves Chibon 282d02
        """
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(
Pierre-Yves Chibon 282d02
            self.session, project_name='test', user='pingou')
Pierre-Yves Chibon 282d02
        self.assertIsNotNone(project)
Pierre-Yves Chibon 282d02
        # Ensure the project isn't read-only
Pierre-Yves Chibon 282d02
        project.read_only = True
Pierre-Yves Chibon 282d02
        self.session.add(project)
Pierre-Yves Chibon 282d02
        self.session.commit()
Pierre-Yves Chibon 282d02
Pierre-Yves Chibon 282d02
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 282d02
            output = self.app.post(
Pierre-Yves Chibon 282d02
                '/fork/pingou/test/delete', follow_redirects=True)
Pierre-Yves Chibon 282d02
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 282d02
            self.assertIn(
Pierre-Yves Chibon 282d02
                '\n                      The ACLs of this project '
Pierre-Yves Chibon 282d02
                'are being refreshed in the backend this prevents the '
Pierre-Yves Chibon 282d02
                'project from being deleted. Please wait for this task to '
Pierre-Yves Chibon 282d02
                'finish before trying again. Thanks!', output.data)
Pierre-Yves Chibon 282d02
Pierre-Yves Chibon 282d02
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon 282d02
        self.assertEqual(len(projects), 4)
Pierre-Yves Chibon 282d02
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': True})
Pierre-Yves Chibon 282d02
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Clement Verna afe475
    @patch('pagure.decorators.admin_session_timedout',
Pierre-Yves Chibon 282d02
           MagicMock(return_value=False))
Pierre-Yves Chibon 3f19a5
    def test_delete_fork_when_project_off(self):
Pierre-Yves Chibon 3f19a5
        """ Test the delete_repo endpoint for a fork when only deleting main
Pierre-Yves Chibon 3f19a5
        project is forbidden.
Pierre-Yves Chibon 3f19a5
        """
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(
Pierre-Yves Chibon 282d02
            self.session, project_name='test', user='pingou')
Pierre-Yves Chibon 282d02
        self.assertIsNotNone(project)
Pierre-Yves Chibon 282d02
        # Ensure the project isn't read-only
Pierre-Yves Chibon 282d02
        project.read_only = False
Pierre-Yves Chibon 282d02
        self.session.add(project)
Pierre-Yves Chibon 282d02
        self.session.commit()
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 3f19a5
            output = self.app.post(
Pierre-Yves Chibon 3f19a5
                '/fork/pingou/test/delete', follow_redirects=True)
Pierre-Yves Chibon 3f19a5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon 3f19a5
        self.assertEqual(len(projects), 3)
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': False})
Pierre-Yves Chibon 3f19a5
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Clement Verna afe475
    @patch('pagure.decorators.admin_session_timedout',
Pierre-Yves Chibon 3f19a5
           MagicMock(return_value=False))
Pierre-Yves Chibon 3f19a5
    def test_delete_fork_when_fork_and_project_off(self):
Pierre-Yves Chibon 3f19a5
        """ Test the delete_repo endpoint for a fork when deleting fork and
Pierre-Yves Chibon 3f19a5
        project is forbidden.
Pierre-Yves Chibon 3f19a5
        """
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 3f19a5
            output = self.app.post(
Pierre-Yves Chibon 3f19a5
                '/fork/pingou/test/delete', follow_redirects=True)
Pierre-Yves Chibon 3f19a5
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 3f19a5
Pierre-Yves Chibon 3f19a5
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon 3f19a5
        self.assertEqual(len(projects), 4)
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': False})
Pierre-Yves Chibon d490c7
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Clement Verna afe475
    @patch('pagure.decorators.admin_session_timedout',
Pierre-Yves Chibon d490c7
           MagicMock(return_value=False))
Pierre-Yves Chibon d490c7
    def test_delete_fork_button_absent(self):
Pierre-Yves Chibon d490c7
        """ Test that the delete button is absent when deletions are not
Pierre-Yves Chibon d490c7
        allowed.
Pierre-Yves Chibon d490c7
        """
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon d490c7
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon d490c7
            output = self.app.get('/fork/pingou/test/settings')
Pierre-Yves Chibon d490c7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 282d02
            self.assertNotIn(
Pierre-Yves Chibon 282d02
                '
Pierre-Yves Chibon 282d02
            self.assertNotIn(
Pierre-Yves Chibon 282d02
                '  Delete the forks/pingou/test project', output.data)
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon d490c7
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon d490c7
        self.assertEqual(len(projects), 4)
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': True})
Pierre-Yves Chibon d490c7
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Clement Verna afe475
    @patch('pagure.decorators.admin_session_timedout',
Pierre-Yves Chibon d490c7
           MagicMock(return_value=False))
Pierre-Yves Chibon d490c7
    def test_delete_fork_button_fork_del_allowed(self):
Pierre-Yves Chibon d490c7
        """ Test that the delete button is present when deletions of projects
Pierre-Yves Chibon d490c7
        is not allowed but deletions of forks is.
Pierre-Yves Chibon d490c7
        """
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(
Pierre-Yves Chibon 282d02
            self.session, project_name='test', user='pingou')
Pierre-Yves Chibon 282d02
        self.assertIsNotNone(project)
Pierre-Yves Chibon 282d02
        # Ensure the project isn't read-only
Pierre-Yves Chibon 282d02
        project.read_only = False
Pierre-Yves Chibon 282d02
        self.session.add(project)
Pierre-Yves Chibon 282d02
        self.session.commit()
Pierre-Yves Chibon 282d02
Pierre-Yves Chibon d490c7
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon d490c7
            output = self.app.get('/fork/pingou/test/settings')
Pierre-Yves Chibon d490c7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 282d02
            self.assertIn(
Pierre-Yves Chibon 282d02
                '
Pierre-Yves Chibon 282d02
            self.assertIn(
Pierre-Yves Chibon 282d02
                '  Delete the forks/pingou/test project', output.data)
Pierre-Yves Chibon d490c7
Pierre-Yves Chibon d490c7
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon d490c7
        self.assertEqual(len(projects), 4)
Pierre-Yves Chibon 282d02
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
Pierre-Yves Chibon b130e5
    @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': True})
Pierre-Yves Chibon 282d02
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Clement Verna afe475
    @patch('pagure.decorators.admin_session_timedout',
Pierre-Yves Chibon 282d02
           MagicMock(return_value=False))
Pierre-Yves Chibon 282d02
    def test_delete_fork_button_fork_del_allowed_read_only(self):
Pierre-Yves Chibon 282d02
        """ Test that the delete button is absent when deletions of projects
Pierre-Yves Chibon 282d02
        is not allowed but deletions of forks is but fork is still being
Pierre-Yves Chibon 282d02
        processed.
Pierre-Yves Chibon 282d02
        """
Pierre-Yves Chibon 282d02
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(
Pierre-Yves Chibon 282d02
            self.session, project_name='test', user='pingou')
Pierre-Yves Chibon 282d02
        self.assertIsNotNone(project)
Pierre-Yves Chibon 282d02
        # Ensure the project is read-only
Pierre-Yves Chibon 282d02
        project.read_only = True
Pierre-Yves Chibon 282d02
        self.session.add(project)
Pierre-Yves Chibon 282d02
        self.session.commit()
Pierre-Yves Chibon 282d02
Pierre-Yves Chibon 282d02
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 282d02
            output = self.app.get('/fork/pingou/test/settings')
Pierre-Yves Chibon 282d02
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 282d02
            self.assertNotIn(
Pierre-Yves Chibon 282d02
                '
Pierre-Yves Chibon 282d02
            self.assertIn(
Pierre-Yves Chibon 282d02
                'title="Action disabled while project\'s ACLs are being '
Pierre-Yves Chibon 282d02
                'refreshed">', output.data)
Pierre-Yves Chibon 282d02
            self.assertIn(
Pierre-Yves Chibon 282d02
                '  Delete the forks/pingou/test project', output.data)