test_pagure_flask_ui_pr_bidi.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2021 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import sys
  10. import os
  11. import pygit2
  12. from mock import patch, MagicMock
  13. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.lib.query
  17. import tests
  18. class PagureFlaskPrBiditests(tests.Modeltests):
  19. """Tests PR in pagure when the PR has bi-directional characters"""
  20. maxDiff = None
  21. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  22. @patch("pagure.lib.notify.fedmsg_publish", MagicMock(return_value=True))
  23. def setUp(self):
  24. """Set up the environnment, ran before every tests."""
  25. super(PagureFlaskPrBiditests, self).setUp()
  26. tests.create_projects(self.session)
  27. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  28. # Create foo's fork of pingou's test project
  29. item = pagure.lib.model.Project(
  30. user_id=2, # foo
  31. name="test",
  32. description="test project #1",
  33. hook_token="aaabbb",
  34. is_fork=True,
  35. parent_id=1,
  36. )
  37. self.session.add(item)
  38. self.session.commit()
  39. # Create the fork's git repo
  40. repo_path = os.path.join(self.path, "repos", item.path)
  41. pygit2.init_repository(repo_path, bare=True)
  42. def set_up_git_repo(
  43. self, repo, fork, branch_from="feature", append_content=None
  44. ):
  45. """Set up the git repo and create the corresponding PullRequest
  46. object.
  47. """
  48. req = tests.add_pull_request_git_repo(
  49. self.path,
  50. self.session,
  51. repo,
  52. fork,
  53. branch_from,
  54. append_content=append_content,
  55. )
  56. self.assertEqual(req.id, 1)
  57. self.assertEqual(req.title, "PR from the %s branch" % branch_from)
  58. tests.clean_pull_requests_path()
  59. def test_accessing_pr_no_bidi(self):
  60. """Test accessing the PR which has no bidi characters."""
  61. project = pagure.lib.query.get_authorized_project(self.session, "test")
  62. fork = pagure.lib.query.get_authorized_project(
  63. self.session, "test", user="foo"
  64. )
  65. self.set_up_git_repo(repo=project, fork=fork)
  66. # Ensure things got setup straight
  67. project = pagure.lib.query.get_authorized_project(self.session, "test")
  68. self.assertEqual(len(project.requests), 1)
  69. # wait for the worker to process the task
  70. path = os.path.join(
  71. self.path, "repos", "test.git", "refs", "pull", "1", "head"
  72. )
  73. self.assertTrue(os.path.exists(path))
  74. # View the pull-request -- no bidi characters found
  75. output = self.app.get("/test/pull-request/1")
  76. self.assertEqual(output.status_code, 200)
  77. self.assertNotIn(
  78. "Special characters such as:", output.get_data(as_text=True)
  79. )
  80. def test_accessing_pr_bidi(self):
  81. """Test accessing the PR which has no bidi characters."""
  82. project = pagure.lib.query.get_authorized_project(self.session, "test")
  83. fork = pagure.lib.query.get_authorized_project(
  84. self.session, "test", user="foo"
  85. )
  86. self.set_up_git_repo(
  87. repo=project, fork=fork, append_content="ahah %s" % chr(0x2067)
  88. )
  89. # Ensure things got setup straight
  90. project = pagure.lib.query.get_authorized_project(self.session, "test")
  91. self.assertEqual(len(project.requests), 1)
  92. # wait for the worker to process the task
  93. path = os.path.join(
  94. self.path, "repos", "test.git", "refs", "pull", "1", "head"
  95. )
  96. self.assertTrue(os.path.exists(path))
  97. # View the pull-request -- bidi characters found
  98. output = self.app.get("/test/pull-request/1")
  99. self.assertEqual(output.status_code, 200)
  100. self.assertIn(
  101. "Special characters such as:", output.get_data(as_text=True)
  102. )
  103. if __name__ == "__main__":
  104. unittest.main(verbosity=2)