test_pagure_flask_ui_repo_flag_commit.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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, absolute_import
  8. import unittest
  9. import sys
  10. import os
  11. from mock import patch
  12. import pygit2
  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 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. )
  38. self.assertIn(
  39. "#commit-overview-collapse", output.get_data(as_text=True)
  40. )
  41. self.assertIn("Merged by Alice Author", output.get_data(as_text=True))
  42. self.assertIn(
  43. "Committed by Cecil Committer", output.get_data(as_text=True)
  44. )
  45. def test_view_commit_pending_flag(self):
  46. """ Test the view_commit endpoint with a pending flag. """
  47. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  48. msg = pagure.lib.query.add_commit_flag(
  49. session=self.session,
  50. repo=repo,
  51. commit_hash=self.commit.oid.hex,
  52. username="simple-koji-ci",
  53. status="pending",
  54. percent=None,
  55. comment="Build is running",
  56. url="https://koji.fp.o/koji...",
  57. uid="uid",
  58. user="foo",
  59. token="aaabbbcccddd",
  60. )
  61. self.session.commit()
  62. self.assertEqual(msg, ("Flag added", "uid"))
  63. # View first commit
  64. output = self.app.get("/test/c/%s" % self.commit.oid.hex)
  65. self.assertEqual(output.status_code, 200)
  66. output_text = output.get_data(as_text=True)
  67. self.assertIn(
  68. "<title>Commit - test - %s - Pagure</title>" % self.commit.oid.hex,
  69. output_text,
  70. )
  71. self.assertIn("#commit-overview-collapse", output_text)
  72. self.assertIn("Merged by Alice Author", output_text)
  73. self.assertIn("Committed by Cecil Committer", output_text)
  74. self.assertIn(
  75. "<span>\n <a "
  76. 'href="https://koji.fp.o/koji...">simple-koji-ci</a>'
  77. "\n </span>",
  78. output_text,
  79. )
  80. self.assertIn(
  81. '<div class="pull-xs-right">\n '
  82. '<span class="badge badge-info">pending</span>'
  83. "\n </div>",
  84. output_text,
  85. )
  86. self.assertIn("<span>Build is running</span>", output_text)
  87. def test_view_commit_success_flag(self):
  88. """ Test the view_commit endpoint with a successful flag. """
  89. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  90. msg = pagure.lib.query.add_commit_flag(
  91. session=self.session,
  92. repo=repo,
  93. commit_hash=self.commit.oid.hex,
  94. username="simple-koji-ci",
  95. status="success",
  96. percent=100,
  97. comment="Build passed",
  98. url="https://koji.fp.o/koji...",
  99. uid="uid",
  100. user="foo",
  101. token="aaabbbcccddd",
  102. )
  103. self.session.commit()
  104. self.assertEqual(msg, ("Flag added", "uid"))
  105. # View first commit
  106. output = self.app.get("/test/c/%s" % self.commit.oid.hex)
  107. self.assertEqual(output.status_code, 200)
  108. output_text = output.get_data(as_text=True)
  109. self.assertIn(
  110. "<title>Commit - test - %s - Pagure</title>" % self.commit.oid.hex,
  111. output_text,
  112. )
  113. self.assertIn("#commit-overview-collapse", output_text)
  114. self.assertIn("Merged by Alice Author", output_text)
  115. self.assertIn("Committed by Cecil Committer", output_text)
  116. self.assertIn(
  117. "<span>\n <a "
  118. 'href="https://koji.fp.o/koji...">simple-koji-ci</a>'
  119. "\n </span>",
  120. output_text,
  121. )
  122. self.assertIn(
  123. '<div class="pull-xs-right">\n '
  124. '<span class="badge badge-success">success (100%)</span>'
  125. "\n </div>",
  126. output_text,
  127. )
  128. self.assertIn("<span>Build passed</span>", output_text)
  129. def test_view_commit_error_flag(self):
  130. """ Test the view_commit endpoint with a error flag. """
  131. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  132. msg = pagure.lib.query.add_commit_flag(
  133. session=self.session,
  134. repo=repo,
  135. commit_hash=self.commit.oid.hex,
  136. username="simple-koji-ci",
  137. status="error",
  138. percent=None,
  139. comment="Build errored",
  140. url="https://koji.fp.o/koji...",
  141. uid="uid",
  142. user="foo",
  143. token="aaabbbcccddd",
  144. )
  145. self.session.commit()
  146. self.assertEqual(msg, ("Flag added", "uid"))
  147. # View first commit
  148. output = self.app.get("/test/c/%s" % self.commit.oid.hex)
  149. self.assertEqual(output.status_code, 200)
  150. output_text = output.get_data(as_text=True)
  151. self.assertIn(
  152. "<title>Commit - test - %s - Pagure</title>" % self.commit.oid.hex,
  153. output_text,
  154. )
  155. self.assertIn("#commit-overview-collapse", output_text)
  156. self.assertIn("Merged by Alice Author", output_text)
  157. self.assertIn("Committed by Cecil Committer", output_text)
  158. self.assertIn(
  159. "<span>\n <a "
  160. 'href="https://koji.fp.o/koji...">simple-koji-ci</a>'
  161. "\n </span>",
  162. output_text,
  163. )
  164. self.assertIn(
  165. '<div class="pull-xs-right">\n '
  166. '<span class="badge badge-danger">error</span>'
  167. "\n </div>",
  168. output_text,
  169. )
  170. self.assertIn("<span>Build errored</span>", output_text)
  171. def test_view_commit_failure_flag(self):
  172. """ Test the view_commit endpoint with a failure flag. """
  173. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  174. msg = pagure.lib.query.add_commit_flag(
  175. session=self.session,
  176. repo=repo,
  177. commit_hash=self.commit.oid.hex,
  178. username="simple-koji-ci",
  179. status="failure",
  180. percent=None,
  181. comment="Build failed",
  182. url="https://koji.fp.o/koji...",
  183. uid="uid",
  184. user="foo",
  185. token="aaabbbcccddd",
  186. )
  187. self.session.commit()
  188. self.assertEqual(msg, ("Flag added", "uid"))
  189. # View first commit
  190. output = self.app.get("/test/c/%s" % self.commit.oid.hex)
  191. self.assertEqual(output.status_code, 200)
  192. output_text = output.get_data(as_text=True)
  193. self.assertIn(
  194. "<title>Commit - test - %s - Pagure</title>" % self.commit.oid.hex,
  195. output_text,
  196. )
  197. self.assertIn("#commit-overview-collapse", output_text)
  198. self.assertIn("Merged by Alice Author", output_text)
  199. self.assertIn("Committed by Cecil Committer", output_text)
  200. self.assertIn(
  201. '<span>\n <a href="https://koji.fp.o/koji...">'
  202. "simple-koji-ci</a>\n </span>",
  203. output_text,
  204. )
  205. self.assertIn(
  206. '<div class="pull-xs-right">\n '
  207. '<span class="badge badge-danger">failure</span>'
  208. "\n </div>",
  209. output_text,
  210. )
  211. self.assertIn("<span>Build failed</span>", output_text)
  212. def test_view_commit_canceled_flag(self):
  213. """ Test the view_commit endpoint with a canceled flag. """
  214. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  215. msg = pagure.lib.query.add_commit_flag(
  216. session=self.session,
  217. repo=repo,
  218. commit_hash=self.commit.oid.hex,
  219. username="simple-koji-ci",
  220. status="canceled",
  221. percent=None,
  222. comment="Build canceled",
  223. url="https://koji.fp.o/koji...",
  224. uid="uid",
  225. user="foo",
  226. token="aaabbbcccddd",
  227. )
  228. self.session.commit()
  229. self.assertEqual(msg, ("Flag added", "uid"))
  230. # View first commit
  231. output = self.app.get("/test/c/%s" % self.commit.oid.hex)
  232. self.assertEqual(output.status_code, 200)
  233. output_text = output.get_data(as_text=True)
  234. self.assertIn(
  235. "<title>Commit - test - %s - Pagure</title>" % self.commit.oid.hex,
  236. output_text,
  237. )
  238. self.assertIn("#commit-overview-collapse", output_text)
  239. self.assertIn("Merged by Alice Author", output_text)
  240. self.assertIn("Committed by Cecil Committer", output_text)
  241. self.assertIn(
  242. '<span>\n <a href="https://koji.fp.o/koji...">'
  243. "simple-koji-ci</a>\n </span>",
  244. output_text,
  245. )
  246. self.assertIn(
  247. '<div class="pull-xs-right">\n '
  248. '<span class="badge badge-warning">canceled</span>'
  249. "\n </div>",
  250. output_text,
  251. )
  252. self.assertIn("<span>Build canceled</span>", output_text)
  253. @patch.dict(
  254. "pagure.config.config",
  255. {
  256. "FLAG_STATUSES_LABELS": {
  257. "status1": "badge-warning",
  258. "otherstatus": "badge-success",
  259. }
  260. },
  261. )
  262. def test_view_commit_with_custom_flags(self):
  263. """ Test the view_commit endpoint while having custom flags. """
  264. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  265. msg = pagure.lib.query.add_commit_flag(
  266. session=self.session,
  267. repo=repo,
  268. commit_hash=self.commit.oid.hex,
  269. username="simple-koji-ci",
  270. status="status1",
  271. percent=None,
  272. comment="Build canceled",
  273. url="https://koji.fp.o/koji...",
  274. uid="uid",
  275. user="foo",
  276. token="aaabbbcccddd",
  277. )
  278. self.session.commit()
  279. self.assertEqual(msg, ("Flag added", "uid"))
  280. # View first commit
  281. output = self.app.get("/test/c/%s" % self.commit.oid.hex)
  282. self.assertEqual(output.status_code, 200)
  283. output_text = output.get_data(as_text=True)
  284. self.assertIn(
  285. "<title>Commit - test - %s - Pagure</title>" % self.commit.oid.hex,
  286. output_text,
  287. )
  288. self.assertIn("#commit-overview-collapse", output_text)
  289. self.assertIn("Merged by Alice Author", output_text)
  290. self.assertIn("Committed by Cecil Committer", output_text)
  291. self.assertIn(
  292. '<span>\n <a href="https://koji.fp.o/koji...">'
  293. "simple-koji-ci</a>\n </span>",
  294. output_text,
  295. )
  296. self.assertIn(
  297. '<div class="pull-xs-right">\n '
  298. '<span class="badge badge-warning">status1</span>'
  299. "\n </div>",
  300. output_text,
  301. )
  302. self.assertIn("<span>Build canceled</span>", output_text)
  303. if __name__ == "__main__":
  304. unittest.main(verbosity=2)