1
0

test_pagure_flask_ui_issue_pr_link.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import json
  10. import unittest
  11. import re
  12. import shutil
  13. import sys
  14. import tempfile
  15. import time
  16. import os
  17. import pygit2
  18. from mock import ANY, patch, MagicMock
  19. sys.path.insert(
  20. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  21. )
  22. import pagure.lib.query
  23. import pagure.lib.tasks
  24. import tests
  25. from pagure.lib.repo import PagureRepo
  26. class PagureFlaskPrIssueLinkTest(tests.Modeltests):
  27. """ Tests pagure when linking PRs to tickets """
  28. maxDiff = None
  29. def setUp(self):
  30. """ Set up the environnment, ran before every tests. """
  31. super(PagureFlaskPrIssueLinkTest, self).setUp()
  32. tests.create_projects(self.session)
  33. tests.create_projects(
  34. self.session, is_fork=True, user_id=2, hook_token_suffix="bar"
  35. )
  36. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  37. tests.create_projects_git(
  38. os.path.join(self.path, "repos", "forks", "foo"), bare=True
  39. )
  40. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  41. # Create issues to play with
  42. msg = pagure.lib.query.new_issue(
  43. session=self.session,
  44. repo=repo,
  45. title="tést íssüé",
  46. content="We should work on this",
  47. user="pingou",
  48. )
  49. self.session.commit()
  50. self.assertEqual(msg.title, "tést íssüé")
  51. msg = pagure.lib.query.new_issue(
  52. session=self.session,
  53. repo=repo,
  54. title="tést íssüé #2",
  55. content="We should still work on this",
  56. user="foo",
  57. )
  58. self.session.commit()
  59. self.assertEqual(msg.title, "tést íssüé #2")
  60. # Add a commit to the fork
  61. newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
  62. repopath = os.path.join(newpath, "test")
  63. clone_repo = pygit2.clone_repository(
  64. os.path.join(self.path, "repos", "forks", "foo", "test.git"),
  65. repopath,
  66. )
  67. # Create a file in that git repo
  68. with open(os.path.join(repopath, "sources"), "w") as stream:
  69. stream.write("foo\n bar")
  70. clone_repo.index.add("sources")
  71. clone_repo.index.write()
  72. try:
  73. com = repo.revparse_single("HEAD")
  74. prev_commit = [com.oid.hex]
  75. except:
  76. prev_commit = []
  77. # Commits the files added
  78. tree = clone_repo.index.write_tree()
  79. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  80. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  81. clone_repo.create_commit(
  82. "refs/heads/master", # the name of the reference to update
  83. author,
  84. committer,
  85. "Add sources file for testing\n\n Relates to #2",
  86. # binary string representing the tree object ID
  87. tree,
  88. # list of binary strings representing parents of the new commit
  89. prev_commit,
  90. )
  91. refname = "refs/heads/master:refs/heads/master"
  92. ori_remote = clone_repo.remotes[0]
  93. PagureRepo.push(ori_remote, refname)
  94. # Create the corresponding PR
  95. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  96. fork_repo = pagure.lib.query.get_authorized_project(
  97. self.session, "test", user="foo"
  98. )
  99. request = pagure.lib.query.new_pull_request(
  100. self.session,
  101. branch_from="master",
  102. repo_to=repo,
  103. branch_to="master",
  104. title="test PR",
  105. user="foo",
  106. initial_comment=None,
  107. repo_from=fork_repo,
  108. )
  109. self.session.commit()
  110. pagure.lib.tasks.link_pr_to_ticket(request.uid)
  111. self.assertEqual(request.id, 3)
  112. def test_ticket_no_link(self):
  113. """ Test that no Related PR(s) block is showing in the issue page.
  114. """
  115. output = self.app.get("/test/issue/1")
  116. self.assertEqual(output.status_code, 200)
  117. self.assertNotIn(
  118. "Related Pull Requests", output.get_data(as_text=True)
  119. )
  120. def test_ticket_link(self):
  121. """ Test that a Related PR(s) block is showing in the issue page.
  122. """
  123. output = self.app.get("/test/issue/2")
  124. self.assertEqual(output.status_code, 200)
  125. self.assertIn("Related Pull Requests", output.get_data(as_text=True))
  126. def test_pr_link_issue_list(self):
  127. """ Test that the related PR(s) shows in the page listing issues
  128. """
  129. output = self.app.get("/test/issues")
  130. self.assertEqual(output.status_code, 200)
  131. self.assertIn(
  132. '<span title="Related to PR#3" class="badge font-weight-bold '
  133. 'text-muted font-size-09" data-toggle="tooltip">\n'
  134. ' <i class="fa fa-link"></i>\n'
  135. ' <a href="/test/pull-request/3" '
  136. 'class="notblue">PR#3</a>\n </span>',
  137. output.get_data(as_text=True),
  138. )
  139. if __name__ == "__main__":
  140. unittest.main(verbosity=2)