1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059 |
- # -*- coding: utf-8 -*-
- """
- (c) 2017-2018 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- """
- from __future__ import unicode_literals, absolute_import
- import datetime # noqa
- import os # noqa
- import platform # noqa
- import shutil # noqa
- import subprocess # noqa
- import sys # noqa
- import unittest # noqa
- import pygit2
- import munch # noqa
- from mock import patch, MagicMock # noqa
- from six import StringIO # noqa
- sys.path.insert(
- 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
- )
- import pagure.config # noqa
- import pagure.exceptions # noqa: E402
- import pagure.cli.admin # noqa
- import pagure.lib.model # noqa
- import tests # noqa
- class PagureAdminAdminTokenEmptytests(tests.Modeltests):
- """Tests for pagure-admin admin-token when there is nothing in the DB"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureAdminAdminTokenEmptytests, self).setUp()
- pagure.cli.admin.session = self.session
- def test_do_create_admin_token_no_user(self):
- """Test the do_create_admin_token function of pagure-admin without
- user.
- """
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- args = munch.Munch(
- {
- "user": "pingou",
- "expiration_date": exp_date.strftime("%Y-%m-%d"),
- }
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_create_admin_token(args)
- self.assertEqual(cm.exception.args[0], 'No user "pingou" found')
- def test_do_list_admin_token_empty(self):
- """Test the do_list_admin_token function of pagure-admin when there
- are not tokens in the db.
- """
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output, "No admin tokens found\n")
- class PagureAdminAdminTokentests(tests.Modeltests):
- """Tests for pagure-admin admin-token"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureAdminAdminTokentests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user="pingou",
- fullname="PY C",
- password="foo",
- default_email="bar@pingou.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- @patch("pagure.cli.admin._get_input")
- @patch("pagure.cli.admin._ask_confirmation")
- def test_do_create_admin_token(self, conf, rinp):
- """Test the do_create_admin_token function of pagure-admin."""
- conf.return_value = True
- rinp.return_value = "1,2,3"
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- args = munch.Munch(
- {
- "user": "pingou",
- "expiration_date": exp_date.strftime("%Y-%m-%d"),
- }
- )
- pagure.cli.admin.do_create_admin_token(args)
- # Check the outcome
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- @patch("pagure.cli.admin._get_input")
- @patch("pagure.cli.admin._ask_confirmation")
- def test_do_list_admin_token(self, conf, rinp):
- """Test the do_list_admin_token function of pagure-admin."""
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = "1,2,3"
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- args = munch.Munch(
- {
- "user": "pingou",
- "expiration_date": exp_date.strftime("%Y-%m-%d"),
- }
- )
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve all tokens
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- # Retrieve pfrields's tokens
- list_args = munch.Munch(
- {
- "user": "pfrields",
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output, "No admin tokens found\n")
- def test_do_list_admin_token_non_admin_acls(self):
- """Test the do_list_admin_token function of pagure-admin for a token
- without any admin ACL."""
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- pagure.lib.query.add_token_to_user(
- self.session,
- project=None,
- acls=["issue_assign", "pull_request_subscribe"],
- username="pingou",
- expiration_date=exp_date,
- )
- # Retrieve all admin tokens
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output, "No admin tokens found\n")
- # Retrieve all tokens
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": True,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- @patch("pagure.cli.admin._get_input")
- @patch("pagure.cli.admin._ask_confirmation")
- def test_do_info_admin_token(self, conf, rinp):
- """Test the do_info_admin_token function of pagure-admin."""
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = "2,4,5"
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- args = munch.Munch(
- {
- "user": "pingou",
- "expiration_date": exp_date.strftime("%Y-%m-%d"),
- }
- )
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- token = output.split(" ", 1)[0]
- args = munch.Munch({"token": token})
- with tests.capture_output() as output:
- pagure.cli.admin.do_info_admin_token(args)
- output = output.getvalue()
- self.assertIn(" -- pingou -- ", output.split("\n", 1)[0])
- self.assertEqual(
- output.split("\n", 1)[1],
- """ACLs:
- - issue_create
- - pull_request_comment
- - pull_request_flag
- """,
- )
- def test_do_info_admin_token_non_admin_acl(self):
- """Test the do_info_admin_token function of pagure-admin for a
- token not having any admin ACL."""
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- pagure.lib.query.add_token_to_user(
- self.session,
- project=None,
- acls=["issue_assign", "pull_request_subscribe"],
- username="pingou",
- expiration_date=exp_date,
- )
- # Retrieve the token
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": True,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- token = output.split(" ", 1)[0]
- args = munch.Munch({"token": token})
- with tests.capture_output() as output:
- pagure.cli.admin.do_info_admin_token(args)
- output = output.getvalue()
- self.assertIn(" -- pingou -- ", output.split("\n", 1)[0])
- self.assertEqual(
- output.split("\n", 1)[1],
- """ACLs:
- - issue_assign
- - pull_request_subscribe
- """,
- )
- @patch("pagure.cli.admin._get_input")
- @patch("pagure.cli.admin._ask_confirmation")
- def test_do_expire_admin_token(self, conf, rinp):
- """Test the do_expire_admin_token function of pagure-admin."""
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = "1,2,3"
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- args = munch.Munch(
- {
- "user": "pingou",
- "expiration_date": exp_date.strftime("%Y-%m-%d"),
- }
- )
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- token = output.split(" ", 1)[0]
- # Before
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": True,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, "No admin tokens found\n")
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- # Expire the token
- args = munch.Munch({"token": token, "all": False})
- pagure.cli.admin.do_expire_admin_token(args)
- # After
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": True,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output, "No admin tokens found\n")
- @patch("pagure.cli.admin._get_input")
- @patch("pagure.cli.admin._ask_confirmation")
- def test_do_expire_admin_token_non_admin_acls(self, conf, rinp):
- """Test the do_expire_admin_token function of pagure-admin for a token
- without any admin ACL."""
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = "1,2,3"
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- pagure.lib.query.add_token_to_user(
- self.session,
- project=None,
- acls=["issue_assign", "pull_request_subscribe"],
- username="pingou",
- expiration_date=exp_date,
- )
- # Retrieve all tokens to get the one of interest
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": True,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- token = output.split(" ", 1)[0]
- # Expire the token
- args = munch.Munch({"token": token, "all": True})
- pagure.cli.admin.do_expire_admin_token(args)
- # After
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": True,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output, "No admin tokens found\n")
- @patch("pagure.cli.admin._get_input")
- @patch("pagure.cli.admin._ask_confirmation")
- def test_do_update_admin_token_invalid_date(self, conf, rinp):
- """Test the do_update_admin_token function of pagure-admin with
- an invalid date."""
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = "1,2,3"
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- args = munch.Munch(
- {
- "user": "pingou",
- "expiration_date": exp_date.strftime("%Y-%m-%d"),
- }
- )
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- token = output.split(" ", 1)[0]
- current_expiration = output.split(" ", 1)[1]
- # Set the expiration date to the token
- args = munch.Munch({"token": token, "date": "aa-bb-cc", "all": False})
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.cli.admin.do_update_admin_token,
- args,
- )
- @patch("pagure.cli.admin._get_input")
- @patch("pagure.cli.admin._ask_confirmation")
- def test_do_update_admin_token_invalid_date2(self, conf, rinp):
- """Test the do_update_admin_token function of pagure-admin with
- an invalid date."""
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = "1,2,3"
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- args = munch.Munch(
- {
- "user": "pingou",
- "expiration_date": exp_date.strftime("%Y-%m-%d"),
- }
- )
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- token = output.split(" ", 1)[0]
- current_expiration = output.split(" ", 1)[1]
- # Set the expiration date to the token
- args = munch.Munch(
- {"token": token, "date": "2017-18-01", "all": False}
- )
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.cli.admin.do_update_admin_token,
- args,
- )
- @patch("pagure.cli.admin._get_input")
- @patch("pagure.cli.admin._ask_confirmation")
- def test_do_update_admin_token_invalid_date3(self, conf, rinp):
- """Test the do_update_admin_token function of pagure-admin with
- an invalid date (is today)."""
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = "1,2,3"
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- args = munch.Munch(
- {
- "user": "pingou",
- "expiration_date": exp_date.strftime("%Y-%m-%d"),
- }
- )
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- token = output.split(" ", 1)[0]
- current_expiration = output.split(" ", 1)[1]
- # Set the expiration date to the token
- args = munch.Munch(
- {
- "token": token,
- "date": datetime.datetime.utcnow().date(),
- "all": False,
- }
- )
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.cli.admin.do_update_admin_token,
- args,
- )
- @patch("pagure.cli.admin._get_input")
- @patch("pagure.cli.admin._ask_confirmation")
- def test_do_update_admin_token(self, conf, rinp):
- """Test the do_update_admin_token function of pagure-admin."""
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = "1,2,3"
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- args = munch.Munch(
- {
- "user": "pingou",
- "expiration_date": exp_date.strftime("%Y-%m-%d"),
- }
- )
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- token = output.split(" ", 1)[0]
- current_expiration = output.strip().split(" -- ", 2)[-1]
- # Before
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": True,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, "No admin tokens found\n")
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- deadline = datetime.datetime.utcnow().date() + datetime.timedelta(
- days=3
- )
- # Set the expiration date to the token
- args = munch.Munch(
- {
- "token": token,
- "date": deadline.strftime("%Y-%m-%d"),
- "all": False,
- }
- )
- pagure.cli.admin.do_update_admin_token(args)
- # After
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": True,
- "expired": False,
- "all": False,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output.split(" ", 1)[0], token)
- self.assertNotEqual(
- output.strip().split(" -- ", 2)[-1], current_expiration
- )
- @patch("pagure.cli.admin._get_input")
- @patch("pagure.cli.admin._ask_confirmation")
- def test_do_update_admin_token_non_admin_acls(self, conf, rinp):
- """Test the do_update_admin_token function of pagure-admin for a token
- without any admin ACL."""
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = "1,2,3"
- exp_date = datetime.date.today() + datetime.timedelta(days=300)
- pagure.lib.query.add_token_to_user(
- self.session,
- project=None,
- acls=["issue_assign", "pull_request_subscribe"],
- username="pingou",
- expiration_date=exp_date,
- )
- # Retrieve all tokens to get the one of interest
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": False,
- "expired": False,
- "all": True,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split("\n")), 2)
- self.assertIn(" -- pingou -- ", output)
- token = output.split(" ", 1)[0]
- current_expiration = output.strip().split(" -- ", 2)[-1]
- deadline = datetime.datetime.utcnow().date() + datetime.timedelta(
- days=3
- )
- # Set the expiration date to the token
- args = munch.Munch(
- {
- "token": token,
- "date": deadline.strftime("%Y-%m-%d"),
- "all": True,
- }
- )
- pagure.cli.admin.do_update_admin_token(args)
- # After
- list_args = munch.Munch(
- {
- "user": None,
- "token": None,
- "active": True,
- "expired": False,
- "all": True,
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output.split(" ", 1)[0], token)
- self.assertNotEqual(
- output.strip().split(" -- ", 2)[-1], current_expiration
- )
- class PagureAdminGetWatchTests(tests.Modeltests):
- """Tests for pagure-admin get-watch"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureAdminGetWatchTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user="pingou",
- fullname="PY C",
- password="foo",
- default_email="bar@pingou.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
- self.session.add(item)
- # Create the user foo
- item = pagure.lib.model.User(
- user="foo",
- fullname="foo B.",
- password="foob",
- default_email="foo@pingou.com",
- )
- self.session.add(item)
- # Create two projects for the user pingou
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test",
- description="namespaced test project",
- hook_token="aaabbbeee",
- namespace="somenamespace",
- )
- self.session.add(item)
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test",
- description="Test project",
- hook_token="aaabbbccc",
- namespace=None,
- )
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- def test_get_watch_get_project_unknown_project(self):
- """Test the get-watch function of pagure-admin with an unknown
- project.
- """
- args = munch.Munch({"project": "foobar", "user": "pingou"})
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_get_watch_status(args)
- self.assertEqual(
- cm.exception.args[0], "No project found with: project=foobar"
- )
- def test_get_watch_get_project_invalid_project(self):
- """Test the get-watch function of pagure-admin with an invalid
- project.
- """
- args = munch.Munch({"project": "fo/o/bar", "user": "pingou"})
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_get_watch_status(args)
- self.assertEqual(
- cm.exception.args[0],
- 'Invalid project name, has more than one "/": fo/o/bar',
- )
- def test_get_watch_get_project_invalid_user(self):
- """Test the get-watch function of pagure-admin on a invalid user."""
- args = munch.Munch({"project": "test", "user": "beebop"})
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_get_watch_status(args)
- self.assertEqual(cm.exception.args[0], 'No user "beebop" found')
- def test_get_watch_get_project(self):
- """Test the get-watch function of pagure-admin on a regular project."""
- args = munch.Munch({"project": "test", "user": "pingou"})
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- "On test user: pingou is watching the following items: "
- "issues, pull-requests\n",
- output,
- )
- def test_get_watch_get_project_not_watching(self):
- """Test the get-watch function of pagure-admin on a regular project."""
- args = munch.Munch({"project": "test", "user": "foo"})
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- "On test user: foo is watching the following items: None\n", output
- )
- def test_get_watch_get_project_namespaced(self):
- """Test the get-watch function of pagure-admin on a namespaced project."""
- args = munch.Munch({"project": "somenamespace/test", "user": "pingou"})
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- "On somenamespace/test user: pingou is watching the following "
- "items: issues, pull-requests\n",
- output,
- )
- def test_get_watch_get_project_namespaced_not_watching(self):
- """Test the get-watch function of pagure-admin on a namespaced project."""
- args = munch.Munch({"project": "somenamespace/test", "user": "foo"})
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- with tests.capture_output() as _discarded:
- pagure.cli.admin.do_get_watch_status(args)
- self.assertEqual(
- "On somenamespace/test user: foo is watching the following "
- "items: None\n",
- output,
- )
- class PagureAdminUpdateWatchTests(tests.Modeltests):
- """Tests for pagure-admin update-watch"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureAdminUpdateWatchTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user="pingou",
- fullname="PY C",
- password="foo",
- default_email="bar@pingou.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
- self.session.add(item)
- # Create the user foo
- item = pagure.lib.model.User(
- user="foo",
- fullname="foo B.",
- password="foob",
- default_email="foo@pingou.com",
- )
- self.session.add(item)
- # Create two projects for the user pingou
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test",
- description="namespaced test project",
- hook_token="aaabbbeee",
- namespace="somenamespace",
- )
- self.session.add(item)
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test",
- description="Test project",
- hook_token="aaabbbccc",
- namespace=None,
- )
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- def test_get_watch_update_project_unknown_project(self):
- """Test the update-watch function of pagure-admin on an unknown
- project.
- """
- args = munch.Munch(
- {"project": "foob", "user": "pingou", "status": "1"}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_update_watch_status(args)
- self.assertEqual(
- cm.exception.args[0], "No project found with: project=foob"
- )
- def test_get_watch_update_project_invalid_project(self):
- """Test the update-watch function of pagure-admin on an invalid
- project.
- """
- args = munch.Munch(
- {"project": "fo/o/b", "user": "pingou", "status": "1"}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_update_watch_status(args)
- self.assertEqual(
- cm.exception.args[0],
- 'Invalid project name, has more than one "/": fo/o/b',
- )
- def test_get_watch_update_project_invalid_user(self):
- """Test the update-watch function of pagure-admin on an invalid user."""
- args = munch.Munch({"project": "test", "user": "foob", "status": "1"})
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_update_watch_status(args)
- self.assertEqual(cm.exception.args[0], 'No user "foob" found')
- def test_get_watch_update_project_invalid_status(self):
- """Test the update-watch function of pagure-admin with an invalid
- status.
- """
- args = munch.Munch(
- {"project": "test", "user": "pingou", "status": "10"}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_update_watch_status(args)
- self.assertEqual(
- cm.exception.args[0],
- "Invalid status provided: 10 not in -1, 0, 1, 2, 3",
- )
- def test_get_watch_update_project_no_effect(self):
- """Test the update-watch function of pagure-admin with a regular
- project - nothing changed.
- """
- args = munch.Munch({"project": "test", "user": "pingou"})
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- "On test user: pingou is watching the following items: "
- "issues, pull-requests\n",
- output,
- )
- args = munch.Munch(
- {"project": "test", "user": "pingou", "status": "1"}
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_update_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- "Updating watch status of pingou to 1 (watch issues and PRs) "
- "on test\n",
- output,
- )
- args = munch.Munch({"project": "test", "user": "pingou"})
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- "On test user: pingou is watching the following items: "
- "issues, pull-requests\n",
- output,
- )
- class PagureNewGroupTests(tests.Modeltests):
- """Tests for pagure-admin new-group"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureNewGroupTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user="pingou",
- fullname="PY C",
- password="foo",
- default_email="bar@pingou.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- def test_missing_display_name(self):
- """Test the new-group function of pagure-admin when the display name
- is missing from the args.
- """
- args = munch.Munch(
- {
- "group_name": "foob",
- "display": None,
- "description": None,
- "username": "pingou",
- }
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_new_group(args)
- self.assertEqual(
- cm.exception.args[0],
- "A display name must be provided for the group",
- )
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- def test_missing_username(self):
- """Test the new-group function of pagure-admin when the username
- is missing from the args.
- """
- args = munch.Munch(
- {
- "group_name": "foob",
- "display": "foo group",
- "description": None,
- "username": None,
- }
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_new_group(args)
- self.assertEqual(
- cm.exception.args[0],
- "An username must be provided to associate with the group",
- )
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- def test_new_group(self):
- """Test the new-group function of pagure-admin when all arguments
- are provided.
- """
- args = munch.Munch(
- {
- "group_name": "foob",
- "display": "foo group",
- "description": None,
- "username": "pingou",
- }
- )
- pagure.cli.admin.do_new_group(args)
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 1)
- @patch.dict("pagure.config.config", {"ENABLE_GROUP_MNGT": False})
- @patch("pagure.cli.admin._ask_confirmation")
- def test_new_group_grp_mngt_off_no(self, conf):
- """Test the new-group function of pagure-admin when all arguments
- are provided and ENABLE_GROUP_MNGT if off in the config and the user
- replies no to the question.
- """
- conf.return_value = False
- args = munch.Munch(
- {
- "group_name": "foob",
- "display": "foo group",
- "description": None,
- "username": "pingou",
- }
- )
- pagure.cli.admin.do_new_group(args)
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- @patch.dict("pagure.config.config", {"ENABLE_GROUP_MNGT": False})
- @patch("pagure.cli.admin._ask_confirmation")
- def test_new_group_grp_mngt_off_yes(self, conf):
- """Test the new-group function of pagure-admin when all arguments
- are provided and ENABLE_GROUP_MNGT if off in the config and the user
- replies yes to the question.
- """
- conf.return_value = True
- args = munch.Munch(
- {
- "group_name": "foob",
- "display": "foo group",
- "description": None,
- "username": "pingou",
- }
- )
- pagure.cli.admin.do_new_group(args)
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 1)
- @patch.dict("pagure.config.config", {"BLACKLISTED_GROUPS": ["foob"]})
- def test_new_group_grp_mngt_off_yes(self):
- """Test the new-group function of pagure-admin when all arguments
- are provided but the group is black listed.
- """
- args = munch.Munch(
- {
- "group_name": "foob",
- "display": "foo group",
- "description": None,
- "username": "pingou",
- }
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_new_group(args)
- self.assertEqual(
- cm.exception.args[0],
- "This group name has been blacklisted, please choose another one",
- )
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- class PagureListGroupEmptyTests(tests.Modeltests):
- """Tests for pagure-admin list-groups"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureListGroupEmptyTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user="pingou",
- fullname="PY C",
- password="foo",
- default_email="bar@pingou.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- @patch("sys.stdout", new_callable=StringIO)
- def test_no_groups(self, mock_stdout):
- """Test the list-groups function of pagure-admin when there are no
- groups in the database
- """
- args = munch.Munch()
- pagure.cli.admin.do_list_groups(args)
- self.assertEqual(
- mock_stdout.getvalue(),
- "No groups found in this pagure instance.\n",
- )
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- class PagureListGroupTests(tests.Modeltests):
- """Tests for pagure-admin list-groups"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureListGroupTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user="pingou",
- fullname="PY C",
- password="foo",
- default_email="bar@pingou.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
- self.session.add(item)
- # Create a group
- pagure.lib.query.add_group(
- self.session,
- group_name="JL",
- display_name="Justice League",
- description="Nope, it's not JLA anymore",
- group_type="user",
- user="pingou",
- is_admin=False,
- blacklist=[],
- )
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 1)
- @patch("sys.stdout", new_callable=StringIO)
- def test_list_groups(self, mock_stdout):
- """Test the list-groups function of pagure-admin when there is one
- group in the database
- """
- args = munch.Munch()
- pagure.cli.admin.do_list_groups(args)
- self.assertEqual(
- mock_stdout.getvalue(),
- "List of groups on this Pagure instance:\n" "Group: 1 - name JL\n",
- )
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 1)
- class PagureBlockUserTests(tests.Modeltests):
- """Tests for pagure-admin block-user"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureBlockUserTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user="pingou",
- fullname="PY C",
- password="foo",
- default_email="bar@pingou.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- user = pagure.lib.query.get_user(self.session, "pingou")
- self.assertIsNone(user.refuse_sessions_before)
- def test_missing_date(self):
- """Test the block-user function of pagure-admin when the no date is
- provided.
- """
- args = munch.Munch({"username": "pingou", "date": None, "list": False})
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_block_user(args)
- self.assertEqual(
- cm.exception.args[0],
- "Invalid date submitted: None, not of the format YYYY-MM-DD",
- )
- user = pagure.lib.query.get_user(self.session, "pingou")
- self.assertIsNone(user.refuse_sessions_before)
- def test_missing_username(self):
- """Test the block-user function of pagure-admin when the username
- is missing from the args.
- """
- args = munch.Munch(
- {"date": "2018-06-11", "username": None, "list": False}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_block_user(args)
- self.assertEqual(cm.exception.args[0], "An username must be specified")
- user = pagure.lib.query.get_user(self.session, "pingou")
- self.assertIsNone(user.refuse_sessions_before)
- def test_invalid_username(self):
- """Test the block-user function of pagure-admin when the username
- provided does correspond to any user in the DB.
- """
- args = munch.Munch(
- {"date": "2018-06-11", "username": "invalid", "list": False}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_block_user(args)
- self.assertEqual(cm.exception.args[0], 'No user "invalid" found')
- user = pagure.lib.query.get_user(self.session, "pingou")
- self.assertIsNone(user.refuse_sessions_before)
- def test_invalide_date(self):
- """Test the block-user function of pagure-admin when the provided
- date is incorrect.
- """
- args = munch.Munch(
- {"date": "2018-14-05", "username": "pingou", "list": False}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_block_user(args)
- self.assertEqual(
- cm.exception.args[0],
- "Invalid date submitted: 2018-14-05, not of the format YYYY-MM-DD",
- )
- user = pagure.lib.query.get_user(self.session, "pingou")
- self.assertIsNone(user.refuse_sessions_before)
- @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
- def test_block_user(self):
- """Test the block-user function of pagure-admin when all arguments
- are provided correctly.
- """
- args = munch.Munch(
- {"date": "2050-12-31", "username": "pingou", "list": False}
- )
- pagure.cli.admin.do_block_user(args)
- user = pagure.lib.query.get_user(self.session, "pingou")
- self.assertIsNotNone(user.refuse_sessions_before)
- def test_list_blocked_user(self):
- """Test the block-user function of pagure-admin when all arguments
- are provided correctly.
- """
- args = munch.Munch({"list": True, "username": None, "date": None})
- with tests.capture_output() as output:
- pagure.cli.admin.do_block_user(args)
- output = output.getvalue()
- self.assertEqual("No users are currently blocked\n", output)
- @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
- def test_list_blocked_user_with_data(self):
- """Test the block-user function of pagure-admin when all arguments
- are provided correctly.
- """
- args = munch.Munch(
- {"date": "2050-12-31", "username": "pingou", "list": False}
- )
- pagure.cli.admin.do_block_user(args)
- args = munch.Munch({"list": True, "username": None, "date": None})
- with tests.capture_output() as output:
- pagure.cli.admin.do_block_user(args)
- output = output.getvalue()
- self.assertEqual(
- "Users blocked:\n"
- " pingou - 2050-12-31T00:00:00\n",
- output,
- )
- @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
- def test_list_blocked_user_with_username_data(self):
- """Test the block-user function of pagure-admin when all arguments
- are provided correctly.
- """
- args = munch.Munch(
- {"date": "2050-12-31", "username": "pingou", "list": False}
- )
- pagure.cli.admin.do_block_user(args)
- args = munch.Munch({"list": True, "username": "ralph", "date": None})
- with tests.capture_output() as output:
- pagure.cli.admin.do_block_user(args)
- output = output.getvalue()
- self.assertEqual("No users are currently blocked\n", output)
- args = munch.Munch({"list": True, "username": "pin*", "date": None})
- with tests.capture_output() as output:
- pagure.cli.admin.do_block_user(args)
- output = output.getvalue()
- self.assertEqual(
- "Users blocked:\n"
- " pingou - 2050-12-31T00:00:00\n",
- output,
- )
- @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
- def test_list_blocked_user_with_date(self):
- """Test the block-user function of pagure-admin when all arguments
- are provided correctly.
- """
- args = munch.Munch(
- {"list": True, "username": None, "date": "2050-12-31"}
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_block_user(args)
- output = output.getvalue()
- self.assertIn("No users are currently blocked\n", output)
- @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
- def test_list_blocked_user_with_date_and_data(self):
- """Test the block-user function of pagure-admin when all arguments
- are provided correctly.
- """
- args = munch.Munch(
- {"date": "2050-12-31", "username": "pingou", "list": False}
- )
- pagure.cli.admin.do_block_user(args)
- args = munch.Munch(
- {"list": True, "username": None, "date": "2050-12-31"}
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_block_user(args)
- output = output.getvalue()
- self.assertIn(
- "Users blocked:\n"
- " pingou - 2050-12-31T00:00:00\n",
- output,
- )
- args = munch.Munch(
- {"list": True, "username": None, "date": "2051-01-01"}
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_block_user(args)
- output = output.getvalue()
- self.assertIn("No users are currently blocked\n", output)
- class PagureAdminDeleteProjectTests(tests.Modeltests):
- """Tests for pagure-admin delete-project"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureAdminDeleteProjectTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user="pingou",
- fullname="PY C",
- password="foo",
- default_email="bar@pingou.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
- self.session.add(item)
- # Create two projects for the user pingou
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test",
- description="namespaced test project",
- hook_token="aaabbbeee",
- namespace="somenamespace",
- )
- self.session.add(item)
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test",
- description="Test project",
- hook_token="aaabbbccc",
- namespace=None,
- )
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- def test_delete_project_unknown_project(self):
- """Test the read-only function of pagure-admin on an unknown
- project.
- """
- args = munch.Munch(
- {"project": "foob", "user": None, "action_user": "pingou"}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_delete_project(args)
- self.assertEqual(
- cm.exception.args[0],
- "No project found with: project=foob, user=None",
- )
- def test_delete_project_invalid_project(self):
- """Test the read-only function of pagure-admin on an invalid
- project.
- """
- args = munch.Munch(
- {"project": "fo/o/b", "user": None, "action_user": "pingou"}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_delete_project(args)
- self.assertEqual(
- cm.exception.args[0],
- 'Invalid project name, has more than one "/": fo/o/b',
- )
- @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
- def test_delete_project(self):
- """Test the read-only function of pagure-admin to get status of
- a non-namespaced project.
- """
- args = munch.Munch(
- {"project": "test", "user": None, "action_user": "pingou"}
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_delete_project(args)
- output = output.getvalue()
- self.assertEqual(
- "Are you sure you want to delete: test?\n"
- " This cannot be undone!\n"
- "Project deleted\n",
- output,
- )
- @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
- def test_delete_project_namespace(self):
- """Test the read-only function of pagure-admin to get status of
- a namespaced project.
- """
- args = munch.Munch(
- {
- "project": "somenamespace/test",
- "user": None,
- "action_user": "pingou",
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_delete_project(args)
- output = output.getvalue()
- self.assertEqual(
- "Are you sure you want to delete: somenamespace/test?\n"
- " This cannot be undone!\n"
- "Project deleted\n",
- output,
- )
- @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
- def test_delete_project_namespace_changed(self):
- """Test the read-only function of pagure-admin to set the status of
- a namespaced project.
- """
- # Before
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 2)
- args = munch.Munch(
- {
- "project": "somenamespace/test",
- "user": None,
- "action_user": "pingou",
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_delete_project(args)
- output = output.getvalue()
- self.assertEqual(
- "Are you sure you want to delete: somenamespace/test?\n"
- " This cannot be undone!\n"
- "Project deleted\n",
- output,
- )
- # After
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 1)
- class PagureCreateBranchTests(tests.Modeltests):
- """Tests for pagure-admin create-branch"""
- populate_db = True
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureCreateBranchTests, self).setUp()
- # Create a couple of projects
- tests.create_projects(self.session)
- # Create their git repo
- tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- def test_create_branch_unknown_project(self):
- """Test the read-only function of pagure-admin on an unknown
- project.
- """
- args = munch.Munch(
- {
- "project": "foob",
- "user": None,
- "new_branch": "new_branch",
- "from_branch": "master",
- "from_commit": None,
- "action_user": "pingou",
- }
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_create_branch(args)
- self.assertEqual(
- cm.exception.args[0],
- "No project found with: project=foob, user=None",
- )
- def test_create_branch_invalid_project(self):
- """Test the read-only function of pagure-admin on an invalid
- project.
- """
- args = munch.Munch(
- {
- "project": "f/o/o/b",
- "user": None,
- "new_branch": "new_branch",
- "from_branch": "master",
- "from_commit": None,
- "action_user": "pingou",
- }
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_create_branch(args)
- self.assertEqual(
- cm.exception.args[0],
- 'Invalid project name, has more than one "/": f/o/o/b',
- )
- def test_create_branch_commit_and_branch_from(self):
- """Test the read-only function of pagure-admin to get status of
- a non-namespaced project.
- """
- args = munch.Munch(
- {
- "project": "test",
- "user": None,
- "new_branch": "new_branch",
- "from_branch": "master",
- "from_commit": "foobar",
- "action_user": "pingou",
- }
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_create_branch(args)
- self.assertEqual(
- cm.exception.args[0],
- "You must create the branch from something, either a commit "
- "or another branch, not from both",
- )
- def test_create_branch_no_branch_from(self):
- """Test the read-only function of pagure-admin to get status of
- a non-namespaced project.
- """
- args = munch.Munch(
- {
- "project": "test",
- "user": None,
- "new_branch": "new_branch",
- "from_branch": "master",
- "from_commit": None,
- "action_user": "pingou",
- }
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_create_branch(args)
- self.assertEqual(
- cm.exception.args[0], 'The "master" branch does not exist'
- )
- def test_create_branch_no_commit_from(self):
- """Test the read-only function of pagure-admin to get status of
- a non-namespaced project.
- """
- args = munch.Munch(
- {
- "project": "test",
- "user": None,
- "new_branch": "new_branch",
- "from_branch": None,
- "from_commit": "foobar",
- "action_user": "pingou",
- }
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_create_branch(args)
- self.assertEqual(
- cm.exception.args[0], "No commit foobar found from which to branch"
- )
- def test_create_branch_from_branch(self):
- """Test the do_create_admin_token function of pagure-admin."""
- gitrepo_path = os.path.join(self.path, "repos", "test.git")
- tests.add_content_git_repo(gitrepo_path)
- # Check branch before:
- gitrepo = pygit2.Repository(gitrepo_path)
- self.assertEqual(gitrepo.listall_branches(), ["master"])
- args = munch.Munch(
- {
- "project": "test",
- "user": None,
- "new_branch": "new_branch",
- "from_branch": "master",
- "from_commit": None,
- "action_user": "pingou",
- }
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_create_branch(args)
- output = output.getvalue()
- self.assertEqual("Branch created\n", output)
- # Check branch after:
- gitrepo = pygit2.Repository(gitrepo_path)
- self.assertEqual(
- sorted(gitrepo.listall_branches()), ["master", "new_branch"]
- )
- class PagureSetDefaultBranchTests(tests.Modeltests):
- """Tests for pagure-admin set-default-branch"""
- populate_db = True
- def setUp(self):
- """Set up the environment, run before every tests."""
- super(PagureSetDefaultBranchTests, self).setUp()
- # Create a couple of projects
- tests.create_projects(self.session)
- # Create their git repo
- tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- def test_set_default_branch_unknown_project(self):
- """Test the set-default-branch function of pagure-admin on an unknown
- project.
- """
- args = munch.Munch(
- {"project": "foob", "user": None, "branch": "master"}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_set_default_branch(args)
- self.assertEqual(
- cm.exception.args[0],
- "No project found with: project=foob, user=None",
- )
- def test_set_default_branch_invalid_project(self):
- """Test the set-default-branch function of pagure-admin on an invalid
- project.
- """
- args = munch.Munch(
- {"project": "f/o/o/b", "user": None, "branch": "master"}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_set_default_branch(args)
- self.assertEqual(
- cm.exception.args[0],
- 'Invalid project name, has more than one "/": f/o/o/b',
- )
- def test_set_default_branch_unknown_branch(self):
- """Test the set-default-branch function of pagure-admin on an unknown
- branch.
- """
- args = munch.Munch({"project": "test", "user": None, "branch": "foob"})
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_set_default_branch(args)
- self.assertEqual(
- cm.exception.args[0], "No foob branch found on project: test"
- )
- def test_set_default_branch_invalid_branch(self):
- """Test the set-default-branch function of pagure-admin on an invalid branch."""
- args = munch.Munch(
- {"project": "test", "user": None, "branch": "~invalid~"}
- )
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_set_default_branch(args)
- self.assertEqual(
- cm.exception.args[0], "No ~invalid~ branch found on project: test"
- )
- def test_set_default_branch(self):
- """Test the set-default-branch funcion of pagure-admin."""
- gitrepo_path = os.path.join(self.path, "repos", "test.git")
- tests.add_content_git_repo(gitrepo_path)
- tests.add_commit_git_repo(gitrepo_path, branch="dev")
- # Check default branch before:
- gitrepo = pygit2.Repository(gitrepo_path)
- self.assertEqual(gitrepo.head.shorthand, "master")
- args = munch.Munch({"project": "test", "user": None, "branch": "dev"})
- with tests.capture_output() as output:
- pagure.cli.admin.do_set_default_branch(args)
- output = output.getvalue()
- self.assertEqual("Branch dev set as default\n", output)
- # Check default branch after:
- self.assertEqual(gitrepo.head.shorthand, "dev")
- class PagureAdminUpdateAclsTests(tests.Modeltests):
- """Tests for pagure-admin update-acls"""
- populate_db = False
- maxDiff = None
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureAdminUpdateAclsTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- def test_update_acls(self):
- """Test the update-acls function of pagure-admin."""
- args = munch.Munch()
- with tests.capture_output() as output:
- pagure.cli.admin.do_update_acls(args)
- output = output.getvalue()
- self.assertEqual(
- "ACLS in the database synced with the list in the configuration file\n",
- output,
- )
- acls = pagure.lib.query.get_acls(self.session)
- self.assertEqual(
- [a.name for a in acls],
- [
- "commit",
- "commit_flag",
- "create_branch",
- "create_git_alias",
- "create_project",
- "delete_git_alias",
- "fork_project",
- "group_modify",
- "internal_access",
- "issue_assign",
- "issue_change_status",
- "issue_comment",
- "issue_create",
- "issue_subscribe",
- "issue_update",
- "issue_update_custom_fields",
- "issue_update_milestone",
- "modify_git_alias",
- "modify_project",
- "pull_request_assign",
- "pull_request_close",
- "pull_request_comment",
- "pull_request_create",
- "pull_request_flag",
- "pull_request_merge",
- "pull_request_rebase",
- "pull_request_subscribe",
- "pull_request_update",
- "tag_project",
- "update_watch_status",
- ],
- )
- @patch.dict(
- "pagure.cli.admin._config",
- {"ACLS": {"dummy_acls": "Dummy ACL for testing"}},
- )
- def test_update_acls_new_acls(self):
- args = munch.Munch({"debug": True})
- with tests.capture_output() as output:
- pagure.cli.admin.do_update_acls(args)
- output = output.getvalue()
- self.assertEqual(
- "ACLS in the database synced with the list in the configuration file\n",
- output,
- )
- acls = pagure.lib.query.get_acls(self.session)
- self.assertEqual(
- [a.name for a in acls],
- [
- "commit",
- "commit_flag",
- "create_branch",
- "create_git_alias",
- "create_project",
- "delete_git_alias",
- "dummy_acls",
- "fork_project",
- "group_modify",
- "internal_access",
- "issue_assign",
- "issue_change_status",
- "issue_comment",
- "issue_create",
- "issue_subscribe",
- "issue_update",
- "issue_update_custom_fields",
- "issue_update_milestone",
- "modify_git_alias",
- "modify_project",
- "pull_request_assign",
- "pull_request_close",
- "pull_request_comment",
- "pull_request_create",
- "pull_request_flag",
- "pull_request_merge",
- "pull_request_rebase",
- "pull_request_subscribe",
- "pull_request_update",
- "tag_project",
- "update_watch_status",
- ],
- )
- if __name__ == "__main__":
- unittest.main(verbosity=2)
|