test_pagure_flask_ui_issue_pr_link.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import datetime
  10. import json
  11. import unittest
  12. import re
  13. import shutil
  14. import sys
  15. import tempfile
  16. import time
  17. import os
  18. import pygit2
  19. from mock import ANY, patch, MagicMock
  20. sys.path.insert(0, os.path.join(os.path.dirname(
  21. os.path.abspath(__file__)), '..'))
  22. import pagure
  23. import pagure.lib
  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. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  36. tests.create_projects_git(os.path.join(
  37. self.path, 'repos', 'forks', 'foo'), bare=True)
  38. repo = pagure.lib.get_authorized_project(self.session, 'test')
  39. # Create issues to play with
  40. msg = pagure.lib.new_issue(
  41. session=self.session,
  42. repo=repo,
  43. title=u'tést íssüé',
  44. content='We should work on this',
  45. user='pingou',
  46. ticketfolder=None
  47. )
  48. self.session.commit()
  49. self.assertEqual(msg.title, u'tést íssüé')
  50. msg = pagure.lib.new_issue(
  51. session=self.session,
  52. repo=repo,
  53. title=u'tést íssüé #2',
  54. content='We should still work on this',
  55. user='foo',
  56. ticketfolder=None
  57. )
  58. self.session.commit()
  59. self.assertEqual(msg.title, u'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(os.path.join(
  64. self.path, 'repos', 'forks', 'foo', 'test.git'), repopath)
  65. # Create a file in that git repo
  66. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  67. stream.write('foo\n bar')
  68. clone_repo.index.add('sources')
  69. clone_repo.index.write()
  70. try:
  71. com = repo.revparse_single('HEAD')
  72. prev_commit = [com.oid.hex]
  73. except:
  74. prev_commit = []
  75. # Commits the files added
  76. tree = clone_repo.index.write_tree()
  77. author = pygit2.Signature(
  78. 'Alice Author', 'alice@authors.tld')
  79. committer = pygit2.Signature(
  80. '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.get_authorized_project(self.session, 'test')
  96. fork_repo = pagure.lib.get_authorized_project(
  97. self.session, 'test', user='foo')
  98. request = pagure.lib.new_pull_request(
  99. self.session,
  100. branch_from='master',
  101. repo_to=repo,
  102. branch_to='master',
  103. title='test PR',
  104. user='foo',
  105. requestfolder=None,
  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. u'<strong>Related PR(s)</strong>',
  119. output.data.decode('utf-8'))
  120. def test_ticket_link(self):
  121. """ Test that no Related PR(s) block is showing in the issue page.
  122. """
  123. time.sleep(1)
  124. output = self.app.get('/test/issue/2')
  125. print output.data.decode('utf-8')
  126. self.assertEqual(output.status_code, 200)
  127. self.assertIn(
  128. u'<strong>Related PR(s)</strong>',
  129. output.data.decode('utf-8'))
  130. if __name__ == '__main__':
  131. unittest.main(verbosity=2)