test_pagure_lib_star_project.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # coding=utf-8
  2. """
  3. (c) 2015-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
  16. import pagure.lib
  17. import tests
  18. class TestStarProjectLib(tests.SimplePagureTest):
  19. ''' Test the star project feature of pagure '''
  20. def setUp(self):
  21. """ Set up the environnment for running each star project lib tests """
  22. super(TestStarProjectLib, self).setUp()
  23. tests.create_projects(self.session)
  24. def test_update_star_project(self):
  25. ''' Test the update_star_project endpoint of pagure.lib '''
  26. repo_obj = pagure.lib._get_project(self.session, 'test')
  27. # test with invalud Star object, should return None
  28. msg = pagure.lib.update_star_project(
  29. self.session,
  30. repo_obj,
  31. None,
  32. 'pingou',
  33. )
  34. self.session.commit()
  35. self.assertEqual(msg, None)
  36. user_obj = pagure.lib.get_user(self.session, 'pingou')
  37. self.assertEqual(len(user_obj.stars), 0)
  38. # test starring the project
  39. repo_obj = pagure.lib._get_project(self.session, 'test')
  40. msg = pagure.lib.update_star_project(
  41. self.session,
  42. repo_obj,
  43. '1',
  44. 'pingou',
  45. )
  46. self.session.commit()
  47. self.assertEqual(msg, 'You starred this project')
  48. user_obj = pagure.lib.get_user(self.session, 'pingou')
  49. self.assertEqual(len(user_obj.stars), 1)
  50. # test unstarring the project
  51. repo_obj = pagure.lib._get_project(self.session, 'test')
  52. msg = pagure.lib.update_star_project(
  53. self.session,
  54. repo_obj,
  55. '0',
  56. 'pingou',
  57. )
  58. self.session.commit()
  59. self.assertEqual(msg, 'You unstarred this project')
  60. user_obj = pagure.lib.get_user(self.session, 'pingou')
  61. self.assertEqual(len(user_obj.stars), 0)
  62. def test_star_project(self):
  63. ''' Test the _star_project endpoint of pagure.lib '''
  64. # test with not all arguments present
  65. user_obj = pagure.lib.get_user(self.session, 'pingou')
  66. repo_obj = pagure.lib._get_project(self.session, 'test')
  67. msg = pagure.lib._star_project(
  68. self.session,
  69. repo_obj,
  70. None
  71. )
  72. self.session.commit()
  73. self.assertEqual(msg, None)
  74. repo_obj = pagure.lib._get_project(self.session, 'test')
  75. user_obj = pagure.lib.get_user(self.session, 'pingou')
  76. msg = pagure.lib._star_project(
  77. self.session,
  78. repo_obj,
  79. user_obj,
  80. )
  81. self.session.commit()
  82. self.assertEqual(msg, 'You starred this project')
  83. user_obj = pagure.lib.get_user(self.session, 'pingou')
  84. self.assertEqual(len(user_obj.stars), 1)
  85. def test_unstar_project(self):
  86. ''' Test the _unstar_project endpoint of pagure.lib '''
  87. # test with not all arguments present
  88. user_obj = pagure.lib.get_user(self.session, 'pingou')
  89. repo_obj = pagure.lib._get_project(self.session, 'test')
  90. msg = pagure.lib._unstar_project(
  91. self.session,
  92. repo_obj,
  93. None
  94. )
  95. self.session.commit()
  96. self.assertEqual(msg, None)
  97. # the user hasn't starred the project before
  98. repo_obj = pagure.lib._get_project(self.session, 'test')
  99. user_obj = pagure.lib.get_user(self.session, 'pingou')
  100. msg = pagure.lib._unstar_project(
  101. self.session,
  102. repo_obj,
  103. user_obj,
  104. )
  105. self.assertEqual(msg, 'You never starred the project')
  106. user_obj = pagure.lib.get_user(self.session, 'pingou')
  107. self.assertEqual(len(user_obj.stars), 0)
  108. # star it for testing
  109. msg = pagure.lib._star_project(
  110. self.session,
  111. repo_obj,
  112. user_obj,
  113. )
  114. self.session.commit()
  115. self.assertEqual(msg, 'You starred this project')
  116. user_obj = pagure.lib.get_user(self.session, 'pingou')
  117. self.assertEqual(len(user_obj.stars), 1)
  118. # the user starred and wishes to unstar
  119. repo_obj = pagure.lib._get_project(self.session, 'test')
  120. user_obj = pagure.lib.get_user(self.session, 'pingou')
  121. msg = pagure.lib._unstar_project(
  122. self.session,
  123. repo_obj,
  124. user_obj,
  125. )
  126. self.session.commit()
  127. self.assertEqual(msg, 'You unstarred this project')
  128. user_obj = pagure.lib.get_user(self.session, 'pingou')
  129. self.assertEqual(len(user_obj.stars), 0)
  130. def test_get_stargazer_obj(self):
  131. ''' Test the _get_stargazer_obj test of pagure.lib '''
  132. # star the project first
  133. repo_obj = pagure.lib._get_project(self.session, 'test')
  134. user_obj = pagure.lib.get_user(self.session, 'pingou')
  135. msg = pagure.lib._star_project(
  136. self.session,
  137. repo_obj,
  138. user_obj,
  139. )
  140. self.session.commit()
  141. self.assertEqual(msg, 'You starred this project')
  142. user_obj = pagure.lib.get_user(self.session, 'pingou')
  143. self.assertEqual(len(user_obj.stars), 1)
  144. # get the object now
  145. repo_obj = pagure.lib._get_project(self.session, 'test')
  146. star_obj = pagure.lib._get_stargazer_obj(
  147. self.session,
  148. repo_obj,
  149. user_obj
  150. )
  151. self.assertEqual(isinstance(star_obj, pagure.lib.model.Star), True)
  152. # unstar it and then try to get the object
  153. repo_obj = pagure.lib._get_project(self.session, 'test')
  154. user_obj = pagure.lib.get_user(self.session, 'pingou')
  155. msg = pagure.lib._unstar_project(
  156. self.session,
  157. repo_obj,
  158. user_obj,
  159. )
  160. self.session.commit()
  161. self.assertEqual(msg, 'You unstarred this project')
  162. user_obj = pagure.lib.get_user(self.session, 'pingou')
  163. self.assertEqual(len(user_obj.stars), 0)
  164. # we don't store if the user has unstarred, we delete the obj
  165. # so, we should get anything back in the query
  166. repo_obj = pagure.lib._get_project(self.session, 'test')
  167. star_obj = pagure.lib._get_stargazer_obj(
  168. self.session,
  169. repo_obj,
  170. user_obj
  171. )
  172. self.assertEqual(star_obj is None, True)
  173. def test_has_starred(self):
  174. ''' Test the has_starred endpoint of pagure.lib '''
  175. # star the project
  176. repo_obj = pagure.lib._get_project(self.session, 'test')
  177. user_obj = pagure.lib.get_user(self.session, 'pingou')
  178. msg = pagure.lib._star_project(
  179. self.session,
  180. repo_obj,
  181. user_obj,
  182. )
  183. self.session.commit()
  184. self.assertEqual(msg, 'You starred this project')
  185. user_obj = pagure.lib.get_user(self.session, 'pingou')
  186. self.assertEqual(len(user_obj.stars), 1)
  187. has_starred = pagure.lib.has_starred(
  188. self.session,
  189. repo_obj,
  190. 'pingou'
  191. )
  192. self.assertEqual(has_starred is True, True)
  193. # unstar it and then test for has_starred
  194. repo_obj = pagure.lib._get_project(self.session, 'test')
  195. user_obj = pagure.lib.get_user(self.session, 'pingou')
  196. msg = pagure.lib._unstar_project(
  197. self.session,
  198. repo_obj,
  199. user_obj,
  200. )
  201. self.session.commit()
  202. self.assertEqual(msg, 'You unstarred this project')
  203. user_obj = pagure.lib.get_user(self.session, 'pingou')
  204. self.assertEqual(len(user_obj.stars), 0)
  205. # check now, it should return False
  206. has_starred = pagure.lib.has_starred(
  207. self.session,
  208. repo_obj,
  209. 'pingou'
  210. )
  211. self.assertEqual(has_starred is False, True)
  212. if __name__ == '__main__':
  213. unittest.main(verbosity=2)