test_pagure_flask_api_user.py 72 KB

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