test_pagure_flask_api_user.py 70 KB

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