test_pagure_flask_ui_star_project.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # coding=utf-8
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. Vivek Anand <vivekanand1101@gmail.com>
  7. """
  8. from __future__ import unicode_literals, absolute_import
  9. import unittest
  10. import sys
  11. import os
  12. sys.path.insert(
  13. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  14. )
  15. import pagure.config
  16. import pagure.lib.query
  17. import tests
  18. class TestStarProjectUI(tests.SimplePagureTest):
  19. def setUp(self):
  20. """Set up the environnment, ran before every tests."""
  21. super(TestStarProjectUI, self).setUp()
  22. tests.create_projects(self.session)
  23. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  24. def _check_star_count(self, data, stars=1):
  25. """Check if the star count is correct or not"""
  26. output = self.app.get("/test/", data=data, follow_redirects=True)
  27. if stars == 1:
  28. self.assertIn(
  29. '<a href="/test/stargazers/" class="btn btn-sm '
  30. 'btn-primary font-weight-bold">1</a>\n',
  31. output.get_data(as_text=True),
  32. )
  33. elif stars == 0:
  34. self.assertIn(
  35. '<a href="/test/stargazers/" class="btn btn-sm '
  36. 'btn-primary font-weight-bold">0</a>\n',
  37. output.get_data(as_text=True),
  38. )
  39. def test_star_project_no_project(self):
  40. """Test the star_project endpoint."""
  41. # No such project
  42. output = self.app.post("/test42/star/1")
  43. self.assertEqual(output.status_code, 404)
  44. def test_star_project_no_csrf(self):
  45. """Test the star_project endpoint for the case when there
  46. is no CSRF token given"""
  47. user = tests.FakeUser()
  48. user.username = "pingou"
  49. with tests.user_set(self.app.application, user):
  50. data = {}
  51. output = self.app.post(
  52. "/test/star/1", data=data, follow_redirects=True
  53. )
  54. self.assertEqual(output.status_code, 400)
  55. def test_star_project_invalid_star(self):
  56. """Test the star_project endpoint for invalid star"""
  57. user = tests.FakeUser()
  58. user.username = "pingou"
  59. with tests.user_set(self.app.application, user):
  60. csrf_token = self.get_csrf()
  61. data = {"csrf_token": csrf_token}
  62. output = self.app.post(
  63. "/test/star/2", data=data, follow_redirects=True
  64. )
  65. self.assertEqual(output.status_code, 400)
  66. self._check_star_count(data=data, stars=0)
  67. def test_star_project_valid_star(self):
  68. """Test the star_project endpoint for correct star"""
  69. user = tests.FakeUser()
  70. user.username = "pingou"
  71. with tests.user_set(self.app.application, user):
  72. csrf_token = self.get_csrf()
  73. data = {"csrf_token": csrf_token}
  74. # try starring the project for pingou
  75. output = self.app.post(
  76. "/test/star/1", data=data, follow_redirects=True
  77. )
  78. self.assertEqual(output.status_code, 200)
  79. self.assertIn(
  80. "You starred this project", output.get_data(as_text=True)
  81. )
  82. # check home page of project for star count
  83. self._check_star_count(data=data, stars=1)
  84. # try unstarring the project for pingou
  85. output = self.app.post(
  86. "/test/star/0", data=data, follow_redirects=True
  87. )
  88. self.assertEqual(output.status_code, 200)
  89. self.assertIn(
  90. "You unstarred this project", output.get_data(as_text=True)
  91. )
  92. self._check_star_count(data=data, stars=0)
  93. def test_repo_stargazers(self):
  94. """Test the repo_stargazers endpoint of pagure.ui.repo"""
  95. # make pingou star the project
  96. # first create pingou
  97. user = tests.FakeUser()
  98. user.username = "pingou"
  99. with tests.user_set(self.app.application, user):
  100. csrf_token = self.get_csrf()
  101. data = {"csrf_token": csrf_token}
  102. output = self.app.post(
  103. "/test/star/1", data=data, follow_redirects=True
  104. )
  105. self.assertEqual(output.status_code, 200)
  106. self.assertIn(
  107. "You starred this project", output.get_data(as_text=True)
  108. )
  109. self._check_star_count(data=data, stars=1)
  110. # now, test if pingou's name comes in repo stargazers
  111. output = self.app.get("/test/stargazers/")
  112. self.assertIn(
  113. "<title>Stargazers of test - Pagure</title>",
  114. output.get_data(as_text=True),
  115. )
  116. self.assertIn(
  117. '<a class="font-weight-bold" href="/user/pingou">pingou</a>',
  118. output.get_data(as_text=True),
  119. )
  120. # make pingou unstar the project
  121. user = tests.FakeUser()
  122. user.username = "pingou"
  123. with tests.user_set(self.app.application, user):
  124. csrf_token = self.get_csrf()
  125. data = {"csrf_token": csrf_token}
  126. output = self.app.post(
  127. "/test/star/0", data=data, follow_redirects=True
  128. )
  129. self.assertEqual(output.status_code, 200)
  130. self.assertIn(
  131. "You unstarred this project", output.get_data(as_text=True)
  132. )
  133. self._check_star_count(data=data, stars=0)
  134. # now, test if pingou's name comes in repo stargazers
  135. # it shouldn't because, he just unstarred
  136. output = self.app.get("/test/stargazers/")
  137. self.assertIn(
  138. "<title>Stargazers of test - Pagure</title>",
  139. output.get_data(as_text=True),
  140. )
  141. self.assertNotIn(
  142. '<a class="font-weight-bold" href="/user/pingou">pingou</a>',
  143. output.get_data(as_text=True),
  144. )
  145. def test_user_stars(self):
  146. """Test the user_stars endpoint of pagure.ui.app"""
  147. # Check pingou's stars before
  148. output = self.app.get("/user/pingou/stars/")
  149. output_text = output.get_data(as_text=True)
  150. self.assertIn("<title>pingou - starred - Pagure</title>", output_text)
  151. self.assertIn(
  152. '<span class="btn btn-outline-secondary disabled opacity-100 '
  153. 'border-0 ml-auto font-weight-bold">0 Project</span>',
  154. output_text,
  155. )
  156. # make pingou star the project
  157. # first create pingou
  158. user = tests.FakeUser()
  159. user.username = "pingou"
  160. with tests.user_set(self.app.application, user):
  161. csrf_token = self.get_csrf()
  162. data = {"csrf_token": csrf_token}
  163. output = self.app.post(
  164. "/test/star/1", data=data, follow_redirects=True
  165. )
  166. self.assertEqual(output.status_code, 200)
  167. self.assertIn(
  168. "You starred this project", output.get_data(as_text=True)
  169. )
  170. self._check_star_count(data=data, stars=1)
  171. # now, test if the project 'test' comes in pingou's stars
  172. output = self.app.get("/user/pingou/stars/")
  173. output_text = output.get_data(as_text=True)
  174. self.assertIn("<title>pingou - starred - Pagure</title>", output_text)
  175. self.assertIn(
  176. '<span class="btn btn-outline-secondary disabled opacity-100 '
  177. 'border-0 ml-auto font-weight-bold">1 Project</span>',
  178. output_text,
  179. )
  180. self.assertEqual(output_text.count('class="list-group-item"'), 1)
  181. self.assertEqual(
  182. output_text.count('<div class="media-body align-self-center">'), 2
  183. )
  184. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  185. self.assertEqual(len(repo.stargazers), 1)
  186. # make pingou unstar the project
  187. user = tests.FakeUser()
  188. user.username = "pingou"
  189. with tests.user_set(self.app.application, user):
  190. csrf_token = self.get_csrf()
  191. data = {"csrf_token": csrf_token}
  192. output = self.app.post(
  193. "/test/star/0", data=data, follow_redirects=True
  194. )
  195. self.assertEqual(output.status_code, 200)
  196. self.assertIn(
  197. "You unstarred this project", output.get_data(as_text=True)
  198. )
  199. self._check_star_count(data=data, stars=0)
  200. self.session = pagure.lib.query.create_session(self.dbpath)
  201. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  202. self.assertEqual(len(repo.stargazers), 0)
  203. # now, test if test's name comes in pingou's stars
  204. # it shouldn't because, he just unstarred
  205. output = self.app.get("/user/pingou/stars/")
  206. output_text = output.get_data(as_text=True)
  207. self.assertIn("<title>pingou - starred - Pagure</title>", output_text)
  208. self.assertIn(
  209. '<span class="btn btn-outline-secondary disabled opacity-100 '
  210. 'border-0 ml-auto font-weight-bold">0 Project</span>',
  211. output_text,
  212. )
  213. self.assertEqual(output_text.count('class="list-group-item"'), 0)
  214. self.assertEqual(
  215. output_text.count('<div class="media-body align-self-center">'), 1
  216. )
  217. if __name__ == "__main__":
  218. unittest.main(verbosity=2)