# -*- coding: utf-8 -*-
"""
(c) 2015-2018 - Copyright Red Hat Inc
Authors:
Pierre-Yves Chibon No issue tracker found for this project No such user found in the database: username No issue tracker found for this project Woohoo a second comment! Woohoo a second comment! Woohoo a second comment!New Issue
\n',
output_text,
)
self.assertEqual(output_text.count("This field is required."), 2)
data["title"] = "Test issue"
output = self.app.post("/test/new_issue", data=data)
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
'New Issue
\n',
output_text,
)
self.assertEqual(output_text.count("This field is required."), 1)
data["issue_content"] = "We really should improve on this issue"
data["status"] = "Open"
output = self.app.post("/test/new_issue", data=data)
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
'New Issue
\n',
output_text,
)
self.assertEqual(output_text.count("This field is required."), 0)
# Invalid user
data["csrf_token"] = csrf_token
output = self.app.post("/test/new_issue", data=data)
self.assertEqual(output.status_code, 404)
self.assertIn(
"New Issue
\n',
output.get_data(as_text=True),
)
csrf_token = self.get_csrf()
with open(os.path.join(tests.HERE, "placebo.png"), "rb") as stream:
data = {
"title": "Test issue",
"issue_content": "We really should improve on this issue\n"
"",
"status": "Open",
"filestream": stream,
"enctype": "multipart/form-data",
"csrf_token": csrf_token,
}
output = self.app.post(
"/test/new_issue", data=data, follow_redirects=True
)
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
"New Issue
\n'
in output.get_data(as_text=True)
)
csrf_token = self.get_csrf()
with open(os.path.join(tests.HERE, "placebo.png"), "rb") as stream:
data = {
"title": "Test issue3",
"issue_content": "We really should improve on this issue\n"
"",
"status": "Open",
"filestream": stream,
"enctype": "multipart/form-data",
"csrf_token": csrf_token,
}
output = self.app.post(
"/somenamespace/test3/new_issue",
data=data,
follow_redirects=True,
)
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
"New Issue
\n'
in output.get_data(as_text=True)
)
csrf_token = self.get_csrf()
with open(os.path.join(tests.HERE, "placebo.png"), "rb") as stream:
with open(
os.path.join(tests.HERE, "pagure.png"), "rb"
) as stream2:
data = {
"title": "Test issue",
"issue_content": "We really should improve on this issue\n"
"\n",
"status": "Open",
"filestream": [stream, stream2],
"enctype": "multipart/form-data",
"csrf_token": csrf_token,
}
output = self.app.post(
"/test/new_issue", data=data, follow_redirects=True
)
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
"New Issue
\n'
in output.get_data(as_text=True)
)
csrf_token = self.get_csrf()
with open(os.path.join(tests.HERE, "placebo.png"), "rb") as stream:
with open(
os.path.join(tests.HERE, "pagure.png"), "rb"
) as stream2:
data = {
"title": "Test issue3",
"issue_content": "We really should improve on this issue\n"
"\n",
"status": "Open",
"filestream": [stream, stream2],
"enctype": "multipart/form-data",
"csrf_token": csrf_token,
}
output = self.app.post(
"/somenamespace/test3/new_issue",
data=data,
follow_redirects=True,
)
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
"New Issue
\n',
output_text,
)
self.assertIn("Tags", output_text)
self.assertIn("Assignee", output_text)
def test_new_issue_metadata_not_user(self):
""" Test the new_issue endpoint when the user does not have access
to the project. """
tests.create_projects(self.session)
tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
tests.create_projects_git(
os.path.join(self.path, "tickets"), bare=True
)
user = tests.FakeUser()
user.username = "foo"
with tests.user_set(self.app.application, user):
output = self.app.get("/test/new_issue")
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
'New Issue
\n',
output_text,
)
self.assertNotIn("Tags", output_text)
self.assertNotIn("Assignee", output_text)
@patch("pagure.lib.git.update_git", MagicMock(return_value=True))
@patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
def test_new_issue_with_metadata(self):
""" Test the new_issue endpoint when the user has access to the
project. """
tests.create_projects(self.session)
tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
tests.create_projects_git(
os.path.join(self.path, "tickets"), bare=True
)
# Set some milestone
repo = pagure.lib.query.get_authorized_project(self.session, "test")
repo.milestones = {"v1.0": "", "v2.0": "Tomorrow!"}
self.session.add(repo)
self.session.commit()
user = tests.FakeUser()
user.username = "pingou"
with tests.user_set(self.app.application, user):
output = self.app.get("/test/new_issue")
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
'New Issue
\n',
output_text,
)
self.assertIn("Tags", output_text)
self.assertIn("Assignee", output_text)
csrf_token = self.get_csrf(output=output)
data = {
"title": "Test issue3",
"issue_content": "We really should improve on this issue\n",
"status": "Open",
"assignee": "foo",
"milestone": "v2.0",
"tag": "tag2",
"csrf_token": csrf_token,
}
output = self.app.post(
"/test/new_issue", data=data, follow_redirects=True
)
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
"New Issue
\n',
output_text,
)
self.assertNotIn("Tags", output_text)
self.assertNotIn("Assignee", output_text)
csrf_token = self.get_csrf(output=output)
data = {
"title": "Test issue3",
"issue_content": "We really should improve on this issue\n",
"status": "Open",
"assignee": "foo",
"milestone": "v2.0",
"tag": "tag2",
"csrf_token": csrf_token,
}
output = self.app.post(
"/test/new_issue", data=data, follow_redirects=True
)
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
"(.*?) ', output.get_data(as_text=True), re.M | re.S)
# arrowed_th = ('Assignee\n ')
# First table row is the header
# self.assertIn(arrowed_th, tr_elements[0])
# _check_assignee_link(output.get_data(as_text=True), [
# '/test/issues?status=Open&assignee=foo',
# '/test/issues?status=Open&assignee=pingou',
# '/test/issues?status=Open&assignee=pingou',
# ])
# New issue button is shown
user = tests.FakeUser()
with tests.user_set(self.app.application, user):
output = self.app.get("/test")
self.assertEqual(output.status_code, 200)
self.assertIn(
'fa-exclamation-circle fa-fw"> New issue',
output.get_data(as_text=True),
)
output = self.app.get("/test")
self.assertEqual(output.status_code, 200)
self.assertNotIn(
'fa-exclamation-circle fa-fw"> New issue',
output.get_data(as_text=True),
)
# Project w/o issue tracker
repo = pagure.lib.query.get_authorized_project(self.session, "test")
repo.settings = {"issue_tracker": False}
self.session.add(repo)
self.session.commit()
output = self.app.get("/test/issues")
self.assertEqual(output.status_code, 404)
# New issue button is hidden
user = tests.FakeUser()
with tests.user_set(self.app.application, user):
output = self.app.get("/test")
self.assertEqual(output.status_code, 200)
self.assertNotIn(
'fa-exclamation-circle fa-fw"> New issue',
output.get_data(as_text=True),
)
@patch("pagure.lib.git.update_git")
@patch("pagure.lib.notify.send_email")
def test_search_issues_unicode(self, p_send_email, p_ugt):
""" Test the view_issues endpoint filtering for an unicode char. """
p_send_email.return_value = True
p_ugt.return_value = True
tests.create_projects(self.session)
tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
repo = pagure.lib.query.get_authorized_project(self.session, "test")
# Create 2 issues to play with
msg = pagure.lib.query.new_issue(
session=self.session,
repo=repo,
title="Test issue ☃",
content="We should work on this ❤",
user="pingou",
)
self.session.commit()
self.assertEqual(msg.title, "Test issue ☃")
msg = pagure.lib.query.new_issue(
session=self.session,
repo=repo,
title="Test issue with milestone",
content="Testing search",
user="pingou",
milestone="1.1",
)
self.session.commit()
self.assertEqual(msg.title, "Test issue with milestone")
# Whole list
output = self.app.get("/test/issues")
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn("
Woohoo a second comment!
", output_text) self.assertEqual(output_text.count('comment_body">'), 2) repo = pagure.lib.query.get_authorized_project(self.session, "test") issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) self.assertEqual(len(issue.comments), 1) data = {"csrf_token": csrf_token, "drop_comment": 1} user = tests.FakeUser() with tests.user_set(self.app.application, user): # Wrong issue id output = self.app.post( "/test/issue/3/update", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 404) # Wrong user output = self.app.post( "/test/issue/1/update", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 403) user = tests.FakeUser() user.username = "pingou" with tests.user_set(self.app.application, user): # Drop the new comment output = self.app.post( "/test/issue/1/update", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( "We should work on this!
"), 1 ) # Project w/o issue tracker repo = pagure.lib.query.get_authorized_project(self.session, "test") repo.settings = {"issue_tracker": False} self.session.add(repo) self.session.commit() user.username = "pingou" with tests.user_set(self.app.application, user): output = self.app.post("/test/issue/1/edit", data=data) self.assertEqual(output.status_code, 404) @patch("pagure.lib.git.update_git", MagicMock(return_value=True)) @patch("pagure.lib.notify.send_email", MagicMock(return_value=True)) def test_edit_issue_no_change(self): """ Test the edit_issue endpoint. """ tests.create_projects(self.session) tests.create_projects_git(os.path.join(self.path, "repos"), bare=True) # Create an issue to play with repo = pagure.lib.query.get_authorized_project(self.session, "test") msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue", content="We should work on this", user="pingou", ) self.session.commit() self.assertEqual(msg.title, "Test issue") user = tests.FakeUser(username="pingou") with tests.user_set(self.app.application, user): output = self.app.get("/test/issue/1/edit") self.assertEqual(output.status_code, 200) self.assertTrue("Edit Issue" in output.get_data(as_text=True)) csrf_token = self.get_csrf(output=output) # Change nothing in the issue data = { "issue_content": "We should work on this", "status": "Open", "title": "Test issue", "csrf_token": csrf_token, } output = self.app.post( "/test/issue/1/edit", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( ' \n' ' #1\n ', output_text, ) self.assertEqual( output_text.count( '' ), 1, ) self.assertEqual(output_text.count('comment_body">'), 1) self.assertEqual( output_text.count("We should work on this
"), 1 ) @patch("pagure.lib.git.update_git", MagicMock(return_value=True)) @patch("pagure.lib.notify.send_email", MagicMock(return_value=True)) def test_edit_tag_issue_disabled(self): """ Test the edit_tag endpoint when issues are disabled. """ tests.create_projects(self.session) tests.create_projects_git(os.path.join(self.path, "repos")) # disable issue tracker repo = pagure.lib.query.get_authorized_project(self.session, "test") repo.settings = {"issue_tracker": False} self.session.add(repo) self.session.commit() # Create issues to play with repo = pagure.lib.query.get_authorized_project(self.session, "test") msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue", content="We should work on this", user="pingou", ) self.session.commit() self.assertEqual(msg.title, "Test issue") # Add a tag to the issue issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) msg = pagure.lib.query.add_tag_obj( session=self.session, obj=issue, tags="tag1", user="pingou" ) self.session.commit() self.assertEqual(msg, "Issue tagged with: tag1") # Before edit, list tags tags = pagure.lib.query.get_tags_of_project(self.session, repo) self.assertEqual([tag.tag for tag in tags], ["tag1"]) # Edit tag user = tests.FakeUser(username="pingou") with tests.user_set(self.app.application, user): output = self.app.get("/test/tag/tag1/edit") self.assertEqual(output.status_code, 200) self.assertTrue( "Edit tag: tag1" in output.get_data(as_text=True) ) csrf_token = self.get_csrf(output=output) data = { "tag": "tag2", "tag_description": "lorem ipsum", "tag_color": "DeepSkyBlue", } output = self.app.post("/test/tag/tag1/edit", data=data) self.assertEqual(output.status_code, 200) self.assertTrue( "Edit tag: tag1" in output.get_data(as_text=True) ) # After edit, list tags self.session.commit() tags = pagure.lib.query.get_tags_of_project(self.session, repo) self.assertEqual([tag.tag for tag in tags], ["tag1"]) @patch("pagure.lib.git.update_git") @patch("pagure.lib.notify.send_email") def test_edit_tag(self, p_send_email, p_ugt): """ Test the edit_tag endpoint. """ p_send_email.return_value = True p_ugt.return_value = True # No Git repo output = self.app.get("/foo/tag/foo/edit") self.assertEqual(output.status_code, 404) user = tests.FakeUser() with tests.user_set(self.app.application, user): output = self.app.get("/foo/tag/foo/edit") self.assertEqual(output.status_code, 404) tests.create_projects(self.session) tests.create_projects_git(os.path.join(self.path, "repos")) output = self.app.get("/test/tag/foo/edit") self.assertEqual(output.status_code, 403) # User not logged in output = self.app.get("/test/tag/foo/edit") self.assertEqual(output.status_code, 302) # Create issues to play with repo = pagure.lib.query.get_authorized_project(self.session, "test") msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue", content="We should work on this", user="pingou", ) self.session.commit() self.assertEqual(msg.title, "Test issue") # Add a tag to the issue issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) msg = pagure.lib.query.add_tag_obj( session=self.session, obj=issue, tags="tag1", user="pingou" ) self.session.commit() self.assertEqual(msg, "Issue tagged with: tag1") # Before edit, list tags tags = pagure.lib.query.get_tags_of_project(self.session, repo) self.assertEqual([tag.tag for tag in tags], ["tag1"]) # Edit tag user.username = "pingou" with tests.user_set(self.app.application, user): # Edit a tag that doesn't exit output = self.app.get("/test/tag/does_not_exist/edit") self.assertEqual(output.status_code, 404) output = self.app.get("/test/tag/tag1/edit") self.assertEqual(output.status_code, 200) self.assertTrue( "Edit tag: tag1" in output.get_data(as_text=True) ) csrf_token = self.get_csrf(output=output) data = { "tag": "tag2", "tag_description": "lorem ipsum", "tag_color": "DeepSkyBlue", } output = self.app.post("/test/tag/tag1/edit", data=data) self.assertEqual(output.status_code, 200) self.assertTrue( "Edit tag: tag1" in output.get_data(as_text=True) ) data["csrf_token"] = csrf_token output = self.app.post( "/test/tag/tag1/edit", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn("Settings - test - Pagure", output_text) self.assertIn( "" "Edited tag: tag1()[DeepSkyBlue] to tag2(lorem ipsum)[DeepSkyBlue]", output_text, ) # update tag with empty description data["tag_description"] = "" output = self.app.post( "/test/tag/tag2/edit", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn("Settings - test - Pagure", output_text) self.assertIn( "" "Edited tag: tag2(lorem ipsum)[DeepSkyBlue] to tag2()[DeepSkyBlue]", output_text, ) # After edit, list tags self.session.commit() tags = pagure.lib.query.get_tags_of_project(self.session, repo) self.assertEqual([tag.tag for tag in tags], ["tag2"]) @patch("pagure.lib.git.update_git", MagicMock(return_value=True)) @patch("pagure.lib.notify.send_email", MagicMock(return_value=True)) def test_remove_tag_issue_disabled(self): """ Test the remove_tag endpoint. """ tests.create_projects(self.session) tests.create_projects_git(os.path.join(self.path, "repos")) # disable issue tracker repo = pagure.lib.query.get_authorized_project(self.session, "test") repo.settings = {"issue_tracker": False} self.session.add(repo) self.session.commit() # Create issues to play with repo = pagure.lib.query.get_authorized_project(self.session, "test") msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue", content="We should work on this", user="pingou", ) self.session.commit() self.assertEqual(msg.title, "Test issue") # Add a tag to the issue issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) msg = pagure.lib.query.add_tag_obj( session=self.session, obj=issue, tags="tag1", user="pingou" ) self.session.commit() self.assertEqual(msg, "Issue tagged with: tag1") # Before edit, list tags tags = pagure.lib.query.get_tags_of_project(self.session, repo) self.assertEqual([tag.tag for tag in tags], ["tag1"]) # Edit tag user = tests.FakeUser(username="pingou") with tests.user_set(self.app.application, user): data = {"tag": "tag1", "csrf_token": self.get_csrf()} output = self.app.post( "/test/droptag/", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( 'Woohoo a second comment!
", output_text) self.assertEqual(output_text.count('comment_body">'), 2) repo = pagure.lib.query.get_authorized_project(self.session, "test") issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) self.assertEqual(len(issue.comments), 1) self.assertEqual(issue.comments[0].comment, "Woohoo a second comment!") data = { "csrf_token": csrf_token, "edit_comment": 1, "update_comment": "Updated comment", } user = tests.FakeUser() with tests.user_set(self.app.application, user): # Wrong issue id output = self.app.post( "/test/issue/3/update", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 404) # Wrong user output = self.app.post( "/test/issue/1/update", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 403) user = tests.FakeUser() user.username = "pingou" with tests.user_set(self.app.application, user): # Edit comment output = self.app.post( "/test/issue/1/update", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( "foo bar #1 see?
foo bar #1 see?
Woohoo a second comment!
" in output_text) self.assertEqual(output_text.count('comment_body">'), 2) self.assertTrue( '' in output_text ) self.assertIn( " Closed: Fixed\n" " just now\n" " \n" " by\n" ' foo.\n', output_text, ) def _set_up_for_reaction_test(self, private=False): tests.create_projects(self.session) tests.create_projects_git(os.path.join(self.path, "repos"), bare=True) self.session.add( pagure.lib.model.User( user="naysayer", fullname="John Doe", password=b"password", default_email="jdoe@example.com", ) ) self.session.commit() repo = pagure.lib.query.get_authorized_project(self.session, "test") msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue", content="Fix me", user="pingou", private=private, ) pagure.lib.query.add_issue_comment( session=self.session, issue=msg, comment="How about no", user="naysayer", ) self.session.commit() @patch("pagure.lib.git.update_git") @patch("pagure.lib.notify.send_email") def test_add_reaction(self, p_send_email, p_ugt): """ Test adding a reaction to an issue comment.""" p_send_email.return_value = True p_ugt.return_value = True self._set_up_for_reaction_test() user = tests.FakeUser() user.username = "pingou" with tests.user_set(self.app.application, user): output = self.app.get("/test/issue/1") self.assertEqual(output.status_code, 200) data = { "csrf_token": self.get_csrf(output=output), "reaction": "Thumbs down", } output = self.app.post( "/test/issue/1/comment/1/react", data=data, follow_redirects=True, ) self.assertEqual(output.status_code, 200) # Load the page and check reaction is added. output = self.app.get("/test/issue/1") self.assertEqual(output.status_code, 200) self.assertIn( "Thumbs down sent by pingou", output.get_data(as_text=True) ) @patch("pagure.lib.git.update_git") @patch("pagure.lib.notify.send_email") def test_add_reaction_unauthenticated(self, p_send_email, p_ugt): """ Test adding a reaction to an issue comment without authentication. """ p_send_email.return_value = True p_ugt.return_value = True self._set_up_for_reaction_test() output = self.app.get("/test/issue/1") self.assertEqual(output.status_code, 200) data = { "csrf_token": self.get_csrf(output=output), "reaction": "Thumbs down", } output = self.app.post( "/test/issue/1/comment/1/react", data=data, follow_redirects=False ) # Redirect to login page self.assertEqual(output.status_code, 302) self.assertIn("/login/", output.headers["Location"]) @patch("pagure.lib.git.update_git") @patch("pagure.lib.notify.send_email") def test_add_reaction_private_issue(self, p_send_email, p_ugt): """Test adding a reaction to a private issue comment.""" p_send_email.return_value = True p_ugt.return_value = True self._set_up_for_reaction_test(private=True) user = tests.FakeUser() user.username = "naysayer" with tests.user_set(self.app.application, user): # Steal CSRF token from new issue page output = self.app.get("/test/new_issue") data = { "csrf_token": self.get_csrf(output=output), "reaction": "Thumbs down", } output = self.app.post( "/test/issue/1/comment/1/react", data=data, follow_redirects=True, ) self.assertEqual(output.status_code, 404) if __name__ == "__main__": unittest.main(verbosity=2)