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