test_pagure_flask_ui_star_project.py 9.4 KB

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