From a2e9049966b2143612afdc175704673bb10eb5d9 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 03 2020 16:41:02 +0000 Subject: [PATCH 1/4] Create a function dedicated to creating user in the DB for testing This will make it easier to create other users for some tests. Make use of this new function in _populate_db. Signed-off-by: Pierre-Yves Chibon --- diff --git a/tests/__init__.py b/tests/__init__.py index 8aeb7cf..e3fd799 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -240,31 +240,30 @@ tests_state = { } -def _populate_db(session): - # Create a couple of users - item = pagure.lib.model.User( - user="pingou", - fullname="PY C", +def create_user(session, username, fullname, emails): + """ Create an user with the provided information. + Note that `emails` should be a list of emails. + """ + user = pagure.lib.model.User( + user=username, + fullname=fullname, password=b"foo", - default_email="bar@pingou.com", + default_email=emails[0], ) - session.add(item) - item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com") - session.add(item) - item = pagure.lib.model.UserEmail(user_id=1, email="foo@pingou.com") - session.add(item) + session.add(user) + session.flush() + for email in emails: + item = pagure.lib.model.UserEmail(user_id=user.id, email=email) + session.add(item) + session.commit() - item = pagure.lib.model.User( - user="foo", - fullname="foo bar", - password=b"foo", - default_email="foo@bar.com", - ) - session.add(item) - item = pagure.lib.model.UserEmail(user_id=2, email="foo@bar.com") - session.add(item) - session.commit() +def _populate_db(session): + # Create a couple of users + create_user( + session, "pingou", "PY C", ["bar@pingou.com", "foo@pingou.com"] + ) + create_user(session, "foo", "foo bar", ["foo@bar.com"]) def store_eager_results(*args, **kwargs): From 2a0516ac5bbabaf0f92c6e93b00aa7c60dde0c05 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 03 2020 17:15:32 +0000 Subject: [PATCH 2/4] Move all the test testing creating new projects to their own class This will make it easier to test only this use-case as well as re-using code if needed. Signed-off-by: Pierre-Yves Chibon --- diff --git a/tests/test_pagure_flask_ui_app.py b/tests/test_pagure_flask_ui_app.py index c2b7700..790b0cb 100644 --- a/tests/test_pagure_flask_ui_app.py +++ b/tests/test_pagure_flask_ui_app.py @@ -180,193 +180,269 @@ class PagureFlaskApptests(tests.Modeltests): output_text, ) - @patch.dict("pagure.config.config", {"ENABLE_UI_NEW_PROJECTS": False}) - def test_new_project_when_turned_off_in_the_ui(self): - """ Test the new_project endpoint when new project creation is - not allowed in the UI of this pagure instance. """ + @patch.dict( + "pagure.config.config", + { + "PAGURE_ADMIN_USERS": ["pingou"], + "ALLOW_ADMIN_IGNORE_EXISTING_REPOS": True, + }, + ) + def test_adopt_repos(self): + """ Test the new_project endpoint with existing git repo. """ + # Before + projects = pagure.lib.query.search_projects(self.session) + self.assertEqual(len(projects), 0) + tests.create_projects_git(os.path.join(self.path, "repos"), bare=True) + tests.add_content_git_repo( + os.path.join(self.path, "repos", "test.git") + ) - user = tests.FakeUser(username="foo") + user = tests.FakeUser(username="pingou") with tests.user_set(self.app.application, user): - output = self.app.get("/new/") - self.assertEqual(output.status_code, 404) - - data = {"description": "Project #1", "name": "project-1"} + data = { + "csrf_token": self.get_csrf(), + "name": "test", + "description": "Project #1", + } output = self.app.post("/new/", data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - @patch.dict("pagure.config.config", {"ENABLE_UI_NEW_PROJECTS": False}) - def test_new_project_button_when_turned_off_in_the_ui_no_project(self): - """ Test the index endpoint when new project creation is - not allowed in the UI of this pagure instance. """ + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn("The main repo test.git already exists", output_text) - user = tests.FakeUser(username="foo") - with tests.user_set(self.app.application, user): - output = self.app.get("/", follow_redirects=True) + data["ignore_existing_repos"] = "y" + output = self.app.post("/new/", data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) - self.assertIn( - '

My Projects

', - output_text, - ) - # master template - self.assertNotIn( - '