1
0

test_pagure_flask_api_user.py 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import pkg_resources
  10. import datetime
  11. import unittest
  12. import shutil
  13. import sys
  14. import os
  15. import json
  16. from mock import patch
  17. sys.path.insert(0, os.path.join(os.path.dirname(
  18. os.path.abspath(__file__)), '..'))
  19. import pagure.api
  20. import pagure.config
  21. import pagure.lib.model as model
  22. import pagure.lib.query
  23. import tests
  24. class PagureFlaskApiUSertests(tests.Modeltests):
  25. """ Tests for the flask API of pagure for issue """
  26. maxDiff = None
  27. def setUp(self):
  28. """ Set up the environnment, ran before every tests. """
  29. super(PagureFlaskApiUSertests, self).setUp()
  30. pagure.config.config['REQUESTS_FOLDER'] = None
  31. def test_api_users(self):
  32. """ Test the api_users function. """
  33. output = self.app.get('/api/0/users')
  34. self.assertEqual(output.status_code, 200)
  35. data = json.loads(output.get_data(as_text=True))
  36. self.assertEqual(sorted(data['users']), ['foo', 'pingou'])
  37. self.assertEqual(sorted(data.keys()), ['mention', 'total_users', 'users'])
  38. self.assertEqual(data['total_users'], 2)
  39. output = self.app.get('/api/0/users?pattern=p')
  40. self.assertEqual(output.status_code, 200)
  41. data = json.loads(output.get_data(as_text=True))
  42. self.assertEqual(data['users'], ['pingou'])
  43. self.assertEqual(sorted(data.keys()), ['mention', 'total_users', 'users'])
  44. self.assertEqual(data['total_users'], 1)
  45. def test_api_view_user(self):
  46. """
  47. Test the api_view_user method of the flask api
  48. The tested user has no project or forks.
  49. """
  50. output = self.app.get('/api/0/user/pingou')
  51. self.assertEqual(output.status_code, 200)
  52. exp = {
  53. "forks": [],
  54. 'forks_pagination': {
  55. 'first': 'http://localhost...',
  56. 'last': 'http://localhost...',
  57. 'next': None,
  58. 'forkpage': 1,
  59. 'pages': 0,
  60. 'per_page': 20,
  61. 'prev': None},
  62. "repos": [],
  63. 'repos_pagination': {
  64. 'first': 'http://localhost...',
  65. 'last': 'http://localhost...',
  66. 'next': None,
  67. 'repopage': 1,
  68. 'pages': 0,
  69. 'per_page': 20,
  70. 'prev': None},
  71. "user": { "fullname": "PY C", "name": "pingou"}}
  72. data = json.loads(output.get_data(as_text=True))
  73. for k in ['forks_pagination', 'repos_pagination']:
  74. for k2 in ['first', 'last']:
  75. self.assertIsNotNone(data[k][k2])
  76. data[k][k2] = 'http://localhost...'
  77. self.assertEqual(data, exp)
  78. def test_api_view_user_with_project(self):
  79. """
  80. Test the api_view_user method of the flask api,
  81. this time the user has some project defined.
  82. """
  83. tests.create_projects(self.session)
  84. output = self.app.get('/api/0/user/pingou')
  85. self.assertEqual(output.status_code, 200)
  86. data = json.loads(output.get_data(as_text=True))
  87. data['repos'][0]['date_created'] = "1490272832"
  88. data['repos'][0]['date_modified'] = "1490272832"
  89. data['repos'][1]['date_created'] = "1490272832"
  90. data['repos'][1]['date_modified'] = "1490272832"
  91. data['repos'][2]['date_created'] = "1490272832"
  92. data['repos'][2]['date_modified'] = "1490272832"
  93. for k in ['forks_pagination', 'repos_pagination']:
  94. for k2 in ['first', 'last']:
  95. self.assertIsNotNone(data[k][k2])
  96. data[k][k2] = 'http://localhost...'
  97. expected_data = {
  98. "forks": [],
  99. 'forks_pagination': {
  100. 'first': 'http://localhost...',
  101. 'last': 'http://localhost...',
  102. 'next': None,
  103. 'forkpage': 1,
  104. 'pages': 0,
  105. 'per_page': 20,
  106. 'prev': None},
  107. "repos": [
  108. {
  109. "access_groups": {
  110. "admin": [],
  111. "commit": [],
  112. "ticket": []
  113. },
  114. "access_users": {
  115. "admin": [],
  116. "commit": [],
  117. "owner": ["pingou"],
  118. "ticket": []
  119. },
  120. "close_status": [
  121. "Invalid",
  122. "Insufficient data",
  123. "Fixed",
  124. "Duplicate"
  125. ],
  126. "custom_keys": [],
  127. "date_created": "1490272832",
  128. "date_modified": "1490272832",
  129. "description": "test project #1",
  130. "fullname": "test",
  131. "url_path": "test",
  132. "id": 1,
  133. "milestones": {},
  134. "name": "test",
  135. "namespace": None,
  136. "parent": None,
  137. "priorities": {},
  138. "tags": [],
  139. "user": {
  140. "fullname": "PY C",
  141. "name": "pingou"
  142. }
  143. },
  144. {
  145. "access_groups": {
  146. "admin": [],
  147. "commit": [],
  148. "ticket": []
  149. },
  150. "access_users": {
  151. "admin": [],
  152. "commit": [],
  153. "owner": ["pingou"],
  154. "ticket": []
  155. },
  156. "close_status": [
  157. "Invalid",
  158. "Insufficient data",
  159. "Fixed",
  160. "Duplicate"
  161. ],
  162. "custom_keys": [],
  163. "date_created": "1490272832",
  164. "date_modified": "1490272832",
  165. "description": "test project #2",
  166. "fullname": "test2",
  167. "url_path": "test2",
  168. "id": 2,
  169. "milestones": {},
  170. "name": "test2",
  171. "namespace": None,
  172. "parent": None,
  173. "priorities": {},
  174. "tags": [],
  175. "user": {
  176. "fullname": "PY C",
  177. "name": "pingou"
  178. }
  179. },
  180. {
  181. "access_groups": {
  182. "admin": [],
  183. "commit": [],
  184. "ticket": []},
  185. "access_users": {
  186. "admin": [],
  187. "commit": [],
  188. "owner": ["pingou"],
  189. "ticket": []
  190. },
  191. "close_status": [
  192. "Invalid",
  193. "Insufficient data",
  194. "Fixed",
  195. "Duplicate"
  196. ],
  197. "custom_keys": [],
  198. "date_created": "1490272832",
  199. "date_modified": "1490272832",
  200. "description": "namespaced test project",
  201. "fullname": "somenamespace/test3",
  202. "url_path": "somenamespace/test3",
  203. "id": 3,
  204. "milestones": {},
  205. "name": "test3",
  206. "namespace": "somenamespace",
  207. "parent": None,
  208. "priorities": {},
  209. "tags": [],
  210. "user": {
  211. "fullname": "PY C",
  212. "name": "pingou"
  213. }
  214. }
  215. ],
  216. 'repos_pagination': {
  217. 'first': 'http://localhost...',
  218. 'last': 'http://localhost...',
  219. 'next': None,
  220. 'repopage': 1,
  221. 'pages': 1,
  222. 'per_page': 20,
  223. 'prev': None},
  224. "user": {
  225. "fullname": "PY C",
  226. "name": "pingou"
  227. }
  228. }
  229. self.assertEqual(data, expected_data)
  230. @patch('pagure.lib.notify.send_email')
  231. def test_api_view_user_activity_stats(self, mockemail):
  232. """ Test the api_view_user_activity_stats method of the flask user
  233. api. """
  234. mockemail.return_value = True
  235. tests.create_projects(self.session)
  236. tests.create_tokens(self.session)
  237. tests.create_tokens_acl(self.session)
  238. headers = {'Authorization': 'token aaabbbcccddd'}
  239. # Create a pull-request
  240. repo = pagure.lib.query._get_project(self.session, 'test')
  241. forked_repo = pagure.lib.query._get_project(self.session, 'test')
  242. req = pagure.lib.query.new_pull_request(
  243. session=self.session,
  244. repo_from=forked_repo,
  245. branch_from='master',
  246. repo_to=repo,
  247. branch_to='master',
  248. title='test pull-request',
  249. user='pingou',
  250. )
  251. self.session.commit()
  252. self.assertEqual(req.id, 1)
  253. self.assertEqual(req.title, 'test pull-request')
  254. # Check comments before
  255. self.session.commit()
  256. request = pagure.lib.query.search_pull_requests(
  257. self.session, project_id=1, requestid=1)
  258. self.assertEqual(len(request.comments), 0)
  259. data = {
  260. 'comment': 'This is a very interesting question',
  261. }
  262. # Valid request
  263. output = self.app.post(
  264. '/api/0/test/pull-request/1/comment', data=data, headers=headers)
  265. self.assertEqual(output.status_code, 200)
  266. data = json.loads(output.get_data(as_text=True))
  267. self.assertDictEqual(
  268. data,
  269. {'message': 'Comment added'}
  270. )
  271. # One comment added
  272. self.session.commit()
  273. request = pagure.lib.query.search_pull_requests(
  274. self.session, project_id=1, requestid=1)
  275. self.assertEqual(len(request.comments), 1)
  276. # Close PR
  277. output = self.app.post(
  278. '/api/0/test/pull-request/1/close', headers=headers)
  279. self.assertEqual(output.status_code, 200)
  280. data = json.loads(output.get_data(as_text=True))
  281. self.assertDictEqual(
  282. data,
  283. {"message": "Pull-request closed!"}
  284. )
  285. # PR closed
  286. self.session.commit()
  287. request = pagure.lib.query.search_pull_requests(
  288. self.session, project_id=1, requestid=1)
  289. self.assertEqual(request.status, 'Closed')
  290. # Finally retrieve the user's logs
  291. output = self.app.get('/api/0/user/pingou/activity/stats')
  292. self.assertEqual(output.status_code, 200)
  293. data = json.loads(output.get_data(as_text=True))
  294. date = datetime.datetime.utcnow().date().strftime('%Y-%m-%d')
  295. self.assertDictEqual(data, {date: 4})
  296. @patch('pagure.lib.notify.send_email')
  297. def test_api_view_user_activity_date(self, mockemail):
  298. """ Test the api_view_user_activity_date method of the flask user
  299. api. """
  300. self.test_api_view_user_activity_stats()
  301. # Invalid date
  302. output = self.app.get('/api/0/user/pingou/activity/AABB')
  303. self.assertEqual(output.status_code, 400)
  304. # Invalid date
  305. output = self.app.get('/api/0/user/pingou/activity/2016asd')
  306. self.assertEqual(output.status_code, 200)
  307. exp = {
  308. "activities": [],
  309. "date": "2016-01-01"
  310. }
  311. self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
  312. # Date parsed, just not really as expected
  313. output = self.app.get('/api/0/user/pingou/activity/20161245')
  314. self.assertEqual(output.status_code, 200)
  315. exp = {
  316. "activities": [],
  317. "date": "1970-08-22"
  318. }
  319. self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
  320. date = datetime.datetime.utcnow().date().strftime('%Y-%m-%d')
  321. # Retrieve the user's logs for today
  322. output = self.app.get('/api/0/user/pingou/activity/%s' % date)
  323. self.assertEqual(output.status_code, 200)
  324. data = json.loads(output.get_data(as_text=True))
  325. exp = {
  326. "activities": [
  327. {
  328. "date": date,
  329. "date_created": "1477558752",
  330. "type": "pull-request",
  331. "description_mk": "<p>pingou created PR <a href=\"/test/pull-request/1\" title=\"[Closed] test pull-request\">test#1</a></p>",
  332. "id": 1,
  333. "ref_id": "1",
  334. "type": "created",
  335. "user": {
  336. "fullname": "PY C",
  337. "name": "pingou"
  338. }
  339. },
  340. {
  341. "date": date,
  342. "date_created": "1477558752",
  343. "type": "pull-request",
  344. "description_mk": "<p>pingou commented on PR <a href=\"/test/pull-request/1\" title=\"[Closed] test pull-request\">test#1</a></p>",
  345. "id": 2,
  346. "ref_id": "1",
  347. "type": "commented",
  348. "user": {
  349. "fullname": "PY C",
  350. "name": "pingou"
  351. }
  352. },
  353. {
  354. "date": date,
  355. "date_created": "1477558752",
  356. "type": "pull-request",
  357. "description_mk": "<p>pingou closed PR <a href=\"/test/pull-request/1\" title=\"[Closed] test pull-request\">test#1</a></p>",
  358. "id": 3,
  359. "ref_id": "1",
  360. "type": "closed",
  361. "user": {
  362. "fullname": "PY C",
  363. "name": "pingou"
  364. }
  365. },
  366. {
  367. "date": date,
  368. "date_created": "1477558752",
  369. "type": "pull-request",
  370. "description_mk": "<p>pingou commented on PR <a href=\"/test/pull-request/1\" title=\"[Closed] test pull-request\">test#1</a></p>",
  371. "id": 4,
  372. "ref_id": "1",
  373. "type": "commented",
  374. "user": {
  375. "fullname": "PY C",
  376. "name": "pingou"
  377. }
  378. }
  379. ],
  380. "date": date,
  381. }
  382. for idx, act in enumerate(data['activities']):
  383. act['date_created'] = '1477558752'
  384. data['activities'][idx] = act
  385. self.assertEqual(data, exp)
  386. @patch('pagure.lib.notify.send_email')
  387. def test_api_view_user_activity_date_1_activity(self, mockemail):
  388. """ Test the api_view_user_activity_date method of the flask user
  389. api when the user only did one action. """
  390. tests.create_projects(self.session)
  391. repo = pagure.lib.query._get_project(self.session, 'test')
  392. now = datetime.datetime.utcnow()
  393. date = now.date().strftime('%Y-%m-%d')
  394. # Create a single commit log
  395. log = model.PagureLog(
  396. user_id=1,
  397. user_email='foo@bar.com',
  398. project_id=1,
  399. log_type='committed',
  400. ref_id='githash',
  401. date=now.date(),
  402. date_created=now
  403. )
  404. self.session.add(log)
  405. self.session.commit()
  406. # Retrieve the user's logs for today
  407. output = self.app.get(
  408. '/api/0/user/pingou/activity/%s?grouped=1' % date)
  409. self.assertEqual(output.status_code, 200)
  410. data = json.loads(output.get_data(as_text=True))
  411. exp = {
  412. "activities": [
  413. {
  414. "description_mk": "<p>pingou committed on test#githash</p>"
  415. }
  416. ],
  417. "date": date,
  418. }
  419. self.assertEqual(data, exp)
  420. @patch('pagure.lib.notify.send_email')
  421. def test_api_view_user_activity_timezone_negative(self, mockemail):
  422. """Test api_view_user_activity{_stats,_date} with the America/
  423. New York timezone, which is 5 hours behind UTC in winter and
  424. 4 hours behind UTC in summer (daylight savings). The events
  425. will occur on 2018-02-15 in UTC, but on 2018-02-14 local.
  426. """
  427. tests.create_projects(self.session)
  428. repo = pagure.lib.query._get_project(self.session, 'test')
  429. dateobj = datetime.datetime(2018, 2, 15, 3, 30)
  430. utcdate = '2018-02-15'
  431. # the Unix timestamp for 2018-02-15 12:00 UTC
  432. utcts = '1518696000'
  433. localdate = '2018-02-14'
  434. # the Unix timestamp for 2018-02-14 12:00 America/New_York
  435. localts = '1518627600'
  436. # Create a single commit log
  437. log = model.PagureLog(
  438. user_id=1,
  439. user_email='foo@bar.com',
  440. project_id=1,
  441. log_type='committed',
  442. ref_id='githash',
  443. date=dateobj.date(),
  444. date_created=dateobj
  445. )
  446. self.session.add(log)
  447. self.session.commit()
  448. # Retrieve the user's stats with no timezone specified (==UTC)
  449. output = self.app.get('/api/0/user/pingou/activity/stats')
  450. self.assertEqual(output.status_code, 200)
  451. data = json.loads(output.get_data(as_text=True))
  452. # date in output should be UTC date
  453. self.assertDictEqual(data, {utcdate: 1})
  454. # Now in timestamp format...
  455. output = self.app.get('/api/0/user/pingou/activity/stats?format=timestamp')
  456. self.assertEqual(output.status_code, 200)
  457. data = json.loads(output.get_data(as_text=True))
  458. # timestamp in output should be UTC ts
  459. self.assertDictEqual(data, {utcts: 1})
  460. # Retrieve the user's stats with local timezone specified
  461. output = self.app.get('/api/0/user/pingou/activity/stats?tz=America/New_York')
  462. self.assertEqual(output.status_code, 200)
  463. data = json.loads(output.get_data(as_text=True))
  464. # date in output should be local date
  465. self.assertDictEqual(data, {localdate: 1})
  466. # Now in timestamp format...
  467. output = self.app.get('/api/0/user/pingou/activity/stats?format=timestamp&tz=America/New_York')
  468. self.assertEqual(output.status_code, 200)
  469. data = json.loads(output.get_data(as_text=True))
  470. # timestamp in output should be local ts
  471. self.assertDictEqual(data, {localts: 1})
  472. # Retrieve the user's logs for 2018-02-15 with no timezone
  473. output = self.app.get(
  474. '/api/0/user/pingou/activity/%s?grouped=1' % utcdate)
  475. self.assertEqual(output.status_code, 200)
  476. data = json.loads(output.get_data(as_text=True))
  477. exp = {
  478. "activities": [
  479. {
  480. "description_mk": "<p>pingou committed on test#githash</p>"
  481. }
  482. ],
  483. "date": utcdate,
  484. }
  485. self.assertEqual(data, exp)
  486. # Now retrieve the user's logs for 2018-02-14 with local time
  487. output = self.app.get(
  488. '/api/0/user/pingou/activity/%s?grouped=1&tz=America/New_York' % localdate)
  489. self.assertEqual(output.status_code, 200)
  490. data = json.loads(output.get_data(as_text=True))
  491. exp['date'] = localdate
  492. self.assertEqual(data, exp)
  493. @patch('pagure.lib.notify.send_email')
  494. def test_api_view_user_activity_timezone_positive(self, mockemail):
  495. """Test api_view_user_activity{_stats,_date} with the Asia/
  496. Dubai timezone, which is 4 hours ahead of UTC. The events will
  497. occur on 2018-02-15 in UTC, but on 2018-02-16 in local time.
  498. """
  499. tests.create_projects(self.session)
  500. repo = pagure.lib.query._get_project(self.session, 'test')
  501. dateobj = datetime.datetime(2018, 2, 15, 22, 30)
  502. utcdate = '2018-02-15'
  503. # the Unix timestamp for 2018-02-15 12:00 UTC
  504. utcts = '1518696000'
  505. localdate = '2018-02-16'
  506. # the Unix timestamp for 2018-02-16 12:00 Asia/Dubai
  507. localts = '1518768000'
  508. # Create a single commit log
  509. log = model.PagureLog(
  510. user_id=1,
  511. user_email='foo@bar.com',
  512. project_id=1,
  513. log_type='committed',
  514. ref_id='githash',
  515. date=dateobj.date(),
  516. date_created=dateobj
  517. )
  518. self.session.add(log)
  519. self.session.commit()
  520. # Retrieve the user's stats with no timezone specified (==UTC)
  521. output = self.app.get('/api/0/user/pingou/activity/stats')
  522. self.assertEqual(output.status_code, 200)
  523. data = json.loads(output.get_data(as_text=True))
  524. # date in output should be UTC date
  525. self.assertDictEqual(data, {utcdate: 1})
  526. # Now in timestamp format...
  527. output = self.app.get('/api/0/user/pingou/activity/stats?format=timestamp')
  528. self.assertEqual(output.status_code, 200)
  529. data = json.loads(output.get_data(as_text=True))
  530. # timestamp in output should be UTC ts
  531. self.assertDictEqual(data, {utcts: 1})
  532. # Retrieve the user's stats with local timezone specified
  533. output = self.app.get('/api/0/user/pingou/activity/stats?tz=Asia/Dubai')
  534. self.assertEqual(output.status_code, 200)
  535. data = json.loads(output.get_data(as_text=True))
  536. # date in output should be local date
  537. self.assertDictEqual(data, {localdate: 1})
  538. # Now in timestamp format...
  539. output = self.app.get('/api/0/user/pingou/activity/stats?format=timestamp&tz=Asia/Dubai')
  540. self.assertEqual(output.status_code, 200)
  541. data = json.loads(output.get_data(as_text=True))
  542. # timestamp in output should be local ts
  543. self.assertDictEqual(data, {localts: 1})
  544. # Retrieve the user's logs for 2018-02-15 with no timezone
  545. output = self.app.get(
  546. '/api/0/user/pingou/activity/%s?grouped=1' % utcdate)
  547. self.assertEqual(output.status_code, 200)
  548. data = json.loads(output.get_data(as_text=True))
  549. exp = {
  550. "activities": [
  551. {
  552. "description_mk": "<p>pingou committed on test#githash</p>"
  553. }
  554. ],
  555. "date": utcdate,
  556. }
  557. self.assertEqual(data, exp)
  558. # Now retrieve the user's logs for 2018-02-16 with local time
  559. output = self.app.get(
  560. '/api/0/user/pingou/activity/%s?grouped=1&tz=Asia/Dubai' % localdate)
  561. self.assertEqual(output.status_code, 200)
  562. data = json.loads(output.get_data(as_text=True))
  563. exp['date'] = localdate
  564. self.assertEqual(data, exp)
  565. class PagureFlaskApiUsertestrequests(tests.Modeltests):
  566. """ Tests for the user requests endpoints """
  567. maxDiff = None
  568. def setUp(self):
  569. """ Set up the environnment, ran before every tests. """
  570. super(PagureFlaskApiUsertestrequests, self).setUp()
  571. pagure.config.config['REQUESTS_FOLDER'] = None
  572. tests.create_projects(self.session)
  573. # Create few pull-requests
  574. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  575. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  576. pagure.lib.query.new_pull_request(
  577. session=self.session,
  578. repo_from=forked_repo,
  579. branch_from='master',
  580. repo_to=repo,
  581. branch_to='master',
  582. title='open pullrequest by user foo on repo test',
  583. user='foo',
  584. )
  585. repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  586. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  587. pagure.lib.query.new_pull_request(
  588. session=self.session,
  589. repo_from=forked_repo,
  590. branch_from='master',
  591. repo_to=repo,
  592. branch_to='master',
  593. title='open pullrequest by user foo on repo test2',
  594. user='foo',
  595. )
  596. self.session.commit()
  597. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  598. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  599. pagure.lib.query.new_pull_request(
  600. session=self.session,
  601. repo_from=forked_repo,
  602. branch_from='master',
  603. repo_to=repo,
  604. branch_to='master',
  605. title='closed pullrequest by user foo on repo test',
  606. user='foo',
  607. status='Closed',
  608. )
  609. repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  610. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  611. pagure.lib.query.new_pull_request(
  612. session=self.session,
  613. repo_from=forked_repo,
  614. branch_from='master',
  615. repo_to=repo,
  616. branch_to='master',
  617. title='closed pullrequest by user foo on repo test2',
  618. user='foo',
  619. status='Closed',
  620. )
  621. self.session.commit()
  622. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  623. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  624. pagure.lib.query.new_pull_request(
  625. session=self.session,
  626. repo_from=forked_repo,
  627. branch_from='master',
  628. repo_to=repo,
  629. branch_to='master',
  630. title='merged pullrequest by user foo on repo test',
  631. user='foo',
  632. status='Merged',
  633. )
  634. repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  635. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  636. pagure.lib.query.new_pull_request(
  637. session=self.session,
  638. repo_from=forked_repo,
  639. branch_from='master',
  640. repo_to=repo,
  641. branch_to='master',
  642. title='merged pullrequest by user foo on repo test2',
  643. user='foo',
  644. status='Merged',
  645. )
  646. self.session.commit()
  647. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  648. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  649. pagure.lib.query.new_pull_request(
  650. session=self.session,
  651. repo_from=forked_repo,
  652. branch_from='master',
  653. repo_to=repo,
  654. branch_to='master',
  655. title='open pullrequest by user pingou on repo test',
  656. user='pingou',
  657. )
  658. self.session.commit()
  659. repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  660. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  661. pagure.lib.query.new_pull_request(
  662. session=self.session,
  663. repo_from=forked_repo,
  664. branch_from='master',
  665. repo_to=repo,
  666. branch_to='master',
  667. title='open pullrequest by user pingou on repo test2',
  668. user='pingou',
  669. )
  670. self.session.commit()
  671. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  672. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  673. pagure.lib.query.new_pull_request(
  674. session=self.session,
  675. repo_from=forked_repo,
  676. branch_from='master',
  677. repo_to=repo,
  678. branch_to='master',
  679. title='closed pullrequest by user pingou on repo test',
  680. user='pingou',
  681. status="Closed",
  682. )
  683. self.session.commit()
  684. repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  685. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  686. pagure.lib.query.new_pull_request(
  687. session=self.session,
  688. repo_from=forked_repo,
  689. branch_from='master',
  690. repo_to=repo,
  691. branch_to='master',
  692. title='closed pullrequest by user pingou on repo test2',
  693. user='pingou',
  694. status="Closed",
  695. )
  696. self.session.commit()
  697. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  698. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  699. pagure.lib.query.new_pull_request(
  700. session=self.session,
  701. repo_from=forked_repo,
  702. branch_from='master',
  703. repo_to=repo,
  704. branch_to='master',
  705. title='merged pullrequest by user pingou on repo test',
  706. user='pingou',
  707. status="Merged",
  708. )
  709. self.session.commit()
  710. repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  711. forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
  712. pagure.lib.query.new_pull_request(
  713. session=self.session,
  714. repo_from=forked_repo,
  715. branch_from='master',
  716. repo_to=repo,
  717. branch_to='master',
  718. title='merged pullrequest by user pingou on repo test2',
  719. user='pingou',
  720. status="Merged",
  721. )
  722. self.session.commit()
  723. @patch('pagure.lib.notify.send_email')
  724. def test_api_view_user_requests_filed(self, mockemail):
  725. """ Test the api_view_user_requests_filed method of the flask user
  726. api """
  727. # First we test without the status parameter. It should default to `open`
  728. output = self.app.get(
  729. '/api/0/user/pingou/requests/filed')
  730. self.assertEqual(output.status_code, 200)
  731. data = json.loads(output.get_data(as_text=True))
  732. self.assertEqual(len(data['requests']), 2)
  733. self.assertEqual(
  734. sorted(data.keys()),
  735. [u'args', u'pagination', u'requests', u'total_requests'])
  736. self.assertEqual(data['requests'][0]['user']['name'], "pingou")
  737. self.assertEqual(data['requests'][1]['user']['name'], "pingou")
  738. self.assertEqual(data['requests'][0]['status'], "Open")
  739. self.assertEqual(data['requests'][1]['status'], "Open")
  740. self.assertEqual(data['requests'][0]['title'], "open pullrequest by user pingou on repo test2")
  741. self.assertEqual(data['requests'][1]['title'], "open pullrequest by user pingou on repo test")
  742. self.assertEqual(data['args']['status'], "open")
  743. self.assertEqual(data['args']['page'], 1)
  744. # Next test with the status parameter set to `open`.
  745. output = self.app.get(
  746. '/api/0/user/pingou/requests/filed?status=open')
  747. self.assertEqual(output.status_code, 200)
  748. data = json.loads(output.get_data(as_text=True))
  749. self.assertEqual(len(data['requests']), 2)
  750. self.assertEqual(
  751. sorted(data.keys()),
  752. [u'args', u'pagination', u'requests', u'total_requests'])
  753. self.assertEqual(data['requests'][0]['user']['name'], "pingou")
  754. self.assertEqual(data['requests'][1]['user']['name'], "pingou")
  755. self.assertEqual(data['requests'][0]['status'], "Open")
  756. self.assertEqual(data['requests'][1]['status'], "Open")
  757. self.assertEqual(data['requests'][0]['title'], "open pullrequest by user pingou on repo test2")
  758. self.assertEqual(data['requests'][1]['title'], "open pullrequest by user pingou on repo test")
  759. self.assertEqual(data['args']['status'], "open")
  760. self.assertEqual(data['args']['page'], 1)
  761. # Next test with the status parameter set to `closed`.
  762. output = self.app.get(
  763. '/api/0/user/pingou/requests/filed?status=closed')
  764. self.assertEqual(output.status_code, 200)
  765. data = json.loads(output.get_data(as_text=True))
  766. self.assertEqual(len(data['requests']), 2)
  767. self.assertEqual(
  768. sorted(data.keys()),
  769. [u'args', u'pagination', u'requests', u'total_requests'])
  770. self.assertEqual(data['requests'][0]['user']['name'], "pingou")
  771. self.assertEqual(data['requests'][1]['user']['name'], "pingou")
  772. self.assertEqual(data['requests'][0]['status'], "Closed")
  773. self.assertEqual(data['requests'][1]['status'], "Closed")
  774. self.assertEqual(data['requests'][0]['title'], "closed pullrequest by user pingou on repo test2")
  775. self.assertEqual(data['requests'][1]['title'], "closed pullrequest by user pingou on repo test")
  776. self.assertEqual(data['args']['status'], "closed")
  777. self.assertEqual(data['args']['page'], 1)
  778. # Next test with the status parameter set to `merged`.
  779. output = self.app.get(
  780. '/api/0/user/pingou/requests/filed?status=merged')
  781. self.assertEqual(output.status_code, 200)
  782. data = json.loads(output.get_data(as_text=True))
  783. self.assertEqual(len(data['requests']), 2)
  784. self.assertEqual(
  785. sorted(data.keys()),
  786. [u'args', u'pagination', u'requests', u'total_requests'])
  787. self.assertEqual(data['requests'][0]['user']['name'], "pingou")
  788. self.assertEqual(data['requests'][1]['user']['name'], "pingou")
  789. self.assertEqual(data['requests'][0]['status'], "Merged")
  790. self.assertEqual(data['requests'][1]['status'], "Merged")
  791. self.assertEqual(data['requests'][0]['title'], "merged pullrequest by user pingou on repo test2")
  792. self.assertEqual(data['requests'][1]['title'], "merged pullrequest by user pingou on repo test")
  793. self.assertEqual(data['args']['status'], "merged")
  794. self.assertEqual(data['args']['page'], 1)
  795. # Finally, test with the status parameter set to `all`.
  796. output = self.app.get(
  797. '/api/0/user/pingou/requests/filed?status=all')
  798. self.assertEqual(output.status_code, 200)
  799. data = json.loads(output.get_data(as_text=True))
  800. self.assertEqual(len(data['requests']), 6)
  801. self.assertEqual(
  802. sorted(data.keys()),
  803. [u'args', u'pagination', u'requests', u'total_requests'])
  804. self.assertEqual(data['requests'][0]['user']['name'], "pingou")
  805. self.assertEqual(data['requests'][1]['user']['name'], "pingou")
  806. self.assertEqual(data['requests'][2]['user']['name'], "pingou")
  807. self.assertEqual(data['requests'][3]['user']['name'], "pingou")
  808. self.assertEqual(data['requests'][4]['user']['name'], "pingou")
  809. self.assertEqual(data['requests'][5]['user']['name'], "pingou")
  810. self.assertEqual(data['requests'][0]['status'], "Merged")
  811. self.assertEqual(data['requests'][1]['status'], "Merged")
  812. self.assertEqual(data['requests'][2]['status'], "Closed")
  813. self.assertEqual(data['requests'][3]['status'], "Closed")
  814. self.assertEqual(data['requests'][4]['status'], "Open")
  815. self.assertEqual(data['requests'][5]['status'], "Open")
  816. self.assertEqual(data['requests'][0]['title'], "merged pullrequest by user pingou on repo test2")
  817. self.assertEqual(data['requests'][1]['title'], "merged pullrequest by user pingou on repo test")
  818. self.assertEqual(data['requests'][2]['title'], "closed pullrequest by user pingou on repo test2")
  819. self.assertEqual(data['requests'][3]['title'], "closed pullrequest by user pingou on repo test")
  820. self.assertEqual(data['requests'][4]['title'], "open pullrequest by user pingou on repo test2")
  821. self.assertEqual(data['requests'][5]['title'], "open pullrequest by user pingou on repo test")
  822. self.assertEqual(data['args']['status'], "all")
  823. self.assertEqual(data['args']['page'], 1)
  824. # Test page 2 with the status parameter set to `all`.
  825. output = self.app.get(
  826. '/api/0/user/pingou/requests/filed?status=all&page=2')
  827. self.assertEqual(output.status_code, 200)
  828. data = json.loads(output.get_data(as_text=True))
  829. self.assertEqual(len(data['requests']), 0)
  830. self.assertEqual(
  831. sorted(data.keys()),
  832. [u'args', u'pagination', u'requests', u'total_requests'])
  833. self.assertEqual(data['args']['page'], 2)
  834. @patch('pagure.lib.notify.send_email')
  835. def test_api_view_user_requests_filed_foo(self, mockemail):
  836. """ Test the api_view_user_requests_filed method of the flask user
  837. api """
  838. # Default data returned
  839. output = self.app.get(
  840. '/api/0/user/foo/requests/filed?status=all&per_page=6')
  841. self.assertEqual(output.status_code, 200)
  842. data = json.loads(output.get_data(as_text=True))
  843. self.assertEqual(len(data['requests']), 6)
  844. self.assertEqual(
  845. sorted(data.keys()),
  846. [u'args', u'pagination', u'requests', u'total_requests'])
  847. # There are 6 PRs, that's 1 page at 6 results per page
  848. self.assertEqual(data['pagination']['pages'], 1)
  849. @patch('pagure.lib.notify.send_email')
  850. def test_api_view_user_requests_filed_foo_grp_access(self, mockemail):
  851. """ Test when the user has accessed to some PRs via a group. """
  852. # Add the user to a group
  853. msg = pagure.lib.query.add_group(
  854. self.session,
  855. group_name='some_group',
  856. display_name='Some Group',
  857. description=None,
  858. group_type='bar',
  859. user='pingou',
  860. is_admin=False,
  861. blacklist=[],
  862. )
  863. self.session.commit()
  864. # Add the group to the project `test2`
  865. project = pagure.lib.query._get_project(self.session, 'test2')
  866. msg = pagure.lib.query.add_group_to_project(
  867. session=self.session,
  868. project=project,
  869. new_group='some_group',
  870. user='pingou',
  871. )
  872. self.session.commit()
  873. self.assertEqual(msg, 'Group added')
  874. # Add foo to the group
  875. group = pagure.lib.query.search_groups(
  876. self.session, group_name='some_group')
  877. result = pagure.lib.query.add_user_to_group(
  878. self.session, 'foo', group, 'pingou', True)
  879. self.session.commit()
  880. self.assertEqual(
  881. result, 'User `foo` added to the group `some_group`.')
  882. # Query the API for foo's filed PRs
  883. output = self.app.get(
  884. '/api/0/user/foo/requests/filed?status=all&per_page=6')
  885. self.assertEqual(output.status_code, 200)
  886. data = json.loads(output.get_data(as_text=True))
  887. self.assertEqual(len(data['requests']), 6)
  888. self.assertEqual(
  889. sorted(data.keys()),
  890. [u'args', u'pagination', u'requests', u'total_requests'])
  891. # There are 6 PRs, that's 1 page at 6 results per page
  892. self.assertEqual(data['pagination']['pages'], 1)
  893. @patch('pagure.lib.notify.send_email')
  894. def test_api_view_user_requests_actionable(self, mockemail):
  895. """ Test the api_view_user_requests_actionable method of the flask user
  896. api """
  897. # First we test without the status parameter. It should default to `open`
  898. output = self.app.get(
  899. '/api/0/user/pingou/requests/actionable')
  900. self.assertEqual(output.status_code, 200)
  901. data = json.loads(output.get_data(as_text=True))
  902. self.assertEqual(len(data['requests']), 2)
  903. self.assertEqual(
  904. sorted(data.keys()),
  905. [u'args', u'pagination', u'requests', u'total_requests'])
  906. self.assertEqual(data['requests'][0]['user']['name'], "foo")
  907. self.assertEqual(data['requests'][1]['user']['name'], "foo")
  908. self.assertEqual(data['requests'][0]['status'], "Open")
  909. self.assertEqual(data['requests'][1]['status'], "Open")
  910. self.assertEqual(data['requests'][0]['title'], "open pullrequest by user foo on repo test2")
  911. self.assertEqual(data['requests'][1]['title'], "open pullrequest by user foo on repo test")
  912. self.assertEqual(data['args']['status'], "open")
  913. self.assertEqual(data['args']['page'], 1)
  914. # Next test with the status parameter set to `open`.
  915. output = self.app.get(
  916. '/api/0/user/pingou/requests/actionable?status=open')
  917. self.assertEqual(output.status_code, 200)
  918. data = json.loads(output.get_data(as_text=True))
  919. self.assertEqual(len(data['requests']), 2)
  920. self.assertEqual(
  921. sorted(data.keys()),
  922. [u'args', u'pagination', u'requests', u'total_requests'])
  923. self.assertEqual(data['requests'][0]['user']['name'], "foo")
  924. self.assertEqual(data['requests'][1]['user']['name'], "foo")
  925. self.assertEqual(data['requests'][0]['status'], "Open")
  926. self.assertEqual(data['requests'][1]['status'], "Open")
  927. self.assertEqual(data['requests'][0]['title'], "open pullrequest by user foo on repo test2")
  928. self.assertEqual(data['requests'][1]['title'], "open pullrequest by user foo on repo test")
  929. self.assertEqual(data['args']['status'], "open")
  930. self.assertEqual(data['args']['page'], 1)
  931. # Next test with the status parameter set to `closed`.
  932. output = self.app.get(
  933. '/api/0/user/pingou/requests/actionable?status=closed')
  934. self.assertEqual(output.status_code, 200)
  935. data = json.loads(output.get_data(as_text=True))
  936. self.assertEqual(len(data['requests']), 2)
  937. self.assertEqual(
  938. sorted(data.keys()),
  939. [u'args', u'pagination', u'requests', u'total_requests'])
  940. self.assertEqual(data['requests'][0]['user']['name'], "foo")
  941. self.assertEqual(data['requests'][1]['user']['name'], "foo")
  942. self.assertEqual(data['requests'][0]['status'], "Closed")
  943. self.assertEqual(data['requests'][1]['status'], "Closed")
  944. self.assertEqual(data['requests'][0]['title'], "closed pullrequest by user foo on repo test2")
  945. self.assertEqual(data['requests'][1]['title'], "closed pullrequest by user foo on repo test")
  946. self.assertEqual(data['args']['status'], "closed")
  947. self.assertEqual(data['args']['page'], 1)
  948. # Next test with the status parameter set to `merged`.
  949. output = self.app.get(
  950. '/api/0/user/pingou/requests/actionable?status=merged')
  951. self.assertEqual(output.status_code, 200)
  952. data = json.loads(output.get_data(as_text=True))
  953. self.assertEqual(len(data['requests']), 2)
  954. self.assertEqual(
  955. sorted(data.keys()),
  956. [u'args', u'pagination', u'requests', u'total_requests'])
  957. self.assertEqual(data['requests'][0]['user']['name'], "foo")
  958. self.assertEqual(data['requests'][1]['user']['name'], "foo")
  959. self.assertEqual(data['requests'][0]['status'], "Merged")
  960. self.assertEqual(data['requests'][1]['status'], "Merged")
  961. self.assertEqual(data['requests'][0]['title'], "merged pullrequest by user foo on repo test2")
  962. self.assertEqual(data['requests'][1]['title'], "merged pullrequest by user foo on repo test")
  963. self.assertEqual(data['args']['status'], "merged")
  964. self.assertEqual(data['args']['page'], 1)
  965. # Finally, test with the status parameter set to `all`.
  966. output = self.app.get(
  967. '/api/0/user/pingou/requests/actionable?status=all')
  968. self.assertEqual(output.status_code, 200)
  969. data = json.loads(output.get_data(as_text=True))
  970. self.assertEqual(len(data['requests']), 6)
  971. self.assertEqual(
  972. sorted(data.keys()),
  973. [u'args', u'pagination', u'requests', u'total_requests'])
  974. self.assertEqual(data['requests'][0]['user']['name'], "foo")
  975. self.assertEqual(data['requests'][1]['user']['name'], "foo")
  976. self.assertEqual(data['requests'][2]['user']['name'], "foo")
  977. self.assertEqual(data['requests'][3]['user']['name'], "foo")
  978. self.assertEqual(data['requests'][4]['user']['name'], "foo")
  979. self.assertEqual(data['requests'][5]['user']['name'], "foo")
  980. self.assertEqual(data['requests'][0]['status'], "Merged")
  981. self.assertEqual(data['requests'][1]['status'], "Merged")
  982. self.assertEqual(data['requests'][2]['status'], "Closed")
  983. self.assertEqual(data['requests'][3]['status'], "Closed")
  984. self.assertEqual(data['requests'][4]['status'], "Open")
  985. self.assertEqual(data['requests'][5]['status'], "Open")
  986. self.assertEqual(data['requests'][0]['title'], "merged pullrequest by user foo on repo test2")
  987. self.assertEqual(data['requests'][1]['title'], "merged pullrequest by user foo on repo test")
  988. self.assertEqual(data['requests'][2]['title'], "closed pullrequest by user foo on repo test2")
  989. self.assertEqual(data['requests'][3]['title'], "closed pullrequest by user foo on repo test")
  990. self.assertEqual(data['requests'][4]['title'], "open pullrequest by user foo on repo test2")
  991. self.assertEqual(data['requests'][5]['title'], "open pullrequest by user foo on repo test")
  992. self.assertEqual(data['args']['status'], "all")
  993. self.assertEqual(data['args']['page'], 1)
  994. # Test page 2 with the status parameter set to `all`.
  995. output = self.app.get(
  996. '/api/0/user/pingou/requests/actionable?status=all&page=2')
  997. self.assertEqual(output.status_code, 200)
  998. data = json.loads(output.get_data(as_text=True))
  999. self.assertEqual(len(data['requests']), 0)
  1000. self.assertEqual(
  1001. sorted(data.keys()),
  1002. [u'args', u'pagination', u'requests', u'total_requests'])
  1003. self.assertEqual(data['args']['page'], 2)
  1004. class PagureFlaskApiUsertestissues(tests.Modeltests):
  1005. """ Tests for the user issues endpoints """
  1006. maxDiff = None
  1007. def setUp(self):
  1008. """ Set up the environnment, ran before every tests. """
  1009. super(PagureFlaskApiUsertestissues, self).setUp()
  1010. pagure.config.config['REQUESTS_FOLDER'] = None
  1011. tests.create_projects(self.session)
  1012. repo = pagure.lib.query._get_project(self.session, 'test')
  1013. # Create issues to play with
  1014. msg = pagure.lib.query.new_issue(
  1015. session=self.session,
  1016. repo=repo,
  1017. title='Test issue',
  1018. content='We should work on this',
  1019. user='pingou',
  1020. )
  1021. self.session.commit()
  1022. self.assertEqual(msg.title, 'Test issue')
  1023. def test_user_issues_empty(self):
  1024. """ Return the list of issues associated with the specified user. """
  1025. output = self.app.get('/api/0/user/foo/issues')
  1026. self.assertEqual(output.status_code, 200)
  1027. data = json.loads(output.get_data(as_text=True))
  1028. for k in ['pagination_issues_assigned', 'pagination_issues_created']:
  1029. for k2 in ['first', 'last']:
  1030. self.assertIsNotNone(data[k][k2])
  1031. data[k][k2] = None
  1032. self.assertEqual(
  1033. data,
  1034. {
  1035. "args": {
  1036. "assignee": True,
  1037. "author": True,
  1038. "milestones": [],
  1039. "no_stones": None,
  1040. "order": None,
  1041. "order_key": None,
  1042. "page": 1,
  1043. "since": None,
  1044. "status": None,
  1045. "tags": []
  1046. },
  1047. "issues_assigned": [],
  1048. "issues_created": [],
  1049. 'pagination_issues_assigned': {
  1050. 'first': None,
  1051. 'last': None,
  1052. 'next': None,
  1053. 'page': 1,
  1054. 'pages': 0,
  1055. 'per_page': 20,
  1056. 'prev': None},
  1057. 'pagination_issues_created': {
  1058. 'first': None,
  1059. 'last': None,
  1060. 'next': None,
  1061. 'page': 1,
  1062. 'pages': 0,
  1063. 'per_page': 20,
  1064. 'prev': None},
  1065. "total_issues_assigned": 0,
  1066. "total_issues_assigned_pages": 1,
  1067. "total_issues_created": 0,
  1068. "total_issues_created_pages": 1
  1069. }
  1070. )
  1071. def test_user_issues(self):
  1072. """ Return the list of issues associated with the specified user. """
  1073. output = self.app.get('/api/0/user/pingou/issues')
  1074. self.assertEqual(output.status_code, 200)
  1075. data = json.loads(output.get_data(as_text=True))
  1076. issues = []
  1077. for issue in data['issues_created']:
  1078. issue['date_created'] = '1513111778'
  1079. issue['last_updated'] = '1513111778'
  1080. issue['project']['date_created'] = '1513111778'
  1081. issue['project']['date_modified'] = '1513111778'
  1082. issues.append(issue)
  1083. data['issues_created'] = issues
  1084. for k in ['pagination_issues_assigned', 'pagination_issues_created']:
  1085. for k2 in ['first', 'last']:
  1086. self.assertIsNotNone(data[k][k2])
  1087. data[k][k2] = None
  1088. self.assertEqual(
  1089. data,
  1090. {
  1091. "args": {
  1092. "assignee": True,
  1093. "author": True,
  1094. "milestones": [],
  1095. "no_stones": None,
  1096. "order": None,
  1097. "order_key": None,
  1098. "page": 1,
  1099. "since": None,
  1100. "status": None,
  1101. "tags": []
  1102. },
  1103. "issues_assigned": [],
  1104. "issues_created": [
  1105. {
  1106. "assignee": None,
  1107. "blocks": [],
  1108. "close_status": None,
  1109. "closed_at": None,
  1110. "closed_by": None,
  1111. "comments": [],
  1112. "content": "We should work on this",
  1113. "custom_fields": [],
  1114. "date_created": "1513111778",
  1115. "depends": [],
  1116. "id": 1,
  1117. "last_updated": "1513111778",
  1118. "milestone": None,
  1119. "priority": None,
  1120. "private": False,
  1121. "project": {
  1122. "access_groups": {
  1123. "admin": [],
  1124. "commit": [],
  1125. "ticket": []
  1126. },
  1127. "access_users": {
  1128. "admin": [],
  1129. "commit": [],
  1130. "owner": [
  1131. "pingou"
  1132. ],
  1133. "ticket": []
  1134. },
  1135. "close_status": [
  1136. "Invalid",
  1137. "Insufficient data",
  1138. "Fixed",
  1139. "Duplicate"
  1140. ],
  1141. "custom_keys": [],
  1142. "date_created": "1513111778",
  1143. "date_modified": "1513111778",
  1144. "description": "test project #1",
  1145. "fullname": "test",
  1146. "id": 1,
  1147. "milestones": {},
  1148. "name": "test",
  1149. "namespace": None,
  1150. "parent": None,
  1151. "priorities": {},
  1152. "tags": [],
  1153. "url_path": "test",
  1154. "user": {
  1155. "fullname": "PY C",
  1156. "name": "pingou"
  1157. }
  1158. },
  1159. "status": "Open",
  1160. "tags": [],
  1161. "title": "Test issue",
  1162. "user": {
  1163. "fullname": "PY C",
  1164. "name": "pingou"
  1165. }
  1166. }
  1167. ],
  1168. 'pagination_issues_assigned': {
  1169. 'first': None,
  1170. 'last': None,
  1171. 'next': None,
  1172. 'page': 1,
  1173. 'pages': 0,
  1174. 'per_page': 20,
  1175. 'prev': None},
  1176. 'pagination_issues_created': {
  1177. 'first': None,
  1178. 'last': None,
  1179. 'next': None,
  1180. 'page': 1,
  1181. 'pages': 1,
  1182. 'per_page': 20,
  1183. 'prev': None},
  1184. "total_issues_assigned": 0,
  1185. "total_issues_assigned_pages": 1,
  1186. "total_issues_created": 1,
  1187. "total_issues_created_pages": 1
  1188. }
  1189. )
  1190. if __name__ == '__main__':
  1191. unittest.main(verbosity=2)