test_pagure_flask_api_user.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import datetime
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import json
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.api
  19. import pagure.lib
  20. import pagure.lib.model as model
  21. import tests
  22. class PagureFlaskApiUSertests(tests.Modeltests):
  23. """ Tests for the flask API of pagure for issue """
  24. def setUp(self):
  25. """ Set up the environnment, ran before every tests. """
  26. super(PagureFlaskApiUSertests, self).setUp()
  27. pagure.APP.config['TESTING'] = True
  28. pagure.SESSION = self.session
  29. pagure.api.SESSION = self.session
  30. pagure.api.fork.SESSION = self.session
  31. pagure.api.user.SESSION = self.session
  32. pagure.lib.SESSION = self.session
  33. pagure.APP.config['REQUESTS_FOLDER'] = None
  34. self.app = pagure.APP.test_client()
  35. def test_api_users(self):
  36. """ Test the api_users function. """
  37. output = self.app.get('/api/0/users')
  38. self.assertEqual(output.status_code, 200)
  39. data = json.loads(output.data)
  40. self.assertEqual(sorted(data['users']), ['foo', 'pingou'])
  41. self.assertEqual(sorted(data.keys()), ['mention', 'total_users', 'users'])
  42. self.assertEqual(data['total_users'], 2)
  43. output = self.app.get('/api/0/users?pattern=p')
  44. self.assertEqual(output.status_code, 200)
  45. data = json.loads(output.data)
  46. self.assertEqual(data['users'], ['pingou'])
  47. self.assertEqual(sorted(data.keys()), ['mention', 'total_users', 'users'])
  48. self.assertEqual(data['total_users'], 1)
  49. def test_api_view_user(self):
  50. """
  51. Test the api_view_user method of the flask api
  52. The tested user has no project or forks.
  53. """
  54. output = self.app.get('/api/0/user/pingou')
  55. self.assertEqual(output.status_code, 200)
  56. exp = {
  57. "forks": [],
  58. "repos": [],
  59. "user": { "fullname": "PY C", "name": "pingou"}}
  60. data = json.loads(output.data)
  61. self.assertEqual(data, exp)
  62. def test_api_view_user_with_project(self):
  63. """
  64. Test the api_view_user method of the flask api,
  65. this time the user has some project defined.
  66. """
  67. tests.create_projects(self.session)
  68. output = self.app.get('/api/0/user/pingou')
  69. self.assertEqual(output.status_code, 200)
  70. data = json.loads(output.data)
  71. data['repos'][0]['date_created'] = "1490272832"
  72. data['repos'][1]['date_created'] = "1490272832"
  73. data['repos'][2]['date_created'] = "1490272832"
  74. expected_data = {
  75. "forks": [],
  76. "repos": [
  77. {
  78. "access_groups": {
  79. "admin": [],
  80. "commit": [],
  81. "ticket": []
  82. },
  83. "access_users": {
  84. "admin": [],
  85. "commit": [],
  86. "owner": ["pingou"],
  87. "ticket": []
  88. },
  89. "close_status": [
  90. "Invalid",
  91. "Insufficient data",
  92. "Fixed",
  93. "Duplicate"
  94. ],
  95. "custom_keys": [],
  96. "date_created": "1490272832",
  97. "description": "test project #1",
  98. "fullname": "test",
  99. "id": 1,
  100. "milestones": {},
  101. "name": "test",
  102. "namespace": None,
  103. "parent": None,
  104. "priorities": {},
  105. "settings": {
  106. "Enforce_signed-off_commits_in_pull-request": False,
  107. "Minimum_score_to_merge_pull-request": -1,
  108. "Only_assignee_can_merge_pull-request": False,
  109. "Web-hooks": None,
  110. "always_merge": False,
  111. "fedmsg_notifications": True,
  112. "issue_tracker": True,
  113. "issues_default_to_private": False,
  114. "pull_request_access_only": False,
  115. "project_documentation": False,
  116. "pull_requests": True
  117. },
  118. "tags": [],
  119. "user": {
  120. "fullname": "PY C",
  121. "name": "pingou"
  122. }
  123. },
  124. {
  125. "access_groups": {
  126. "admin": [],
  127. "commit": [],
  128. "ticket": []
  129. },
  130. "access_users": {
  131. "admin": [],
  132. "commit": [],
  133. "owner": ["pingou"],
  134. "ticket": []
  135. },
  136. "close_status": [
  137. "Invalid",
  138. "Insufficient data",
  139. "Fixed",
  140. "Duplicate"
  141. ],
  142. "custom_keys": [],
  143. "date_created": "1490272832",
  144. "description": "test project #2",
  145. "fullname": "test2",
  146. "id": 2,
  147. "milestones": {},
  148. "name": "test2",
  149. "namespace": None,
  150. "parent": None,
  151. "priorities": {},
  152. "settings": {
  153. "Enforce_signed-off_commits_in_pull-request": False,
  154. "Minimum_score_to_merge_pull-request": -1,
  155. "Only_assignee_can_merge_pull-request": False,
  156. "Web-hooks": None,
  157. "always_merge": False,
  158. "fedmsg_notifications": True,
  159. "issue_tracker": True,
  160. "issues_default_to_private": False,
  161. "pull_request_access_only": False,
  162. "project_documentation": False,
  163. "pull_requests": True
  164. },
  165. "tags": [],
  166. "user": {
  167. "fullname": "PY C",
  168. "name": "pingou"
  169. }
  170. },
  171. {
  172. "access_groups": {
  173. "admin": [],
  174. "commit": [],
  175. "ticket": []},
  176. "access_users": {
  177. "admin": [],
  178. "commit": [],
  179. "owner": ["pingou"],
  180. "ticket": []
  181. },
  182. "close_status": [
  183. "Invalid",
  184. "Insufficient data",
  185. "Fixed",
  186. "Duplicate"
  187. ],
  188. "custom_keys": [],
  189. "date_created": "1490272832",
  190. "description": "namespaced test project",
  191. "fullname": "somenamespace/test3",
  192. "id": 3,
  193. "milestones": {},
  194. "name": "test3",
  195. "namespace": "somenamespace",
  196. "parent": None,
  197. "priorities": {},
  198. "settings": {
  199. "Enforce_signed-off_commits_in_pull-request": False,
  200. "Minimum_score_to_merge_pull-request": -1,
  201. "Only_assignee_can_merge_pull-request": False,
  202. "Web-hooks": None,
  203. "always_merge": False,
  204. "fedmsg_notifications": True,
  205. "issue_tracker": True,
  206. "issues_default_to_private": False,
  207. "project_documentation": False,
  208. "pull_request_access_only": False,
  209. "pull_requests": True
  210. },
  211. "tags": [],
  212. "user": {
  213. "fullname": "PY C",
  214. "name": "pingou"
  215. }
  216. }
  217. ],
  218. "user": {
  219. "fullname": "PY C",
  220. "name": "pingou"
  221. }
  222. }
  223. self.assertEqual(data, expected_data)
  224. @patch('pagure.lib.notify.send_email')
  225. def test_api_view_user_activity_stats(self, mockemail):
  226. """ Test the api_view_user_activity_stats method of the flask user
  227. api. """
  228. mockemail.return_value = True
  229. tests.create_projects(self.session)
  230. tests.create_tokens(self.session)
  231. tests.create_tokens_acl(self.session)
  232. headers = {'Authorization': 'token aaabbbcccddd'}
  233. # Create a pull-request
  234. repo = pagure.lib._get_project(self.session, 'test')
  235. forked_repo = pagure.lib._get_project(self.session, 'test')
  236. req = pagure.lib.new_pull_request(
  237. session=self.session,
  238. repo_from=forked_repo,
  239. branch_from='master',
  240. repo_to=repo,
  241. branch_to='master',
  242. title='test pull-request',
  243. user='pingou',
  244. requestfolder=None,
  245. )
  246. self.session.commit()
  247. self.assertEqual(req.id, 1)
  248. self.assertEqual(req.title, 'test pull-request')
  249. # Check comments before
  250. request = pagure.lib.search_pull_requests(
  251. self.session, project_id=1, requestid=1)
  252. self.assertEqual(len(request.comments), 0)
  253. data = {
  254. 'comment': 'This is a very interesting question',
  255. }
  256. # Valid request
  257. output = self.app.post(
  258. '/api/0/test/pull-request/1/comment', data=data, headers=headers)
  259. self.assertEqual(output.status_code, 200)
  260. data = json.loads(output.data)
  261. self.assertDictEqual(
  262. data,
  263. {'message': 'Comment added'}
  264. )
  265. # One comment added
  266. request = pagure.lib.search_pull_requests(
  267. self.session, project_id=1, requestid=1)
  268. self.assertEqual(len(request.comments), 1)
  269. # Close PR
  270. output = self.app.post(
  271. '/api/0/test/pull-request/1/close', headers=headers)
  272. self.assertEqual(output.status_code, 200)
  273. data = json.loads(output.data)
  274. self.assertDictEqual(
  275. data,
  276. {"message": "Pull-request closed!"}
  277. )
  278. # PR closed
  279. request = pagure.lib.search_pull_requests(
  280. self.session, project_id=1, requestid=1)
  281. self.assertEqual(request.status, 'Closed')
  282. # Finally retrieve the user's logs
  283. output = self.app.get('/api/0/user/pingou/activity/stats')
  284. self.assertEqual(output.status_code, 200)
  285. data = json.loads(output.data)
  286. date = datetime.datetime.utcnow().date().strftime('%Y-%m-%d')
  287. # There seems to be a difference in the JSON generated between
  288. # flask-0.10.1 (F23) and 0.11.1 (jenkins)
  289. self.assertTrue(
  290. data == {date: 4}
  291. or
  292. data == [[date, 4]]
  293. )
  294. @patch('pagure.lib.notify.send_email')
  295. def test_api_view_user_activity_date(self, mockemail):
  296. """ Test the api_view_user_activity_date method of the flask user
  297. api. """
  298. self.test_api_view_user_activity_stats()
  299. # Invalid date
  300. output = self.app.get('/api/0/user/pingou/activity/AABB')
  301. self.assertEqual(output.status_code, 400)
  302. # Invalid date
  303. output = self.app.get('/api/0/user/pingou/activity/2016asd')
  304. self.assertEqual(output.status_code, 200)
  305. exp = {
  306. "activities": [],
  307. "date": "2016-01-01"
  308. }
  309. self.assertEqual(json.loads(output.data), exp)
  310. # Date parsed, just not really as expected
  311. output = self.app.get('/api/0/user/pingou/activity/20161245')
  312. self.assertEqual(output.status_code, 200)
  313. exp = {
  314. "activities": [],
  315. "date": "1970-08-22"
  316. }
  317. self.assertEqual(json.loads(output.data), exp)
  318. date = datetime.datetime.utcnow().date().strftime('%Y-%m-%d')
  319. # Retrieve the user's logs for today
  320. output = self.app.get('/api/0/user/pingou/activity/%s' % date)
  321. self.assertEqual(output.status_code, 200)
  322. data = json.loads(output.data)
  323. exp = {
  324. "activities": [
  325. {
  326. "date": date,
  327. "date_created": "1477558752",
  328. "type": "pull-request",
  329. "description_mk": "<p>pingou created PR <a href=\"/test/pull-request/1\" title=\"test pull-request\">test#1</a></p>",
  330. "id": 1,
  331. "ref_id": "1",
  332. "type": "created",
  333. "user": {
  334. "fullname": "PY C",
  335. "name": "pingou"
  336. }
  337. },
  338. {
  339. "date": date,
  340. "date_created": "1477558752",
  341. "type": "pull-request",
  342. "description_mk": "<p>pingou commented on PR <a href=\"/test/pull-request/1\" title=\"test pull-request\">test#1</a></p>",
  343. "id": 2,
  344. "ref_id": "1",
  345. "type": "commented",
  346. "user": {
  347. "fullname": "PY C",
  348. "name": "pingou"
  349. }
  350. },
  351. {
  352. "date": date,
  353. "date_created": "1477558752",
  354. "type": "pull-request",
  355. "description_mk": "<p>pingou closed PR <a href=\"/test/pull-request/1\" title=\"test pull-request\">test#1</a></p>",
  356. "id": 3,
  357. "ref_id": "1",
  358. "type": "closed",
  359. "user": {
  360. "fullname": "PY C",
  361. "name": "pingou"
  362. }
  363. },
  364. {
  365. "date": date,
  366. "date_created": "1477558752",
  367. "type": "pull-request",
  368. "description_mk": "<p>pingou commented on PR <a href=\"/test/pull-request/1\" title=\"test pull-request\">test#1</a></p>",
  369. "id": 4,
  370. "ref_id": "1",
  371. "type": "commented",
  372. "user": {
  373. "fullname": "PY C",
  374. "name": "pingou"
  375. }
  376. }
  377. ],
  378. "date": date,
  379. }
  380. for idx, act in enumerate(data['activities']):
  381. act['date_created'] = '1477558752'
  382. data['activities'][idx] = act
  383. self.assertEqual(data, exp)
  384. @patch('pagure.lib.notify.send_email')
  385. def test_api_view_user_activity_date_1_activity(self, mockemail):
  386. """ Test the api_view_user_activity_date method of the flask user
  387. api when the user only did one action. """
  388. tests.create_projects(self.session)
  389. repo = pagure.lib._get_project(self.session, 'test')
  390. now = datetime.datetime.utcnow()
  391. date = now.date().strftime('%Y-%m-%d')
  392. # Create a single commit log
  393. log = model.PagureLog(
  394. user_id=1,
  395. user_email='foo@bar.com',
  396. project_id=1,
  397. log_type='committed',
  398. ref_id='githash',
  399. date=now.date(),
  400. date_created=now
  401. )
  402. self.session.add(log)
  403. self.session.commit()
  404. # Retrieve the user's logs for today
  405. output = self.app.get(
  406. '/api/0/user/pingou/activity/%s?grouped=1' % date)
  407. self.assertEqual(output.status_code, 200)
  408. data = json.loads(output.data)
  409. exp = {
  410. "activities": [
  411. {
  412. "description_mk": "<p>pingou committed on test#githash</p>"
  413. }
  414. ],
  415. "date": date,
  416. }
  417. self.assertEqual(data, exp)
  418. if __name__ == '__main__':
  419. unittest.main(verbosity=2)