123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- # -*- coding: utf-8 -*-
- """
- (c) 2017 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- """
- from __future__ import unicode_literals, absolute_import
- import sys
- import os
- import pagure_messages
- from mock import ANY, patch, MagicMock
- from fedora_messaging import testing
- sys.path.insert(
- 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
- )
- import pagure.lib.query
- import tests
- class PagureFlaskDeleteRepotests(tests.Modeltests):
- """Tests for deleting a project in pagure"""
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureFlaskDeleteRepotests, self).setUp()
- # Create some projects
- tests.create_projects(self.session)
- tests.create_projects_git(os.path.join(self.path, "repos"))
- self.session.commit()
- # Create all the git repos
- tests.create_projects_git(os.path.join(self.path, "repos"))
- tests.create_projects_git(os.path.join(self.path, "repos", "docs"))
- tests.create_projects_git(
- os.path.join(self.path, "repos", "tickets"), bare=True
- )
- tests.create_projects_git(
- os.path.join(self.path, "repos", "requests"), bare=True
- )
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- self.assertIsNotNone(project)
- # Ensure the project isn't read-only
- project.read_only = False
- self.session.add(project)
- self.session.commit()
- # Create a fork
- task = pagure.lib.query.fork_project(
- session=self.session, user="pingou", repo=project
- )
- task.get()
- # Ensure everything was correctly created
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 4)
- @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- @patch(
- "pagure.decorators.admin_session_timedout",
- MagicMock(return_value=False),
- )
- def test_delete_repo_when_turned_off(self):
- """Test the delete_repo endpoint for a fork when only deleting main
- project is forbidden.
- """
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- output = self.app.post("/test/delete", follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 4)
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- @patch(
- "pagure.decorators.admin_session_timedout",
- MagicMock(return_value=False),
- )
- def test_delete_button_present(self):
- """Test that the delete button is present when deletions are
- allowed.
- """
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/settings")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<form action="/test/delete"', output.get_data(as_text=True)
- )
- self.assertIn(
- " Delete the test project", output.get_data(as_text=True)
- )
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 4)
- @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- @patch(
- "pagure.decorators.admin_session_timedout",
- MagicMock(return_value=False),
- )
- def test_delete_button_absent(self):
- """Test that the delete button is absent when deletions are not
- allowed.
- """
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/settings")
- self.assertEqual(output.status_code, 200)
- self.assertNotIn(
- '<form action="/test/delete"', output.get_data(as_text=True)
- )
- self.assertNotIn(
- " Delete the test project", output.get_data(as_text=True)
- )
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 4)
- @patch.dict(
- "pagure.config.config", {"FEDORA_MESSAGING_NOTIFICATIONS": True}
- )
- @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
- @patch.dict("pagure.config.config", {"ENABLE_DEL_FORKS": True})
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- @patch(
- "pagure.decorators.admin_session_timedout",
- MagicMock(return_value=False),
- )
- def test_delete_fork_when_project_off(self):
- """Test the delete_repo endpoint for a fork when only deleting main
- project is forbidden.
- """
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test", user="pingou"
- )
- self.assertIsNotNone(project)
- # Ensure the project isn't read-only
- project.read_only = False
- self.session.add(project)
- self.session.commit()
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- with testing.mock_sends(
- pagure_messages.ProjectDeletedV1(
- topic="pagure.project.deleted",
- body={
- "project": {
- "id": 4,
- "name": "test",
- "fullname": "forks/pingou/test",
- "url_path": "fork/pingou/test",
- "full_url": "http://localhost.localdomain/fork/pingou/test",
- "description": "test project #1",
- "namespace": None,
- "parent": {
- "id": 1,
- "name": "test",
- "fullname": "test",
- "url_path": "test",
- "full_url": "http://localhost.localdomain/test",
- "description": "test project #1",
- "namespace": None,
- "parent": None,
- "date_created": ANY,
- "date_modified": ANY,
- "user": {
- "name": "pingou",
- "fullname": "PY C",
- "url_path": "user/pingou",
- "full_url": "http://localhost.localdomain/user/pingou",
- },
- "access_users": {
- "owner": ["pingou"],
- "admin": [],
- "commit": [],
- "collaborator": [],
- "ticket": [],
- },
- "access_groups": {
- "admin": [],
- "commit": [],
- "collaborator": [],
- "ticket": [],
- },
- "tags": [],
- "priorities": {},
- "custom_keys": [],
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "milestones": {},
- },
- "date_created": ANY,
- "date_modified": ANY,
- "user": {
- "name": "pingou",
- "fullname": "PY C",
- "url_path": "user/pingou",
- "full_url": "http://localhost.localdomain/user/pingou",
- },
- "access_users": {
- "owner": ["pingou"],
- "admin": [],
- "commit": [],
- "collaborator": [],
- "ticket": [],
- },
- "access_groups": {
- "admin": [],
- "commit": [],
- "collaborator": [],
- "ticket": [],
- },
- "tags": [],
- "priorities": {},
- "custom_keys": [],
- "close_status": [],
- "milestones": {},
- },
- "agent": "pingou",
- },
- )
- ):
- output = self.app.post(
- "/fork/pingou/test/delete", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 3)
- @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
- @patch.dict("pagure.config.config", {"ENABLE_DEL_FORKS": False})
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- @patch(
- "pagure.decorators.admin_session_timedout",
- MagicMock(return_value=False),
- )
- def test_delete_fork_when_fork_and_project_off(self):
- """Test the delete_repo endpoint for a fork when deleting fork and
- project is forbidden.
- """
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- output = self.app.post(
- "/fork/pingou/test/delete", follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 4)
- @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
- @patch.dict("pagure.config.config", {"ENABLE_DEL_FORKS": False})
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- @patch(
- "pagure.decorators.admin_session_timedout",
- MagicMock(return_value=False),
- )
- def test_delete_fork_button_absent(self):
- """Test that the delete button is absent when deletions are not
- allowed.
- """
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- output = self.app.get("/fork/pingou/test/settings")
- self.assertEqual(output.status_code, 200)
- self.assertNotIn(
- '<form action="/fork/pingou/test/delete"',
- output.get_data(as_text=True),
- )
- self.assertNotIn(
- " Delete the forks/pingou/test project",
- output.get_data(as_text=True),
- )
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 4)
- @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
- @patch.dict("pagure.config.config", {"ENABLE_DEL_FORKS": True})
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- @patch(
- "pagure.decorators.admin_session_timedout",
- MagicMock(return_value=False),
- )
- def test_delete_fork_button_fork_del_allowed(self):
- """Test that the delete button is present when deletions of projects
- is not allowed but deletions of forks is.
- """
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test", user="pingou"
- )
- self.assertIsNotNone(project)
- # Ensure the project isn't read-only
- project.read_only = False
- self.session.add(project)
- self.session.commit()
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- output = self.app.get("/fork/pingou/test/settings")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<form action="/fork/pingou/test/delete"',
- output.get_data(as_text=True),
- )
- self.assertIn(
- " Delete the forks/pingou/test project",
- output.get_data(as_text=True),
- )
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 4)
|