test_pagure_flask_ui_star_project.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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(0, os.path.join(os.path.dirname(
  13. os.path.abspath(__file__)), '..'))
  14. import pagure.config
  15. import pagure.lib.query
  16. import tests
  17. class TestStarProjectUI(tests.SimplePagureTest):
  18. def setUp(self):
  19. """ Set up the environnment, ran before every tests. """
  20. super(TestStarProjectUI, self).setUp()
  21. tests.create_projects(self.session)
  22. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  23. def _check_star_count(self, data, stars=1):
  24. """ Check if the star count is correct or not """
  25. output = self.app.get(
  26. '/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. self.assertEqual(output.status_code, 400)
  54. def test_star_project_invalid_star(self):
  55. """ Test the star_project endpoint for invalid star """
  56. user = tests.FakeUser()
  57. user.username = 'pingou'
  58. with tests.user_set(self.app.application, user):
  59. csrf_token = self.get_csrf()
  60. data = {
  61. 'csrf_token': csrf_token,
  62. }
  63. output = self.app.post(
  64. '/test/star/2', data=data, follow_redirects=True)
  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 = {
  74. 'csrf_token': csrf_token,
  75. }
  76. # try starring the project for pingou
  77. output = self.app.post(
  78. '/test/star/1', data=data, follow_redirects=True)
  79. self.assertEqual(output.status_code, 200)
  80. self.assertIn(
  81. 'You starred this project',
  82. output.get_data(as_text=True)
  83. )
  84. # check home page of project for star count
  85. self._check_star_count(data=data, stars=1)
  86. # try unstarring the project for pingou
  87. output = self.app.post(
  88. '/test/star/0', data=data, follow_redirects=True)
  89. self.assertEqual(output.status_code, 200)
  90. self.assertIn(
  91. 'You unstarred this project',
  92. output.get_data(as_text=True)
  93. )
  94. self._check_star_count(data=data, stars=0)
  95. def test_repo_stargazers(self):
  96. """ Test the repo_stargazers endpoint of pagure.ui.repo """
  97. # make pingou star the project
  98. # first create pingou
  99. user = tests.FakeUser()
  100. user.username = 'pingou'
  101. with tests.user_set(self.app.application, user):
  102. csrf_token = self.get_csrf()
  103. data = {
  104. 'csrf_token': csrf_token,
  105. }
  106. output = self.app.post(
  107. '/test/star/1', data=data, follow_redirects=True)
  108. self.assertEqual(output.status_code, 200)
  109. self.assertIn(
  110. 'You starred this project',
  111. output.get_data(as_text=True)
  112. )
  113. self._check_star_count(data=data, stars=1)
  114. # now, test if pingou's name comes in repo stargazers
  115. output = self.app.get(
  116. '/test/stargazers/'
  117. )
  118. self.assertIn(
  119. '<title>Stargazers of test - Pagure</title>',
  120. output.get_data(as_text=True)
  121. )
  122. self.assertIn(
  123. '<a class="font-weight-bold" href="/user/pingou">pingou</a>',
  124. output.get_data(as_text=True)
  125. )
  126. # make pingou unstar the project
  127. user = tests.FakeUser()
  128. user.username = 'pingou'
  129. with tests.user_set(self.app.application, user):
  130. csrf_token = self.get_csrf()
  131. data = {
  132. 'csrf_token': csrf_token,
  133. }
  134. output = self.app.post(
  135. '/test/star/0', data=data, follow_redirects=True)
  136. self.assertEqual(output.status_code, 200)
  137. self.assertIn(
  138. 'You unstarred this project',
  139. output.get_data(as_text=True)
  140. )
  141. self._check_star_count(data=data, stars=0)
  142. # now, test if pingou's name comes in repo stargazers
  143. # it shouldn't because, he just unstarred
  144. output = self.app.get(
  145. '/test/stargazers/'
  146. )
  147. self.assertIn(
  148. '<title>Stargazers of test - Pagure</title>',
  149. output.get_data(as_text=True)
  150. )
  151. self.assertNotIn(
  152. '<a class="font-weight-bold" href="/user/pingou">pingou</a>',
  153. output.get_data(as_text=True)
  154. )
  155. def test_user_stars(self):
  156. """ Test the user_stars endpoint of pagure.ui.app """
  157. # Check pingou's stars before
  158. output = self.app.get('/user/pingou/stars/')
  159. output_text = output.get_data(as_text=True)
  160. self.assertIn(
  161. "<title>pingou - starred - Pagure</title>", output_text
  162. )
  163. self.assertIn(
  164. '<span class="btn btn-outline-secondary disabled opacity-100 '
  165. 'border-0 ml-auto font-weight-bold">0 Projects</span>',
  166. output_text)
  167. # make pingou star the project
  168. # first create pingou
  169. user = tests.FakeUser()
  170. user.username = 'pingou'
  171. with tests.user_set(self.app.application, user):
  172. csrf_token = self.get_csrf()
  173. data = {
  174. 'csrf_token': csrf_token,
  175. }
  176. output = self.app.post(
  177. '/test/star/1', data=data, follow_redirects=True)
  178. self.assertEqual(output.status_code, 200)
  179. self.assertIn(
  180. 'You starred this project',
  181. output.get_data(as_text=True)
  182. )
  183. self._check_star_count(data=data, stars=1)
  184. # now, test if the project 'test' comes in pingou's stars
  185. output = self.app.get('/user/pingou/stars/')
  186. output_text = output.get_data(as_text=True)
  187. self.assertIn(
  188. "<title>pingou - starred - Pagure</title>", output_text
  189. )
  190. self.assertIn(
  191. '<span class="btn btn-outline-secondary disabled opacity-100 '
  192. 'border-0 ml-auto font-weight-bold">1 Projects</span>',
  193. output_text)
  194. self.assertEqual(
  195. output_text.count('class="list-group-item"'), 1)
  196. self.assertEqual(
  197. output_text.count('<div class="media-body align-self-center">'), 2)
  198. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  199. self.assertEqual(len(repo.stargazers), 1)
  200. # make pingou unstar the project
  201. user = tests.FakeUser()
  202. user.username = 'pingou'
  203. with tests.user_set(self.app.application, user):
  204. csrf_token = self.get_csrf()
  205. data = {
  206. 'csrf_token': csrf_token,
  207. }
  208. output = self.app.post(
  209. '/test/star/0', data=data, follow_redirects=True)
  210. self.assertEqual(output.status_code, 200)
  211. self.assertIn(
  212. 'You unstarred this project',
  213. output.get_data(as_text=True)
  214. )
  215. self._check_star_count(data=data, stars=0)
  216. self.session = pagure.lib.query.create_session(self.dbpath)
  217. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  218. self.assertEqual(len(repo.stargazers), 0)
  219. # now, test if test's name comes in pingou's stars
  220. # it shouldn't because, he just unstarred
  221. output = self.app.get('/user/pingou/stars/')
  222. output_text = output.get_data(as_text=True)
  223. self.assertIn(
  224. "<title>pingou - starred - Pagure</title>", output_text
  225. )
  226. self.assertIn(
  227. '<span class="btn btn-outline-secondary disabled opacity-100 '
  228. 'border-0 ml-auto font-weight-bold">0 Projects</span>',
  229. output_text)
  230. self.assertEqual(
  231. output_text.count('class="list-group-item"'), 0)
  232. self.assertEqual(
  233. output_text.count('<div class="media-body align-self-center">'), 1)
  234. if __name__ == '__main__':
  235. unittest.main(verbosity=2)