12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375 |
- # -*- 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 PagureAdminAdminRefreshGitolitetests(tests.Modeltests):
- """Tests for pagure-admin refresh-gitolite"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureAdminAdminRefreshGitolitetests, 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()
- # Create a couple of projects
- tests.create_projects(self.session)
- # Add a group
- msg = pagure.lib.query.add_group(
- self.session,
- group_name="foo",
- display_name="foo group",
- description=None,
- group_type="bar",
- user="pingou",
- is_admin=False,
- blacklist=[],
- )
- self.session.commit()
- self.assertEqual(msg, "User `pingou` added to the group `foo`.")
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- @patch("pagure.cli.admin._ask_confirmation")
- @patch("pagure.lib.git_auth.get_git_auth_helper")
- def test_do_refresh_gitolite_no_args(self, get_helper, conf):
- """Test the do_generate_acl function with no special args."""
- conf.return_value = True
- helper = MagicMock()
- get_helper.return_value = helper
- args = munch.Munch(
- {"group": None, "project": None, "all_": False, "user": None}
- )
- pagure.cli.admin.do_generate_acl(args)
- get_helper.assert_called_with()
- args = helper.generate_acls.call_args
- self.assertIsNone(args[1].get("group"))
- self.assertIsNone(args[1].get("project"))
- @patch("pagure.cli.admin._ask_confirmation")
- @patch("pagure.lib.git_auth.get_git_auth_helper")
- def test_do_refresh_gitolite_all_project(self, get_helper, conf):
- """Test the do_generate_acl function for all projects."""
- conf.return_value = True
- helper = MagicMock()
- get_helper.return_value = helper
- args = munch.Munch(
- {"group": None, "project": None, "all_": True, "user": None}
- )
- pagure.cli.admin.do_generate_acl(args)
- get_helper.assert_called_with()
- args = helper.generate_acls.call_args
- self.assertIsNone(args[1].get("group"))
- self.assertEqual(args[1].get("project"), -1)
- @patch("pagure.cli.admin._ask_confirmation")
- @patch("pagure.lib.git_auth.get_git_auth_helper")
- def test_do_refresh_gitolite_one_project(self, get_helper, conf):
- """Test the do_generate_acl function for a certain project."""
- conf.return_value = True
- helper = MagicMock()
- get_helper.return_value = helper
- args = munch.Munch(
- {"group": None, "project": "test", "all_": False, "user": None}
- )
- pagure.cli.admin.do_generate_acl(args)
- get_helper.assert_called_with()
- args = helper.generate_acls.call_args
- self.assertIsNone(args[1].get("group"))
- self.assertEqual(args[1].get("project").fullname, "test")
- @patch("pagure.cli.admin._ask_confirmation")
- @patch("pagure.lib.git_auth.get_git_auth_helper")
- def test_do_refresh_gitolite_one_project_and_all(self, get_helper, conf):
- """Test the do_generate_acl function for a certain project and all."""
- conf.return_value = True
- helper = MagicMock()
- get_helper.return_value = helper
- args = munch.Munch(
- {"group": None, "project": "test", "all_": True, "user": None}
- )
- pagure.cli.admin.do_generate_acl(args)
- get_helper.assert_called_with()
- args = helper.generate_acls.call_args
- self.assertIsNone(args[1].get("group"))
- self.assertEqual(args[1].get("project"), -1)
- @patch("pagure.cli.admin._ask_confirmation")
- @patch("pagure.lib.git_auth.get_git_auth_helper")
- def test_do_refresh_gitolite_one_group(self, get_helper, conf):
- """Test the do_generate_acl function for a certain group."""
- conf.return_value = True
- helper = MagicMock()
- get_helper.return_value = helper
- args = munch.Munch(
- {"group": "foo", "project": None, "all_": False, "user": None}
- )
- pagure.cli.admin.do_generate_acl(args)
- get_helper.assert_called_with()
- args = helper.generate_acls.call_args
- self.assertEqual(args[1].get("group").group_name, "foo")
- self.assertIsNone(args[1].get("project"))
- 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 PagureAdminReadOnlyTests(tests.Modeltests):
- """Tests for pagure-admin read-only"""
- populate_db = False
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureAdminReadOnlyTests, 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_read_only_unknown_project(self):
- """Test the read-only function of pagure-admin on an unknown
- project.
- """
- args = munch.Munch({"project": "foob", "user": None, "ro": None})
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_read_only(args)
- self.assertEqual(
- cm.exception.args[0], "No project found with: project=foob"
- )
- def test_read_only_invalid_project(self):
- """Test the read-only function of pagure-admin on an invalid
- project.
- """
- args = munch.Munch({"project": "fo/o/b", "user": None, "ro": None})
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_read_only(args)
- self.assertEqual(
- cm.exception.args[0],
- 'Invalid project name, has more than one "/": fo/o/b',
- )
- def test_read_only(self):
- """Test the read-only function of pagure-admin to get status of
- a non-namespaced project.
- """
- args = munch.Munch({"project": "test", "user": None, "ro": None})
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- "The current read-only flag of the project test is set to True\n",
- output,
- )
- def test_read_only_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, "ro": None}
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- "The current read-only flag of the project somenamespace/test "
- "is set to True\n",
- output,
- )
- def test_read_only_namespace_changed(self):
- """Test the read-only function of pagure-admin to set the status of
- a namespaced project.
- """
- # Before
- args = munch.Munch(
- {"project": "somenamespace/test", "user": None, "ro": None}
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- "The current read-only flag of the project somenamespace/test "
- "is set to True\n",
- output,
- )
- args = munch.Munch(
- {"project": "somenamespace/test", "user": None, "ro": "false"}
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- "The read-only flag of the project somenamespace/test has been "
- "set to False\n",
- output,
- )
- # After
- args = munch.Munch(
- {"project": "somenamespace/test", "user": None, "ro": None}
- )
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- "The current read-only flag of the project somenamespace/test "
- "is set to False\n",
- output,
- )
- def test_read_only_no_change(self):
- """Test the read-only function of pagure-admin to set the status of
- a namespaced project.
- """
- # Before
- args = munch.Munch({"project": "test", "user": None, "ro": None})
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- "The current read-only flag of the project test "
- "is set to True\n",
- output,
- )
- args = munch.Munch({"project": "test", "user": None, "ro": "true"})
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- "The read-only flag of the project test has been " "set to True\n",
- output,
- )
- # After
- args = munch.Munch({"project": "test", "user": None, "ro": None})
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- "The current read-only flag of the project test "
- "is set to True\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",
- "generate_acls_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",
- "generate_acls_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)
|