test_pagure_flask_api_user.py 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  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, absolute_import
  8. import datetime
  9. import os
  10. import pytz
  11. import shutil
  12. import sys
  13. import unittest
  14. import json
  15. from mock import patch
  16. sys.path.insert(
  17. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  18. )
  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(
  38. sorted(data.keys()), ["mention", "total_users", "users"]
  39. )
  40. self.assertEqual(data["total_users"], 2)
  41. output = self.app.get("/api/0/users?pattern=p")
  42. self.assertEqual(output.status_code, 200)
  43. data = json.loads(output.get_data(as_text=True))
  44. self.assertEqual(data["users"], ["pingou"])
  45. self.assertEqual(
  46. sorted(data.keys()), ["mention", "total_users", "users"]
  47. )
  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. "forks_pagination": {
  59. "first": "http://localhost...",
  60. "last": "http://localhost...",
  61. "next": None,
  62. "forkpage": 1,
  63. "pages": 0,
  64. "per_page": 20,
  65. "prev": None,
  66. },
  67. "repos": [],
  68. "repos_pagination": {
  69. "first": "http://localhost...",
  70. "last": "http://localhost...",
  71. "next": None,
  72. "repopage": 1,
  73. "pages": 0,
  74. "per_page": 20,
  75. "prev": None,
  76. },
  77. "user": {
  78. "fullname": "PY C",
  79. "name": "pingou",
  80. "url_path": "user/pingou",
  81. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  82. },
  83. }
  84. data = json.loads(output.get_data(as_text=True))
  85. data["user"]["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  86. for k in ["forks_pagination", "repos_pagination"]:
  87. for k2 in ["first", "last"]:
  88. self.assertIsNotNone(data[k][k2])
  89. data[k][k2] = "http://localhost..."
  90. self.assertEqual(data, exp)
  91. def test_api_view_user_with_project(self):
  92. """
  93. Test the api_view_user method of the flask api,
  94. this time the user has some project defined.
  95. """
  96. tests.create_projects(self.session)
  97. output = self.app.get("/api/0/user/pingou")
  98. self.assertEqual(output.status_code, 200)
  99. data = json.loads(output.get_data(as_text=True))
  100. data["repos"][0]["date_created"] = "1490272832"
  101. data["repos"][0]["date_modified"] = "1490272832"
  102. data["repos"][1]["date_created"] = "1490272832"
  103. data["repos"][1]["date_modified"] = "1490272832"
  104. data["repos"][2]["date_created"] = "1490272832"
  105. data["repos"][2]["date_modified"] = "1490272832"
  106. for k in ["forks_pagination", "repos_pagination"]:
  107. for k2 in ["first", "last"]:
  108. self.assertIsNotNone(data[k][k2])
  109. data[k][k2] = "http://localhost..."
  110. expected_data = {
  111. "forks": [],
  112. "forks_pagination": {
  113. "first": "http://localhost...",
  114. "last": "http://localhost...",
  115. "next": None,
  116. "forkpage": 1,
  117. "pages": 0,
  118. "per_page": 20,
  119. "prev": None,
  120. },
  121. "repos": [
  122. {
  123. "access_groups": {"admin": [], "commit": [], "ticket": []},
  124. "access_users": {
  125. "admin": [],
  126. "commit": [],
  127. "owner": ["pingou"],
  128. "ticket": [],
  129. },
  130. "close_status": [
  131. "Invalid",
  132. "Insufficient data",
  133. "Fixed",
  134. "Duplicate",
  135. ],
  136. "custom_keys": [],
  137. "date_created": "1490272832",
  138. "date_modified": "1490272832",
  139. "description": "test project #1",
  140. "fullname": "test",
  141. "url_path": "test",
  142. "id": 1,
  143. "milestones": {},
  144. "name": "test",
  145. "namespace": None,
  146. "parent": None,
  147. "priorities": {},
  148. "tags": [],
  149. "user": {
  150. "fullname": "PY C",
  151. "name": "pingou",
  152. "url_path": "user/pingou",
  153. },
  154. },
  155. {
  156. "access_groups": {"admin": [], "commit": [], "ticket": []},
  157. "access_users": {
  158. "admin": [],
  159. "commit": [],
  160. "owner": ["pingou"],
  161. "ticket": [],
  162. },
  163. "close_status": [
  164. "Invalid",
  165. "Insufficient data",
  166. "Fixed",
  167. "Duplicate",
  168. ],
  169. "custom_keys": [],
  170. "date_created": "1490272832",
  171. "date_modified": "1490272832",
  172. "description": "test project #2",
  173. "fullname": "test2",
  174. "url_path": "test2",
  175. "id": 2,
  176. "milestones": {},
  177. "name": "test2",
  178. "namespace": None,
  179. "parent": None,
  180. "priorities": {},
  181. "tags": [],
  182. "user": {
  183. "fullname": "PY C",
  184. "name": "pingou",
  185. "url_path": "user/pingou",
  186. },
  187. },
  188. {
  189. "access_groups": {"admin": [], "commit": [], "ticket": []},
  190. "access_users": {
  191. "admin": [],
  192. "commit": [],
  193. "owner": ["pingou"],
  194. "ticket": [],
  195. },
  196. "close_status": [
  197. "Invalid",
  198. "Insufficient data",
  199. "Fixed",
  200. "Duplicate",
  201. ],
  202. "custom_keys": [],
  203. "date_created": "1490272832",
  204. "date_modified": "1490272832",
  205. "description": "namespaced test project",
  206. "fullname": "somenamespace/test3",
  207. "url_path": "somenamespace/test3",
  208. "id": 3,
  209. "milestones": {},
  210. "name": "test3",
  211. "namespace": "somenamespace",
  212. "parent": None,
  213. "priorities": {},
  214. "tags": [],
  215. "user": {
  216. "fullname": "PY C",
  217. "name": "pingou",
  218. "url_path": "user/pingou",
  219. },
  220. },
  221. ],
  222. "repos_pagination": {
  223. "first": "http://localhost...",
  224. "last": "http://localhost...",
  225. "next": None,
  226. "repopage": 1,
  227. "pages": 1,
  228. "per_page": 20,
  229. "prev": None,
  230. },
  231. "user": {
  232. "fullname": "PY C",
  233. "name": "pingou",
  234. "url_path": "user/pingou",
  235. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  236. },
  237. }
  238. data["user"]["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  239. self.assertEqual(data, expected_data)
  240. @patch("pagure.lib.notify.send_email")
  241. def test_api_view_user_activity_stats(self, mockemail):
  242. """ Test the api_view_user_activity_stats method of the flask user
  243. api. """
  244. mockemail.return_value = True
  245. tests.create_projects(self.session)
  246. tests.create_tokens(self.session)
  247. tests.create_tokens_acl(self.session)
  248. headers = {"Authorization": "token aaabbbcccddd"}
  249. # Create a pull-request
  250. repo = pagure.lib.query._get_project(self.session, "test")
  251. forked_repo = pagure.lib.query._get_project(self.session, "test")
  252. req = pagure.lib.query.new_pull_request(
  253. session=self.session,
  254. repo_from=forked_repo,
  255. branch_from="master",
  256. repo_to=repo,
  257. branch_to="master",
  258. title="test pull-request",
  259. user="pingou",
  260. )
  261. self.session.commit()
  262. self.assertEqual(req.id, 1)
  263. self.assertEqual(req.title, "test pull-request")
  264. # Check comments before
  265. self.session.commit()
  266. request = pagure.lib.query.search_pull_requests(
  267. self.session, project_id=1, requestid=1
  268. )
  269. self.assertEqual(len(request.comments), 0)
  270. data = {"comment": "This is a very interesting question"}
  271. # Valid request
  272. output = self.app.post(
  273. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  274. )
  275. self.assertEqual(output.status_code, 200)
  276. data = json.loads(output.get_data(as_text=True))
  277. self.assertDictEqual(data, {"message": "Comment added"})
  278. # One comment added
  279. self.session.commit()
  280. request = pagure.lib.query.search_pull_requests(
  281. self.session, project_id=1, requestid=1
  282. )
  283. self.assertEqual(len(request.comments), 1)
  284. # Close PR
  285. output = self.app.post(
  286. "/api/0/test/pull-request/1/close", headers=headers
  287. )
  288. self.assertEqual(output.status_code, 200)
  289. data = json.loads(output.get_data(as_text=True))
  290. self.assertDictEqual(data, {"message": "Pull-request closed!"})
  291. # PR closed
  292. self.session.commit()
  293. request = pagure.lib.query.search_pull_requests(
  294. self.session, project_id=1, requestid=1
  295. )
  296. self.assertEqual(request.status, "Closed")
  297. # Finally retrieve the user's logs
  298. output = self.app.get("/api/0/user/pingou/activity/stats")
  299. self.assertEqual(output.status_code, 200)
  300. data = json.loads(output.get_data(as_text=True))
  301. date = datetime.datetime.utcnow().date().strftime("%Y-%m-%d")
  302. self.assertDictEqual(data, {date: 4})
  303. @patch("pagure.lib.notify.send_email")
  304. def test_api_view_user_activity_date(self, mockemail):
  305. """ Test the api_view_user_activity_date method of the flask user
  306. api. """
  307. self.test_api_view_user_activity_stats()
  308. # Invalid date
  309. output = self.app.get("/api/0/user/pingou/activity/AABB")
  310. self.assertEqual(output.status_code, 400)
  311. # Invalid date, arrow >= 0.15 throws an exception,
  312. # previous versions parsed it
  313. output = self.app.get("/api/0/user/pingou/activity/2016asd")
  314. arrow_version = self.get_arrow_version()
  315. if arrow_version >= (0, 15, 6):
  316. self.assertEqual(output.status_code, 400)
  317. exp = {
  318. "error": "Could not match input '2016asd' to any of the following formats: "
  319. "YYYY-MM-DD, YYYY-M-DD, YYYY-M-D, YYYY/MM/DD, YYYY/M/DD, YYYY/M/D, "
  320. "YYYY.MM.DD, YYYY.M.DD, YYYY.M.D, YYYYMMDD, YYYY-DDDD, YYYYDDDD, "
  321. "YYYY-MM, YYYY/MM, YYYY.MM, YYYY, W",
  322. "error_code": "ENOCODE",
  323. }
  324. self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
  325. elif arrow_version >= (0, 15):
  326. self.assertEqual(output.status_code, 400)
  327. exp = {
  328. "error": "Could not match input '2016asd' to any of the following formats: "
  329. "YYYY-MM-DD, YYYY-M-DD, YYYY-M-D, YYYY/MM/DD, YYYY/M/DD, YYYY/M/D, "
  330. "YYYY.MM.DD, YYYY.M.DD, YYYY.M.D, YYYYMMDD, YYYY-DDDD, YYYYDDDD, "
  331. "YYYY-MM, YYYY/MM, YYYY.MM, YYYY",
  332. "error_code": "ENOCODE",
  333. }
  334. self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
  335. else:
  336. self.assertEqual(output.status_code, 200)
  337. exp = {"activities": [], "date": "2016-01-01"}
  338. self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
  339. # Invalid date, arrow => throws an exception,
  340. # previous versions parsed it just not really as expected
  341. output = self.app.get("/api/0/user/pingou/activity/20161245")
  342. if self.get_arrow_version() >= (0, 15):
  343. self.assertEqual(output.status_code, 400)
  344. exp = {
  345. "error": "day is out of range for month",
  346. "error_code": "ENOCODE",
  347. }
  348. self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
  349. else:
  350. self.assertEqual(output.status_code, 200)
  351. exp = {"activities": [], "date": "1970-08-22"}
  352. self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
  353. date = datetime.datetime.utcnow().date().strftime("%Y-%m-%d")
  354. # Retrieve the user's logs for today
  355. output = self.app.get("/api/0/user/pingou/activity/%s" % date)
  356. self.assertEqual(output.status_code, 200)
  357. data = json.loads(output.get_data(as_text=True))
  358. exp = {
  359. "activities": [
  360. {
  361. "date": date,
  362. "date_created": "1477558752",
  363. "type": "pull-request",
  364. "description_mk": '<div class="markdown"><p>pingou created PR <a href="/test/pull-request/1" title="[Closed] test pull-request">test#1</a></p></div>',
  365. "id": 1,
  366. "ref_id": "1",
  367. "type": "created",
  368. "user": {
  369. "fullname": "PY C",
  370. "name": "pingou",
  371. "url_path": "user/pingou",
  372. },
  373. },
  374. {
  375. "date": date,
  376. "date_created": "1477558752",
  377. "type": "pull-request",
  378. "description_mk": '<div class="markdown"><p>pingou commented on PR <a href="/test/pull-request/1" title="[Closed] test pull-request">test#1</a></p></div>',
  379. "id": 2,
  380. "ref_id": "1",
  381. "type": "commented",
  382. "user": {
  383. "fullname": "PY C",
  384. "name": "pingou",
  385. "url_path": "user/pingou",
  386. },
  387. },
  388. {
  389. "date": date,
  390. "date_created": "1477558752",
  391. "type": "pull-request",
  392. "description_mk": '<div class="markdown"><p>pingou closed PR <a href="/test/pull-request/1" title="[Closed] test pull-request">test#1</a></p></div>',
  393. "id": 3,
  394. "ref_id": "1",
  395. "type": "closed",
  396. "user": {
  397. "fullname": "PY C",
  398. "name": "pingou",
  399. "url_path": "user/pingou",
  400. },
  401. },
  402. {
  403. "date": date,
  404. "date_created": "1477558752",
  405. "type": "pull-request",
  406. "description_mk": '<div class="markdown"><p>pingou commented on PR <a href="/test/pull-request/1" title="[Closed] test pull-request">test#1</a></p></div>',
  407. "id": 4,
  408. "ref_id": "1",
  409. "type": "commented",
  410. "user": {
  411. "fullname": "PY C",
  412. "name": "pingou",
  413. "url_path": "user/pingou",
  414. },
  415. },
  416. ],
  417. "date": date,
  418. }
  419. for idx, act in enumerate(data["activities"]):
  420. act["date_created"] = "1477558752"
  421. data["activities"][idx] = act
  422. self.assertEqual(data, exp)
  423. @patch("pagure.lib.notify.send_email")
  424. def test_api_view_user_activity_date_1_activity(self, mockemail):
  425. """ Test the api_view_user_activity_date method of the flask user
  426. api when the user only did one action. """
  427. tests.create_projects(self.session)
  428. repo = pagure.lib.query._get_project(self.session, "test")
  429. now = datetime.datetime.utcnow()
  430. date = now.date().strftime("%Y-%m-%d")
  431. # Create a single commit log
  432. log = model.PagureLog(
  433. user_id=1,
  434. user_email="foo@bar.com",
  435. project_id=1,
  436. log_type="committed",
  437. ref_id="githash",
  438. date=now.date(),
  439. date_created=now,
  440. )
  441. self.session.add(log)
  442. self.session.commit()
  443. # Retrieve the user's logs for today
  444. output = self.app.get(
  445. "/api/0/user/pingou/activity/%s?grouped=1" % date
  446. )
  447. self.assertEqual(output.status_code, 200)
  448. data = json.loads(output.get_data(as_text=True))
  449. exp = {
  450. "activities": [
  451. {
  452. "description_mk": '<div class="markdown"><p>pingou committed on test#githash</p></div>'
  453. }
  454. ],
  455. "date": date,
  456. }
  457. self.assertEqual(data, exp)
  458. @patch("pagure.lib.notify.send_email")
  459. def test_api_view_user_activity_timezone_negative(self, mockemail):
  460. """Test api_view_user_activity{_stats,_date} with the America/
  461. New York timezone, which is 5 hours behind UTC in winter and
  462. 4 hours behind UTC in summer (daylight savings). The events
  463. will occur on XXXX-02-15 in UTC, but on XXXX-02-14 local.
  464. """
  465. tests.create_projects(self.session)
  466. today = datetime.datetime.utcnow()
  467. # stats/activty returns data for now()-1year time range, so alter
  468. # year as needed to select 15th feb of current or previous year
  469. if today < datetime.datetime(today.year, 2, 15, 3, 30):
  470. year = today.year - 1
  471. else:
  472. year = today.year
  473. dateobj = datetime.datetime(year, 2, 15, 3, 30)
  474. utcdate = "%s-02-15" % year
  475. # the Unix timestamp for YYYY-02-15 12:00 UTC
  476. utcts = str(
  477. int(
  478. (
  479. datetime.datetime(year, 2, 15, 12, 0, tzinfo=pytz.UTC)
  480. - datetime.datetime(1970, 1, 1, tzinfo=pytz.UTC)
  481. ).total_seconds()
  482. )
  483. )
  484. localdate = "%s-02-14" % year
  485. # the Unix timestamp for YYYY-02-15 18:00 America/New_York
  486. localts = str(
  487. int(
  488. (
  489. datetime.datetime(
  490. year,
  491. 2,
  492. 14,
  493. 17,
  494. 0,
  495. tzinfo=pytz.timezone("America/New_York"),
  496. )
  497. - datetime.datetime(
  498. 1970, 1, 1, tzinfo=pytz.timezone("America/New_York")
  499. )
  500. ).total_seconds()
  501. )
  502. )
  503. # Create a single commit log
  504. log = model.PagureLog(
  505. user_id=1,
  506. user_email="foo@bar.com",
  507. project_id=1,
  508. log_type="committed",
  509. ref_id="githash",
  510. date=dateobj.date(),
  511. date_created=dateobj,
  512. )
  513. self.session.add(log)
  514. self.session.commit()
  515. # Retrieve the user's stats with no timezone specified (==UTC)
  516. output = self.app.get("/api/0/user/pingou/activity/stats")
  517. self.assertEqual(output.status_code, 200)
  518. data = json.loads(output.get_data(as_text=True))
  519. # date in output should be UTC date
  520. self.assertDictEqual(data, {utcdate: 1})
  521. # Now in timestamp format...
  522. output = self.app.get(
  523. "/api/0/user/pingou/activity/stats?format=timestamp"
  524. )
  525. self.assertEqual(output.status_code, 200)
  526. data = json.loads(output.get_data(as_text=True))
  527. # timestamp in output should be UTC ts
  528. self.assertDictEqual(data, {utcts: 1})
  529. # Retrieve the user's stats with local timezone specified
  530. output = self.app.get(
  531. "/api/0/user/pingou/activity/stats?tz=America/New_York"
  532. )
  533. self.assertEqual(output.status_code, 200)
  534. data = json.loads(output.get_data(as_text=True))
  535. # date in output should be local date
  536. self.assertDictEqual(data, {localdate: 1})
  537. # Now in timestamp format...
  538. output = self.app.get(
  539. "/api/0/user/pingou/activity/stats?format=timestamp&tz=America/New_York"
  540. )
  541. self.assertEqual(output.status_code, 200)
  542. data = json.loads(output.get_data(as_text=True))
  543. # timestamp in output should be local ts
  544. self.assertDictEqual(data, {localts: 1})
  545. # Retrieve the user's logs for 2018-02-15 with no timezone
  546. output = self.app.get(
  547. "/api/0/user/pingou/activity/%s?grouped=1" % utcdate
  548. )
  549. self.assertEqual(output.status_code, 200)
  550. data = json.loads(output.get_data(as_text=True))
  551. exp = {
  552. "activities": [
  553. {
  554. "description_mk": '<div class="markdown"><p>pingou committed on test#githash</p></div>'
  555. }
  556. ],
  557. "date": utcdate,
  558. }
  559. self.assertEqual(data, exp)
  560. # Now retrieve the user's logs for 2018-02-14 with local time
  561. output = self.app.get(
  562. "/api/0/user/pingou/activity/%s?grouped=1&tz=America/New_York"
  563. % localdate
  564. )
  565. self.assertEqual(output.status_code, 200)
  566. data = json.loads(output.get_data(as_text=True))
  567. exp["date"] = localdate
  568. self.assertEqual(data, exp)
  569. @patch("pagure.lib.notify.send_email")
  570. def test_api_view_user_activity_timezone_positive(self, mockemail):
  571. """Test api_view_user_activity{_stats,_date} with the Asia/
  572. Dubai timezone, which is 4 hours ahead of UTC. The events will
  573. occur on XXXX-02-15 in UTC, but on XXXX-02-16 in local time.
  574. """
  575. tests.create_projects(self.session)
  576. today = datetime.datetime.utcnow()
  577. # stats/activty returns data for now()-1year time range, so alter
  578. # year as needed to select 15th feb of current or previous year
  579. if today < datetime.datetime(today.year, 2, 15, 3, 30):
  580. year = today.year - 1
  581. else:
  582. year = today.year
  583. dateobj = datetime.datetime(year, 2, 15, 22, 30)
  584. utcdate = "%s-02-15" % year
  585. # the Unix timestamp for YYYY-02-15 12:00 UTC
  586. utcts = str(
  587. int(
  588. (
  589. datetime.datetime(year, 2, 15, 12, 0, tzinfo=pytz.UTC)
  590. - datetime.datetime(1970, 1, 1, tzinfo=pytz.UTC)
  591. ).total_seconds()
  592. )
  593. )
  594. localdate = "%s-02-16" % year
  595. # the Unix timestamp for YYYY-02-16 9:00 Asia/Dubai
  596. localts = str(
  597. int(
  598. (
  599. datetime.datetime(
  600. year, 2, 16, 8, 0, tzinfo=pytz.timezone("Asia/Dubai")
  601. )
  602. - datetime.datetime(
  603. 1970, 1, 1, tzinfo=pytz.timezone("Asia/Dubai")
  604. )
  605. ).total_seconds()
  606. )
  607. )
  608. # Create a single commit log
  609. log = model.PagureLog(
  610. user_id=1,
  611. user_email="foo@bar.com",
  612. project_id=1,
  613. log_type="committed",
  614. ref_id="githash",
  615. date=dateobj.date(),
  616. date_created=dateobj,
  617. )
  618. self.session.add(log)
  619. self.session.commit()
  620. # Retrieve the user's stats with no timezone specified (==UTC)
  621. output = self.app.get("/api/0/user/pingou/activity/stats")
  622. self.assertEqual(output.status_code, 200)
  623. data = json.loads(output.get_data(as_text=True))
  624. # date in output should be UTC date
  625. self.assertDictEqual(data, {utcdate: 1})
  626. # Now in timestamp format...
  627. output = self.app.get(
  628. "/api/0/user/pingou/activity/stats?format=timestamp"
  629. )
  630. self.assertEqual(output.status_code, 200)
  631. data = json.loads(output.get_data(as_text=True))
  632. # timestamp in output should be UTC ts
  633. self.assertDictEqual(data, {utcts: 1})
  634. # Retrieve the user's stats with local timezone specified
  635. output = self.app.get(
  636. "/api/0/user/pingou/activity/stats?tz=Asia/Dubai"
  637. )
  638. self.assertEqual(output.status_code, 200)
  639. data = json.loads(output.get_data(as_text=True))
  640. # date in output should be local date
  641. self.assertDictEqual(data, {localdate: 1})
  642. # Now in timestamp format...
  643. output = self.app.get(
  644. "/api/0/user/pingou/activity/stats?format=timestamp&tz=Asia/Dubai"
  645. )
  646. self.assertEqual(output.status_code, 200)
  647. data = json.loads(output.get_data(as_text=True))
  648. # timestamp in output should be local ts
  649. self.assertDictEqual(data, {localts: 1})
  650. # Retrieve the user's logs for 2018-02-15 with no timezone
  651. output = self.app.get(
  652. "/api/0/user/pingou/activity/%s?grouped=1" % utcdate
  653. )
  654. self.assertEqual(output.status_code, 200)
  655. data = json.loads(output.get_data(as_text=True))
  656. exp = {
  657. "activities": [
  658. {
  659. "description_mk": '<div class="markdown"><p>pingou committed on test#githash</p></div>'
  660. }
  661. ],
  662. "date": utcdate,
  663. }
  664. self.assertEqual(data, exp)
  665. # Now retrieve the user's logs for 2018-02-16 with local time
  666. output = self.app.get(
  667. "/api/0/user/pingou/activity/%s?grouped=1&tz=Asia/Dubai"
  668. % localdate
  669. )
  670. self.assertEqual(output.status_code, 200)
  671. data = json.loads(output.get_data(as_text=True))
  672. exp["date"] = localdate
  673. self.assertEqual(data, exp)
  674. class PagureFlaskApiUsertestrequests(tests.Modeltests):
  675. """ Tests for the user requests endpoints """
  676. maxDiff = None
  677. def setUp(self):
  678. """ Set up the environnment, ran before every tests. """
  679. super(PagureFlaskApiUsertestrequests, self).setUp()
  680. pagure.config.config["REQUESTS_FOLDER"] = None
  681. tests.create_projects(self.session)
  682. # Create few pull-requests
  683. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  684. forked_repo = pagure.lib.query.get_authorized_project(
  685. self.session, "test"
  686. )
  687. pagure.lib.query.new_pull_request(
  688. session=self.session,
  689. repo_from=forked_repo,
  690. branch_from="master",
  691. repo_to=repo,
  692. branch_to="master",
  693. title="open pullrequest by user foo on repo test",
  694. user="foo",
  695. )
  696. repo = pagure.lib.query.get_authorized_project(self.session, "test2")
  697. forked_repo = pagure.lib.query.get_authorized_project(
  698. self.session, "test2"
  699. )
  700. pagure.lib.query.new_pull_request(
  701. session=self.session,
  702. repo_from=forked_repo,
  703. branch_from="master",
  704. repo_to=repo,
  705. branch_to="master",
  706. title="open pullrequest by user foo on repo test2",
  707. user="foo",
  708. )
  709. self.session.commit()
  710. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  711. forked_repo = pagure.lib.query.get_authorized_project(
  712. self.session, "test"
  713. )
  714. pagure.lib.query.new_pull_request(
  715. session=self.session,
  716. repo_from=forked_repo,
  717. branch_from="master",
  718. repo_to=repo,
  719. branch_to="master",
  720. title="closed pullrequest by user foo on repo test",
  721. user="foo",
  722. status="Closed",
  723. )
  724. repo = pagure.lib.query.get_authorized_project(self.session, "test2")
  725. forked_repo = pagure.lib.query.get_authorized_project(
  726. self.session, "test2"
  727. )
  728. pagure.lib.query.new_pull_request(
  729. session=self.session,
  730. repo_from=forked_repo,
  731. branch_from="master",
  732. repo_to=repo,
  733. branch_to="master",
  734. title="closed pullrequest by user foo on repo test2",
  735. user="foo",
  736. status="Closed",
  737. )
  738. self.session.commit()
  739. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  740. forked_repo = pagure.lib.query.get_authorized_project(
  741. self.session, "test"
  742. )
  743. pagure.lib.query.new_pull_request(
  744. session=self.session,
  745. repo_from=forked_repo,
  746. branch_from="master",
  747. repo_to=repo,
  748. branch_to="master",
  749. title="merged pullrequest by user foo on repo test",
  750. user="foo",
  751. status="Merged",
  752. )
  753. repo = pagure.lib.query.get_authorized_project(self.session, "test2")
  754. forked_repo = pagure.lib.query.get_authorized_project(
  755. self.session, "test2"
  756. )
  757. pagure.lib.query.new_pull_request(
  758. session=self.session,
  759. repo_from=forked_repo,
  760. branch_from="master",
  761. repo_to=repo,
  762. branch_to="master",
  763. title="merged pullrequest by user foo on repo test2",
  764. user="foo",
  765. status="Merged",
  766. )
  767. self.session.commit()
  768. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  769. forked_repo = pagure.lib.query.get_authorized_project(
  770. self.session, "test"
  771. )
  772. pagure.lib.query.new_pull_request(
  773. session=self.session,
  774. repo_from=forked_repo,
  775. branch_from="master",
  776. repo_to=repo,
  777. branch_to="master",
  778. title="open pullrequest by user pingou on repo test",
  779. user="pingou",
  780. )
  781. self.session.commit()
  782. repo = pagure.lib.query.get_authorized_project(self.session, "test2")
  783. forked_repo = pagure.lib.query.get_authorized_project(
  784. self.session, "test2"
  785. )
  786. pagure.lib.query.new_pull_request(
  787. session=self.session,
  788. repo_from=forked_repo,
  789. branch_from="master",
  790. repo_to=repo,
  791. branch_to="master",
  792. title="open pullrequest by user pingou on repo test2",
  793. user="pingou",
  794. )
  795. self.session.commit()
  796. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  797. forked_repo = pagure.lib.query.get_authorized_project(
  798. self.session, "test"
  799. )
  800. pagure.lib.query.new_pull_request(
  801. session=self.session,
  802. repo_from=forked_repo,
  803. branch_from="master",
  804. repo_to=repo,
  805. branch_to="master",
  806. title="closed pullrequest by user pingou on repo test",
  807. user="pingou",
  808. status="Closed",
  809. )
  810. self.session.commit()
  811. repo = pagure.lib.query.get_authorized_project(self.session, "test2")
  812. forked_repo = pagure.lib.query.get_authorized_project(
  813. self.session, "test2"
  814. )
  815. pagure.lib.query.new_pull_request(
  816. session=self.session,
  817. repo_from=forked_repo,
  818. branch_from="master",
  819. repo_to=repo,
  820. branch_to="master",
  821. title="closed pullrequest by user pingou on repo test2",
  822. user="pingou",
  823. status="Closed",
  824. )
  825. self.session.commit()
  826. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  827. forked_repo = pagure.lib.query.get_authorized_project(
  828. self.session, "test"
  829. )
  830. pagure.lib.query.new_pull_request(
  831. session=self.session,
  832. repo_from=forked_repo,
  833. branch_from="master",
  834. repo_to=repo,
  835. branch_to="master",
  836. title="merged pullrequest by user pingou on repo test",
  837. user="pingou",
  838. status="Merged",
  839. )
  840. self.session.commit()
  841. repo = pagure.lib.query.get_authorized_project(self.session, "test2")
  842. forked_repo = pagure.lib.query.get_authorized_project(
  843. self.session, "test2"
  844. )
  845. pagure.lib.query.new_pull_request(
  846. session=self.session,
  847. repo_from=forked_repo,
  848. branch_from="master",
  849. repo_to=repo,
  850. branch_to="master",
  851. title="merged pullrequest by user pingou on repo test2",
  852. user="pingou",
  853. status="Merged",
  854. )
  855. self.session.commit()
  856. @patch("pagure.lib.notify.send_email")
  857. def test_api_view_user_requests_filed(self, mockemail):
  858. """ Test the api_view_user_requests_filed method of the flask user
  859. api """
  860. # First we test without the status parameter. It should default to `open`
  861. output = self.app.get("/api/0/user/pingou/requests/filed")
  862. self.assertEqual(output.status_code, 200)
  863. data = json.loads(output.get_data(as_text=True))
  864. self.assertEqual(len(data["requests"]), 2)
  865. self.assertEqual(
  866. sorted(data.keys()),
  867. ["args", "pagination", "requests", "total_requests"],
  868. )
  869. self.assertEqual(data["requests"][0]["user"]["name"], "pingou")
  870. self.assertEqual(data["requests"][1]["user"]["name"], "pingou")
  871. self.assertEqual(data["requests"][0]["status"], "Open")
  872. self.assertEqual(data["requests"][1]["status"], "Open")
  873. self.assertEqual(
  874. data["requests"][0]["title"],
  875. "open pullrequest by user pingou on repo test2",
  876. )
  877. self.assertEqual(
  878. data["requests"][1]["title"],
  879. "open pullrequest by user pingou on repo test",
  880. )
  881. self.assertEqual(data["args"]["status"], "open")
  882. self.assertEqual(data["args"]["page"], 1)
  883. # Next test with the status parameter set to `open`.
  884. output = self.app.get("/api/0/user/pingou/requests/filed?status=open")
  885. self.assertEqual(output.status_code, 200)
  886. data = json.loads(output.get_data(as_text=True))
  887. self.assertEqual(len(data["requests"]), 2)
  888. self.assertEqual(
  889. sorted(data.keys()),
  890. ["args", "pagination", "requests", "total_requests"],
  891. )
  892. self.assertEqual(data["requests"][0]["user"]["name"], "pingou")
  893. self.assertEqual(data["requests"][1]["user"]["name"], "pingou")
  894. self.assertEqual(data["requests"][0]["status"], "Open")
  895. self.assertEqual(data["requests"][1]["status"], "Open")
  896. self.assertEqual(
  897. data["requests"][0]["title"],
  898. "open pullrequest by user pingou on repo test2",
  899. )
  900. self.assertEqual(
  901. data["requests"][1]["title"],
  902. "open pullrequest by user pingou on repo test",
  903. )
  904. self.assertEqual(data["args"]["status"], "open")
  905. self.assertEqual(data["args"]["page"], 1)
  906. # Next test with the status parameter set to `closed`.
  907. output = self.app.get(
  908. "/api/0/user/pingou/requests/filed?status=closed"
  909. )
  910. self.assertEqual(output.status_code, 200)
  911. data = json.loads(output.get_data(as_text=True))
  912. self.assertEqual(len(data["requests"]), 2)
  913. self.assertEqual(
  914. sorted(data.keys()),
  915. ["args", "pagination", "requests", "total_requests"],
  916. )
  917. self.assertEqual(data["requests"][0]["user"]["name"], "pingou")
  918. self.assertEqual(data["requests"][1]["user"]["name"], "pingou")
  919. self.assertEqual(data["requests"][0]["status"], "Closed")
  920. self.assertEqual(data["requests"][1]["status"], "Closed")
  921. self.assertEqual(
  922. data["requests"][0]["title"],
  923. "closed pullrequest by user pingou on repo test2",
  924. )
  925. self.assertEqual(
  926. data["requests"][1]["title"],
  927. "closed pullrequest by user pingou on repo test",
  928. )
  929. self.assertEqual(data["args"]["status"], "closed")
  930. self.assertEqual(data["args"]["page"], 1)
  931. # Next test with the status parameter set to `merged`.
  932. output = self.app.get(
  933. "/api/0/user/pingou/requests/filed?status=merged"
  934. )
  935. self.assertEqual(output.status_code, 200)
  936. data = json.loads(output.get_data(as_text=True))
  937. self.assertEqual(len(data["requests"]), 2)
  938. self.assertEqual(
  939. sorted(data.keys()),
  940. ["args", "pagination", "requests", "total_requests"],
  941. )
  942. self.assertEqual(data["requests"][0]["user"]["name"], "pingou")
  943. self.assertEqual(data["requests"][1]["user"]["name"], "pingou")
  944. self.assertEqual(data["requests"][0]["status"], "Merged")
  945. self.assertEqual(data["requests"][1]["status"], "Merged")
  946. self.assertEqual(
  947. data["requests"][0]["title"],
  948. "merged pullrequest by user pingou on repo test2",
  949. )
  950. self.assertEqual(
  951. data["requests"][1]["title"],
  952. "merged pullrequest by user pingou on repo test",
  953. )
  954. self.assertEqual(data["args"]["status"], "merged")
  955. self.assertEqual(data["args"]["page"], 1)
  956. # Finally, test with the status parameter set to `all`.
  957. output = self.app.get("/api/0/user/pingou/requests/filed?status=all")
  958. self.assertEqual(output.status_code, 200)
  959. data = json.loads(output.get_data(as_text=True))
  960. self.assertEqual(len(data["requests"]), 6)
  961. self.assertEqual(
  962. sorted(data.keys()),
  963. ["args", "pagination", "requests", "total_requests"],
  964. )
  965. self.assertEqual(data["requests"][0]["user"]["name"], "pingou")
  966. self.assertEqual(data["requests"][1]["user"]["name"], "pingou")
  967. self.assertEqual(data["requests"][2]["user"]["name"], "pingou")
  968. self.assertEqual(data["requests"][3]["user"]["name"], "pingou")
  969. self.assertEqual(data["requests"][4]["user"]["name"], "pingou")
  970. self.assertEqual(data["requests"][5]["user"]["name"], "pingou")
  971. self.assertEqual(data["requests"][0]["status"], "Merged")
  972. self.assertEqual(data["requests"][1]["status"], "Merged")
  973. self.assertEqual(data["requests"][2]["status"], "Closed")
  974. self.assertEqual(data["requests"][3]["status"], "Closed")
  975. self.assertEqual(data["requests"][4]["status"], "Open")
  976. self.assertEqual(data["requests"][5]["status"], "Open")
  977. self.assertEqual(
  978. data["requests"][0]["title"],
  979. "merged pullrequest by user pingou on repo test2",
  980. )
  981. self.assertEqual(
  982. data["requests"][1]["title"],
  983. "merged pullrequest by user pingou on repo test",
  984. )
  985. self.assertEqual(
  986. data["requests"][2]["title"],
  987. "closed pullrequest by user pingou on repo test2",
  988. )
  989. self.assertEqual(
  990. data["requests"][3]["title"],
  991. "closed pullrequest by user pingou on repo test",
  992. )
  993. self.assertEqual(
  994. data["requests"][4]["title"],
  995. "open pullrequest by user pingou on repo test2",
  996. )
  997. self.assertEqual(
  998. data["requests"][5]["title"],
  999. "open pullrequest by user pingou on repo test",
  1000. )
  1001. self.assertEqual(data["args"]["status"], "all")
  1002. self.assertEqual(data["args"]["page"], 1)
  1003. # Test page 2 with the status parameter set to `all`.
  1004. output = self.app.get(
  1005. "/api/0/user/pingou/requests/filed?status=all&page=2"
  1006. )
  1007. self.assertEqual(output.status_code, 200)
  1008. data = json.loads(output.get_data(as_text=True))
  1009. self.assertEqual(len(data["requests"]), 0)
  1010. self.assertEqual(
  1011. sorted(data.keys()),
  1012. ["args", "pagination", "requests", "total_requests"],
  1013. )
  1014. self.assertEqual(data["args"]["page"], 2)
  1015. @patch("pagure.lib.notify.send_email")
  1016. def test_api_view_user_requests_filed_created(self, mockemail):
  1017. """ Test the api_view_user_requests_filed method of the flask user
  1018. api with the created parameter """
  1019. today = datetime.datetime.utcnow().date()
  1020. output = self.app.get(
  1021. "/api/0/user/pingou/requests/filed?status=all&created=%s"
  1022. % (today.isoformat())
  1023. )
  1024. self.assertEqual(output.status_code, 200)
  1025. data = json.loads(output.get_data(as_text=True))
  1026. self.assertEqual(len(data["requests"]), 6)
  1027. yesterday = today - datetime.timedelta(days=1)
  1028. output = self.app.get(
  1029. "/api/0/user/pingou/requests/filed?status=all&created=%s"
  1030. % (yesterday.isoformat())
  1031. )
  1032. self.assertEqual(output.status_code, 200)
  1033. data = json.loads(output.get_data(as_text=True))
  1034. self.assertEqual(len(data["requests"]), 6)
  1035. tomorrow = today + datetime.timedelta(days=1)
  1036. output = self.app.get(
  1037. "/api/0/user/pingou/requests/filed?status=all&created=%s"
  1038. % (tomorrow.isoformat())
  1039. )
  1040. self.assertEqual(output.status_code, 200)
  1041. data = json.loads(output.get_data(as_text=True))
  1042. self.assertEqual(len(data["requests"]), 0)
  1043. output = self.app.get(
  1044. "/api/0/user/pingou/requests/filed?status=all&created=..%s"
  1045. % (today.isoformat())
  1046. )
  1047. self.assertEqual(output.status_code, 200)
  1048. data = json.loads(output.get_data(as_text=True))
  1049. self.assertEqual(len(data["requests"]), 0)
  1050. output = self.app.get(
  1051. "/api/0/user/pingou/requests/filed?status=all&created=..%s"
  1052. % (yesterday.isoformat())
  1053. )
  1054. self.assertEqual(output.status_code, 200)
  1055. data = json.loads(output.get_data(as_text=True))
  1056. self.assertEqual(len(data["requests"]), 0)
  1057. thedaybefore = today - datetime.timedelta(days=1)
  1058. output = self.app.get(
  1059. "/api/0/user/pingou/requests/filed?status=all&created=..%s"
  1060. % (thedaybefore.isoformat())
  1061. )
  1062. self.assertEqual(output.status_code, 200)
  1063. data = json.loads(output.get_data(as_text=True))
  1064. self.assertEqual(len(data["requests"]), 0)
  1065. output = self.app.get(
  1066. "/api/0/user/pingou/requests/filed?status=all&created=..%s"
  1067. % (tomorrow.isoformat())
  1068. )
  1069. self.assertEqual(output.status_code, 200)
  1070. data = json.loads(output.get_data(as_text=True))
  1071. self.assertEqual(len(data["requests"]), 6)
  1072. output = self.app.get(
  1073. "/api/0/user/pingou/requests/filed?status=all&created=%s..%s"
  1074. % (thedaybefore.isoformat(), tomorrow.isoformat())
  1075. )
  1076. self.assertEqual(output.status_code, 200)
  1077. data = json.loads(output.get_data(as_text=True))
  1078. self.assertEqual(len(data["requests"]), 6)
  1079. @patch("pagure.lib.notify.send_email")
  1080. def test_api_view_user_requests_filed_updated(self, mockemail):
  1081. """ Test the api_view_user_requests_filed method of the flask user
  1082. api with the created parameter """
  1083. today = datetime.datetime.utcnow().date()
  1084. output = self.app.get(
  1085. "/api/0/user/pingou/requests/filed?status=all&updated=%s"
  1086. % (today.isoformat())
  1087. )
  1088. self.assertEqual(output.status_code, 200)
  1089. data = json.loads(output.get_data(as_text=True))
  1090. self.assertEqual(len(data["requests"]), 0)
  1091. yesterday = today - datetime.timedelta(days=1)
  1092. output = self.app.get(
  1093. "/api/0/user/pingou/requests/filed?status=all&updated=%s"
  1094. % (yesterday.isoformat())
  1095. )
  1096. self.assertEqual(output.status_code, 200)
  1097. data = json.loads(output.get_data(as_text=True))
  1098. self.assertEqual(len(data["requests"]), 0)
  1099. tomorrow = today + datetime.timedelta(days=1)
  1100. output = self.app.get(
  1101. "/api/0/user/pingou/requests/filed?status=all&updated=%s"
  1102. % (tomorrow.isoformat())
  1103. )
  1104. self.assertEqual(output.status_code, 200)
  1105. data = json.loads(output.get_data(as_text=True))
  1106. self.assertEqual(len(data["requests"]), 6)
  1107. @patch("pagure.lib.notify.send_email")
  1108. def test_api_view_user_requests_filed_closed(self, mockemail):
  1109. """ Test the api_view_user_requests_filed method of the flask user
  1110. api with the created parameter """
  1111. today = datetime.datetime.utcnow().date()
  1112. output = self.app.get(
  1113. "/api/0/user/pingou/requests/filed?status=all&closed=%s"
  1114. % (today.isoformat())
  1115. )
  1116. self.assertEqual(output.status_code, 200)
  1117. data = json.loads(output.get_data(as_text=True))
  1118. self.assertEqual(len(data["requests"]), 0)
  1119. yesterday = today - datetime.timedelta(days=1)
  1120. output = self.app.get(
  1121. "/api/0/user/pingou/requests/filed?status=all&closed=%s"
  1122. % (yesterday.isoformat())
  1123. )
  1124. self.assertEqual(output.status_code, 200)
  1125. data = json.loads(output.get_data(as_text=True))
  1126. self.assertEqual(len(data["requests"]), 0)
  1127. tomorrow = today + datetime.timedelta(days=1)
  1128. output = self.app.get(
  1129. "/api/0/user/pingou/requests/filed?status=all&closed=%s"
  1130. % (tomorrow.isoformat())
  1131. )
  1132. self.assertEqual(output.status_code, 200)
  1133. data = json.loads(output.get_data(as_text=True))
  1134. self.assertEqual(len(data["requests"]), 0)
  1135. @patch("pagure.lib.notify.send_email")
  1136. def test_api_view_user_requests_filed_foo(self, mockemail):
  1137. """ Test the api_view_user_requests_filed method of the flask user
  1138. api """
  1139. # Default data returned
  1140. output = self.app.get(
  1141. "/api/0/user/foo/requests/filed?status=all&per_page=6"
  1142. )
  1143. self.assertEqual(output.status_code, 200)
  1144. data = json.loads(output.get_data(as_text=True))
  1145. self.assertEqual(len(data["requests"]), 6)
  1146. self.assertEqual(
  1147. sorted(data.keys()),
  1148. ["args", "pagination", "requests", "total_requests"],
  1149. )
  1150. # There are 6 PRs, that's 1 page at 6 results per page
  1151. self.assertEqual(data["pagination"]["pages"], 1)
  1152. @patch("pagure.lib.notify.send_email")
  1153. def test_api_view_user_requests_filed_foo_grp_access(self, mockemail):
  1154. """ Test when the user has accessed to some PRs via a group. """
  1155. # Add the user to a group
  1156. msg = pagure.lib.query.add_group(
  1157. self.session,
  1158. group_name="some_group",
  1159. display_name="Some Group",
  1160. description=None,
  1161. group_type="bar",
  1162. user="pingou",
  1163. is_admin=False,
  1164. blacklist=[],
  1165. )
  1166. self.session.commit()
  1167. # Add the group to the project `test2`
  1168. project = pagure.lib.query._get_project(self.session, "test2")
  1169. msg = pagure.lib.query.add_group_to_project(
  1170. session=self.session,
  1171. project=project,
  1172. new_group="some_group",
  1173. user="pingou",
  1174. )
  1175. self.session.commit()
  1176. self.assertEqual(msg, "Group added")
  1177. # Add foo to the group
  1178. group = pagure.lib.query.search_groups(
  1179. self.session, group_name="some_group"
  1180. )
  1181. result = pagure.lib.query.add_user_to_group(
  1182. self.session, "foo", group, "pingou", True
  1183. )
  1184. self.session.commit()
  1185. self.assertEqual(result, "User `foo` added to the group `some_group`.")
  1186. # Query the API for foo's filed PRs
  1187. output = self.app.get(
  1188. "/api/0/user/foo/requests/filed?status=all&per_page=6"
  1189. )
  1190. self.assertEqual(output.status_code, 200)
  1191. data = json.loads(output.get_data(as_text=True))
  1192. self.assertEqual(len(data["requests"]), 6)
  1193. self.assertEqual(
  1194. sorted(data.keys()),
  1195. ["args", "pagination", "requests", "total_requests"],
  1196. )
  1197. # There are 6 PRs, that's 1 page at 6 results per page
  1198. self.assertEqual(data["pagination"]["pages"], 1)
  1199. @patch("pagure.lib.notify.send_email")
  1200. def test_api_view_user_requests_actionable(self, mockemail):
  1201. """ Test the api_view_user_requests_actionable method of the flask user
  1202. api """
  1203. # First we test without the status parameter. It should default to `open`
  1204. output = self.app.get("/api/0/user/pingou/requests/actionable")
  1205. self.assertEqual(output.status_code, 200)
  1206. data = json.loads(output.get_data(as_text=True))
  1207. self.assertEqual(len(data["requests"]), 2)
  1208. self.assertEqual(
  1209. sorted(data.keys()),
  1210. ["args", "pagination", "requests", "total_requests"],
  1211. )
  1212. self.assertEqual(data["requests"][0]["user"]["name"], "foo")
  1213. self.assertEqual(data["requests"][1]["user"]["name"], "foo")
  1214. self.assertEqual(data["requests"][0]["status"], "Open")
  1215. self.assertEqual(data["requests"][1]["status"], "Open")
  1216. self.assertEqual(
  1217. data["requests"][0]["title"],
  1218. "open pullrequest by user foo on repo test2",
  1219. )
  1220. self.assertEqual(
  1221. data["requests"][1]["title"],
  1222. "open pullrequest by user foo on repo test",
  1223. )
  1224. self.assertEqual(data["args"]["status"], "open")
  1225. self.assertEqual(data["args"]["page"], 1)
  1226. # Next test with the status parameter set to `open`.
  1227. output = self.app.get(
  1228. "/api/0/user/pingou/requests/actionable?status=open"
  1229. )
  1230. self.assertEqual(output.status_code, 200)
  1231. data = json.loads(output.get_data(as_text=True))
  1232. self.assertEqual(len(data["requests"]), 2)
  1233. self.assertEqual(
  1234. sorted(data.keys()),
  1235. ["args", "pagination", "requests", "total_requests"],
  1236. )
  1237. self.assertEqual(data["requests"][0]["user"]["name"], "foo")
  1238. self.assertEqual(data["requests"][1]["user"]["name"], "foo")
  1239. self.assertEqual(data["requests"][0]["status"], "Open")
  1240. self.assertEqual(data["requests"][1]["status"], "Open")
  1241. self.assertEqual(
  1242. data["requests"][0]["title"],
  1243. "open pullrequest by user foo on repo test2",
  1244. )
  1245. self.assertEqual(
  1246. data["requests"][1]["title"],
  1247. "open pullrequest by user foo on repo test",
  1248. )
  1249. self.assertEqual(data["args"]["status"], "open")
  1250. self.assertEqual(data["args"]["page"], 1)
  1251. # Next test with the status parameter set to `closed`.
  1252. output = self.app.get(
  1253. "/api/0/user/pingou/requests/actionable?status=closed"
  1254. )
  1255. self.assertEqual(output.status_code, 200)
  1256. data = json.loads(output.get_data(as_text=True))
  1257. self.assertEqual(len(data["requests"]), 2)
  1258. self.assertEqual(
  1259. sorted(data.keys()),
  1260. ["args", "pagination", "requests", "total_requests"],
  1261. )
  1262. self.assertEqual(data["requests"][0]["user"]["name"], "foo")
  1263. self.assertEqual(data["requests"][1]["user"]["name"], "foo")
  1264. self.assertEqual(data["requests"][0]["status"], "Closed")
  1265. self.assertEqual(data["requests"][1]["status"], "Closed")
  1266. self.assertEqual(
  1267. data["requests"][0]["title"],
  1268. "closed pullrequest by user foo on repo test2",
  1269. )
  1270. self.assertEqual(
  1271. data["requests"][1]["title"],
  1272. "closed pullrequest by user foo on repo test",
  1273. )
  1274. self.assertEqual(data["args"]["status"], "closed")
  1275. self.assertEqual(data["args"]["page"], 1)
  1276. # Next test with the status parameter set to `merged`.
  1277. output = self.app.get(
  1278. "/api/0/user/pingou/requests/actionable?status=merged"
  1279. )
  1280. self.assertEqual(output.status_code, 200)
  1281. data = json.loads(output.get_data(as_text=True))
  1282. self.assertEqual(len(data["requests"]), 2)
  1283. self.assertEqual(
  1284. sorted(data.keys()),
  1285. ["args", "pagination", "requests", "total_requests"],
  1286. )
  1287. self.assertEqual(data["requests"][0]["user"]["name"], "foo")
  1288. self.assertEqual(data["requests"][1]["user"]["name"], "foo")
  1289. self.assertEqual(data["requests"][0]["status"], "Merged")
  1290. self.assertEqual(data["requests"][1]["status"], "Merged")
  1291. self.assertEqual(
  1292. data["requests"][0]["title"],
  1293. "merged pullrequest by user foo on repo test2",
  1294. )
  1295. self.assertEqual(
  1296. data["requests"][1]["title"],
  1297. "merged pullrequest by user foo on repo test",
  1298. )
  1299. self.assertEqual(data["args"]["status"], "merged")
  1300. self.assertEqual(data["args"]["page"], 1)
  1301. # Finally, test with the status parameter set to `all`.
  1302. output = self.app.get(
  1303. "/api/0/user/pingou/requests/actionable?status=all"
  1304. )
  1305. self.assertEqual(output.status_code, 200)
  1306. data = json.loads(output.get_data(as_text=True))
  1307. self.assertEqual(len(data["requests"]), 6)
  1308. self.assertEqual(
  1309. sorted(data.keys()),
  1310. ["args", "pagination", "requests", "total_requests"],
  1311. )
  1312. self.assertEqual(data["requests"][0]["user"]["name"], "foo")
  1313. self.assertEqual(data["requests"][1]["user"]["name"], "foo")
  1314. self.assertEqual(data["requests"][2]["user"]["name"], "foo")
  1315. self.assertEqual(data["requests"][3]["user"]["name"], "foo")
  1316. self.assertEqual(data["requests"][4]["user"]["name"], "foo")
  1317. self.assertEqual(data["requests"][5]["user"]["name"], "foo")
  1318. self.assertEqual(data["requests"][0]["status"], "Merged")
  1319. self.assertEqual(data["requests"][1]["status"], "Merged")
  1320. self.assertEqual(data["requests"][2]["status"], "Closed")
  1321. self.assertEqual(data["requests"][3]["status"], "Closed")
  1322. self.assertEqual(data["requests"][4]["status"], "Open")
  1323. self.assertEqual(data["requests"][5]["status"], "Open")
  1324. self.assertEqual(
  1325. data["requests"][0]["title"],
  1326. "merged pullrequest by user foo on repo test2",
  1327. )
  1328. self.assertEqual(
  1329. data["requests"][1]["title"],
  1330. "merged pullrequest by user foo on repo test",
  1331. )
  1332. self.assertEqual(
  1333. data["requests"][2]["title"],
  1334. "closed pullrequest by user foo on repo test2",
  1335. )
  1336. self.assertEqual(
  1337. data["requests"][3]["title"],
  1338. "closed pullrequest by user foo on repo test",
  1339. )
  1340. self.assertEqual(
  1341. data["requests"][4]["title"],
  1342. "open pullrequest by user foo on repo test2",
  1343. )
  1344. self.assertEqual(
  1345. data["requests"][5]["title"],
  1346. "open pullrequest by user foo on repo test",
  1347. )
  1348. self.assertEqual(data["args"]["status"], "all")
  1349. self.assertEqual(data["args"]["page"], 1)
  1350. # Test page 2 with the status parameter set to `all`.
  1351. output = self.app.get(
  1352. "/api/0/user/pingou/requests/actionable?status=all&page=2"
  1353. )
  1354. self.assertEqual(output.status_code, 200)
  1355. data = json.loads(output.get_data(as_text=True))
  1356. self.assertEqual(len(data["requests"]), 0)
  1357. self.assertEqual(
  1358. sorted(data.keys()),
  1359. ["args", "pagination", "requests", "total_requests"],
  1360. )
  1361. self.assertEqual(data["args"]["page"], 2)
  1362. @patch("pagure.lib.notify.send_email")
  1363. def test_api_view_user_requests_actionable_created(self, mockemail):
  1364. """ Test the api_view_user_requests_filed method of the flask user
  1365. api with the created parameter """
  1366. today = datetime.datetime.utcnow().date()
  1367. output = self.app.get(
  1368. "/api/0/user/pingou/requests/actionable?status=all&created=%s"
  1369. % (today.isoformat())
  1370. )
  1371. self.assertEqual(output.status_code, 200)
  1372. data = json.loads(output.get_data(as_text=True))
  1373. self.assertEqual(len(data["requests"]), 6)
  1374. yesterday = today - datetime.timedelta(days=1)
  1375. output = self.app.get(
  1376. "/api/0/user/pingou/requests/actionable?status=all&created=%s"
  1377. % (yesterday.isoformat())
  1378. )
  1379. self.assertEqual(output.status_code, 200)
  1380. data = json.loads(output.get_data(as_text=True))
  1381. self.assertEqual(len(data["requests"]), 6)
  1382. tomorrow = today + datetime.timedelta(days=1)
  1383. output = self.app.get(
  1384. "/api/0/user/pingou/requests/actionable?status=all&created=%s"
  1385. % (tomorrow.isoformat())
  1386. )
  1387. self.assertEqual(output.status_code, 200)
  1388. data = json.loads(output.get_data(as_text=True))
  1389. self.assertEqual(len(data["requests"]), 0)
  1390. output = self.app.get(
  1391. "/api/0/user/pingou/requests/actionable?status=all&created=..%s"
  1392. % (today.isoformat())
  1393. )
  1394. self.assertEqual(output.status_code, 200)
  1395. data = json.loads(output.get_data(as_text=True))
  1396. self.assertEqual(len(data["requests"]), 0)
  1397. output = self.app.get(
  1398. "/api/0/user/pingou/requests/actionable?status=all&created=..%s"
  1399. % (yesterday.isoformat())
  1400. )
  1401. self.assertEqual(output.status_code, 200)
  1402. data = json.loads(output.get_data(as_text=True))
  1403. self.assertEqual(len(data["requests"]), 0)
  1404. thedaybefore = today - datetime.timedelta(days=1)
  1405. output = self.app.get(
  1406. "/api/0/user/pingou/requests/actionable?status=all&created=..%s"
  1407. % (thedaybefore.isoformat())
  1408. )
  1409. self.assertEqual(output.status_code, 200)
  1410. data = json.loads(output.get_data(as_text=True))
  1411. self.assertEqual(len(data["requests"]), 0)
  1412. output = self.app.get(
  1413. "/api/0/user/pingou/requests/actionable?status=all&created=..%s"
  1414. % (tomorrow.isoformat())
  1415. )
  1416. self.assertEqual(output.status_code, 200)
  1417. data = json.loads(output.get_data(as_text=True))
  1418. self.assertEqual(len(data["requests"]), 6)
  1419. output = self.app.get(
  1420. "/api/0/user/pingou/requests/actionable?status=all&created=%s..%s"
  1421. % (thedaybefore.isoformat(), tomorrow.isoformat())
  1422. )
  1423. self.assertEqual(output.status_code, 200)
  1424. data = json.loads(output.get_data(as_text=True))
  1425. self.assertEqual(len(data["requests"]), 6)
  1426. @patch("pagure.lib.notify.send_email")
  1427. def test_api_view_user_requests_actionable_updated(self, mockemail):
  1428. """ Test the api_view_user_requests_filed method of the flask user
  1429. api with the created parameter """
  1430. today = datetime.datetime.utcnow().date()
  1431. output = self.app.get(
  1432. "/api/0/user/pingou/requests/actionable?status=all&updated=%s"
  1433. % (today.isoformat())
  1434. )
  1435. self.assertEqual(output.status_code, 200)
  1436. data = json.loads(output.get_data(as_text=True))
  1437. self.assertEqual(len(data["requests"]), 0)
  1438. yesterday = today - datetime.timedelta(days=1)
  1439. output = self.app.get(
  1440. "/api/0/user/pingou/requests/actionable?status=all&updated=%s"
  1441. % (yesterday.isoformat())
  1442. )
  1443. self.assertEqual(output.status_code, 200)
  1444. data = json.loads(output.get_data(as_text=True))
  1445. self.assertEqual(len(data["requests"]), 0)
  1446. tomorrow = today + datetime.timedelta(days=1)
  1447. output = self.app.get(
  1448. "/api/0/user/pingou/requests/actionable?status=all&updated=%s"
  1449. % (tomorrow.isoformat())
  1450. )
  1451. self.assertEqual(output.status_code, 200)
  1452. data = json.loads(output.get_data(as_text=True))
  1453. self.assertEqual(len(data["requests"]), 6)
  1454. @patch("pagure.lib.notify.send_email")
  1455. def test_api_view_user_requests_actionable_closed(self, mockemail):
  1456. """ Test the api_view_user_requests_filed method of the flask user
  1457. api with the created parameter """
  1458. today = datetime.datetime.utcnow().date()
  1459. output = self.app.get(
  1460. "/api/0/user/pingou/requests/actionable?status=all&closed=%s"
  1461. % (today.isoformat())
  1462. )
  1463. self.assertEqual(output.status_code, 200)
  1464. data = json.loads(output.get_data(as_text=True))
  1465. self.assertEqual(len(data["requests"]), 0)
  1466. yesterday = today - datetime.timedelta(days=1)
  1467. output = self.app.get(
  1468. "/api/0/user/pingou/requests/actionable?status=all&closed=%s"
  1469. % (yesterday.isoformat())
  1470. )
  1471. self.assertEqual(output.status_code, 200)
  1472. data = json.loads(output.get_data(as_text=True))
  1473. self.assertEqual(len(data["requests"]), 0)
  1474. tomorrow = today + datetime.timedelta(days=1)
  1475. output = self.app.get(
  1476. "/api/0/user/pingou/requests/actionable?status=all&closed=%s"
  1477. % (tomorrow.isoformat())
  1478. )
  1479. self.assertEqual(output.status_code, 200)
  1480. data = json.loads(output.get_data(as_text=True))
  1481. self.assertEqual(len(data["requests"]), 0)
  1482. class PagureFlaskApiUsertestissues(tests.Modeltests):
  1483. """ Tests for the user issues endpoints """
  1484. maxDiff = None
  1485. def setUp(self):
  1486. """ Set up the environnment, ran before every tests. """
  1487. super(PagureFlaskApiUsertestissues, self).setUp()
  1488. pagure.config.config["REQUESTS_FOLDER"] = None
  1489. tests.create_projects(self.session)
  1490. repo = pagure.lib.query._get_project(self.session, "test")
  1491. # Create issues to play with
  1492. msg = pagure.lib.query.new_issue(
  1493. session=self.session,
  1494. repo=repo,
  1495. title="Test issue",
  1496. content="We should work on this",
  1497. user="pingou",
  1498. )
  1499. self.session.commit()
  1500. self.assertEqual(msg.title, "Test issue")
  1501. def test_user_issues_empty(self):
  1502. """ Return the list of issues associated with the specified user. """
  1503. output = self.app.get("/api/0/user/foo/issues")
  1504. self.assertEqual(output.status_code, 200)
  1505. data = json.loads(output.get_data(as_text=True))
  1506. for k in ["pagination_issues_assigned", "pagination_issues_created"]:
  1507. for k2 in ["first", "last"]:
  1508. self.assertIsNotNone(data[k][k2])
  1509. data[k][k2] = None
  1510. self.assertEqual(
  1511. data,
  1512. {
  1513. "args": {
  1514. "assignee": True,
  1515. "author": True,
  1516. "closed": None,
  1517. "created": None,
  1518. "milestones": [],
  1519. "no_stones": None,
  1520. "order": None,
  1521. "order_key": None,
  1522. "page": 1,
  1523. "since": None,
  1524. "status": None,
  1525. "tags": [],
  1526. "updated": None,
  1527. },
  1528. "issues_assigned": [],
  1529. "issues_created": [],
  1530. "pagination_issues_assigned": {
  1531. "first": None,
  1532. "last": None,
  1533. "next": None,
  1534. "page": 1,
  1535. "pages": 0,
  1536. "per_page": 20,
  1537. "prev": None,
  1538. },
  1539. "pagination_issues_created": {
  1540. "first": None,
  1541. "last": None,
  1542. "next": None,
  1543. "page": 1,
  1544. "pages": 0,
  1545. "per_page": 20,
  1546. "prev": None,
  1547. },
  1548. "total_issues_assigned": 0,
  1549. "total_issues_assigned_pages": 1,
  1550. "total_issues_created": 0,
  1551. "total_issues_created_pages": 1,
  1552. },
  1553. )
  1554. def test_user_issues(self):
  1555. """ Return the list of issues associated with the specified user. """
  1556. output = self.app.get("/api/0/user/pingou/issues")
  1557. self.assertEqual(output.status_code, 200)
  1558. data = json.loads(output.get_data(as_text=True))
  1559. issues = []
  1560. for issue in data["issues_created"]:
  1561. issue["date_created"] = "1513111778"
  1562. issue["last_updated"] = "1513111778"
  1563. issue["project"]["date_created"] = "1513111778"
  1564. issue["project"]["date_modified"] = "1513111778"
  1565. issues.append(issue)
  1566. data["issues_created"] = issues
  1567. for k in ["pagination_issues_assigned", "pagination_issues_created"]:
  1568. for k2 in ["first", "last"]:
  1569. self.assertIsNotNone(data[k][k2])
  1570. data[k][k2] = None
  1571. self.assertEqual(
  1572. data,
  1573. {
  1574. "args": {
  1575. "assignee": True,
  1576. "author": True,
  1577. "closed": None,
  1578. "created": None,
  1579. "milestones": [],
  1580. "no_stones": None,
  1581. "order": None,
  1582. "order_key": None,
  1583. "page": 1,
  1584. "since": None,
  1585. "status": None,
  1586. "tags": [],
  1587. "updated": None,
  1588. },
  1589. "issues_assigned": [],
  1590. "issues_created": [
  1591. {
  1592. "assignee": None,
  1593. "blocks": [],
  1594. "close_status": None,
  1595. "closed_at": None,
  1596. "closed_by": None,
  1597. "comments": [],
  1598. "content": "We should work on this",
  1599. "custom_fields": [],
  1600. "date_created": "1513111778",
  1601. "depends": [],
  1602. "id": 1,
  1603. "last_updated": "1513111778",
  1604. "milestone": None,
  1605. "priority": None,
  1606. "private": False,
  1607. "project": {
  1608. "access_groups": {
  1609. "admin": [],
  1610. "commit": [],
  1611. "ticket": [],
  1612. },
  1613. "access_users": {
  1614. "admin": [],
  1615. "commit": [],
  1616. "owner": ["pingou"],
  1617. "ticket": [],
  1618. },
  1619. "close_status": [
  1620. "Invalid",
  1621. "Insufficient data",
  1622. "Fixed",
  1623. "Duplicate",
  1624. ],
  1625. "custom_keys": [],
  1626. "date_created": "1513111778",
  1627. "date_modified": "1513111778",
  1628. "description": "test project #1",
  1629. "fullname": "test",
  1630. "id": 1,
  1631. "milestones": {},
  1632. "name": "test",
  1633. "namespace": None,
  1634. "parent": None,
  1635. "priorities": {},
  1636. "tags": [],
  1637. "url_path": "test",
  1638. "user": {
  1639. "fullname": "PY C",
  1640. "name": "pingou",
  1641. "url_path": "user/pingou",
  1642. },
  1643. },
  1644. "related_prs": [],
  1645. "status": "Open",
  1646. "tags": [],
  1647. "title": "Test issue",
  1648. "user": {
  1649. "fullname": "PY C",
  1650. "name": "pingou",
  1651. "url_path": "user/pingou",
  1652. },
  1653. }
  1654. ],
  1655. "pagination_issues_assigned": {
  1656. "first": None,
  1657. "last": None,
  1658. "next": None,
  1659. "page": 1,
  1660. "pages": 0,
  1661. "per_page": 20,
  1662. "prev": None,
  1663. },
  1664. "pagination_issues_created": {
  1665. "first": None,
  1666. "last": None,
  1667. "next": None,
  1668. "page": 1,
  1669. "pages": 1,
  1670. "per_page": 20,
  1671. "prev": None,
  1672. },
  1673. "total_issues_assigned": 0,
  1674. "total_issues_assigned_pages": 1,
  1675. "total_issues_created": 1,
  1676. "total_issues_created_pages": 1,
  1677. },
  1678. )
  1679. def test_user_issues_created(self):
  1680. """ Return the list of issues associated with the specified user
  1681. and play with the created filter. """
  1682. today = datetime.datetime.utcnow().date()
  1683. output = self.app.get(
  1684. "/api/0/user/pingou/issues?created=%s" % (today.isoformat())
  1685. )
  1686. self.assertEqual(output.status_code, 200)
  1687. data = json.loads(output.get_data(as_text=True))
  1688. self.assertEqual(data["total_issues_assigned"], 0)
  1689. self.assertEqual(data["total_issues_created"], 1)
  1690. yesterday = today - datetime.timedelta(days=1)
  1691. output = self.app.get(
  1692. "/api/0/user/pingou/issues?created=%s" % (yesterday.isoformat())
  1693. )
  1694. self.assertEqual(output.status_code, 200)
  1695. data = json.loads(output.get_data(as_text=True))
  1696. self.assertEqual(data["total_issues_assigned"], 0)
  1697. self.assertEqual(data["total_issues_created"], 1)
  1698. tomorrow = today + datetime.timedelta(days=1)
  1699. output = self.app.get(
  1700. "/api/0/user/pingou/issues?created=%s" % (tomorrow.isoformat())
  1701. )
  1702. self.assertEqual(output.status_code, 200)
  1703. data = json.loads(output.get_data(as_text=True))
  1704. self.assertEqual(data["total_issues_assigned"], 0)
  1705. self.assertEqual(data["total_issues_created"], 0)
  1706. output = self.app.get(
  1707. "/api/0/user/pingou/issues?created=..%s" % (yesterday.isoformat())
  1708. )
  1709. self.assertEqual(output.status_code, 200)
  1710. data = json.loads(output.get_data(as_text=True))
  1711. self.assertEqual(data["total_issues_assigned"], 0)
  1712. self.assertEqual(data["total_issues_created"], 0)
  1713. output = self.app.get(
  1714. "/api/0/user/pingou/issues?created=%s..%s"
  1715. % (yesterday.isoformat(), today.isoformat())
  1716. )
  1717. self.assertEqual(output.status_code, 200)
  1718. data = json.loads(output.get_data(as_text=True))
  1719. self.assertEqual(data["total_issues_assigned"], 0)
  1720. self.assertEqual(data["total_issues_created"], 0)
  1721. output = self.app.get(
  1722. "/api/0/user/pingou/issues?created=%s..%s"
  1723. % (yesterday.isoformat(), tomorrow.isoformat())
  1724. )
  1725. self.assertEqual(output.status_code, 200)
  1726. data = json.loads(output.get_data(as_text=True))
  1727. self.assertEqual(data["total_issues_assigned"], 0)
  1728. self.assertEqual(data["total_issues_created"], 1)
  1729. if __name__ == "__main__":
  1730. unittest.main(verbosity=2)