1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891 |
- # -*- coding: utf-8 -*-
- """
- (c) 2015-2018 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- """
- from __future__ import unicode_literals, absolute_import
- import datetime
- import os
- import pytz
- import shutil
- import sys
- import unittest
- import json
- from mock import patch
- sys.path.insert(
- 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
- )
- import pagure.api
- import pagure.config
- import pagure.lib.model as model
- import pagure.lib.query
- import tests
- class PagureFlaskApiUSertests(tests.Modeltests):
- """ Tests for the flask API of pagure for issue """
- maxDiff = None
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureFlaskApiUSertests, self).setUp()
- pagure.config.config["REQUESTS_FOLDER"] = None
- def test_api_users(self):
- """ Test the api_users function. """
- output = self.app.get("/api/0/users")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(sorted(data["users"]), ["foo", "pingou"])
- self.assertEqual(
- sorted(data.keys()), ["mention", "total_users", "users"]
- )
- self.assertEqual(data["total_users"], 2)
- output = self.app.get("/api/0/users?pattern=p")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(data["users"], ["pingou"])
- self.assertEqual(
- sorted(data.keys()), ["mention", "total_users", "users"]
- )
- self.assertEqual(data["total_users"], 1)
- def test_api_view_user(self):
- """
- Test the api_view_user method of the flask api
- The tested user has no project or forks.
- """
- output = self.app.get("/api/0/user/pingou")
- self.assertEqual(output.status_code, 200)
- exp = {
- "forks": [],
- "forks_pagination": {
- "first": "http://localhost...",
- "last": "http://localhost...",
- "next": None,
- "forkpage": 1,
- "pages": 0,
- "per_page": 20,
- "prev": None,
- },
- "repos": [],
- "repos_pagination": {
- "first": "http://localhost...",
- "last": "http://localhost...",
- "next": None,
- "repopage": 1,
- "pages": 0,
- "per_page": 20,
- "prev": None,
- },
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- "avatar_url": "https://seccdn.libravatar.org/avatar/...",
- },
- }
- data = json.loads(output.get_data(as_text=True))
- data["user"]["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
- for k in ["forks_pagination", "repos_pagination"]:
- for k2 in ["first", "last"]:
- self.assertIsNotNone(data[k][k2])
- data[k][k2] = "http://localhost..."
- self.assertEqual(data, exp)
- def test_api_view_user_with_project(self):
- """
- Test the api_view_user method of the flask api,
- this time the user has some project defined.
- """
- tests.create_projects(self.session)
- output = self.app.get("/api/0/user/pingou")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- data["repos"][0]["date_created"] = "1490272832"
- data["repos"][0]["date_modified"] = "1490272832"
- data["repos"][1]["date_created"] = "1490272832"
- data["repos"][1]["date_modified"] = "1490272832"
- data["repos"][2]["date_created"] = "1490272832"
- data["repos"][2]["date_modified"] = "1490272832"
- for k in ["forks_pagination", "repos_pagination"]:
- for k2 in ["first", "last"]:
- self.assertIsNotNone(data[k][k2])
- data[k][k2] = "http://localhost..."
- expected_data = {
- "forks": [],
- "forks_pagination": {
- "first": "http://localhost...",
- "last": "http://localhost...",
- "next": None,
- "forkpage": 1,
- "pages": 0,
- "per_page": 20,
- "prev": None,
- },
- "repos": [
- {
- "access_groups": {"admin": [], "commit": [], "ticket": []},
- "access_users": {
- "admin": [],
- "commit": [],
- "owner": ["pingou"],
- "ticket": [],
- },
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "custom_keys": [],
- "date_created": "1490272832",
- "date_modified": "1490272832",
- "description": "test project #1",
- "fullname": "test",
- "url_path": "test",
- "id": 1,
- "milestones": {},
- "name": "test",
- "namespace": None,
- "parent": None,
- "priorities": {},
- "tags": [],
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- {
- "access_groups": {"admin": [], "commit": [], "ticket": []},
- "access_users": {
- "admin": [],
- "commit": [],
- "owner": ["pingou"],
- "ticket": [],
- },
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "custom_keys": [],
- "date_created": "1490272832",
- "date_modified": "1490272832",
- "description": "test project #2",
- "fullname": "test2",
- "url_path": "test2",
- "id": 2,
- "milestones": {},
- "name": "test2",
- "namespace": None,
- "parent": None,
- "priorities": {},
- "tags": [],
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- {
- "access_groups": {"admin": [], "commit": [], "ticket": []},
- "access_users": {
- "admin": [],
- "commit": [],
- "owner": ["pingou"],
- "ticket": [],
- },
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "custom_keys": [],
- "date_created": "1490272832",
- "date_modified": "1490272832",
- "description": "namespaced test project",
- "fullname": "somenamespace/test3",
- "url_path": "somenamespace/test3",
- "id": 3,
- "milestones": {},
- "name": "test3",
- "namespace": "somenamespace",
- "parent": None,
- "priorities": {},
- "tags": [],
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- ],
- "repos_pagination": {
- "first": "http://localhost...",
- "last": "http://localhost...",
- "next": None,
- "repopage": 1,
- "pages": 1,
- "per_page": 20,
- "prev": None,
- },
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- "avatar_url": "https://seccdn.libravatar.org/avatar/...",
- },
- }
- data["user"]["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
- self.assertEqual(data, expected_data)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_activity_stats(self, mockemail):
- """ Test the api_view_user_activity_stats method of the flask user
- api. """
- mockemail.return_value = True
- tests.create_projects(self.session)
- tests.create_tokens(self.session)
- tests.create_tokens_acl(self.session)
- headers = {"Authorization": "token aaabbbcccddd"}
- # Create a pull-request
- repo = pagure.lib.query._get_project(self.session, "test")
- forked_repo = pagure.lib.query._get_project(self.session, "test")
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="test pull-request",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, "test pull-request")
- # Check comments before
- self.session.commit()
- request = pagure.lib.query.search_pull_requests(
- self.session, project_id=1, requestid=1
- )
- self.assertEqual(len(request.comments), 0)
- data = {"comment": "This is a very interesting question"}
- # Valid request
- output = self.app.post(
- "/api/0/test/pull-request/1/comment", data=data, headers=headers
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, {"message": "Comment added"})
- # One comment added
- self.session.commit()
- request = pagure.lib.query.search_pull_requests(
- self.session, project_id=1, requestid=1
- )
- self.assertEqual(len(request.comments), 1)
- # Close PR
- output = self.app.post(
- "/api/0/test/pull-request/1/close", headers=headers
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, {"message": "Pull-request closed!"})
- # PR closed
- self.session.commit()
- request = pagure.lib.query.search_pull_requests(
- self.session, project_id=1, requestid=1
- )
- self.assertEqual(request.status, "Closed")
- # Finally retrieve the user's logs
- output = self.app.get("/api/0/user/pingou/activity/stats")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- date = datetime.datetime.utcnow().date().strftime("%Y-%m-%d")
- self.assertDictEqual(data, {date: 4})
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_activity_date(self, mockemail):
- """ Test the api_view_user_activity_date method of the flask user
- api. """
- self.test_api_view_user_activity_stats()
- # Invalid date
- output = self.app.get("/api/0/user/pingou/activity/AABB")
- self.assertEqual(output.status_code, 400)
- # Invalid date, arrow >= 0.15 throws an exception,
- # previous versions parsed it
- output = self.app.get("/api/0/user/pingou/activity/2016asd")
- arrow_version = self.get_arrow_version()
- if arrow_version >= (0, 15, 6):
- self.assertEqual(output.status_code, 400)
- exp = {
- "error": "Could not match input '2016asd' to any of the following formats: "
- "YYYY-MM-DD, YYYY-M-DD, YYYY-M-D, YYYY/MM/DD, YYYY/M/DD, YYYY/M/D, "
- "YYYY.MM.DD, YYYY.M.DD, YYYY.M.D, YYYYMMDD, YYYY-DDDD, YYYYDDDD, "
- "YYYY-MM, YYYY/MM, YYYY.MM, YYYY, W",
- "error_code": "ENOCODE",
- }
- self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
- elif arrow_version >= (0, 15):
- self.assertEqual(output.status_code, 400)
- exp = {
- "error": "Could not match input '2016asd' to any of the following formats: "
- "YYYY-MM-DD, YYYY-M-DD, YYYY-M-D, YYYY/MM/DD, YYYY/M/DD, YYYY/M/D, "
- "YYYY.MM.DD, YYYY.M.DD, YYYY.M.D, YYYYMMDD, YYYY-DDDD, YYYYDDDD, "
- "YYYY-MM, YYYY/MM, YYYY.MM, YYYY",
- "error_code": "ENOCODE",
- }
- self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
- else:
- self.assertEqual(output.status_code, 200)
- exp = {"activities": [], "date": "2016-01-01"}
- self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
- # Invalid date, arrow => throws an exception,
- # previous versions parsed it just not really as expected
- output = self.app.get("/api/0/user/pingou/activity/20161245")
- if self.get_arrow_version() >= (0, 15):
- self.assertEqual(output.status_code, 400)
- exp = {
- "error": "day is out of range for month",
- "error_code": "ENOCODE",
- }
- self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
- else:
- self.assertEqual(output.status_code, 200)
- exp = {"activities": [], "date": "1970-08-22"}
- self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
- date = datetime.datetime.utcnow().date().strftime("%Y-%m-%d")
- # Retrieve the user's logs for today
- output = self.app.get("/api/0/user/pingou/activity/%s" % date)
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- exp = {
- "activities": [
- {
- "date": date,
- "date_created": "1477558752",
- "type": "pull-request",
- "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>',
- "id": 1,
- "ref_id": "1",
- "type": "created",
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- {
- "date": date,
- "date_created": "1477558752",
- "type": "pull-request",
- "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>',
- "id": 2,
- "ref_id": "1",
- "type": "commented",
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- {
- "date": date,
- "date_created": "1477558752",
- "type": "pull-request",
- "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>',
- "id": 3,
- "ref_id": "1",
- "type": "closed",
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- {
- "date": date,
- "date_created": "1477558752",
- "type": "pull-request",
- "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>',
- "id": 4,
- "ref_id": "1",
- "type": "commented",
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- ],
- "date": date,
- }
- for idx, act in enumerate(data["activities"]):
- act["date_created"] = "1477558752"
- data["activities"][idx] = act
- self.assertEqual(data, exp)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_activity_date_1_activity(self, mockemail):
- """ Test the api_view_user_activity_date method of the flask user
- api when the user only did one action. """
- tests.create_projects(self.session)
- repo = pagure.lib.query._get_project(self.session, "test")
- now = datetime.datetime.utcnow()
- date = now.date().strftime("%Y-%m-%d")
- # Create a single commit log
- log = model.PagureLog(
- user_id=1,
- user_email="foo@bar.com",
- project_id=1,
- log_type="committed",
- ref_id="githash",
- date=now.date(),
- date_created=now,
- )
- self.session.add(log)
- self.session.commit()
- # Retrieve the user's logs for today
- output = self.app.get(
- "/api/0/user/pingou/activity/%s?grouped=1" % date
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- exp = {
- "activities": [
- {
- "description_mk": '<div class="markdown"><p>pingou committed on test#githash</p></div>'
- }
- ],
- "date": date,
- }
- self.assertEqual(data, exp)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_activity_timezone_negative(self, mockemail):
- """Test api_view_user_activity{_stats,_date} with the America/
- New York timezone, which is 5 hours behind UTC in winter and
- 4 hours behind UTC in summer (daylight savings). The events
- will occur on XXXX-02-15 in UTC, but on XXXX-02-14 local.
- """
- tests.create_projects(self.session)
- today = datetime.datetime.utcnow()
- # stats/activty returns data for now()-1year time range, so alter
- # year as needed to select 15th feb of current or previous year
- if today < datetime.datetime(today.year, 2, 15, 3, 30):
- year = today.year - 1
- else:
- year = today.year
- dateobj = datetime.datetime(year, 2, 15, 3, 30)
- utcdate = "%s-02-15" % year
- # the Unix timestamp for YYYY-02-15 12:00 UTC
- utcts = str(
- int(
- (
- datetime.datetime(year, 2, 15, 12, 0, tzinfo=pytz.UTC)
- - datetime.datetime(1970, 1, 1, tzinfo=pytz.UTC)
- ).total_seconds()
- )
- )
- localdate = "%s-02-14" % year
- # the Unix timestamp for YYYY-02-15 18:00 America/New_York
- localts = str(
- int(
- (
- datetime.datetime(
- year,
- 2,
- 14,
- 17,
- 0,
- tzinfo=pytz.timezone("America/New_York"),
- )
- - datetime.datetime(
- 1970, 1, 1, tzinfo=pytz.timezone("America/New_York")
- )
- ).total_seconds()
- )
- )
- # Create a single commit log
- log = model.PagureLog(
- user_id=1,
- user_email="foo@bar.com",
- project_id=1,
- log_type="committed",
- ref_id="githash",
- date=dateobj.date(),
- date_created=dateobj,
- )
- self.session.add(log)
- self.session.commit()
- # Retrieve the user's stats with no timezone specified (==UTC)
- output = self.app.get("/api/0/user/pingou/activity/stats")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- # date in output should be UTC date
- self.assertDictEqual(data, {utcdate: 1})
- # Now in timestamp format...
- output = self.app.get(
- "/api/0/user/pingou/activity/stats?format=timestamp"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- # timestamp in output should be UTC ts
- self.assertDictEqual(data, {utcts: 1})
- # Retrieve the user's stats with local timezone specified
- output = self.app.get(
- "/api/0/user/pingou/activity/stats?tz=America/New_York"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- # date in output should be local date
- self.assertDictEqual(data, {localdate: 1})
- # Now in timestamp format...
- output = self.app.get(
- "/api/0/user/pingou/activity/stats?format=timestamp&tz=America/New_York"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- # timestamp in output should be local ts
- self.assertDictEqual(data, {localts: 1})
- # Retrieve the user's logs for 2018-02-15 with no timezone
- output = self.app.get(
- "/api/0/user/pingou/activity/%s?grouped=1" % utcdate
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- exp = {
- "activities": [
- {
- "description_mk": '<div class="markdown"><p>pingou committed on test#githash</p></div>'
- }
- ],
- "date": utcdate,
- }
- self.assertEqual(data, exp)
- # Now retrieve the user's logs for 2018-02-14 with local time
- output = self.app.get(
- "/api/0/user/pingou/activity/%s?grouped=1&tz=America/New_York"
- % localdate
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- exp["date"] = localdate
- self.assertEqual(data, exp)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_activity_timezone_positive(self, mockemail):
- """Test api_view_user_activity{_stats,_date} with the Asia/
- Dubai timezone, which is 4 hours ahead of UTC. The events will
- occur on XXXX-02-15 in UTC, but on XXXX-02-16 in local time.
- """
- tests.create_projects(self.session)
- today = datetime.datetime.utcnow()
- # stats/activty returns data for now()-1year time range, so alter
- # year as needed to select 15th feb of current or previous year
- if today < datetime.datetime(today.year, 2, 15, 3, 30):
- year = today.year - 1
- else:
- year = today.year
- dateobj = datetime.datetime(year, 2, 15, 22, 30)
- utcdate = "%s-02-15" % year
- # the Unix timestamp for YYYY-02-15 12:00 UTC
- utcts = str(
- int(
- (
- datetime.datetime(year, 2, 15, 12, 0, tzinfo=pytz.UTC)
- - datetime.datetime(1970, 1, 1, tzinfo=pytz.UTC)
- ).total_seconds()
- )
- )
- localdate = "%s-02-16" % year
- # the Unix timestamp for YYYY-02-16 9:00 Asia/Dubai
- localts = str(
- int(
- (
- datetime.datetime(
- year, 2, 16, 8, 0, tzinfo=pytz.timezone("Asia/Dubai")
- )
- - datetime.datetime(
- 1970, 1, 1, tzinfo=pytz.timezone("Asia/Dubai")
- )
- ).total_seconds()
- )
- )
- # Create a single commit log
- log = model.PagureLog(
- user_id=1,
- user_email="foo@bar.com",
- project_id=1,
- log_type="committed",
- ref_id="githash",
- date=dateobj.date(),
- date_created=dateobj,
- )
- self.session.add(log)
- self.session.commit()
- # Retrieve the user's stats with no timezone specified (==UTC)
- output = self.app.get("/api/0/user/pingou/activity/stats")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- # date in output should be UTC date
- self.assertDictEqual(data, {utcdate: 1})
- # Now in timestamp format...
- output = self.app.get(
- "/api/0/user/pingou/activity/stats?format=timestamp"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- # timestamp in output should be UTC ts
- self.assertDictEqual(data, {utcts: 1})
- # Retrieve the user's stats with local timezone specified
- output = self.app.get(
- "/api/0/user/pingou/activity/stats?tz=Asia/Dubai"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- # date in output should be local date
- self.assertDictEqual(data, {localdate: 1})
- # Now in timestamp format...
- output = self.app.get(
- "/api/0/user/pingou/activity/stats?format=timestamp&tz=Asia/Dubai"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- # timestamp in output should be local ts
- self.assertDictEqual(data, {localts: 1})
- # Retrieve the user's logs for 2018-02-15 with no timezone
- output = self.app.get(
- "/api/0/user/pingou/activity/%s?grouped=1" % utcdate
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- exp = {
- "activities": [
- {
- "description_mk": '<div class="markdown"><p>pingou committed on test#githash</p></div>'
- }
- ],
- "date": utcdate,
- }
- self.assertEqual(data, exp)
- # Now retrieve the user's logs for 2018-02-16 with local time
- output = self.app.get(
- "/api/0/user/pingou/activity/%s?grouped=1&tz=Asia/Dubai"
- % localdate
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- exp["date"] = localdate
- self.assertEqual(data, exp)
- class PagureFlaskApiUsertestrequests(tests.Modeltests):
- """ Tests for the user requests endpoints """
- maxDiff = None
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureFlaskApiUsertestrequests, self).setUp()
- pagure.config.config["REQUESTS_FOLDER"] = None
- tests.create_projects(self.session)
- # Create few pull-requests
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="open pullrequest by user foo on repo test",
- user="foo",
- )
- repo = pagure.lib.query.get_authorized_project(self.session, "test2")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test2"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="open pullrequest by user foo on repo test2",
- user="foo",
- )
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="closed pullrequest by user foo on repo test",
- user="foo",
- status="Closed",
- )
- repo = pagure.lib.query.get_authorized_project(self.session, "test2")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test2"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="closed pullrequest by user foo on repo test2",
- user="foo",
- status="Closed",
- )
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="merged pullrequest by user foo on repo test",
- user="foo",
- status="Merged",
- )
- repo = pagure.lib.query.get_authorized_project(self.session, "test2")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test2"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="merged pullrequest by user foo on repo test2",
- user="foo",
- status="Merged",
- )
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="open pullrequest by user pingou on repo test",
- user="pingou",
- )
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(self.session, "test2")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test2"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="open pullrequest by user pingou on repo test2",
- user="pingou",
- )
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="closed pullrequest by user pingou on repo test",
- user="pingou",
- status="Closed",
- )
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(self.session, "test2")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test2"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="closed pullrequest by user pingou on repo test2",
- user="pingou",
- status="Closed",
- )
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="merged pullrequest by user pingou on repo test",
- user="pingou",
- status="Merged",
- )
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(self.session, "test2")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test2"
- )
- pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="merged pullrequest by user pingou on repo test2",
- user="pingou",
- status="Merged",
- )
- self.session.commit()
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_requests_filed(self, mockemail):
- """ Test the api_view_user_requests_filed method of the flask user
- api """
- # First we test without the status parameter. It should default to `open`
- output = self.app.get("/api/0/user/pingou/requests/filed")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 2)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["requests"][0]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][1]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][0]["status"], "Open")
- self.assertEqual(data["requests"][1]["status"], "Open")
- self.assertEqual(
- data["requests"][0]["title"],
- "open pullrequest by user pingou on repo test2",
- )
- self.assertEqual(
- data["requests"][1]["title"],
- "open pullrequest by user pingou on repo test",
- )
- self.assertEqual(data["args"]["status"], "open")
- self.assertEqual(data["args"]["page"], 1)
- # Next test with the status parameter set to `open`.
- output = self.app.get("/api/0/user/pingou/requests/filed?status=open")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 2)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["requests"][0]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][1]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][0]["status"], "Open")
- self.assertEqual(data["requests"][1]["status"], "Open")
- self.assertEqual(
- data["requests"][0]["title"],
- "open pullrequest by user pingou on repo test2",
- )
- self.assertEqual(
- data["requests"][1]["title"],
- "open pullrequest by user pingou on repo test",
- )
- self.assertEqual(data["args"]["status"], "open")
- self.assertEqual(data["args"]["page"], 1)
- # Next test with the status parameter set to `closed`.
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=closed"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 2)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["requests"][0]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][1]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][0]["status"], "Closed")
- self.assertEqual(data["requests"][1]["status"], "Closed")
- self.assertEqual(
- data["requests"][0]["title"],
- "closed pullrequest by user pingou on repo test2",
- )
- self.assertEqual(
- data["requests"][1]["title"],
- "closed pullrequest by user pingou on repo test",
- )
- self.assertEqual(data["args"]["status"], "closed")
- self.assertEqual(data["args"]["page"], 1)
- # Next test with the status parameter set to `merged`.
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=merged"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 2)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["requests"][0]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][1]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][0]["status"], "Merged")
- self.assertEqual(data["requests"][1]["status"], "Merged")
- self.assertEqual(
- data["requests"][0]["title"],
- "merged pullrequest by user pingou on repo test2",
- )
- self.assertEqual(
- data["requests"][1]["title"],
- "merged pullrequest by user pingou on repo test",
- )
- self.assertEqual(data["args"]["status"], "merged")
- self.assertEqual(data["args"]["page"], 1)
- # Finally, test with the status parameter set to `all`.
- output = self.app.get("/api/0/user/pingou/requests/filed?status=all")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["requests"][0]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][1]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][2]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][3]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][4]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][5]["user"]["name"], "pingou")
- self.assertEqual(data["requests"][0]["status"], "Merged")
- self.assertEqual(data["requests"][1]["status"], "Merged")
- self.assertEqual(data["requests"][2]["status"], "Closed")
- self.assertEqual(data["requests"][3]["status"], "Closed")
- self.assertEqual(data["requests"][4]["status"], "Open")
- self.assertEqual(data["requests"][5]["status"], "Open")
- self.assertEqual(
- data["requests"][0]["title"],
- "merged pullrequest by user pingou on repo test2",
- )
- self.assertEqual(
- data["requests"][1]["title"],
- "merged pullrequest by user pingou on repo test",
- )
- self.assertEqual(
- data["requests"][2]["title"],
- "closed pullrequest by user pingou on repo test2",
- )
- self.assertEqual(
- data["requests"][3]["title"],
- "closed pullrequest by user pingou on repo test",
- )
- self.assertEqual(
- data["requests"][4]["title"],
- "open pullrequest by user pingou on repo test2",
- )
- self.assertEqual(
- data["requests"][5]["title"],
- "open pullrequest by user pingou on repo test",
- )
- self.assertEqual(data["args"]["status"], "all")
- self.assertEqual(data["args"]["page"], 1)
- # Test page 2 with the status parameter set to `all`.
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&page=2"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["args"]["page"], 2)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_requests_filed_created(self, mockemail):
- """ Test the api_view_user_requests_filed method of the flask user
- api with the created parameter """
- today = datetime.datetime.utcnow().date()
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&created=%s"
- % (today.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- yesterday = today - datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&created=%s"
- % (yesterday.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- tomorrow = today + datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&created=%s"
- % (tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&created=..%s"
- % (today.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&created=..%s"
- % (yesterday.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- thedaybefore = today - datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&created=..%s"
- % (thedaybefore.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&created=..%s"
- % (tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&created=%s..%s"
- % (thedaybefore.isoformat(), tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_requests_filed_updated(self, mockemail):
- """ Test the api_view_user_requests_filed method of the flask user
- api with the created parameter """
- today = datetime.datetime.utcnow().date()
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&updated=%s"
- % (today.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- yesterday = today - datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&updated=%s"
- % (yesterday.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- tomorrow = today + datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&updated=%s"
- % (tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_requests_filed_closed(self, mockemail):
- """ Test the api_view_user_requests_filed method of the flask user
- api with the created parameter """
- today = datetime.datetime.utcnow().date()
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&closed=%s"
- % (today.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- yesterday = today - datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&closed=%s"
- % (yesterday.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- tomorrow = today + datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/filed?status=all&closed=%s"
- % (tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_requests_filed_foo(self, mockemail):
- """ Test the api_view_user_requests_filed method of the flask user
- api """
- # Default data returned
- output = self.app.get(
- "/api/0/user/foo/requests/filed?status=all&per_page=6"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- # There are 6 PRs, that's 1 page at 6 results per page
- self.assertEqual(data["pagination"]["pages"], 1)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_requests_filed_foo_grp_access(self, mockemail):
- """ Test when the user has accessed to some PRs via a group. """
- # Add the user to a group
- msg = pagure.lib.query.add_group(
- self.session,
- group_name="some_group",
- display_name="Some Group",
- description=None,
- group_type="bar",
- user="pingou",
- is_admin=False,
- blacklist=[],
- )
- self.session.commit()
- # Add the group to the project `test2`
- project = pagure.lib.query._get_project(self.session, "test2")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="some_group",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- # Add foo to the group
- group = pagure.lib.query.search_groups(
- self.session, group_name="some_group"
- )
- result = pagure.lib.query.add_user_to_group(
- self.session, "foo", group, "pingou", True
- )
- self.session.commit()
- self.assertEqual(result, "User `foo` added to the group `some_group`.")
- # Query the API for foo's filed PRs
- output = self.app.get(
- "/api/0/user/foo/requests/filed?status=all&per_page=6"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- # There are 6 PRs, that's 1 page at 6 results per page
- self.assertEqual(data["pagination"]["pages"], 1)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_requests_actionable(self, mockemail):
- """ Test the api_view_user_requests_actionable method of the flask user
- api """
- # First we test without the status parameter. It should default to `open`
- output = self.app.get("/api/0/user/pingou/requests/actionable")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 2)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["requests"][0]["user"]["name"], "foo")
- self.assertEqual(data["requests"][1]["user"]["name"], "foo")
- self.assertEqual(data["requests"][0]["status"], "Open")
- self.assertEqual(data["requests"][1]["status"], "Open")
- self.assertEqual(
- data["requests"][0]["title"],
- "open pullrequest by user foo on repo test2",
- )
- self.assertEqual(
- data["requests"][1]["title"],
- "open pullrequest by user foo on repo test",
- )
- self.assertEqual(data["args"]["status"], "open")
- self.assertEqual(data["args"]["page"], 1)
- # Next test with the status parameter set to `open`.
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=open"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 2)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["requests"][0]["user"]["name"], "foo")
- self.assertEqual(data["requests"][1]["user"]["name"], "foo")
- self.assertEqual(data["requests"][0]["status"], "Open")
- self.assertEqual(data["requests"][1]["status"], "Open")
- self.assertEqual(
- data["requests"][0]["title"],
- "open pullrequest by user foo on repo test2",
- )
- self.assertEqual(
- data["requests"][1]["title"],
- "open pullrequest by user foo on repo test",
- )
- self.assertEqual(data["args"]["status"], "open")
- self.assertEqual(data["args"]["page"], 1)
- # Next test with the status parameter set to `closed`.
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=closed"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 2)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["requests"][0]["user"]["name"], "foo")
- self.assertEqual(data["requests"][1]["user"]["name"], "foo")
- self.assertEqual(data["requests"][0]["status"], "Closed")
- self.assertEqual(data["requests"][1]["status"], "Closed")
- self.assertEqual(
- data["requests"][0]["title"],
- "closed pullrequest by user foo on repo test2",
- )
- self.assertEqual(
- data["requests"][1]["title"],
- "closed pullrequest by user foo on repo test",
- )
- self.assertEqual(data["args"]["status"], "closed")
- self.assertEqual(data["args"]["page"], 1)
- # Next test with the status parameter set to `merged`.
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=merged"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 2)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["requests"][0]["user"]["name"], "foo")
- self.assertEqual(data["requests"][1]["user"]["name"], "foo")
- self.assertEqual(data["requests"][0]["status"], "Merged")
- self.assertEqual(data["requests"][1]["status"], "Merged")
- self.assertEqual(
- data["requests"][0]["title"],
- "merged pullrequest by user foo on repo test2",
- )
- self.assertEqual(
- data["requests"][1]["title"],
- "merged pullrequest by user foo on repo test",
- )
- self.assertEqual(data["args"]["status"], "merged")
- self.assertEqual(data["args"]["page"], 1)
- # Finally, test with the status parameter set to `all`.
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["requests"][0]["user"]["name"], "foo")
- self.assertEqual(data["requests"][1]["user"]["name"], "foo")
- self.assertEqual(data["requests"][2]["user"]["name"], "foo")
- self.assertEqual(data["requests"][3]["user"]["name"], "foo")
- self.assertEqual(data["requests"][4]["user"]["name"], "foo")
- self.assertEqual(data["requests"][5]["user"]["name"], "foo")
- self.assertEqual(data["requests"][0]["status"], "Merged")
- self.assertEqual(data["requests"][1]["status"], "Merged")
- self.assertEqual(data["requests"][2]["status"], "Closed")
- self.assertEqual(data["requests"][3]["status"], "Closed")
- self.assertEqual(data["requests"][4]["status"], "Open")
- self.assertEqual(data["requests"][5]["status"], "Open")
- self.assertEqual(
- data["requests"][0]["title"],
- "merged pullrequest by user foo on repo test2",
- )
- self.assertEqual(
- data["requests"][1]["title"],
- "merged pullrequest by user foo on repo test",
- )
- self.assertEqual(
- data["requests"][2]["title"],
- "closed pullrequest by user foo on repo test2",
- )
- self.assertEqual(
- data["requests"][3]["title"],
- "closed pullrequest by user foo on repo test",
- )
- self.assertEqual(
- data["requests"][4]["title"],
- "open pullrequest by user foo on repo test2",
- )
- self.assertEqual(
- data["requests"][5]["title"],
- "open pullrequest by user foo on repo test",
- )
- self.assertEqual(data["args"]["status"], "all")
- self.assertEqual(data["args"]["page"], 1)
- # Test page 2 with the status parameter set to `all`.
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&page=2"
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- self.assertEqual(
- sorted(data.keys()),
- ["args", "pagination", "requests", "total_requests"],
- )
- self.assertEqual(data["args"]["page"], 2)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_requests_actionable_created(self, mockemail):
- """ Test the api_view_user_requests_filed method of the flask user
- api with the created parameter """
- today = datetime.datetime.utcnow().date()
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&created=%s"
- % (today.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- yesterday = today - datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&created=%s"
- % (yesterday.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- tomorrow = today + datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&created=%s"
- % (tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&created=..%s"
- % (today.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&created=..%s"
- % (yesterday.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- thedaybefore = today - datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&created=..%s"
- % (thedaybefore.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&created=..%s"
- % (tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&created=%s..%s"
- % (thedaybefore.isoformat(), tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_requests_actionable_updated(self, mockemail):
- """ Test the api_view_user_requests_filed method of the flask user
- api with the created parameter """
- today = datetime.datetime.utcnow().date()
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&updated=%s"
- % (today.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- yesterday = today - datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&updated=%s"
- % (yesterday.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- tomorrow = today + datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&updated=%s"
- % (tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 6)
- @patch("pagure.lib.notify.send_email")
- def test_api_view_user_requests_actionable_closed(self, mockemail):
- """ Test the api_view_user_requests_filed method of the flask user
- api with the created parameter """
- today = datetime.datetime.utcnow().date()
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&closed=%s"
- % (today.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- yesterday = today - datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&closed=%s"
- % (yesterday.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- tomorrow = today + datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/requests/actionable?status=all&closed=%s"
- % (tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(len(data["requests"]), 0)
- class PagureFlaskApiUsertestissues(tests.Modeltests):
- """ Tests for the user issues endpoints """
- maxDiff = None
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureFlaskApiUsertestissues, self).setUp()
- pagure.config.config["REQUESTS_FOLDER"] = None
- tests.create_projects(self.session)
- repo = pagure.lib.query._get_project(self.session, "test")
- # Create issues to play with
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=repo,
- title="Test issue",
- content="We should work on this",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue")
- def test_user_issues_empty(self):
- """ Return the list of issues associated with the specified user. """
- output = self.app.get("/api/0/user/foo/issues")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- for k in ["pagination_issues_assigned", "pagination_issues_created"]:
- for k2 in ["first", "last"]:
- self.assertIsNotNone(data[k][k2])
- data[k][k2] = None
- self.assertEqual(
- data,
- {
- "args": {
- "assignee": True,
- "author": True,
- "closed": None,
- "created": None,
- "milestones": [],
- "no_stones": None,
- "order": None,
- "order_key": None,
- "page": 1,
- "since": None,
- "status": None,
- "tags": [],
- "updated": None,
- },
- "issues_assigned": [],
- "issues_created": [],
- "pagination_issues_assigned": {
- "first": None,
- "last": None,
- "next": None,
- "page": 1,
- "pages": 0,
- "per_page": 20,
- "prev": None,
- },
- "pagination_issues_created": {
- "first": None,
- "last": None,
- "next": None,
- "page": 1,
- "pages": 0,
- "per_page": 20,
- "prev": None,
- },
- "total_issues_assigned": 0,
- "total_issues_assigned_pages": 1,
- "total_issues_created": 0,
- "total_issues_created_pages": 1,
- },
- )
- def test_user_issues(self):
- """ Return the list of issues associated with the specified user. """
- output = self.app.get("/api/0/user/pingou/issues")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- issues = []
- for issue in data["issues_created"]:
- issue["date_created"] = "1513111778"
- issue["last_updated"] = "1513111778"
- issue["project"]["date_created"] = "1513111778"
- issue["project"]["date_modified"] = "1513111778"
- issues.append(issue)
- data["issues_created"] = issues
- for k in ["pagination_issues_assigned", "pagination_issues_created"]:
- for k2 in ["first", "last"]:
- self.assertIsNotNone(data[k][k2])
- data[k][k2] = None
- self.assertEqual(
- data,
- {
- "args": {
- "assignee": True,
- "author": True,
- "closed": None,
- "created": None,
- "milestones": [],
- "no_stones": None,
- "order": None,
- "order_key": None,
- "page": 1,
- "since": None,
- "status": None,
- "tags": [],
- "updated": None,
- },
- "issues_assigned": [],
- "issues_created": [
- {
- "assignee": None,
- "blocks": [],
- "close_status": None,
- "closed_at": None,
- "closed_by": None,
- "comments": [],
- "content": "We should work on this",
- "custom_fields": [],
- "date_created": "1513111778",
- "depends": [],
- "id": 1,
- "last_updated": "1513111778",
- "milestone": None,
- "priority": None,
- "private": False,
- "project": {
- "access_groups": {
- "admin": [],
- "commit": [],
- "ticket": [],
- },
- "access_users": {
- "admin": [],
- "commit": [],
- "owner": ["pingou"],
- "ticket": [],
- },
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "custom_keys": [],
- "date_created": "1513111778",
- "date_modified": "1513111778",
- "description": "test project #1",
- "fullname": "test",
- "id": 1,
- "milestones": {},
- "name": "test",
- "namespace": None,
- "parent": None,
- "priorities": {},
- "tags": [],
- "url_path": "test",
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- "related_prs": [],
- "status": "Open",
- "tags": [],
- "title": "Test issue",
- "user": {
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- }
- ],
- "pagination_issues_assigned": {
- "first": None,
- "last": None,
- "next": None,
- "page": 1,
- "pages": 0,
- "per_page": 20,
- "prev": None,
- },
- "pagination_issues_created": {
- "first": None,
- "last": None,
- "next": None,
- "page": 1,
- "pages": 1,
- "per_page": 20,
- "prev": None,
- },
- "total_issues_assigned": 0,
- "total_issues_assigned_pages": 1,
- "total_issues_created": 1,
- "total_issues_created_pages": 1,
- },
- )
- def test_user_issues_created(self):
- """ Return the list of issues associated with the specified user
- and play with the created filter. """
- today = datetime.datetime.utcnow().date()
- output = self.app.get(
- "/api/0/user/pingou/issues?created=%s" % (today.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(data["total_issues_assigned"], 0)
- self.assertEqual(data["total_issues_created"], 1)
- yesterday = today - datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/issues?created=%s" % (yesterday.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(data["total_issues_assigned"], 0)
- self.assertEqual(data["total_issues_created"], 1)
- tomorrow = today + datetime.timedelta(days=1)
- output = self.app.get(
- "/api/0/user/pingou/issues?created=%s" % (tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(data["total_issues_assigned"], 0)
- self.assertEqual(data["total_issues_created"], 0)
- output = self.app.get(
- "/api/0/user/pingou/issues?created=..%s" % (yesterday.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(data["total_issues_assigned"], 0)
- self.assertEqual(data["total_issues_created"], 0)
- output = self.app.get(
- "/api/0/user/pingou/issues?created=%s..%s"
- % (yesterday.isoformat(), today.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(data["total_issues_assigned"], 0)
- self.assertEqual(data["total_issues_created"], 0)
- output = self.app.get(
- "/api/0/user/pingou/issues?created=%s..%s"
- % (yesterday.isoformat(), tomorrow.isoformat())
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(data["total_issues_assigned"], 0)
- self.assertEqual(data["total_issues_created"], 1)
- if __name__ == "__main__":
- unittest.main(verbosity=2)
|