1
0

test_pagure_admin.py 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime # noqa
  9. import os # noqa
  10. import platform # noqa
  11. import shutil # noqa
  12. import subprocess # noqa
  13. import sys # noqa
  14. import unittest # noqa
  15. import pygit2
  16. import munch # noqa
  17. from mock import patch, MagicMock # noqa
  18. from six import StringIO # noqa
  19. sys.path.insert(
  20. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  21. )
  22. import pagure.config # noqa
  23. import pagure.exceptions # noqa: E402
  24. import pagure.cli.admin # noqa
  25. import pagure.lib.model # noqa
  26. import tests # noqa
  27. class PagureAdminAdminTokenEmptytests(tests.Modeltests):
  28. """Tests for pagure-admin admin-token when there is nothing in the DB"""
  29. populate_db = False
  30. def setUp(self):
  31. """Set up the environnment, ran before every tests."""
  32. super(PagureAdminAdminTokenEmptytests, self).setUp()
  33. pagure.cli.admin.session = self.session
  34. def test_do_create_admin_token_no_user(self):
  35. """Test the do_create_admin_token function of pagure-admin without
  36. user.
  37. """
  38. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  39. args = munch.Munch(
  40. {
  41. "user": "pingou",
  42. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  43. }
  44. )
  45. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  46. pagure.cli.admin.do_create_admin_token(args)
  47. self.assertEqual(cm.exception.args[0], 'No user "pingou" found')
  48. def test_do_list_admin_token_empty(self):
  49. """Test the do_list_admin_token function of pagure-admin when there
  50. are not tokens in the db.
  51. """
  52. list_args = munch.Munch(
  53. {
  54. "user": None,
  55. "token": None,
  56. "active": False,
  57. "expired": False,
  58. "all": False,
  59. }
  60. )
  61. with tests.capture_output() as output:
  62. pagure.cli.admin.do_list_admin_token(list_args)
  63. output = output.getvalue()
  64. self.assertEqual(output, "No admin tokens found\n")
  65. class PagureAdminAdminRefreshGitolitetests(tests.Modeltests):
  66. """Tests for pagure-admin refresh-gitolite"""
  67. populate_db = False
  68. def setUp(self):
  69. """Set up the environnment, ran before every tests."""
  70. super(PagureAdminAdminRefreshGitolitetests, self).setUp()
  71. pagure.cli.admin.session = self.session
  72. # Create the user pingou
  73. item = pagure.lib.model.User(
  74. user="pingou",
  75. fullname="PY C",
  76. password="foo",
  77. default_email="bar@pingou.com",
  78. )
  79. self.session.add(item)
  80. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  81. self.session.add(item)
  82. self.session.commit()
  83. # Create a couple of projects
  84. tests.create_projects(self.session)
  85. # Add a group
  86. msg = pagure.lib.query.add_group(
  87. self.session,
  88. group_name="foo",
  89. display_name="foo group",
  90. description=None,
  91. group_type="bar",
  92. user="pingou",
  93. is_admin=False,
  94. blacklist=[],
  95. )
  96. self.session.commit()
  97. self.assertEqual(msg, "User `pingou` added to the group `foo`.")
  98. # Make the imported pagure use the correct db session
  99. pagure.cli.admin.session = self.session
  100. @patch("pagure.cli.admin._ask_confirmation")
  101. @patch("pagure.lib.git_auth.get_git_auth_helper")
  102. def test_do_refresh_gitolite_no_args(self, get_helper, conf):
  103. """Test the do_generate_acl function with no special args."""
  104. conf.return_value = True
  105. helper = MagicMock()
  106. get_helper.return_value = helper
  107. args = munch.Munch(
  108. {"group": None, "project": None, "all_": False, "user": None}
  109. )
  110. pagure.cli.admin.do_generate_acl(args)
  111. get_helper.assert_called_with()
  112. args = helper.generate_acls.call_args
  113. self.assertIsNone(args[1].get("group"))
  114. self.assertIsNone(args[1].get("project"))
  115. @patch("pagure.cli.admin._ask_confirmation")
  116. @patch("pagure.lib.git_auth.get_git_auth_helper")
  117. def test_do_refresh_gitolite_all_project(self, get_helper, conf):
  118. """Test the do_generate_acl function for all projects."""
  119. conf.return_value = True
  120. helper = MagicMock()
  121. get_helper.return_value = helper
  122. args = munch.Munch(
  123. {"group": None, "project": None, "all_": True, "user": None}
  124. )
  125. pagure.cli.admin.do_generate_acl(args)
  126. get_helper.assert_called_with()
  127. args = helper.generate_acls.call_args
  128. self.assertIsNone(args[1].get("group"))
  129. self.assertEqual(args[1].get("project"), -1)
  130. @patch("pagure.cli.admin._ask_confirmation")
  131. @patch("pagure.lib.git_auth.get_git_auth_helper")
  132. def test_do_refresh_gitolite_one_project(self, get_helper, conf):
  133. """Test the do_generate_acl function for a certain project."""
  134. conf.return_value = True
  135. helper = MagicMock()
  136. get_helper.return_value = helper
  137. args = munch.Munch(
  138. {"group": None, "project": "test", "all_": False, "user": None}
  139. )
  140. pagure.cli.admin.do_generate_acl(args)
  141. get_helper.assert_called_with()
  142. args = helper.generate_acls.call_args
  143. self.assertIsNone(args[1].get("group"))
  144. self.assertEqual(args[1].get("project").fullname, "test")
  145. @patch("pagure.cli.admin._ask_confirmation")
  146. @patch("pagure.lib.git_auth.get_git_auth_helper")
  147. def test_do_refresh_gitolite_one_project_and_all(self, get_helper, conf):
  148. """Test the do_generate_acl function for a certain project and all."""
  149. conf.return_value = True
  150. helper = MagicMock()
  151. get_helper.return_value = helper
  152. args = munch.Munch(
  153. {"group": None, "project": "test", "all_": True, "user": None}
  154. )
  155. pagure.cli.admin.do_generate_acl(args)
  156. get_helper.assert_called_with()
  157. args = helper.generate_acls.call_args
  158. self.assertIsNone(args[1].get("group"))
  159. self.assertEqual(args[1].get("project"), -1)
  160. @patch("pagure.cli.admin._ask_confirmation")
  161. @patch("pagure.lib.git_auth.get_git_auth_helper")
  162. def test_do_refresh_gitolite_one_group(self, get_helper, conf):
  163. """Test the do_generate_acl function for a certain group."""
  164. conf.return_value = True
  165. helper = MagicMock()
  166. get_helper.return_value = helper
  167. args = munch.Munch(
  168. {"group": "foo", "project": None, "all_": False, "user": None}
  169. )
  170. pagure.cli.admin.do_generate_acl(args)
  171. get_helper.assert_called_with()
  172. args = helper.generate_acls.call_args
  173. self.assertEqual(args[1].get("group").group_name, "foo")
  174. self.assertIsNone(args[1].get("project"))
  175. class PagureAdminAdminTokentests(tests.Modeltests):
  176. """Tests for pagure-admin admin-token"""
  177. populate_db = False
  178. def setUp(self):
  179. """Set up the environnment, ran before every tests."""
  180. super(PagureAdminAdminTokentests, self).setUp()
  181. pagure.cli.admin.session = self.session
  182. # Create the user pingou
  183. item = pagure.lib.model.User(
  184. user="pingou",
  185. fullname="PY C",
  186. password="foo",
  187. default_email="bar@pingou.com",
  188. )
  189. self.session.add(item)
  190. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  191. self.session.add(item)
  192. self.session.commit()
  193. # Make the imported pagure use the correct db session
  194. pagure.cli.admin.session = self.session
  195. @patch("pagure.cli.admin._get_input")
  196. @patch("pagure.cli.admin._ask_confirmation")
  197. def test_do_create_admin_token(self, conf, rinp):
  198. """Test the do_create_admin_token function of pagure-admin."""
  199. conf.return_value = True
  200. rinp.return_value = "1,2,3"
  201. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  202. args = munch.Munch(
  203. {
  204. "user": "pingou",
  205. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  206. }
  207. )
  208. pagure.cli.admin.do_create_admin_token(args)
  209. # Check the outcome
  210. list_args = munch.Munch(
  211. {
  212. "user": None,
  213. "token": None,
  214. "active": False,
  215. "expired": False,
  216. "all": False,
  217. }
  218. )
  219. with tests.capture_output() as output:
  220. pagure.cli.admin.do_list_admin_token(list_args)
  221. output = output.getvalue()
  222. self.assertNotEqual(output, 'No user "pingou" found\n')
  223. self.assertEqual(len(output.split("\n")), 2)
  224. self.assertIn(" -- pingou -- ", output)
  225. @patch("pagure.cli.admin._get_input")
  226. @patch("pagure.cli.admin._ask_confirmation")
  227. def test_do_list_admin_token(self, conf, rinp):
  228. """Test the do_list_admin_token function of pagure-admin."""
  229. # Create an admin token to use
  230. conf.return_value = True
  231. rinp.return_value = "1,2,3"
  232. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  233. args = munch.Munch(
  234. {
  235. "user": "pingou",
  236. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  237. }
  238. )
  239. pagure.cli.admin.do_create_admin_token(args)
  240. # Retrieve all tokens
  241. list_args = munch.Munch(
  242. {
  243. "user": None,
  244. "token": None,
  245. "active": False,
  246. "expired": False,
  247. "all": False,
  248. }
  249. )
  250. with tests.capture_output() as output:
  251. pagure.cli.admin.do_list_admin_token(list_args)
  252. output = output.getvalue()
  253. self.assertNotEqual(output, 'No user "pingou" found\n')
  254. self.assertEqual(len(output.split("\n")), 2)
  255. self.assertIn(" -- pingou -- ", output)
  256. # Retrieve pfrields's tokens
  257. list_args = munch.Munch(
  258. {
  259. "user": "pfrields",
  260. "token": None,
  261. "active": False,
  262. "expired": False,
  263. "all": False,
  264. }
  265. )
  266. with tests.capture_output() as output:
  267. pagure.cli.admin.do_list_admin_token(list_args)
  268. output = output.getvalue()
  269. self.assertEqual(output, "No admin tokens found\n")
  270. def test_do_list_admin_token_non_admin_acls(self):
  271. """Test the do_list_admin_token function of pagure-admin for a token
  272. without any admin ACL."""
  273. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  274. pagure.lib.query.add_token_to_user(
  275. self.session,
  276. project=None,
  277. acls=["issue_assign", "pull_request_subscribe"],
  278. username="pingou",
  279. expiration_date=exp_date,
  280. )
  281. # Retrieve all admin tokens
  282. list_args = munch.Munch(
  283. {
  284. "user": None,
  285. "token": None,
  286. "active": False,
  287. "expired": False,
  288. "all": False,
  289. }
  290. )
  291. with tests.capture_output() as output:
  292. pagure.cli.admin.do_list_admin_token(list_args)
  293. output = output.getvalue()
  294. self.assertEqual(output, "No admin tokens found\n")
  295. # Retrieve all tokens
  296. list_args = munch.Munch(
  297. {
  298. "user": None,
  299. "token": None,
  300. "active": False,
  301. "expired": False,
  302. "all": True,
  303. }
  304. )
  305. with tests.capture_output() as output:
  306. pagure.cli.admin.do_list_admin_token(list_args)
  307. output = output.getvalue()
  308. self.assertNotEqual(output, 'No user "pingou" found\n')
  309. self.assertEqual(len(output.split("\n")), 2)
  310. self.assertIn(" -- pingou -- ", output)
  311. @patch("pagure.cli.admin._get_input")
  312. @patch("pagure.cli.admin._ask_confirmation")
  313. def test_do_info_admin_token(self, conf, rinp):
  314. """Test the do_info_admin_token function of pagure-admin."""
  315. # Create an admin token to use
  316. conf.return_value = True
  317. rinp.return_value = "2,4,5"
  318. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  319. args = munch.Munch(
  320. {
  321. "user": "pingou",
  322. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  323. }
  324. )
  325. pagure.cli.admin.do_create_admin_token(args)
  326. # Retrieve the token
  327. list_args = munch.Munch(
  328. {
  329. "user": None,
  330. "token": None,
  331. "active": False,
  332. "expired": False,
  333. "all": False,
  334. }
  335. )
  336. with tests.capture_output() as output:
  337. pagure.cli.admin.do_list_admin_token(list_args)
  338. output = output.getvalue()
  339. self.assertNotEqual(output, 'No user "pingou" found\n')
  340. self.assertEqual(len(output.split("\n")), 2)
  341. self.assertIn(" -- pingou -- ", output)
  342. token = output.split(" ", 1)[0]
  343. args = munch.Munch({"token": token})
  344. with tests.capture_output() as output:
  345. pagure.cli.admin.do_info_admin_token(args)
  346. output = output.getvalue()
  347. self.assertIn(" -- pingou -- ", output.split("\n", 1)[0])
  348. self.assertEqual(
  349. output.split("\n", 1)[1],
  350. """ACLs:
  351. - issue_create
  352. - pull_request_comment
  353. - pull_request_flag
  354. """,
  355. )
  356. def test_do_info_admin_token_non_admin_acl(self):
  357. """Test the do_info_admin_token function of pagure-admin for a
  358. token not having any admin ACL."""
  359. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  360. pagure.lib.query.add_token_to_user(
  361. self.session,
  362. project=None,
  363. acls=["issue_assign", "pull_request_subscribe"],
  364. username="pingou",
  365. expiration_date=exp_date,
  366. )
  367. # Retrieve the token
  368. list_args = munch.Munch(
  369. {
  370. "user": None,
  371. "token": None,
  372. "active": False,
  373. "expired": False,
  374. "all": True,
  375. }
  376. )
  377. with tests.capture_output() as output:
  378. pagure.cli.admin.do_list_admin_token(list_args)
  379. output = output.getvalue()
  380. self.assertNotEqual(output, 'No user "pingou" found\n')
  381. self.assertEqual(len(output.split("\n")), 2)
  382. self.assertIn(" -- pingou -- ", output)
  383. token = output.split(" ", 1)[0]
  384. args = munch.Munch({"token": token})
  385. with tests.capture_output() as output:
  386. pagure.cli.admin.do_info_admin_token(args)
  387. output = output.getvalue()
  388. self.assertIn(" -- pingou -- ", output.split("\n", 1)[0])
  389. self.assertEqual(
  390. output.split("\n", 1)[1],
  391. """ACLs:
  392. - issue_assign
  393. - pull_request_subscribe
  394. """,
  395. )
  396. @patch("pagure.cli.admin._get_input")
  397. @patch("pagure.cli.admin._ask_confirmation")
  398. def test_do_expire_admin_token(self, conf, rinp):
  399. """Test the do_expire_admin_token function of pagure-admin."""
  400. # Create an admin token to use
  401. conf.return_value = True
  402. rinp.return_value = "1,2,3"
  403. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  404. args = munch.Munch(
  405. {
  406. "user": "pingou",
  407. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  408. }
  409. )
  410. pagure.cli.admin.do_create_admin_token(args)
  411. # Retrieve the token
  412. list_args = munch.Munch(
  413. {
  414. "user": None,
  415. "token": None,
  416. "active": False,
  417. "expired": False,
  418. "all": False,
  419. }
  420. )
  421. with tests.capture_output() as output:
  422. pagure.cli.admin.do_list_admin_token(list_args)
  423. output = output.getvalue()
  424. self.assertNotEqual(output, 'No user "pingou" found\n')
  425. self.assertEqual(len(output.split("\n")), 2)
  426. self.assertIn(" -- pingou -- ", output)
  427. token = output.split(" ", 1)[0]
  428. # Before
  429. list_args = munch.Munch(
  430. {
  431. "user": None,
  432. "token": None,
  433. "active": True,
  434. "expired": False,
  435. "all": False,
  436. }
  437. )
  438. with tests.capture_output() as output:
  439. pagure.cli.admin.do_list_admin_token(list_args)
  440. output = output.getvalue()
  441. self.assertNotEqual(output, "No admin tokens found\n")
  442. self.assertEqual(len(output.split("\n")), 2)
  443. self.assertIn(" -- pingou -- ", output)
  444. # Expire the token
  445. args = munch.Munch({"token": token, "all": False})
  446. pagure.cli.admin.do_expire_admin_token(args)
  447. # After
  448. list_args = munch.Munch(
  449. {
  450. "user": None,
  451. "token": None,
  452. "active": True,
  453. "expired": False,
  454. "all": False,
  455. }
  456. )
  457. with tests.capture_output() as output:
  458. pagure.cli.admin.do_list_admin_token(list_args)
  459. output = output.getvalue()
  460. self.assertEqual(output, "No admin tokens found\n")
  461. @patch("pagure.cli.admin._get_input")
  462. @patch("pagure.cli.admin._ask_confirmation")
  463. def test_do_expire_admin_token_non_admin_acls(self, conf, rinp):
  464. """Test the do_expire_admin_token function of pagure-admin for a token
  465. without any admin ACL."""
  466. # Create an admin token to use
  467. conf.return_value = True
  468. rinp.return_value = "1,2,3"
  469. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  470. pagure.lib.query.add_token_to_user(
  471. self.session,
  472. project=None,
  473. acls=["issue_assign", "pull_request_subscribe"],
  474. username="pingou",
  475. expiration_date=exp_date,
  476. )
  477. # Retrieve all tokens to get the one of interest
  478. list_args = munch.Munch(
  479. {
  480. "user": None,
  481. "token": None,
  482. "active": False,
  483. "expired": False,
  484. "all": True,
  485. }
  486. )
  487. with tests.capture_output() as output:
  488. pagure.cli.admin.do_list_admin_token(list_args)
  489. output = output.getvalue()
  490. self.assertNotEqual(output, 'No user "pingou" found\n')
  491. self.assertEqual(len(output.split("\n")), 2)
  492. self.assertIn(" -- pingou -- ", output)
  493. token = output.split(" ", 1)[0]
  494. # Expire the token
  495. args = munch.Munch({"token": token, "all": True})
  496. pagure.cli.admin.do_expire_admin_token(args)
  497. # After
  498. list_args = munch.Munch(
  499. {
  500. "user": None,
  501. "token": None,
  502. "active": True,
  503. "expired": False,
  504. "all": False,
  505. }
  506. )
  507. with tests.capture_output() as output:
  508. pagure.cli.admin.do_list_admin_token(list_args)
  509. output = output.getvalue()
  510. self.assertEqual(output, "No admin tokens found\n")
  511. @patch("pagure.cli.admin._get_input")
  512. @patch("pagure.cli.admin._ask_confirmation")
  513. def test_do_update_admin_token_invalid_date(self, conf, rinp):
  514. """Test the do_update_admin_token function of pagure-admin with
  515. an invalid date."""
  516. # Create an admin token to use
  517. conf.return_value = True
  518. rinp.return_value = "1,2,3"
  519. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  520. args = munch.Munch(
  521. {
  522. "user": "pingou",
  523. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  524. }
  525. )
  526. pagure.cli.admin.do_create_admin_token(args)
  527. # Retrieve the token
  528. list_args = munch.Munch(
  529. {
  530. "user": None,
  531. "token": None,
  532. "active": False,
  533. "expired": False,
  534. "all": False,
  535. }
  536. )
  537. with tests.capture_output() as output:
  538. pagure.cli.admin.do_list_admin_token(list_args)
  539. output = output.getvalue()
  540. self.assertNotEqual(output, 'No user "pingou" found\n')
  541. self.assertEqual(len(output.split("\n")), 2)
  542. self.assertIn(" -- pingou -- ", output)
  543. token = output.split(" ", 1)[0]
  544. current_expiration = output.split(" ", 1)[1]
  545. # Set the expiration date to the token
  546. args = munch.Munch({"token": token, "date": "aa-bb-cc", "all": False})
  547. self.assertRaises(
  548. pagure.exceptions.PagureException,
  549. pagure.cli.admin.do_update_admin_token,
  550. args,
  551. )
  552. @patch("pagure.cli.admin._get_input")
  553. @patch("pagure.cli.admin._ask_confirmation")
  554. def test_do_update_admin_token_invalid_date2(self, conf, rinp):
  555. """Test the do_update_admin_token function of pagure-admin with
  556. an invalid date."""
  557. # Create an admin token to use
  558. conf.return_value = True
  559. rinp.return_value = "1,2,3"
  560. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  561. args = munch.Munch(
  562. {
  563. "user": "pingou",
  564. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  565. }
  566. )
  567. pagure.cli.admin.do_create_admin_token(args)
  568. # Retrieve the token
  569. list_args = munch.Munch(
  570. {
  571. "user": None,
  572. "token": None,
  573. "active": False,
  574. "expired": False,
  575. "all": False,
  576. }
  577. )
  578. with tests.capture_output() as output:
  579. pagure.cli.admin.do_list_admin_token(list_args)
  580. output = output.getvalue()
  581. self.assertNotEqual(output, 'No user "pingou" found\n')
  582. self.assertEqual(len(output.split("\n")), 2)
  583. self.assertIn(" -- pingou -- ", output)
  584. token = output.split(" ", 1)[0]
  585. current_expiration = output.split(" ", 1)[1]
  586. # Set the expiration date to the token
  587. args = munch.Munch(
  588. {"token": token, "date": "2017-18-01", "all": False}
  589. )
  590. self.assertRaises(
  591. pagure.exceptions.PagureException,
  592. pagure.cli.admin.do_update_admin_token,
  593. args,
  594. )
  595. @patch("pagure.cli.admin._get_input")
  596. @patch("pagure.cli.admin._ask_confirmation")
  597. def test_do_update_admin_token_invalid_date3(self, conf, rinp):
  598. """Test the do_update_admin_token function of pagure-admin with
  599. an invalid date (is today)."""
  600. # Create an admin token to use
  601. conf.return_value = True
  602. rinp.return_value = "1,2,3"
  603. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  604. args = munch.Munch(
  605. {
  606. "user": "pingou",
  607. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  608. }
  609. )
  610. pagure.cli.admin.do_create_admin_token(args)
  611. # Retrieve the token
  612. list_args = munch.Munch(
  613. {
  614. "user": None,
  615. "token": None,
  616. "active": False,
  617. "expired": False,
  618. "all": False,
  619. }
  620. )
  621. with tests.capture_output() as output:
  622. pagure.cli.admin.do_list_admin_token(list_args)
  623. output = output.getvalue()
  624. self.assertNotEqual(output, 'No user "pingou" found\n')
  625. self.assertEqual(len(output.split("\n")), 2)
  626. self.assertIn(" -- pingou -- ", output)
  627. token = output.split(" ", 1)[0]
  628. current_expiration = output.split(" ", 1)[1]
  629. # Set the expiration date to the token
  630. args = munch.Munch(
  631. {
  632. "token": token,
  633. "date": datetime.datetime.utcnow().date(),
  634. "all": False,
  635. }
  636. )
  637. self.assertRaises(
  638. pagure.exceptions.PagureException,
  639. pagure.cli.admin.do_update_admin_token,
  640. args,
  641. )
  642. @patch("pagure.cli.admin._get_input")
  643. @patch("pagure.cli.admin._ask_confirmation")
  644. def test_do_update_admin_token(self, conf, rinp):
  645. """Test the do_update_admin_token function of pagure-admin."""
  646. # Create an admin token to use
  647. conf.return_value = True
  648. rinp.return_value = "1,2,3"
  649. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  650. args = munch.Munch(
  651. {
  652. "user": "pingou",
  653. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  654. }
  655. )
  656. pagure.cli.admin.do_create_admin_token(args)
  657. # Retrieve the token
  658. list_args = munch.Munch(
  659. {
  660. "user": None,
  661. "token": None,
  662. "active": False,
  663. "expired": False,
  664. "all": False,
  665. }
  666. )
  667. with tests.capture_output() as output:
  668. pagure.cli.admin.do_list_admin_token(list_args)
  669. output = output.getvalue()
  670. self.assertNotEqual(output, 'No user "pingou" found\n')
  671. self.assertEqual(len(output.split("\n")), 2)
  672. self.assertIn(" -- pingou -- ", output)
  673. token = output.split(" ", 1)[0]
  674. current_expiration = output.strip().split(" -- ", 2)[-1]
  675. # Before
  676. list_args = munch.Munch(
  677. {
  678. "user": None,
  679. "token": None,
  680. "active": True,
  681. "expired": False,
  682. "all": False,
  683. }
  684. )
  685. with tests.capture_output() as output:
  686. pagure.cli.admin.do_list_admin_token(list_args)
  687. output = output.getvalue()
  688. self.assertNotEqual(output, "No admin tokens found\n")
  689. self.assertEqual(len(output.split("\n")), 2)
  690. self.assertIn(" -- pingou -- ", output)
  691. deadline = datetime.datetime.utcnow().date() + datetime.timedelta(
  692. days=3
  693. )
  694. # Set the expiration date to the token
  695. args = munch.Munch(
  696. {
  697. "token": token,
  698. "date": deadline.strftime("%Y-%m-%d"),
  699. "all": False,
  700. }
  701. )
  702. pagure.cli.admin.do_update_admin_token(args)
  703. # After
  704. list_args = munch.Munch(
  705. {
  706. "user": None,
  707. "token": None,
  708. "active": True,
  709. "expired": False,
  710. "all": False,
  711. }
  712. )
  713. with tests.capture_output() as output:
  714. pagure.cli.admin.do_list_admin_token(list_args)
  715. output = output.getvalue()
  716. self.assertEqual(output.split(" ", 1)[0], token)
  717. self.assertNotEqual(
  718. output.strip().split(" -- ", 2)[-1], current_expiration
  719. )
  720. @patch("pagure.cli.admin._get_input")
  721. @patch("pagure.cli.admin._ask_confirmation")
  722. def test_do_update_admin_token_non_admin_acls(self, conf, rinp):
  723. """Test the do_update_admin_token function of pagure-admin for a token
  724. without any admin ACL."""
  725. # Create an admin token to use
  726. conf.return_value = True
  727. rinp.return_value = "1,2,3"
  728. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  729. pagure.lib.query.add_token_to_user(
  730. self.session,
  731. project=None,
  732. acls=["issue_assign", "pull_request_subscribe"],
  733. username="pingou",
  734. expiration_date=exp_date,
  735. )
  736. # Retrieve all tokens to get the one of interest
  737. list_args = munch.Munch(
  738. {
  739. "user": None,
  740. "token": None,
  741. "active": False,
  742. "expired": False,
  743. "all": True,
  744. }
  745. )
  746. with tests.capture_output() as output:
  747. pagure.cli.admin.do_list_admin_token(list_args)
  748. output = output.getvalue()
  749. self.assertNotEqual(output, 'No user "pingou" found\n')
  750. self.assertEqual(len(output.split("\n")), 2)
  751. self.assertIn(" -- pingou -- ", output)
  752. token = output.split(" ", 1)[0]
  753. current_expiration = output.strip().split(" -- ", 2)[-1]
  754. deadline = datetime.datetime.utcnow().date() + datetime.timedelta(
  755. days=3
  756. )
  757. # Set the expiration date to the token
  758. args = munch.Munch(
  759. {
  760. "token": token,
  761. "date": deadline.strftime("%Y-%m-%d"),
  762. "all": True,
  763. }
  764. )
  765. pagure.cli.admin.do_update_admin_token(args)
  766. # After
  767. list_args = munch.Munch(
  768. {
  769. "user": None,
  770. "token": None,
  771. "active": True,
  772. "expired": False,
  773. "all": True,
  774. }
  775. )
  776. with tests.capture_output() as output:
  777. pagure.cli.admin.do_list_admin_token(list_args)
  778. output = output.getvalue()
  779. self.assertEqual(output.split(" ", 1)[0], token)
  780. self.assertNotEqual(
  781. output.strip().split(" -- ", 2)[-1], current_expiration
  782. )
  783. class PagureAdminGetWatchTests(tests.Modeltests):
  784. """Tests for pagure-admin get-watch"""
  785. populate_db = False
  786. def setUp(self):
  787. """Set up the environnment, ran before every tests."""
  788. super(PagureAdminGetWatchTests, self).setUp()
  789. pagure.cli.admin.session = self.session
  790. # Create the user pingou
  791. item = pagure.lib.model.User(
  792. user="pingou",
  793. fullname="PY C",
  794. password="foo",
  795. default_email="bar@pingou.com",
  796. )
  797. self.session.add(item)
  798. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  799. self.session.add(item)
  800. # Create the user foo
  801. item = pagure.lib.model.User(
  802. user="foo",
  803. fullname="foo B.",
  804. password="foob",
  805. default_email="foo@pingou.com",
  806. )
  807. self.session.add(item)
  808. # Create two projects for the user pingou
  809. item = pagure.lib.model.Project(
  810. user_id=1, # pingou
  811. name="test",
  812. description="namespaced test project",
  813. hook_token="aaabbbeee",
  814. namespace="somenamespace",
  815. )
  816. self.session.add(item)
  817. item = pagure.lib.model.Project(
  818. user_id=1, # pingou
  819. name="test",
  820. description="Test project",
  821. hook_token="aaabbbccc",
  822. namespace=None,
  823. )
  824. self.session.add(item)
  825. self.session.commit()
  826. # Make the imported pagure use the correct db session
  827. pagure.cli.admin.session = self.session
  828. def test_get_watch_get_project_unknown_project(self):
  829. """Test the get-watch function of pagure-admin with an unknown
  830. project.
  831. """
  832. args = munch.Munch({"project": "foobar", "user": "pingou"})
  833. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  834. pagure.cli.admin.do_get_watch_status(args)
  835. self.assertEqual(
  836. cm.exception.args[0], "No project found with: project=foobar"
  837. )
  838. def test_get_watch_get_project_invalid_project(self):
  839. """Test the get-watch function of pagure-admin with an invalid
  840. project.
  841. """
  842. args = munch.Munch({"project": "fo/o/bar", "user": "pingou"})
  843. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  844. pagure.cli.admin.do_get_watch_status(args)
  845. self.assertEqual(
  846. cm.exception.args[0],
  847. 'Invalid project name, has more than one "/": fo/o/bar',
  848. )
  849. def test_get_watch_get_project_invalid_user(self):
  850. """Test the get-watch function of pagure-admin on a invalid user."""
  851. args = munch.Munch({"project": "test", "user": "beebop"})
  852. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  853. pagure.cli.admin.do_get_watch_status(args)
  854. self.assertEqual(cm.exception.args[0], 'No user "beebop" found')
  855. def test_get_watch_get_project(self):
  856. """Test the get-watch function of pagure-admin on a regular project."""
  857. args = munch.Munch({"project": "test", "user": "pingou"})
  858. with tests.capture_output() as output:
  859. pagure.cli.admin.do_get_watch_status(args)
  860. output = output.getvalue()
  861. self.assertEqual(
  862. "On test user: pingou is watching the following items: "
  863. "issues, pull-requests\n",
  864. output,
  865. )
  866. def test_get_watch_get_project_not_watching(self):
  867. """Test the get-watch function of pagure-admin on a regular project."""
  868. args = munch.Munch({"project": "test", "user": "foo"})
  869. with tests.capture_output() as output:
  870. pagure.cli.admin.do_get_watch_status(args)
  871. output = output.getvalue()
  872. self.assertEqual(
  873. "On test user: foo is watching the following items: None\n", output
  874. )
  875. def test_get_watch_get_project_namespaced(self):
  876. """Test the get-watch function of pagure-admin on a namespaced project."""
  877. args = munch.Munch({"project": "somenamespace/test", "user": "pingou"})
  878. with tests.capture_output() as output:
  879. pagure.cli.admin.do_get_watch_status(args)
  880. output = output.getvalue()
  881. self.assertEqual(
  882. "On somenamespace/test user: pingou is watching the following "
  883. "items: issues, pull-requests\n",
  884. output,
  885. )
  886. def test_get_watch_get_project_namespaced_not_watching(self):
  887. """Test the get-watch function of pagure-admin on a namespaced project."""
  888. args = munch.Munch({"project": "somenamespace/test", "user": "foo"})
  889. with tests.capture_output() as output:
  890. pagure.cli.admin.do_get_watch_status(args)
  891. output = output.getvalue()
  892. with tests.capture_output() as _discarded:
  893. pagure.cli.admin.do_get_watch_status(args)
  894. self.assertEqual(
  895. "On somenamespace/test user: foo is watching the following "
  896. "items: None\n",
  897. output,
  898. )
  899. class PagureAdminUpdateWatchTests(tests.Modeltests):
  900. """Tests for pagure-admin update-watch"""
  901. populate_db = False
  902. def setUp(self):
  903. """Set up the environnment, ran before every tests."""
  904. super(PagureAdminUpdateWatchTests, self).setUp()
  905. pagure.cli.admin.session = self.session
  906. # Create the user pingou
  907. item = pagure.lib.model.User(
  908. user="pingou",
  909. fullname="PY C",
  910. password="foo",
  911. default_email="bar@pingou.com",
  912. )
  913. self.session.add(item)
  914. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  915. self.session.add(item)
  916. # Create the user foo
  917. item = pagure.lib.model.User(
  918. user="foo",
  919. fullname="foo B.",
  920. password="foob",
  921. default_email="foo@pingou.com",
  922. )
  923. self.session.add(item)
  924. # Create two projects for the user pingou
  925. item = pagure.lib.model.Project(
  926. user_id=1, # pingou
  927. name="test",
  928. description="namespaced test project",
  929. hook_token="aaabbbeee",
  930. namespace="somenamespace",
  931. )
  932. self.session.add(item)
  933. item = pagure.lib.model.Project(
  934. user_id=1, # pingou
  935. name="test",
  936. description="Test project",
  937. hook_token="aaabbbccc",
  938. namespace=None,
  939. )
  940. self.session.add(item)
  941. self.session.commit()
  942. # Make the imported pagure use the correct db session
  943. pagure.cli.admin.session = self.session
  944. def test_get_watch_update_project_unknown_project(self):
  945. """Test the update-watch function of pagure-admin on an unknown
  946. project.
  947. """
  948. args = munch.Munch(
  949. {"project": "foob", "user": "pingou", "status": "1"}
  950. )
  951. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  952. pagure.cli.admin.do_update_watch_status(args)
  953. self.assertEqual(
  954. cm.exception.args[0], "No project found with: project=foob"
  955. )
  956. def test_get_watch_update_project_invalid_project(self):
  957. """Test the update-watch function of pagure-admin on an invalid
  958. project.
  959. """
  960. args = munch.Munch(
  961. {"project": "fo/o/b", "user": "pingou", "status": "1"}
  962. )
  963. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  964. pagure.cli.admin.do_update_watch_status(args)
  965. self.assertEqual(
  966. cm.exception.args[0],
  967. 'Invalid project name, has more than one "/": fo/o/b',
  968. )
  969. def test_get_watch_update_project_invalid_user(self):
  970. """Test the update-watch function of pagure-admin on an invalid user."""
  971. args = munch.Munch({"project": "test", "user": "foob", "status": "1"})
  972. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  973. pagure.cli.admin.do_update_watch_status(args)
  974. self.assertEqual(cm.exception.args[0], 'No user "foob" found')
  975. def test_get_watch_update_project_invalid_status(self):
  976. """Test the update-watch function of pagure-admin with an invalid
  977. status.
  978. """
  979. args = munch.Munch(
  980. {"project": "test", "user": "pingou", "status": "10"}
  981. )
  982. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  983. pagure.cli.admin.do_update_watch_status(args)
  984. self.assertEqual(
  985. cm.exception.args[0],
  986. "Invalid status provided: 10 not in -1, 0, 1, 2, 3",
  987. )
  988. def test_get_watch_update_project_no_effect(self):
  989. """Test the update-watch function of pagure-admin with a regular
  990. project - nothing changed.
  991. """
  992. args = munch.Munch({"project": "test", "user": "pingou"})
  993. with tests.capture_output() as output:
  994. pagure.cli.admin.do_get_watch_status(args)
  995. output = output.getvalue()
  996. self.assertEqual(
  997. "On test user: pingou is watching the following items: "
  998. "issues, pull-requests\n",
  999. output,
  1000. )
  1001. args = munch.Munch(
  1002. {"project": "test", "user": "pingou", "status": "1"}
  1003. )
  1004. with tests.capture_output() as output:
  1005. pagure.cli.admin.do_update_watch_status(args)
  1006. output = output.getvalue()
  1007. self.assertEqual(
  1008. "Updating watch status of pingou to 1 (watch issues and PRs) "
  1009. "on test\n",
  1010. output,
  1011. )
  1012. args = munch.Munch({"project": "test", "user": "pingou"})
  1013. with tests.capture_output() as output:
  1014. pagure.cli.admin.do_get_watch_status(args)
  1015. output = output.getvalue()
  1016. self.assertEqual(
  1017. "On test user: pingou is watching the following items: "
  1018. "issues, pull-requests\n",
  1019. output,
  1020. )
  1021. class PagureAdminReadOnlyTests(tests.Modeltests):
  1022. """Tests for pagure-admin read-only"""
  1023. populate_db = False
  1024. def setUp(self):
  1025. """Set up the environnment, ran before every tests."""
  1026. super(PagureAdminReadOnlyTests, self).setUp()
  1027. pagure.cli.admin.session = self.session
  1028. # Create the user pingou
  1029. item = pagure.lib.model.User(
  1030. user="pingou",
  1031. fullname="PY C",
  1032. password="foo",
  1033. default_email="bar@pingou.com",
  1034. )
  1035. self.session.add(item)
  1036. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1037. self.session.add(item)
  1038. # Create two projects for the user pingou
  1039. item = pagure.lib.model.Project(
  1040. user_id=1, # pingou
  1041. name="test",
  1042. description="namespaced test project",
  1043. hook_token="aaabbbeee",
  1044. namespace="somenamespace",
  1045. )
  1046. self.session.add(item)
  1047. item = pagure.lib.model.Project(
  1048. user_id=1, # pingou
  1049. name="test",
  1050. description="Test project",
  1051. hook_token="aaabbbccc",
  1052. namespace=None,
  1053. )
  1054. self.session.add(item)
  1055. self.session.commit()
  1056. # Make the imported pagure use the correct db session
  1057. pagure.cli.admin.session = self.session
  1058. def test_read_only_unknown_project(self):
  1059. """Test the read-only function of pagure-admin on an unknown
  1060. project.
  1061. """
  1062. args = munch.Munch({"project": "foob", "user": None, "ro": None})
  1063. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1064. pagure.cli.admin.do_read_only(args)
  1065. self.assertEqual(
  1066. cm.exception.args[0], "No project found with: project=foob"
  1067. )
  1068. def test_read_only_invalid_project(self):
  1069. """Test the read-only function of pagure-admin on an invalid
  1070. project.
  1071. """
  1072. args = munch.Munch({"project": "fo/o/b", "user": None, "ro": None})
  1073. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1074. pagure.cli.admin.do_read_only(args)
  1075. self.assertEqual(
  1076. cm.exception.args[0],
  1077. 'Invalid project name, has more than one "/": fo/o/b',
  1078. )
  1079. def test_read_only(self):
  1080. """Test the read-only function of pagure-admin to get status of
  1081. a non-namespaced project.
  1082. """
  1083. args = munch.Munch({"project": "test", "user": None, "ro": None})
  1084. with tests.capture_output() as output:
  1085. pagure.cli.admin.do_read_only(args)
  1086. output = output.getvalue()
  1087. self.assertEqual(
  1088. "The current read-only flag of the project test is set to True\n",
  1089. output,
  1090. )
  1091. def test_read_only_namespace(self):
  1092. """Test the read-only function of pagure-admin to get status of
  1093. a namespaced project.
  1094. """
  1095. args = munch.Munch(
  1096. {"project": "somenamespace/test", "user": None, "ro": None}
  1097. )
  1098. with tests.capture_output() as output:
  1099. pagure.cli.admin.do_read_only(args)
  1100. output = output.getvalue()
  1101. self.assertEqual(
  1102. "The current read-only flag of the project somenamespace/test "
  1103. "is set to True\n",
  1104. output,
  1105. )
  1106. def test_read_only_namespace_changed(self):
  1107. """Test the read-only function of pagure-admin to set the status of
  1108. a namespaced project.
  1109. """
  1110. # Before
  1111. args = munch.Munch(
  1112. {"project": "somenamespace/test", "user": None, "ro": None}
  1113. )
  1114. with tests.capture_output() as output:
  1115. pagure.cli.admin.do_read_only(args)
  1116. output = output.getvalue()
  1117. self.assertEqual(
  1118. "The current read-only flag of the project somenamespace/test "
  1119. "is set to True\n",
  1120. output,
  1121. )
  1122. args = munch.Munch(
  1123. {"project": "somenamespace/test", "user": None, "ro": "false"}
  1124. )
  1125. with tests.capture_output() as output:
  1126. pagure.cli.admin.do_read_only(args)
  1127. output = output.getvalue()
  1128. self.assertEqual(
  1129. "The read-only flag of the project somenamespace/test has been "
  1130. "set to False\n",
  1131. output,
  1132. )
  1133. # After
  1134. args = munch.Munch(
  1135. {"project": "somenamespace/test", "user": None, "ro": None}
  1136. )
  1137. with tests.capture_output() as output:
  1138. pagure.cli.admin.do_read_only(args)
  1139. output = output.getvalue()
  1140. self.assertEqual(
  1141. "The current read-only flag of the project somenamespace/test "
  1142. "is set to False\n",
  1143. output,
  1144. )
  1145. def test_read_only_no_change(self):
  1146. """Test the read-only function of pagure-admin to set the status of
  1147. a namespaced project.
  1148. """
  1149. # Before
  1150. args = munch.Munch({"project": "test", "user": None, "ro": None})
  1151. with tests.capture_output() as output:
  1152. pagure.cli.admin.do_read_only(args)
  1153. output = output.getvalue()
  1154. self.assertEqual(
  1155. "The current read-only flag of the project test "
  1156. "is set to True\n",
  1157. output,
  1158. )
  1159. args = munch.Munch({"project": "test", "user": None, "ro": "true"})
  1160. with tests.capture_output() as output:
  1161. pagure.cli.admin.do_read_only(args)
  1162. output = output.getvalue()
  1163. self.assertEqual(
  1164. "The read-only flag of the project test has been " "set to True\n",
  1165. output,
  1166. )
  1167. # After
  1168. args = munch.Munch({"project": "test", "user": None, "ro": None})
  1169. with tests.capture_output() as output:
  1170. pagure.cli.admin.do_read_only(args)
  1171. output = output.getvalue()
  1172. self.assertEqual(
  1173. "The current read-only flag of the project test "
  1174. "is set to True\n",
  1175. output,
  1176. )
  1177. class PagureNewGroupTests(tests.Modeltests):
  1178. """Tests for pagure-admin new-group"""
  1179. populate_db = False
  1180. def setUp(self):
  1181. """Set up the environnment, ran before every tests."""
  1182. super(PagureNewGroupTests, self).setUp()
  1183. pagure.cli.admin.session = self.session
  1184. # Create the user pingou
  1185. item = pagure.lib.model.User(
  1186. user="pingou",
  1187. fullname="PY C",
  1188. password="foo",
  1189. default_email="bar@pingou.com",
  1190. )
  1191. self.session.add(item)
  1192. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1193. self.session.add(item)
  1194. self.session.commit()
  1195. # Make the imported pagure use the correct db session
  1196. pagure.cli.admin.session = self.session
  1197. groups = pagure.lib.query.search_groups(self.session)
  1198. self.assertEqual(len(groups), 0)
  1199. def test_missing_display_name(self):
  1200. """Test the new-group function of pagure-admin when the display name
  1201. is missing from the args.
  1202. """
  1203. args = munch.Munch(
  1204. {
  1205. "group_name": "foob",
  1206. "display": None,
  1207. "description": None,
  1208. "username": "pingou",
  1209. }
  1210. )
  1211. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1212. pagure.cli.admin.do_new_group(args)
  1213. self.assertEqual(
  1214. cm.exception.args[0],
  1215. "A display name must be provided for the group",
  1216. )
  1217. groups = pagure.lib.query.search_groups(self.session)
  1218. self.assertEqual(len(groups), 0)
  1219. def test_missing_username(self):
  1220. """Test the new-group function of pagure-admin when the username
  1221. is missing from the args.
  1222. """
  1223. args = munch.Munch(
  1224. {
  1225. "group_name": "foob",
  1226. "display": "foo group",
  1227. "description": None,
  1228. "username": None,
  1229. }
  1230. )
  1231. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1232. pagure.cli.admin.do_new_group(args)
  1233. self.assertEqual(
  1234. cm.exception.args[0],
  1235. "An username must be provided to associate with the group",
  1236. )
  1237. groups = pagure.lib.query.search_groups(self.session)
  1238. self.assertEqual(len(groups), 0)
  1239. def test_new_group(self):
  1240. """Test the new-group function of pagure-admin when all arguments
  1241. are provided.
  1242. """
  1243. args = munch.Munch(
  1244. {
  1245. "group_name": "foob",
  1246. "display": "foo group",
  1247. "description": None,
  1248. "username": "pingou",
  1249. }
  1250. )
  1251. pagure.cli.admin.do_new_group(args)
  1252. groups = pagure.lib.query.search_groups(self.session)
  1253. self.assertEqual(len(groups), 1)
  1254. @patch.dict("pagure.config.config", {"ENABLE_GROUP_MNGT": False})
  1255. @patch("pagure.cli.admin._ask_confirmation")
  1256. def test_new_group_grp_mngt_off_no(self, conf):
  1257. """Test the new-group function of pagure-admin when all arguments
  1258. are provided and ENABLE_GROUP_MNGT if off in the config and the user
  1259. replies no to the question.
  1260. """
  1261. conf.return_value = False
  1262. args = munch.Munch(
  1263. {
  1264. "group_name": "foob",
  1265. "display": "foo group",
  1266. "description": None,
  1267. "username": "pingou",
  1268. }
  1269. )
  1270. pagure.cli.admin.do_new_group(args)
  1271. groups = pagure.lib.query.search_groups(self.session)
  1272. self.assertEqual(len(groups), 0)
  1273. @patch.dict("pagure.config.config", {"ENABLE_GROUP_MNGT": False})
  1274. @patch("pagure.cli.admin._ask_confirmation")
  1275. def test_new_group_grp_mngt_off_yes(self, conf):
  1276. """Test the new-group function of pagure-admin when all arguments
  1277. are provided and ENABLE_GROUP_MNGT if off in the config and the user
  1278. replies yes to the question.
  1279. """
  1280. conf.return_value = True
  1281. args = munch.Munch(
  1282. {
  1283. "group_name": "foob",
  1284. "display": "foo group",
  1285. "description": None,
  1286. "username": "pingou",
  1287. }
  1288. )
  1289. pagure.cli.admin.do_new_group(args)
  1290. groups = pagure.lib.query.search_groups(self.session)
  1291. self.assertEqual(len(groups), 1)
  1292. @patch.dict("pagure.config.config", {"BLACKLISTED_GROUPS": ["foob"]})
  1293. def test_new_group_grp_mngt_off_yes(self):
  1294. """Test the new-group function of pagure-admin when all arguments
  1295. are provided but the group is black listed.
  1296. """
  1297. args = munch.Munch(
  1298. {
  1299. "group_name": "foob",
  1300. "display": "foo group",
  1301. "description": None,
  1302. "username": "pingou",
  1303. }
  1304. )
  1305. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1306. pagure.cli.admin.do_new_group(args)
  1307. self.assertEqual(
  1308. cm.exception.args[0],
  1309. "This group name has been blacklisted, please choose another one",
  1310. )
  1311. groups = pagure.lib.query.search_groups(self.session)
  1312. self.assertEqual(len(groups), 0)
  1313. class PagureListGroupEmptyTests(tests.Modeltests):
  1314. """Tests for pagure-admin list-groups"""
  1315. populate_db = False
  1316. def setUp(self):
  1317. """Set up the environnment, ran before every tests."""
  1318. super(PagureListGroupEmptyTests, self).setUp()
  1319. pagure.cli.admin.session = self.session
  1320. # Create the user pingou
  1321. item = pagure.lib.model.User(
  1322. user="pingou",
  1323. fullname="PY C",
  1324. password="foo",
  1325. default_email="bar@pingou.com",
  1326. )
  1327. self.session.add(item)
  1328. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1329. self.session.add(item)
  1330. self.session.commit()
  1331. # Make the imported pagure use the correct db session
  1332. pagure.cli.admin.session = self.session
  1333. groups = pagure.lib.query.search_groups(self.session)
  1334. self.assertEqual(len(groups), 0)
  1335. @patch("sys.stdout", new_callable=StringIO)
  1336. def test_no_groups(self, mock_stdout):
  1337. """Test the list-groups function of pagure-admin when there are no
  1338. groups in the database
  1339. """
  1340. args = munch.Munch()
  1341. pagure.cli.admin.do_list_groups(args)
  1342. self.assertEqual(
  1343. mock_stdout.getvalue(),
  1344. "No groups found in this pagure instance.\n",
  1345. )
  1346. groups = pagure.lib.query.search_groups(self.session)
  1347. self.assertEqual(len(groups), 0)
  1348. class PagureListGroupTests(tests.Modeltests):
  1349. """Tests for pagure-admin list-groups"""
  1350. populate_db = False
  1351. def setUp(self):
  1352. """Set up the environnment, ran before every tests."""
  1353. super(PagureListGroupTests, self).setUp()
  1354. pagure.cli.admin.session = self.session
  1355. # Create the user pingou
  1356. item = pagure.lib.model.User(
  1357. user="pingou",
  1358. fullname="PY C",
  1359. password="foo",
  1360. default_email="bar@pingou.com",
  1361. )
  1362. self.session.add(item)
  1363. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1364. self.session.add(item)
  1365. # Create a group
  1366. pagure.lib.query.add_group(
  1367. self.session,
  1368. group_name="JL",
  1369. display_name="Justice League",
  1370. description="Nope, it's not JLA anymore",
  1371. group_type="user",
  1372. user="pingou",
  1373. is_admin=False,
  1374. blacklist=[],
  1375. )
  1376. self.session.commit()
  1377. # Make the imported pagure use the correct db session
  1378. pagure.cli.admin.session = self.session
  1379. groups = pagure.lib.query.search_groups(self.session)
  1380. self.assertEqual(len(groups), 1)
  1381. @patch("sys.stdout", new_callable=StringIO)
  1382. def test_list_groups(self, mock_stdout):
  1383. """Test the list-groups function of pagure-admin when there is one
  1384. group in the database
  1385. """
  1386. args = munch.Munch()
  1387. pagure.cli.admin.do_list_groups(args)
  1388. self.assertEqual(
  1389. mock_stdout.getvalue(),
  1390. "List of groups on this Pagure instance:\n" "Group: 1 - name JL\n",
  1391. )
  1392. groups = pagure.lib.query.search_groups(self.session)
  1393. self.assertEqual(len(groups), 1)
  1394. class PagureBlockUserTests(tests.Modeltests):
  1395. """Tests for pagure-admin block-user"""
  1396. populate_db = False
  1397. def setUp(self):
  1398. """Set up the environnment, ran before every tests."""
  1399. super(PagureBlockUserTests, self).setUp()
  1400. pagure.cli.admin.session = self.session
  1401. # Create the user pingou
  1402. item = pagure.lib.model.User(
  1403. user="pingou",
  1404. fullname="PY C",
  1405. password="foo",
  1406. default_email="bar@pingou.com",
  1407. )
  1408. self.session.add(item)
  1409. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1410. self.session.add(item)
  1411. self.session.commit()
  1412. # Make the imported pagure use the correct db session
  1413. pagure.cli.admin.session = self.session
  1414. user = pagure.lib.query.get_user(self.session, "pingou")
  1415. self.assertIsNone(user.refuse_sessions_before)
  1416. def test_missing_date(self):
  1417. """Test the block-user function of pagure-admin when the no date is
  1418. provided.
  1419. """
  1420. args = munch.Munch({"username": "pingou", "date": None, "list": False})
  1421. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1422. pagure.cli.admin.do_block_user(args)
  1423. self.assertEqual(
  1424. cm.exception.args[0],
  1425. "Invalid date submitted: None, not of the format YYYY-MM-DD",
  1426. )
  1427. user = pagure.lib.query.get_user(self.session, "pingou")
  1428. self.assertIsNone(user.refuse_sessions_before)
  1429. def test_missing_username(self):
  1430. """Test the block-user function of pagure-admin when the username
  1431. is missing from the args.
  1432. """
  1433. args = munch.Munch(
  1434. {"date": "2018-06-11", "username": None, "list": False}
  1435. )
  1436. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1437. pagure.cli.admin.do_block_user(args)
  1438. self.assertEqual(cm.exception.args[0], "An username must be specified")
  1439. user = pagure.lib.query.get_user(self.session, "pingou")
  1440. self.assertIsNone(user.refuse_sessions_before)
  1441. def test_invalid_username(self):
  1442. """Test the block-user function of pagure-admin when the username
  1443. provided does correspond to any user in the DB.
  1444. """
  1445. args = munch.Munch(
  1446. {"date": "2018-06-11", "username": "invalid", "list": False}
  1447. )
  1448. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1449. pagure.cli.admin.do_block_user(args)
  1450. self.assertEqual(cm.exception.args[0], 'No user "invalid" found')
  1451. user = pagure.lib.query.get_user(self.session, "pingou")
  1452. self.assertIsNone(user.refuse_sessions_before)
  1453. def test_invalide_date(self):
  1454. """Test the block-user function of pagure-admin when the provided
  1455. date is incorrect.
  1456. """
  1457. args = munch.Munch(
  1458. {"date": "2018-14-05", "username": "pingou", "list": False}
  1459. )
  1460. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1461. pagure.cli.admin.do_block_user(args)
  1462. self.assertEqual(
  1463. cm.exception.args[0],
  1464. "Invalid date submitted: 2018-14-05, not of the format YYYY-MM-DD",
  1465. )
  1466. user = pagure.lib.query.get_user(self.session, "pingou")
  1467. self.assertIsNone(user.refuse_sessions_before)
  1468. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1469. def test_block_user(self):
  1470. """Test the block-user function of pagure-admin when all arguments
  1471. are provided correctly.
  1472. """
  1473. args = munch.Munch(
  1474. {"date": "2050-12-31", "username": "pingou", "list": False}
  1475. )
  1476. pagure.cli.admin.do_block_user(args)
  1477. user = pagure.lib.query.get_user(self.session, "pingou")
  1478. self.assertIsNotNone(user.refuse_sessions_before)
  1479. def test_list_blocked_user(self):
  1480. """Test the block-user function of pagure-admin when all arguments
  1481. are provided correctly.
  1482. """
  1483. args = munch.Munch({"list": True, "username": None, "date": None})
  1484. with tests.capture_output() as output:
  1485. pagure.cli.admin.do_block_user(args)
  1486. output = output.getvalue()
  1487. self.assertEqual("No users are currently blocked\n", output)
  1488. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1489. def test_list_blocked_user_with_data(self):
  1490. """Test the block-user function of pagure-admin when all arguments
  1491. are provided correctly.
  1492. """
  1493. args = munch.Munch(
  1494. {"date": "2050-12-31", "username": "pingou", "list": False}
  1495. )
  1496. pagure.cli.admin.do_block_user(args)
  1497. args = munch.Munch({"list": True, "username": None, "date": None})
  1498. with tests.capture_output() as output:
  1499. pagure.cli.admin.do_block_user(args)
  1500. output = output.getvalue()
  1501. self.assertEqual(
  1502. "Users blocked:\n"
  1503. " pingou - 2050-12-31T00:00:00\n",
  1504. output,
  1505. )
  1506. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1507. def test_list_blocked_user_with_username_data(self):
  1508. """Test the block-user function of pagure-admin when all arguments
  1509. are provided correctly.
  1510. """
  1511. args = munch.Munch(
  1512. {"date": "2050-12-31", "username": "pingou", "list": False}
  1513. )
  1514. pagure.cli.admin.do_block_user(args)
  1515. args = munch.Munch({"list": True, "username": "ralph", "date": None})
  1516. with tests.capture_output() as output:
  1517. pagure.cli.admin.do_block_user(args)
  1518. output = output.getvalue()
  1519. self.assertEqual("No users are currently blocked\n", output)
  1520. args = munch.Munch({"list": True, "username": "pin*", "date": None})
  1521. with tests.capture_output() as output:
  1522. pagure.cli.admin.do_block_user(args)
  1523. output = output.getvalue()
  1524. self.assertEqual(
  1525. "Users blocked:\n"
  1526. " pingou - 2050-12-31T00:00:00\n",
  1527. output,
  1528. )
  1529. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1530. def test_list_blocked_user_with_date(self):
  1531. """Test the block-user function of pagure-admin when all arguments
  1532. are provided correctly.
  1533. """
  1534. args = munch.Munch(
  1535. {"list": True, "username": None, "date": "2050-12-31"}
  1536. )
  1537. with tests.capture_output() as output:
  1538. pagure.cli.admin.do_block_user(args)
  1539. output = output.getvalue()
  1540. self.assertIn("No users are currently blocked\n", output)
  1541. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1542. def test_list_blocked_user_with_date_and_data(self):
  1543. """Test the block-user function of pagure-admin when all arguments
  1544. are provided correctly.
  1545. """
  1546. args = munch.Munch(
  1547. {"date": "2050-12-31", "username": "pingou", "list": False}
  1548. )
  1549. pagure.cli.admin.do_block_user(args)
  1550. args = munch.Munch(
  1551. {"list": True, "username": None, "date": "2050-12-31"}
  1552. )
  1553. with tests.capture_output() as output:
  1554. pagure.cli.admin.do_block_user(args)
  1555. output = output.getvalue()
  1556. self.assertIn(
  1557. "Users blocked:\n"
  1558. " pingou - 2050-12-31T00:00:00\n",
  1559. output,
  1560. )
  1561. args = munch.Munch(
  1562. {"list": True, "username": None, "date": "2051-01-01"}
  1563. )
  1564. with tests.capture_output() as output:
  1565. pagure.cli.admin.do_block_user(args)
  1566. output = output.getvalue()
  1567. self.assertIn("No users are currently blocked\n", output)
  1568. class PagureAdminDeleteProjectTests(tests.Modeltests):
  1569. """Tests for pagure-admin delete-project"""
  1570. populate_db = False
  1571. def setUp(self):
  1572. """Set up the environnment, ran before every tests."""
  1573. super(PagureAdminDeleteProjectTests, self).setUp()
  1574. pagure.cli.admin.session = self.session
  1575. # Create the user pingou
  1576. item = pagure.lib.model.User(
  1577. user="pingou",
  1578. fullname="PY C",
  1579. password="foo",
  1580. default_email="bar@pingou.com",
  1581. )
  1582. self.session.add(item)
  1583. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1584. self.session.add(item)
  1585. # Create two projects for the user pingou
  1586. item = pagure.lib.model.Project(
  1587. user_id=1, # pingou
  1588. name="test",
  1589. description="namespaced test project",
  1590. hook_token="aaabbbeee",
  1591. namespace="somenamespace",
  1592. )
  1593. self.session.add(item)
  1594. item = pagure.lib.model.Project(
  1595. user_id=1, # pingou
  1596. name="test",
  1597. description="Test project",
  1598. hook_token="aaabbbccc",
  1599. namespace=None,
  1600. )
  1601. self.session.add(item)
  1602. self.session.commit()
  1603. # Make the imported pagure use the correct db session
  1604. pagure.cli.admin.session = self.session
  1605. def test_delete_project_unknown_project(self):
  1606. """Test the read-only function of pagure-admin on an unknown
  1607. project.
  1608. """
  1609. args = munch.Munch(
  1610. {"project": "foob", "user": None, "action_user": "pingou"}
  1611. )
  1612. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1613. pagure.cli.admin.do_delete_project(args)
  1614. self.assertEqual(
  1615. cm.exception.args[0],
  1616. "No project found with: project=foob, user=None",
  1617. )
  1618. def test_delete_project_invalid_project(self):
  1619. """Test the read-only function of pagure-admin on an invalid
  1620. project.
  1621. """
  1622. args = munch.Munch(
  1623. {"project": "fo/o/b", "user": None, "action_user": "pingou"}
  1624. )
  1625. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1626. pagure.cli.admin.do_delete_project(args)
  1627. self.assertEqual(
  1628. cm.exception.args[0],
  1629. 'Invalid project name, has more than one "/": fo/o/b',
  1630. )
  1631. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1632. def test_delete_project(self):
  1633. """Test the read-only function of pagure-admin to get status of
  1634. a non-namespaced project.
  1635. """
  1636. args = munch.Munch(
  1637. {"project": "test", "user": None, "action_user": "pingou"}
  1638. )
  1639. with tests.capture_output() as output:
  1640. pagure.cli.admin.do_delete_project(args)
  1641. output = output.getvalue()
  1642. self.assertEqual(
  1643. "Are you sure you want to delete: test?\n"
  1644. " This cannot be undone!\n"
  1645. "Project deleted\n",
  1646. output,
  1647. )
  1648. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1649. def test_delete_project_namespace(self):
  1650. """Test the read-only function of pagure-admin to get status of
  1651. a namespaced project.
  1652. """
  1653. args = munch.Munch(
  1654. {
  1655. "project": "somenamespace/test",
  1656. "user": None,
  1657. "action_user": "pingou",
  1658. }
  1659. )
  1660. with tests.capture_output() as output:
  1661. pagure.cli.admin.do_delete_project(args)
  1662. output = output.getvalue()
  1663. self.assertEqual(
  1664. "Are you sure you want to delete: somenamespace/test?\n"
  1665. " This cannot be undone!\n"
  1666. "Project deleted\n",
  1667. output,
  1668. )
  1669. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1670. def test_delete_project_namespace_changed(self):
  1671. """Test the read-only function of pagure-admin to set the status of
  1672. a namespaced project.
  1673. """
  1674. # Before
  1675. projects = pagure.lib.query.search_projects(self.session)
  1676. self.assertEqual(len(projects), 2)
  1677. args = munch.Munch(
  1678. {
  1679. "project": "somenamespace/test",
  1680. "user": None,
  1681. "action_user": "pingou",
  1682. }
  1683. )
  1684. with tests.capture_output() as output:
  1685. pagure.cli.admin.do_delete_project(args)
  1686. output = output.getvalue()
  1687. self.assertEqual(
  1688. "Are you sure you want to delete: somenamespace/test?\n"
  1689. " This cannot be undone!\n"
  1690. "Project deleted\n",
  1691. output,
  1692. )
  1693. # After
  1694. projects = pagure.lib.query.search_projects(self.session)
  1695. self.assertEqual(len(projects), 1)
  1696. class PagureCreateBranchTests(tests.Modeltests):
  1697. """Tests for pagure-admin create-branch"""
  1698. populate_db = True
  1699. def setUp(self):
  1700. """Set up the environnment, ran before every tests."""
  1701. super(PagureCreateBranchTests, self).setUp()
  1702. # Create a couple of projects
  1703. tests.create_projects(self.session)
  1704. # Create their git repo
  1705. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1706. # Make the imported pagure use the correct db session
  1707. pagure.cli.admin.session = self.session
  1708. def test_create_branch_unknown_project(self):
  1709. """Test the read-only function of pagure-admin on an unknown
  1710. project.
  1711. """
  1712. args = munch.Munch(
  1713. {
  1714. "project": "foob",
  1715. "user": None,
  1716. "new_branch": "new_branch",
  1717. "from_branch": "master",
  1718. "from_commit": None,
  1719. "action_user": "pingou",
  1720. }
  1721. )
  1722. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1723. pagure.cli.admin.do_create_branch(args)
  1724. self.assertEqual(
  1725. cm.exception.args[0],
  1726. "No project found with: project=foob, user=None",
  1727. )
  1728. def test_create_branch_invalid_project(self):
  1729. """Test the read-only function of pagure-admin on an invalid
  1730. project.
  1731. """
  1732. args = munch.Munch(
  1733. {
  1734. "project": "f/o/o/b",
  1735. "user": None,
  1736. "new_branch": "new_branch",
  1737. "from_branch": "master",
  1738. "from_commit": None,
  1739. "action_user": "pingou",
  1740. }
  1741. )
  1742. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1743. pagure.cli.admin.do_create_branch(args)
  1744. self.assertEqual(
  1745. cm.exception.args[0],
  1746. 'Invalid project name, has more than one "/": f/o/o/b',
  1747. )
  1748. def test_create_branch_commit_and_branch_from(self):
  1749. """Test the read-only function of pagure-admin to get status of
  1750. a non-namespaced project.
  1751. """
  1752. args = munch.Munch(
  1753. {
  1754. "project": "test",
  1755. "user": None,
  1756. "new_branch": "new_branch",
  1757. "from_branch": "master",
  1758. "from_commit": "foobar",
  1759. "action_user": "pingou",
  1760. }
  1761. )
  1762. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1763. pagure.cli.admin.do_create_branch(args)
  1764. self.assertEqual(
  1765. cm.exception.args[0],
  1766. "You must create the branch from something, either a commit "
  1767. "or another branch, not from both",
  1768. )
  1769. def test_create_branch_no_branch_from(self):
  1770. """Test the read-only function of pagure-admin to get status of
  1771. a non-namespaced project.
  1772. """
  1773. args = munch.Munch(
  1774. {
  1775. "project": "test",
  1776. "user": None,
  1777. "new_branch": "new_branch",
  1778. "from_branch": "master",
  1779. "from_commit": None,
  1780. "action_user": "pingou",
  1781. }
  1782. )
  1783. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1784. pagure.cli.admin.do_create_branch(args)
  1785. self.assertEqual(
  1786. cm.exception.args[0], 'The "master" branch does not exist'
  1787. )
  1788. def test_create_branch_no_commit_from(self):
  1789. """Test the read-only function of pagure-admin to get status of
  1790. a non-namespaced project.
  1791. """
  1792. args = munch.Munch(
  1793. {
  1794. "project": "test",
  1795. "user": None,
  1796. "new_branch": "new_branch",
  1797. "from_branch": None,
  1798. "from_commit": "foobar",
  1799. "action_user": "pingou",
  1800. }
  1801. )
  1802. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1803. pagure.cli.admin.do_create_branch(args)
  1804. self.assertEqual(
  1805. cm.exception.args[0], "No commit foobar found from which to branch"
  1806. )
  1807. def test_create_branch_from_branch(self):
  1808. """Test the do_create_admin_token function of pagure-admin."""
  1809. gitrepo_path = os.path.join(self.path, "repos", "test.git")
  1810. tests.add_content_git_repo(gitrepo_path)
  1811. # Check branch before:
  1812. gitrepo = pygit2.Repository(gitrepo_path)
  1813. self.assertEqual(gitrepo.listall_branches(), ["master"])
  1814. args = munch.Munch(
  1815. {
  1816. "project": "test",
  1817. "user": None,
  1818. "new_branch": "new_branch",
  1819. "from_branch": "master",
  1820. "from_commit": None,
  1821. "action_user": "pingou",
  1822. }
  1823. )
  1824. with tests.capture_output() as output:
  1825. pagure.cli.admin.do_create_branch(args)
  1826. output = output.getvalue()
  1827. self.assertEqual("Branch created\n", output)
  1828. # Check branch after:
  1829. gitrepo = pygit2.Repository(gitrepo_path)
  1830. self.assertEqual(
  1831. sorted(gitrepo.listall_branches()), ["master", "new_branch"]
  1832. )
  1833. class PagureSetDefaultBranchTests(tests.Modeltests):
  1834. """Tests for pagure-admin set-default-branch"""
  1835. populate_db = True
  1836. def setUp(self):
  1837. """Set up the environment, run before every tests."""
  1838. super(PagureSetDefaultBranchTests, self).setUp()
  1839. # Create a couple of projects
  1840. tests.create_projects(self.session)
  1841. # Create their git repo
  1842. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1843. # Make the imported pagure use the correct db session
  1844. pagure.cli.admin.session = self.session
  1845. def test_set_default_branch_unknown_project(self):
  1846. """Test the set-default-branch function of pagure-admin on an unknown
  1847. project.
  1848. """
  1849. args = munch.Munch(
  1850. {"project": "foob", "user": None, "branch": "master"}
  1851. )
  1852. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1853. pagure.cli.admin.do_set_default_branch(args)
  1854. self.assertEqual(
  1855. cm.exception.args[0],
  1856. "No project found with: project=foob, user=None",
  1857. )
  1858. def test_set_default_branch_invalid_project(self):
  1859. """Test the set-default-branch function of pagure-admin on an invalid
  1860. project.
  1861. """
  1862. args = munch.Munch(
  1863. {"project": "f/o/o/b", "user": None, "branch": "master"}
  1864. )
  1865. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1866. pagure.cli.admin.do_set_default_branch(args)
  1867. self.assertEqual(
  1868. cm.exception.args[0],
  1869. 'Invalid project name, has more than one "/": f/o/o/b',
  1870. )
  1871. def test_set_default_branch_unknown_branch(self):
  1872. """Test the set-default-branch function of pagure-admin on an unknown
  1873. branch.
  1874. """
  1875. args = munch.Munch({"project": "test", "user": None, "branch": "foob"})
  1876. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1877. pagure.cli.admin.do_set_default_branch(args)
  1878. self.assertEqual(
  1879. cm.exception.args[0], "No foob branch found on project: test"
  1880. )
  1881. def test_set_default_branch_invalid_branch(self):
  1882. """Test the set-default-branch function of pagure-admin on an invalid branch."""
  1883. args = munch.Munch(
  1884. {"project": "test", "user": None, "branch": "~invalid~"}
  1885. )
  1886. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1887. pagure.cli.admin.do_set_default_branch(args)
  1888. self.assertEqual(
  1889. cm.exception.args[0], "No ~invalid~ branch found on project: test"
  1890. )
  1891. def test_set_default_branch(self):
  1892. """Test the set-default-branch funcion of pagure-admin."""
  1893. gitrepo_path = os.path.join(self.path, "repos", "test.git")
  1894. tests.add_content_git_repo(gitrepo_path)
  1895. tests.add_commit_git_repo(gitrepo_path, branch="dev")
  1896. # Check default branch before:
  1897. gitrepo = pygit2.Repository(gitrepo_path)
  1898. self.assertEqual(gitrepo.head.shorthand, "master")
  1899. args = munch.Munch({"project": "test", "user": None, "branch": "dev"})
  1900. with tests.capture_output() as output:
  1901. pagure.cli.admin.do_set_default_branch(args)
  1902. output = output.getvalue()
  1903. self.assertEqual("Branch dev set as default\n", output)
  1904. # Check default branch after:
  1905. self.assertEqual(gitrepo.head.shorthand, "dev")
  1906. class PagureAdminUpdateAclsTests(tests.Modeltests):
  1907. """Tests for pagure-admin update-acls"""
  1908. populate_db = False
  1909. maxDiff = None
  1910. def setUp(self):
  1911. """Set up the environnment, ran before every tests."""
  1912. super(PagureAdminUpdateAclsTests, self).setUp()
  1913. pagure.cli.admin.session = self.session
  1914. # Make the imported pagure use the correct db session
  1915. pagure.cli.admin.session = self.session
  1916. def test_update_acls(self):
  1917. """Test the update-acls function of pagure-admin."""
  1918. args = munch.Munch()
  1919. with tests.capture_output() as output:
  1920. pagure.cli.admin.do_update_acls(args)
  1921. output = output.getvalue()
  1922. self.assertEqual(
  1923. "ACLS in the database synced with the list in the configuration file\n",
  1924. output,
  1925. )
  1926. acls = pagure.lib.query.get_acls(self.session)
  1927. self.assertEqual(
  1928. [a.name for a in acls],
  1929. [
  1930. "commit",
  1931. "commit_flag",
  1932. "create_branch",
  1933. "create_git_alias",
  1934. "create_project",
  1935. "delete_git_alias",
  1936. "fork_project",
  1937. "generate_acls_project",
  1938. "group_modify",
  1939. "internal_access",
  1940. "issue_assign",
  1941. "issue_change_status",
  1942. "issue_comment",
  1943. "issue_create",
  1944. "issue_subscribe",
  1945. "issue_update",
  1946. "issue_update_custom_fields",
  1947. "issue_update_milestone",
  1948. "modify_git_alias",
  1949. "modify_project",
  1950. "pull_request_assign",
  1951. "pull_request_close",
  1952. "pull_request_comment",
  1953. "pull_request_create",
  1954. "pull_request_flag",
  1955. "pull_request_merge",
  1956. "pull_request_rebase",
  1957. "pull_request_subscribe",
  1958. "pull_request_update",
  1959. "tag_project",
  1960. "update_watch_status",
  1961. ],
  1962. )
  1963. @patch.dict(
  1964. "pagure.cli.admin._config",
  1965. {"ACLS": {"dummy_acls": "Dummy ACL for testing"}},
  1966. )
  1967. def test_update_acls_new_acls(self):
  1968. args = munch.Munch({"debug": True})
  1969. with tests.capture_output() as output:
  1970. pagure.cli.admin.do_update_acls(args)
  1971. output = output.getvalue()
  1972. self.assertEqual(
  1973. "ACLS in the database synced with the list in the configuration file\n",
  1974. output,
  1975. )
  1976. acls = pagure.lib.query.get_acls(self.session)
  1977. self.assertEqual(
  1978. [a.name for a in acls],
  1979. [
  1980. "commit",
  1981. "commit_flag",
  1982. "create_branch",
  1983. "create_git_alias",
  1984. "create_project",
  1985. "delete_git_alias",
  1986. "dummy_acls",
  1987. "fork_project",
  1988. "generate_acls_project",
  1989. "group_modify",
  1990. "internal_access",
  1991. "issue_assign",
  1992. "issue_change_status",
  1993. "issue_comment",
  1994. "issue_create",
  1995. "issue_subscribe",
  1996. "issue_update",
  1997. "issue_update_custom_fields",
  1998. "issue_update_milestone",
  1999. "modify_git_alias",
  2000. "modify_project",
  2001. "pull_request_assign",
  2002. "pull_request_close",
  2003. "pull_request_comment",
  2004. "pull_request_create",
  2005. "pull_request_flag",
  2006. "pull_request_merge",
  2007. "pull_request_rebase",
  2008. "pull_request_subscribe",
  2009. "pull_request_update",
  2010. "tag_project",
  2011. "update_watch_status",
  2012. ],
  2013. )
  2014. if __name__ == "__main__":
  2015. unittest.main(verbosity=2)