test_pagure_flask_ui_star_project.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. __requires__ = ['SQLAlchemy >= 0.8']
  9. import pkg_resources
  10. import unittest
  11. import sys
  12. import os
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure.config
  16. import pagure.lib
  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(
  27. '/test/', data=data, follow_redirects=True)
  28. if stars == 1:
  29. self.assertIn(
  30. '<a href="/test/stargazers/" class="btn '
  31. 'btn-sm btn-primary">1</a>',
  32. output.data
  33. )
  34. elif stars == 0:
  35. self.assertIn(
  36. '<a href="/test/stargazers/" class="btn '
  37. 'btn-sm btn-primary">0</a>',
  38. output.data
  39. )
  40. def test_star_project_no_project(self):
  41. """ Test the star_project endpoint. """
  42. # No such project
  43. output = self.app.post('/test42/star/1')
  44. self.assertEqual(output.status_code, 404)
  45. def test_star_project_no_csrf(self):
  46. """ Test the star_project endpoint for the case when there
  47. is no CSRF token given """
  48. user = tests.FakeUser()
  49. user.username = 'pingou'
  50. with tests.user_set(self.app.application, user):
  51. data = {}
  52. output = self.app.post(
  53. '/test/star/1', data=data, follow_redirects=True)
  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 = {
  62. 'csrf_token': csrf_token,
  63. }
  64. output = self.app.post(
  65. '/test/star/2', data=data, follow_redirects=True)
  66. self.assertEqual(output.status_code, 400)
  67. self._check_star_count(data=data, stars=0)
  68. def test_star_project_valid_star(self):
  69. """ Test the star_project endpoint for correct star """
  70. user = tests.FakeUser()
  71. user.username = 'pingou'
  72. with tests.user_set(self.app.application, user):
  73. csrf_token = self.get_csrf()
  74. data = {
  75. 'csrf_token': csrf_token,
  76. }
  77. # try starring the project for pingou
  78. output = self.app.post(
  79. '/test/star/1', data=data, follow_redirects=True)
  80. self.assertEqual(output.status_code, 200)
  81. self.assertIn(
  82. '</button>\n You starred '
  83. 'this project\n </div>',
  84. output.data
  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. '</button>\n You unstarred '
  94. 'this project\n </div>',
  95. output.data
  96. )
  97. self._check_star_count(data=data, stars=0)
  98. def test_repo_stargazers(self):
  99. """ Test the repo_stargazers endpoint of pagure.ui.repo """
  100. # make pingou star the project
  101. # first create pingou
  102. user = tests.FakeUser()
  103. user.username = 'pingou'
  104. with tests.user_set(self.app.application, user):
  105. csrf_token = self.get_csrf()
  106. data = {
  107. 'csrf_token': csrf_token,
  108. }
  109. output = self.app.post(
  110. '/test/star/1', data=data, follow_redirects=True)
  111. self.assertEqual(output.status_code, 200)
  112. self.assertIn(
  113. '</button>\n You starred '
  114. 'this project\n </div>',
  115. output.data
  116. )
  117. self._check_star_count(data=data, stars=1)
  118. # now, test if pingou's name comes in repo stargazers
  119. output = self.app.get(
  120. '/test/stargazers/'
  121. )
  122. self.assertIn(
  123. '<title>Stargazers of test - Pagure</title>',
  124. output.data
  125. )
  126. self.assertIn(
  127. '<a href="/user/pingou">pingou\n </a>',
  128. output.data
  129. )
  130. # make pingou unstar the project
  131. user = tests.FakeUser()
  132. user.username = 'pingou'
  133. with tests.user_set(self.app.application, user):
  134. csrf_token = self.get_csrf()
  135. data = {
  136. 'csrf_token': csrf_token,
  137. }
  138. output = self.app.post(
  139. '/test/star/0', data=data, follow_redirects=True)
  140. self.assertEqual(output.status_code, 200)
  141. self.assertIn(
  142. '</button>\n You unstarred '
  143. 'this project\n </div>',
  144. output.data
  145. )
  146. self._check_star_count(data=data, stars=0)
  147. # now, test if pingou's name comes in repo stargazers
  148. # it shouldn't because, he just unstarred
  149. output = self.app.get(
  150. '/test/stargazers/'
  151. )
  152. self.assertIn(
  153. '<title>Stargazers of test - Pagure</title>',
  154. output.data
  155. )
  156. self.assertNotIn(
  157. '<a href="/user/pingou">pingou\n </a>',
  158. output.data
  159. )
  160. def test_user_stars(self):
  161. """ Test the user_stars endpoint of pagure.ui.app """
  162. # make pingou star the project
  163. # first create pingou
  164. user = tests.FakeUser()
  165. user.username = 'pingou'
  166. with tests.user_set(self.app.application, user):
  167. csrf_token = self.get_csrf()
  168. data = {
  169. 'csrf_token': csrf_token,
  170. }
  171. output = self.app.post(
  172. '/test/star/1', data=data, follow_redirects=True)
  173. self.assertEqual(output.status_code, 200)
  174. self.assertIn(
  175. '</button>\n You starred '
  176. 'this project\n </div>',
  177. output.data
  178. )
  179. self._check_star_count(data=data, stars=1)
  180. # now, test if the project 'test' comes in pingou's stars
  181. output = self.app.get(
  182. '/user/pingou/stars'
  183. )
  184. self.assertIn(
  185. "<title>pingou's starred Projects - Pagure</title>",
  186. output.data
  187. )
  188. self.assertIn(
  189. '<a class="list-group-item" href="/test">', output.data)
  190. self.assertEqual(output.data.count('class="list-group-item"'), 1)
  191. self.assertEqual(
  192. output.data.count('<span class="oi" data-glyph="document"></span>'),
  193. 1)
  194. # make pingou unstar the project
  195. user = tests.FakeUser()
  196. user.username = 'pingou'
  197. with tests.user_set(self.app.application, user):
  198. csrf_token = self.get_csrf()
  199. data = {
  200. 'csrf_token': csrf_token,
  201. }
  202. output = self.app.post(
  203. '/test/star/0', data=data, follow_redirects=True)
  204. self.assertEqual(output.status_code, 200)
  205. self.assertIn(
  206. '</button>\n You unstarred '
  207. 'this project\n </div>',
  208. output.data
  209. )
  210. self._check_star_count(data=data, stars=0)
  211. # now, test if test's name comes in pingou's stars
  212. # it shouldn't because, he just unstarred
  213. output = self.app.get(
  214. '/user/pingou/stars/'
  215. )
  216. self.assertIn(
  217. "<title>pingou's starred Projects - Pagure</title>",
  218. output.data
  219. )
  220. self.assertNotIn(
  221. '<a class="list-group-item" href="/test">test</a>\n',
  222. output.data
  223. )
  224. self.assertEqual(
  225. output.data.count('<span class="oi" data-glyph="document"></span>'),
  226. 0)
  227. self.assertEqual(output.data.count('class="list-group-item"'), 0)
  228. if __name__ == '__main__':
  229. unittest.main(verbosity=2)