test_pagure_lib_star_project.py 8.2 KB

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