test_pagure_flask_api_project_blockuser.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2019 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import arrow
  9. import copy
  10. import datetime
  11. import unittest
  12. import shutil
  13. import sys
  14. import time
  15. import os
  16. import flask
  17. import json
  18. import munch
  19. from mock import patch, MagicMock
  20. from sqlalchemy.exc import SQLAlchemyError
  21. sys.path.insert(
  22. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  23. )
  24. import pagure.lib.query
  25. import tests
  26. class PagureFlaskApiProjectBlockuserTests(tests.SimplePagureTest):
  27. """ Tests for the flask API of pagure for assigning a PR """
  28. maxDiff = None
  29. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  30. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  31. def setUp(self):
  32. """ Set up the environnment, ran before every tests. """
  33. super(PagureFlaskApiProjectBlockuserTests, self).setUp()
  34. tests.create_projects(self.session)
  35. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  36. tests.create_tokens(self.session)
  37. tests.create_tokens_acl(self.session)
  38. item = pagure.lib.model.Token(
  39. id="aaabbbcccdddeee",
  40. user_id=2,
  41. project_id=1,
  42. expiration=datetime.datetime.utcnow()
  43. + datetime.timedelta(days=30),
  44. )
  45. self.session.add(item)
  46. self.session.commit()
  47. tests.create_tokens_acl(self.session, token_id="aaabbbcccdddeee")
  48. project = pagure.lib.query.get_authorized_project(self.session, "test")
  49. self.assertEqual(project.block_users, [])
  50. self.blocked_users = []
  51. project = pagure.lib.query.get_authorized_project(
  52. self.session, "test2"
  53. )
  54. project.block_users = ["foo"]
  55. self.session.add(project)
  56. self.session.commit()
  57. def tearDown(self):
  58. """ Tears down the environment at the end of the tests. """
  59. project = pagure.lib.query.get_authorized_project(self.session, "test")
  60. self.assertEqual(project.block_users, self.blocked_users)
  61. super(PagureFlaskApiProjectBlockuserTests, self).tearDown()
  62. def test_api_blockuser_no_token(self):
  63. """ Test api_project_block_user method when no token is provided.
  64. """
  65. # No token
  66. output = self.app.post("/api/0/test/blockuser")
  67. self.assertEqual(output.status_code, 401)
  68. data = json.loads(output.get_data(as_text=True))
  69. self.assertDictEqual(
  70. data,
  71. {
  72. "error": "Invalid or expired token. Please visit "
  73. "http://localhost.localdomain/settings#nav-api-tab to "
  74. "get or renew your API token.",
  75. "error_code": "EINVALIDTOK",
  76. "errors": "Invalid token",
  77. },
  78. )
  79. def test_api_blockuser_invalid_token(self):
  80. """ Test api_project_block_user method when the token provided is invalid.
  81. """
  82. headers = {"Authorization": "token aaabbbcccd"}
  83. # Invalid token
  84. output = self.app.post("/api/0/test/blockuser", headers=headers)
  85. self.assertEqual(output.status_code, 401)
  86. data = json.loads(output.get_data(as_text=True))
  87. self.assertDictEqual(
  88. data,
  89. {
  90. "error": "Invalid or expired token. Please visit "
  91. "http://localhost.localdomain/settings#nav-api-tab to "
  92. "get or renew your API token.",
  93. "error_code": "EINVALIDTOK",
  94. "errors": "Invalid token",
  95. },
  96. )
  97. def test_api_blockuser_no_data(self):
  98. """ Test api_project_block_user method when no data is provided.
  99. """
  100. headers = {"Authorization": "token aaabbbcccddd"}
  101. # No user blocked
  102. output = self.app.post("/api/0/test/blockuser", headers=headers)
  103. self.assertEqual(output.status_code, 200)
  104. data = json.loads(output.get_data(as_text=True))
  105. self.assertDictEqual(data, {"message": "User(s) blocked"})
  106. def test_api_blockuser_invalid_user(self):
  107. """ Test api_project_block_user method when the data provided includes
  108. an invalid username.
  109. """
  110. headers = {"Authorization": "token aaabbbcccddd"}
  111. data = {"username": ["invalid"]}
  112. # No user blocked
  113. output = self.app.post(
  114. "/api/0/test/blockuser", headers=headers, data=data
  115. )
  116. self.assertEqual(output.status_code, 400)
  117. data = json.loads(output.get_data(as_text=True))
  118. self.assertDictEqual(
  119. data, {"error": 'No user "invalid" found', "error_code": "ENOCODE"}
  120. )
  121. def test_api_blockuser_insufficient_rights(self):
  122. """ Test api_project_block_user method when the user doing the action
  123. does not have admin priviledges.
  124. """
  125. headers = {"Authorization": "token aaabbbcccdddeee"}
  126. data = {"username": ["invalid"]}
  127. # No user blocked
  128. output = self.app.post(
  129. "/api/0/test/blockuser", headers=headers, data=data
  130. )
  131. self.assertEqual(output.status_code, 401)
  132. data = json.loads(output.get_data(as_text=True))
  133. self.assertDictEqual(
  134. data,
  135. {
  136. "error": "You do not have sufficient permissions to perform "
  137. "this action",
  138. "error_code": "ENOTHIGHENOUGH",
  139. },
  140. )
  141. def test_api_blockuser_with_data(self):
  142. """ Test api_pull_request_assign method when the project doesn't exist.
  143. """
  144. self.blocked_users = ["foo"]
  145. headers = {"Authorization": "token aaabbbcccddd"}
  146. data = {"username": ["foo"]}
  147. # user blocked
  148. output = self.app.post(
  149. "/api/0/test/blockuser", headers=headers, data=data
  150. )
  151. self.assertEqual(output.status_code, 200)
  152. data = json.loads(output.get_data(as_text=True))
  153. self.assertDictEqual(data, {"message": "User(s) blocked"})
  154. # Second request, no changes
  155. headers = {"Authorization": "token aaabbbcccddd"}
  156. data = {"username": ["foo"]}
  157. output = self.app.post(
  158. "/api/0/test/blockuser", headers=headers, data=data
  159. )
  160. self.assertEqual(output.status_code, 200)
  161. data = json.loads(output.get_data(as_text=True))
  162. self.assertDictEqual(data, {"message": "User(s) blocked"})
  163. def test_api_blockeduser_api(self):
  164. """ Test doing a POST request to the API when the user is blocked.
  165. """
  166. self.blocked_users = ["pingou"]
  167. headers = {"Authorization": "token aaabbbcccddd"}
  168. data = {"username": ["pingou"]}
  169. # user blocked
  170. output = self.app.post(
  171. "/api/0/test/blockuser", headers=headers, data=data
  172. )
  173. self.assertEqual(output.status_code, 200)
  174. data = json.loads(output.get_data(as_text=True))
  175. self.assertDictEqual(data, {"message": "User(s) blocked"})
  176. # Second request, but user is blocked
  177. headers = {"Authorization": "token aaabbbcccddd"}
  178. data = {"username": ["foo"]}
  179. output = self.app.post(
  180. "/api/0/test/blockuser", headers=headers, data=data
  181. )
  182. self.assertEqual(output.status_code, 403)
  183. data = json.loads(output.get_data(as_text=True))
  184. self.assertDictEqual(
  185. data,
  186. {
  187. "error": "You have been blocked from this project",
  188. "error_code": "EUBLOCKED",
  189. },
  190. )
  191. def test_ui_new_issue_user_blocked(self):
  192. """ Test doing a POST request to the UI when the user is blocked.
  193. """
  194. user = tests.FakeUser(username="foo")
  195. with tests.user_set(self.app.application, user):
  196. output = self.app.get("/test2/new_issue")
  197. self.assertEqual(output.status_code, 200)
  198. self.assertIn("New Issue", output.get_data(as_text=True))
  199. csrf_token = self.get_csrf(output=output)
  200. data = {
  201. "title": "Test issue",
  202. "issue_content": "We really should improve on this issue",
  203. "status": "Open",
  204. "csrf_token": csrf_token,
  205. }
  206. output = self.app.post("/test2/new_issue", data=data)
  207. self.assertEqual(output.status_code, 403)
  208. output_text = output.get_data(as_text=True)
  209. self.assertIn(
  210. "<p>You have been blocked from this project</p>", output_text
  211. )
  212. if __name__ == "__main__":
  213. unittest.main(verbosity=2)