test_pagure_lib_star_project.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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(
  13. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  14. )
  15. import pagure.lib.model
  16. import pagure.lib.query
  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.query._get_project(self.session, "test")
  27. # test with invalud Star object, should return None
  28. msg = pagure.lib.query.update_star_project(
  29. self.session, repo_obj, None, "pingou"
  30. )
  31. self.session.commit()
  32. self.assertEqual(msg, None)
  33. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  34. self.assertEqual(len(user_obj.stars), 0)
  35. # test starring the project
  36. repo_obj = pagure.lib.query._get_project(self.session, "test")
  37. msg = pagure.lib.query.update_star_project(
  38. self.session, repo_obj, "1", "pingou"
  39. )
  40. self.session.commit()
  41. self.assertEqual(msg, "You starred this project")
  42. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  43. self.assertEqual(len(user_obj.stars), 1)
  44. # test unstarring the project
  45. repo_obj = pagure.lib.query._get_project(self.session, "test")
  46. msg = pagure.lib.query.update_star_project(
  47. self.session, repo_obj, "0", "pingou"
  48. )
  49. self.session.commit()
  50. self.assertEqual(msg, "You unstarred this project")
  51. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  52. self.assertEqual(len(user_obj.stars), 0)
  53. def test_star_project(self):
  54. """Test the _star_project endpoint of pagure.lib"""
  55. # test with not all arguments present
  56. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  57. repo_obj = pagure.lib.query._get_project(self.session, "test")
  58. msg = pagure.lib.query._star_project(self.session, repo_obj, None)
  59. self.session.commit()
  60. self.assertEqual(msg, None)
  61. repo_obj = pagure.lib.query._get_project(self.session, "test")
  62. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  63. msg = pagure.lib.query._star_project(self.session, repo_obj, user_obj)
  64. self.session.commit()
  65. self.assertEqual(msg, "You starred this project")
  66. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  67. self.assertEqual(len(user_obj.stars), 1)
  68. def test_unstar_project(self):
  69. """Test the _unstar_project endpoint of pagure.lib"""
  70. # test with not all arguments present
  71. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  72. repo_obj = pagure.lib.query._get_project(self.session, "test")
  73. msg = pagure.lib.query._unstar_project(self.session, repo_obj, None)
  74. self.session.commit()
  75. self.assertEqual(msg, None)
  76. # the user hasn't starred the project before
  77. repo_obj = pagure.lib.query._get_project(self.session, "test")
  78. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  79. msg = pagure.lib.query._unstar_project(
  80. self.session, repo_obj, user_obj
  81. )
  82. self.assertEqual(msg, "You never starred the project")
  83. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  84. self.assertEqual(len(user_obj.stars), 0)
  85. # star it for testing
  86. msg = pagure.lib.query._star_project(self.session, repo_obj, user_obj)
  87. self.session.commit()
  88. self.assertEqual(msg, "You starred this project")
  89. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  90. self.assertEqual(len(user_obj.stars), 1)
  91. # the user starred and wishes to unstar
  92. repo_obj = pagure.lib.query._get_project(self.session, "test")
  93. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  94. msg = pagure.lib.query._unstar_project(
  95. self.session, repo_obj, user_obj
  96. )
  97. self.session.commit()
  98. self.assertEqual(msg, "You unstarred this project")
  99. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  100. self.assertEqual(len(user_obj.stars), 0)
  101. def test_get_stargazer_obj(self):
  102. """Test the _get_stargazer_obj test of pagure.lib"""
  103. # star the project first
  104. repo_obj = pagure.lib.query._get_project(self.session, "test")
  105. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  106. msg = pagure.lib.query._star_project(self.session, repo_obj, user_obj)
  107. self.session.commit()
  108. self.assertEqual(msg, "You starred this project")
  109. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  110. self.assertEqual(len(user_obj.stars), 1)
  111. # get the object now
  112. repo_obj = pagure.lib.query._get_project(self.session, "test")
  113. star_obj = pagure.lib.query._get_stargazer_obj(
  114. self.session, repo_obj, user_obj
  115. )
  116. self.assertEqual(isinstance(star_obj, pagure.lib.model.Star), True)
  117. # unstar it and then try to get the object
  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, repo_obj, user_obj
  122. )
  123. self.session.commit()
  124. self.assertEqual(msg, "You unstarred this project")
  125. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  126. self.assertEqual(len(user_obj.stars), 0)
  127. # we don't store if the user has unstarred, we delete the obj
  128. # so, we should get anything back in the query
  129. repo_obj = pagure.lib.query._get_project(self.session, "test")
  130. star_obj = pagure.lib.query._get_stargazer_obj(
  131. self.session, repo_obj, user_obj
  132. )
  133. self.assertEqual(star_obj is None, True)
  134. def test_has_starred(self):
  135. """Test the has_starred endpoint of pagure.lib"""
  136. # star the project
  137. repo_obj = pagure.lib.query._get_project(self.session, "test")
  138. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  139. msg = pagure.lib.query._star_project(self.session, repo_obj, user_obj)
  140. self.session.commit()
  141. self.assertEqual(msg, "You starred this project")
  142. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  143. self.assertEqual(len(user_obj.stars), 1)
  144. has_starred = pagure.lib.query.has_starred(
  145. self.session, repo_obj, "pingou"
  146. )
  147. self.assertEqual(has_starred is True, True)
  148. # unstar it and then test for has_starred
  149. repo_obj = pagure.lib.query._get_project(self.session, "test")
  150. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  151. msg = pagure.lib.query._unstar_project(
  152. self.session, repo_obj, user_obj
  153. )
  154. self.session.commit()
  155. self.assertEqual(msg, "You unstarred this project")
  156. user_obj = pagure.lib.query.get_user(self.session, "pingou")
  157. self.assertEqual(len(user_obj.stars), 0)
  158. # check now, it should return False
  159. has_starred = pagure.lib.query.has_starred(
  160. self.session, repo_obj, "pingou"
  161. )
  162. self.assertEqual(has_starred is False, True)
  163. if __name__ == "__main__":
  164. unittest.main(verbosity=2)