test_pagure_flask_ui_repo_flag_commit.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. import unittest
  9. import sys
  10. import os
  11. from mock import patch
  12. import pygit2
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure
  16. import pagure.lib
  17. import tests
  18. class ViewCommitFlagtests(tests.SimplePagureTest):
  19. """ Tests for the UI related to commit flags """
  20. def setUp(self):
  21. """ Set up the environnment, ran before every tests. """
  22. super(ViewCommitFlagtests, self).setUp()
  23. tests.create_projects(self.session)
  24. tests.create_tokens(self.session)
  25. # Add a README to the git repo - First commit
  26. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  27. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  28. self.commit = repo.revparse_single('HEAD')
  29. def test_view_commit_no_flag(self):
  30. """ Test the view_commit endpoint. """
  31. # View first commit
  32. output = self.app.get('/test/c/%s' % self.commit.oid.hex)
  33. self.assertEqual(output.status_code, 200)
  34. self.assertIn(
  35. '<title>Commit - test - %s - Pagure</title>' % self.commit.oid.hex,
  36. output.get_data(as_text=True))
  37. self.assertIn(
  38. '#commit-overview-collapse',
  39. output.get_data(as_text=True))
  40. self.assertIn('Merged by Alice Author', output.get_data(as_text=True))
  41. self.assertIn('Committed by Cecil Committer', output.get_data(as_text=True))
  42. def test_view_commit_pending_flag(self):
  43. """ Test the view_commit endpoint with a pending flag. """
  44. repo = pagure.lib.get_authorized_project(self.session, 'test')
  45. msg = pagure.lib.add_commit_flag(
  46. session=self.session,
  47. repo=repo,
  48. commit_hash=self.commit.oid.hex,
  49. username='simple-koji-ci',
  50. status='pending',
  51. percent=None,
  52. comment='Build is running',
  53. url='https://koji.fp.o/koji...',
  54. uid='uid',
  55. user='foo',
  56. token='aaabbbcccddd'
  57. )
  58. self.session.commit()
  59. self.assertEqual(msg, ('Flag added', 'uid'))
  60. # View first commit
  61. output = self.app.get('/test/c/%s' % self.commit.oid.hex)
  62. self.assertEqual(output.status_code, 200)
  63. output_text = output.get_data(as_text=True)
  64. self.assertIn(
  65. '<title>Commit - test - %s - Pagure</title>' % self.commit.oid.hex,
  66. output_text)
  67. self.assertIn(
  68. '#commit-overview-collapse',
  69. output_text)
  70. self.assertIn('Merged by Alice Author', output_text)
  71. self.assertIn('Committed by Cecil Committer', output_text)
  72. self.assertIn(
  73. '<span>\n <a '
  74. 'href="https://koji.fp.o/koji...">simple-koji-ci</a>'
  75. '\n </span>', output_text)
  76. self.assertIn(
  77. '<div class="pull-xs-right">\n '
  78. '<span class="badge badge-info">pending</span>'
  79. '\n </div>', output_text)
  80. self.assertIn(
  81. '<span>Build is running</span>', output_text)
  82. def test_view_commit_success_flag(self):
  83. """ Test the view_commit endpoint with a successful flag. """
  84. repo = pagure.lib.get_authorized_project(self.session, 'test')
  85. msg = pagure.lib.add_commit_flag(
  86. session=self.session,
  87. repo=repo,
  88. commit_hash=self.commit.oid.hex,
  89. username='simple-koji-ci',
  90. status='success',
  91. percent=100,
  92. comment='Build passed',
  93. url='https://koji.fp.o/koji...',
  94. uid='uid',
  95. user='foo',
  96. token='aaabbbcccddd'
  97. )
  98. self.session.commit()
  99. self.assertEqual(msg, ('Flag added', 'uid'))
  100. # View first commit
  101. output = self.app.get('/test/c/%s' % self.commit.oid.hex)
  102. self.assertEqual(output.status_code, 200)
  103. output_text = output.get_data(as_text=True)
  104. self.assertIn(
  105. '<title>Commit - test - %s - Pagure</title>' % self.commit.oid.hex,
  106. output_text)
  107. self.assertIn(
  108. '#commit-overview-collapse',
  109. output_text)
  110. self.assertIn('Merged by Alice Author', output_text)
  111. self.assertIn('Committed by Cecil Committer', output_text)
  112. self.assertIn(
  113. '<span>\n <a '
  114. 'href="https://koji.fp.o/koji...">simple-koji-ci</a>'
  115. '\n </span>', output_text)
  116. self.assertIn(
  117. '<div class="pull-xs-right">\n '
  118. '<span class="badge badge-success">success (100%)</span>'
  119. '\n </div>', output_text)
  120. self.assertIn(
  121. '<span>Build passed</span>', output_text)
  122. def test_view_commit_error_flag(self):
  123. """ Test the view_commit endpoint with a error flag. """
  124. repo = pagure.lib.get_authorized_project(self.session, 'test')
  125. msg = pagure.lib.add_commit_flag(
  126. session=self.session,
  127. repo=repo,
  128. commit_hash=self.commit.oid.hex,
  129. username='simple-koji-ci',
  130. status='error',
  131. percent=None,
  132. comment='Build errored',
  133. url='https://koji.fp.o/koji...',
  134. uid='uid',
  135. user='foo',
  136. token='aaabbbcccddd'
  137. )
  138. self.session.commit()
  139. self.assertEqual(msg, ('Flag added', 'uid'))
  140. # View first commit
  141. output = self.app.get('/test/c/%s' % self.commit.oid.hex)
  142. self.assertEqual(output.status_code, 200)
  143. output_text = output.get_data(as_text=True)
  144. self.assertIn(
  145. '<title>Commit - test - %s - Pagure</title>' % self.commit.oid.hex,
  146. output_text)
  147. self.assertIn(
  148. '#commit-overview-collapse',
  149. output_text)
  150. self.assertIn('Merged by Alice Author', output_text)
  151. self.assertIn('Committed by Cecil Committer', output_text)
  152. self.assertIn(
  153. '<span>\n <a '
  154. 'href="https://koji.fp.o/koji...">simple-koji-ci</a>'
  155. '\n </span>', output_text)
  156. self.assertIn(
  157. '<div class="pull-xs-right">\n '
  158. '<span class="badge badge-danger">error</span>'
  159. '\n </div>', output_text)
  160. self.assertIn(
  161. '<span>Build errored</span>', output_text)
  162. def test_view_commit_failure_flag(self):
  163. """ Test the view_commit endpoint with a failure flag. """
  164. repo = pagure.lib.get_authorized_project(self.session, 'test')
  165. msg = pagure.lib.add_commit_flag(
  166. session=self.session,
  167. repo=repo,
  168. commit_hash=self.commit.oid.hex,
  169. username='simple-koji-ci',
  170. status='failure',
  171. percent=None,
  172. comment='Build failed',
  173. url='https://koji.fp.o/koji...',
  174. uid='uid',
  175. user='foo',
  176. token='aaabbbcccddd'
  177. )
  178. self.session.commit()
  179. self.assertEqual(msg, ('Flag added', 'uid'))
  180. # View first commit
  181. output = self.app.get('/test/c/%s' % self.commit.oid.hex)
  182. self.assertEqual(output.status_code, 200)
  183. output_text = output.get_data(as_text=True)
  184. self.assertIn(
  185. '<title>Commit - test - %s - Pagure</title>' % self.commit.oid.hex,
  186. output_text)
  187. self.assertIn(
  188. '#commit-overview-collapse',
  189. output_text)
  190. self.assertIn('Merged by Alice Author', output_text)
  191. self.assertIn('Committed by Cecil Committer', output_text)
  192. self.assertIn(
  193. '<span>\n <a href="https://koji.fp.o/koji...">'
  194. 'simple-koji-ci</a>\n </span>', output_text)
  195. self.assertIn(
  196. '<div class="pull-xs-right">\n '
  197. '<span class="badge badge-danger">failure</span>'
  198. '\n </div>', output_text)
  199. self.assertIn(
  200. '<span>Build failed</span>', output_text)
  201. def test_view_commit_canceled_flag(self):
  202. """ Test the view_commit endpoint with a canceled flag. """
  203. repo = pagure.lib.get_authorized_project(self.session, 'test')
  204. msg = pagure.lib.add_commit_flag(
  205. session=self.session,
  206. repo=repo,
  207. commit_hash=self.commit.oid.hex,
  208. username='simple-koji-ci',
  209. status='canceled',
  210. percent=None,
  211. comment='Build canceled',
  212. url='https://koji.fp.o/koji...',
  213. uid='uid',
  214. user='foo',
  215. token='aaabbbcccddd'
  216. )
  217. self.session.commit()
  218. self.assertEqual(msg, ('Flag added', 'uid'))
  219. # View first commit
  220. output = self.app.get('/test/c/%s' % self.commit.oid.hex)
  221. self.assertEqual(output.status_code, 200)
  222. output_text = output.get_data(as_text=True)
  223. self.assertIn(
  224. '<title>Commit - test - %s - Pagure</title>' % self.commit.oid.hex,
  225. output_text)
  226. self.assertIn(
  227. '#commit-overview-collapse',
  228. output_text)
  229. self.assertIn('Merged by Alice Author', output_text)
  230. self.assertIn('Committed by Cecil Committer', output_text)
  231. self.assertIn(
  232. '<span>\n <a href="https://koji.fp.o/koji...">'
  233. 'simple-koji-ci</a>\n </span>', output_text)
  234. self.assertIn(
  235. '<div class="pull-xs-right">\n '
  236. '<span class="badge badge-warning">canceled</span>'
  237. '\n </div>', output_text)
  238. self.assertIn(
  239. '<span>Build canceled</span>', output_text)
  240. @patch.dict('pagure.config.config',
  241. {
  242. 'FLAG_STATUSES_LABELS':
  243. {
  244. 'status1': 'badge-warning',
  245. 'otherstatus': 'badge-success',
  246. },
  247. })
  248. def test_view_commit_with_custom_flags(self):
  249. """ Test the view_commit endpoint while having custom flags. """
  250. repo = pagure.lib.get_authorized_project(self.session, 'test')
  251. msg = pagure.lib.add_commit_flag(
  252. session=self.session,
  253. repo=repo,
  254. commit_hash=self.commit.oid.hex,
  255. username='simple-koji-ci',
  256. status='status1',
  257. percent=None,
  258. comment='Build canceled',
  259. url='https://koji.fp.o/koji...',
  260. uid='uid',
  261. user='foo',
  262. token='aaabbbcccddd'
  263. )
  264. self.session.commit()
  265. self.assertEqual(msg, ('Flag added', 'uid'))
  266. # View first commit
  267. output = self.app.get('/test/c/%s' % self.commit.oid.hex)
  268. self.assertEqual(output.status_code, 200)
  269. output_text = output.get_data(as_text=True)
  270. self.assertIn(
  271. '<title>Commit - test - %s - Pagure</title>' % self.commit.oid.hex,
  272. output_text)
  273. self.assertIn(
  274. '#commit-overview-collapse',
  275. output_text)
  276. self.assertIn('Merged by Alice Author', output_text)
  277. self.assertIn('Committed by Cecil Committer', output_text)
  278. self.assertIn(
  279. '<span>\n <a href="https://koji.fp.o/koji...">'
  280. 'simple-koji-ci</a>\n </span>', output_text)
  281. self.assertIn(
  282. '<div class="pull-xs-right">\n '
  283. '<span class="badge badge-warning">status1</span>'
  284. '\n </div>', output_text)
  285. self.assertIn(
  286. '<span>Build canceled</span>', output_text)
  287. if __name__ == '__main__':
  288. unittest.main(verbosity=2)