1
0

test_pagure_lib.py 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import unittest
  10. import shutil
  11. import sys
  12. import os
  13. from mock import patch
  14. sys.path.insert(0, os.path.join(os.path.dirname(
  15. os.path.abspath(__file__)), '..'))
  16. import pagure.lib
  17. import pagure.lib.model
  18. import tests
  19. class PagureLibtests(tests.Modeltests):
  20. """ Tests for pagure.lib """
  21. def test_get_next_id(self):
  22. """ Test the get_next_id function of pagure.lib. """
  23. tests.create_projects(self.session)
  24. self.assertEqual(1, pagure.lib.get_next_id(self.session, 1))
  25. def test_search_user_all(self):
  26. """ Test the search_user of pagure.lib. """
  27. # Retrieve all users
  28. items = pagure.lib.search_user(self.session)
  29. self.assertEqual(2, len(items))
  30. self.assertEqual(2, items[0].id)
  31. self.assertEqual('foo', items[0].user)
  32. self.assertEqual('foo', items[0].username)
  33. self.assertEqual([], items[1].groups)
  34. self.assertEqual(1, items[1].id)
  35. self.assertEqual('pingou', items[1].user)
  36. self.assertEqual('pingou', items[1].username)
  37. self.assertEqual([], items[1].groups)
  38. def test_search_user_username(self):
  39. """ Test the search_user of pagure.lib. """
  40. # Retrieve user by username
  41. item = pagure.lib.search_user(self.session, username='foo')
  42. self.assertEqual('foo', item.user)
  43. self.assertEqual('foo', item.username)
  44. self.assertEqual([], item.groups)
  45. item = pagure.lib.search_user(self.session, username='bar')
  46. self.assertEqual(None, item)
  47. def test_search_user_email(self):
  48. """ Test the search_user of pagure.lib. """
  49. # Retrieve user by email
  50. item = pagure.lib.search_user(self.session, email='foo@foo.com')
  51. self.assertEqual(None, item)
  52. item = pagure.lib.search_user(self.session, email='foo@bar.com')
  53. self.assertEqual('foo', item.user)
  54. self.assertEqual('foo', item.username)
  55. self.assertEqual([], item.groups)
  56. self.assertEqual(
  57. ['foo@bar.com'], [email.email for email in item.emails])
  58. item = pagure.lib.search_user(self.session, email='foo@pingou.com')
  59. self.assertEqual('pingou', item.user)
  60. self.assertEqual(
  61. ['bar@pingou.com', 'foo@pingou.com'],
  62. [email.email for email in item.emails])
  63. def test_search_user_token(self):
  64. """ Test the search_user of pagure.lib. """
  65. # Retrieve user by token
  66. item = pagure.lib.search_user(self.session, token='aaa')
  67. self.assertEqual(None, item)
  68. item = pagure.lib.model.User(
  69. user='pingou2',
  70. fullname='PY C',
  71. token='aaabbb',
  72. default_email='bar@pingou.com',
  73. )
  74. self.session.add(item)
  75. self.session.commit()
  76. item = pagure.lib.search_user(self.session, token='aaabbb')
  77. self.assertEqual('pingou2', item.user)
  78. self.assertEqual('PY C', item.fullname)
  79. def test_search_user_pattern(self):
  80. """ Test the search_user of pagure.lib. """
  81. # Retrieve user by pattern
  82. item = pagure.lib.search_user(self.session, pattern='a*')
  83. self.assertEqual([], item)
  84. item = pagure.lib.model.User(
  85. user='pingou2',
  86. fullname='PY C',
  87. token='aaabbb',
  88. default_email='bar@pingou.com',
  89. )
  90. self.session.add(item)
  91. self.session.commit()
  92. items = pagure.lib.search_user(self.session, pattern='p*')
  93. self.assertEqual(2, len(items))
  94. self.assertEqual(1, items[0].id)
  95. self.assertEqual('pingou', items[0].user)
  96. self.assertEqual('pingou', items[0].username)
  97. self.assertEqual([], items[0].groups)
  98. self.assertEqual(
  99. ['bar@pingou.com', 'foo@pingou.com'],
  100. [email.email for email in items[0].emails])
  101. self.assertEqual(3, items[1].id)
  102. self.assertEqual('pingou2', items[1].user)
  103. self.assertEqual('pingou2', items[1].username)
  104. self.assertEqual([], items[1].groups)
  105. self.assertEqual(
  106. [], [email.email for email in items[1].emails])
  107. @patch('pagure.lib.git.update_git')
  108. @patch('pagure.lib.notify.send_email')
  109. def test_new_issue(self, p_send_email, p_ugt):
  110. """ Test the new_issue of pagure.lib. """
  111. p_send_email.return_value = True
  112. p_ugt.return_value = True
  113. tests.create_projects(self.session)
  114. repo = pagure.lib.get_project(self.session, 'test')
  115. # Before
  116. issues = pagure.lib.search_issues(self.session, repo)
  117. self.assertEqual(len(issues), 0)
  118. self.assertEqual(repo.open_tickets, 0)
  119. self.assertEqual(repo.open_tickets_public, 0)
  120. # See where it fails
  121. self.assertRaises(
  122. pagure.exceptions.PagureException,
  123. pagure.lib.new_issue,
  124. session=self.session,
  125. repo=repo,
  126. title='Test issue',
  127. content='We should work on this',
  128. user='blah',
  129. ticketfolder=None
  130. )
  131. # Add an extra user to project `foo`
  132. repo = pagure.lib.get_project(self.session, 'test')
  133. msg = pagure.lib.add_user_to_project(
  134. session=self.session,
  135. project=repo,
  136. new_user='foo',
  137. user='pingou'
  138. )
  139. self.session.commit()
  140. self.assertEqual(msg, 'User added')
  141. # Try adding again this extra user to project `foo`
  142. self.assertRaises(
  143. pagure.exceptions.PagureException,
  144. pagure.lib.add_user_to_project,
  145. session=self.session,
  146. project=repo,
  147. new_user='foo',
  148. user='pingou'
  149. )
  150. self.session.commit()
  151. self.assertEqual(msg, 'User added')
  152. # Create issues to play with
  153. msg = pagure.lib.new_issue(
  154. session=self.session,
  155. repo=repo,
  156. title='Test issue',
  157. content='We should work on this',
  158. user='pingou',
  159. ticketfolder=None
  160. )
  161. self.session.commit()
  162. self.assertEqual(msg.title, 'Test issue')
  163. self.assertEqual(repo.open_tickets, 1)
  164. self.assertEqual(repo.open_tickets_public, 1)
  165. msg = pagure.lib.new_issue(
  166. session=self.session,
  167. repo=repo,
  168. title='Test issue #2',
  169. content='We should work on this for the second time',
  170. user='foo',
  171. status='Open',
  172. ticketfolder=None
  173. )
  174. self.session.commit()
  175. self.assertEqual(msg.title, 'Test issue #2')
  176. self.assertEqual(repo.open_tickets, 2)
  177. self.assertEqual(repo.open_tickets_public, 2)
  178. # After
  179. issues = pagure.lib.search_issues(self.session, repo)
  180. self.assertEqual(len(issues), 2)
  181. @patch('pagure.lib.git.update_git')
  182. @patch('pagure.lib.notify.send_email')
  183. def test_edit_issue(self, p_send_email, p_ugt):
  184. """ Test the edit_issue of pagure.lib. """
  185. p_send_email.return_value = True
  186. p_ugt.return_value = True
  187. self.test_new_issue()
  188. repo = pagure.lib.get_project(self.session, 'test')
  189. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  190. self.assertEqual(repo.open_tickets, 2)
  191. self.assertEqual(repo.open_tickets_public, 2)
  192. # Edit the issue
  193. msg = pagure.lib.edit_issue(
  194. session=self.session,
  195. issue=issue,
  196. user='pingou',
  197. ticketfolder=None)
  198. self.session.commit()
  199. self.assertEqual(msg, None)
  200. msg = pagure.lib.edit_issue(
  201. session=self.session,
  202. issue=issue,
  203. user='pingou',
  204. ticketfolder=None,
  205. title='Test issue #2',
  206. content='We should work on this for the second time',
  207. status='Open',
  208. )
  209. self.session.commit()
  210. self.assertEqual(msg, None)
  211. msg = pagure.lib.edit_issue(
  212. session=self.session,
  213. issue=issue,
  214. user='pingou',
  215. ticketfolder=None,
  216. title='Foo issue #2',
  217. content='We should work on this period',
  218. status='Invalid',
  219. private=True,
  220. )
  221. self.session.commit()
  222. self.assertEqual(msg, 'Successfully edited issue #2')
  223. self.assertEqual(repo.open_tickets, 1)
  224. self.assertEqual(repo.open_tickets_public, 1)
  225. @patch('pagure.lib.git.update_git')
  226. @patch('pagure.lib.notify.send_email')
  227. def test_add_issue_dependency(self, p_send_email, p_ugt):
  228. """ Test the add_issue_dependency of pagure.lib. """
  229. p_send_email.return_value = True
  230. p_ugt.return_value = True
  231. self.test_new_issue()
  232. repo = pagure.lib.get_project(self.session, 'test')
  233. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  234. issue_blocked = pagure.lib.search_issues(
  235. self.session, repo, issueid=2)
  236. # Before
  237. self.assertEqual(issue.parents, [])
  238. self.assertEqual(issue.children, [])
  239. self.assertEqual(issue_blocked.parents, [])
  240. self.assertEqual(issue_blocked.children, [])
  241. self.assertRaises(
  242. pagure.exceptions.PagureException,
  243. pagure.lib.add_issue_dependency,
  244. session=self.session,
  245. issue=issue,
  246. issue_blocked=issue,
  247. user='pingou',
  248. ticketfolder=None)
  249. msg = pagure.lib.add_issue_dependency(
  250. session=self.session,
  251. issue=issue,
  252. issue_blocked=issue_blocked,
  253. user='pingou',
  254. ticketfolder=None)
  255. self.session.commit()
  256. self.assertEqual(msg, 'Dependency added')
  257. # After
  258. self.assertEqual(len(issue.parents), 1)
  259. self.assertEqual(issue.parents[0].id, 2)
  260. self.assertEqual(len(issue.children), 0)
  261. self.assertEqual(issue.children, [])
  262. self.assertEqual(len(issue_blocked.parents), 0)
  263. self.assertEqual(issue_blocked.parents, [])
  264. self.assertEqual(len(issue_blocked.children), 1)
  265. self.assertEqual(issue_blocked.children[0].id, 1)
  266. @patch('pagure.lib.git.update_git')
  267. @patch('pagure.lib.notify.send_email')
  268. def test_add_tag_obj(self, p_send_email, p_ugt):
  269. """ Test the add_tag_obj of pagure.lib. """
  270. p_send_email.return_value = True
  271. p_ugt.return_value = True
  272. self.test_edit_issue()
  273. repo = pagure.lib.get_project(self.session, 'test')
  274. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  275. # Add a tag to the issue
  276. msg = pagure.lib.add_tag_obj(
  277. session=self.session,
  278. obj=issue,
  279. tags='tag1',
  280. user='pingou',
  281. ticketfolder=None)
  282. self.session.commit()
  283. self.assertEqual(msg, 'Tag added: tag1')
  284. # Try a second time
  285. msg = pagure.lib.add_tag_obj(
  286. session=self.session,
  287. obj=issue,
  288. tags='tag1',
  289. user='pingou',
  290. ticketfolder=None)
  291. self.session.commit()
  292. self.assertEqual(msg, 'Nothing to add')
  293. issues = pagure.lib.search_issues(self.session, repo, tags='tag1')
  294. self.assertEqual(len(issues), 1)
  295. self.assertEqual(issues[0].id, 1)
  296. self.assertEqual(issues[0].project_id, 1)
  297. self.assertEqual(issues[0].status, 'Open')
  298. self.assertEqual([tag.tag for tag in issues[0].tags], ['tag1'])
  299. @patch('pagure.lib.git.update_git')
  300. @patch('pagure.lib.notify.send_email')
  301. def test_remove_tags(self, p_send_email, p_ugt):
  302. """ Test the remove_tags of pagure.lib. """
  303. p_send_email.return_value = True
  304. p_ugt.return_value = True
  305. self.test_add_tag_obj()
  306. repo = pagure.lib.get_project(self.session, 'test')
  307. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  308. self.assertRaises(
  309. pagure.exceptions.PagureException,
  310. pagure.lib.remove_tags,
  311. session=self.session,
  312. project=repo,
  313. tags='foo',
  314. user='pingou',
  315. ticketfolder=None)
  316. msgs = pagure.lib.remove_tags(
  317. session=self.session,
  318. project=repo,
  319. tags='tag1',
  320. user='pingou',
  321. ticketfolder=None)
  322. self.assertEqual(msgs, [u'Removed tag: tag1'])
  323. @patch('pagure.lib.git.update_git')
  324. @patch('pagure.lib.notify.send_email')
  325. def test_remove_tags_obj(self, p_send_email, p_ugt):
  326. """ Test the remove_tags_obj of pagure.lib. """
  327. p_send_email.return_value = True
  328. p_ugt.return_value = True
  329. self.test_add_tag_obj()
  330. repo = pagure.lib.get_project(self.session, 'test')
  331. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  332. msgs = pagure.lib.remove_tags_obj(
  333. session=self.session,
  334. obj=issue,
  335. tags='tag1',
  336. user='pingou',
  337. ticketfolder=None)
  338. self.assertEqual(msgs, 'Removed tag: tag1')
  339. @patch('pagure.lib.git.update_git')
  340. @patch('pagure.lib.notify.send_email')
  341. def test_edit_issue_tags(self, p_send_email, p_ugt):
  342. """ Test the edit_issue_tags of pagure.lib. """
  343. p_send_email.return_value = True
  344. p_ugt.return_value = True
  345. self.test_add_tag_obj()
  346. repo = pagure.lib.get_project(self.session, 'test')
  347. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  348. self.assertRaises(
  349. pagure.exceptions.PagureException,
  350. pagure.lib.edit_issue_tags,
  351. session=self.session,
  352. project=repo,
  353. old_tag='foo',
  354. new_tag='bar',
  355. user='pingou',
  356. ticketfolder=None,
  357. )
  358. msgs = pagure.lib.edit_issue_tags(
  359. session=self.session,
  360. project=repo,
  361. old_tag='tag1',
  362. new_tag='tag2',
  363. user='pingou',
  364. ticketfolder=None,
  365. )
  366. self.session.commit()
  367. self.assertEqual(msgs, ['Edited tag: tag1 to tag2'])
  368. # Add a new tag
  369. msg = pagure.lib.add_tag_obj(
  370. session=self.session,
  371. obj=issue,
  372. tags='tag3',
  373. user='pingou',
  374. ticketfolder=None)
  375. self.session.commit()
  376. self.assertEqual(msg, 'Tag added: tag3')
  377. self.assertEqual([tag.tag for tag in issue.tags], ['tag2', 'tag3'])
  378. # Rename an existing tag into another existing one
  379. msgs = pagure.lib.edit_issue_tags(
  380. session=self.session,
  381. project=repo,
  382. old_tag='tag2',
  383. new_tag='tag3',
  384. user='pingou',
  385. ticketfolder=None,
  386. )
  387. self.session.commit()
  388. self.assertEqual(msgs, ['Edited tag: tag2 to tag3'])
  389. self.assertEqual([tag.tag for tag in issue.tags], ['tag3'])
  390. self.assertRaises(
  391. pagure.exceptions.PagureException,
  392. pagure.lib.edit_issue_tags,
  393. session=self.session,
  394. project=repo,
  395. old_tag='tag2',
  396. new_tag='tag2',
  397. user='pingou',
  398. ticketfolder=None,
  399. )
  400. @patch('pagure.lib.git.update_git')
  401. @patch('pagure.lib.notify.send_email')
  402. def test_search_issues(self, p_send_email, p_ugt):
  403. """ Test the search_issues of pagure.lib. """
  404. p_send_email.return_value = True
  405. p_ugt.return_value = True
  406. self.test_edit_issue()
  407. repo = pagure.lib.get_project(self.session, 'test')
  408. # All issues
  409. issues = pagure.lib.search_issues(self.session, repo)
  410. self.assertEqual(len(issues), 2)
  411. self.assertEqual(issues[0].id, 1)
  412. self.assertEqual(issues[0].project_id, 1)
  413. self.assertEqual(issues[0].status, 'Open')
  414. self.assertEqual(issues[0].tags, [])
  415. self.assertEqual(issues[1].id, 2)
  416. self.assertEqual(issues[1].project_id, 1)
  417. self.assertEqual(issues[1].status, 'Invalid')
  418. self.assertEqual(issues[1].tags, [])
  419. # Issues by status
  420. issues = pagure.lib.search_issues(
  421. self.session, repo, status='Invalid')
  422. self.assertEqual(len(issues), 1)
  423. self.assertEqual(issues[0].id, 2)
  424. self.assertEqual(issues[0].project_id, 1)
  425. self.assertEqual(issues[0].status, 'Invalid')
  426. self.assertEqual(issues[0].tags, [])
  427. # Issues closed
  428. issues = pagure.lib.search_issues(
  429. self.session, repo, closed=True)
  430. self.assertEqual(len(issues), 1)
  431. self.assertEqual(issues[0].id, 2)
  432. self.assertEqual(issues[0].project_id, 1)
  433. self.assertEqual(issues[0].status, 'Invalid')
  434. self.assertEqual(issues[0].tags, [])
  435. # Issues by tag
  436. issues = pagure.lib.search_issues(self.session, repo, tags='foo')
  437. self.assertEqual(len(issues), 0)
  438. issues = pagure.lib.search_issues(self.session, repo, tags='!foo')
  439. self.assertEqual(len(issues), 2)
  440. # Issue by id
  441. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  442. self.assertEqual(issue.title, 'Test issue')
  443. self.assertEqual(issue.user.user, 'pingou')
  444. self.assertEqual(issue.tags, [])
  445. # Issues by authors
  446. issues = pagure.lib.search_issues(self.session, repo, author='foo')
  447. self.assertEqual(len(issues), 1)
  448. self.assertEqual(issues[0].id, 2)
  449. self.assertEqual(issues[0].project_id, 1)
  450. self.assertEqual(issues[0].status, 'Invalid')
  451. self.assertEqual(issues[0].tags, [])
  452. # Issues by assignee
  453. issues = pagure.lib.search_issues(self.session, repo, assignee='foo')
  454. self.assertEqual(len(issues), 0)
  455. issues = pagure.lib.search_issues(self.session, repo, assignee='!foo')
  456. self.assertEqual(len(issues), 2)
  457. issues = pagure.lib.search_issues(self.session, repo, private='foo')
  458. self.assertEqual(len(issues), 2)
  459. @patch('pagure.lib.git.update_git')
  460. @patch('pagure.lib.notify.send_email')
  461. def test_add_issue_assignee(self, p_send_email, p_ugt):
  462. """ Test the add_issue_assignee of pagure.lib. """
  463. p_send_email.return_value = True
  464. p_ugt.return_value = True
  465. self.test_new_issue()
  466. repo = pagure.lib.get_project(self.session, 'test')
  467. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  468. # Before
  469. issues = pagure.lib.search_issues(
  470. self.session, repo, assignee='pingou')
  471. self.assertEqual(len(issues), 0)
  472. # Test when it fails
  473. self.assertRaises(
  474. pagure.exceptions.PagureException,
  475. pagure.lib.add_issue_assignee,
  476. session=self.session,
  477. issue=issue,
  478. assignee='foo@foobar.com',
  479. user='foo@pingou.com',
  480. ticketfolder=None
  481. )
  482. self.assertRaises(
  483. pagure.exceptions.PagureException,
  484. pagure.lib.add_issue_assignee,
  485. session=self.session,
  486. issue=issue,
  487. assignee='foo@bar.com',
  488. user='foo@foopingou.com',
  489. ticketfolder=None
  490. )
  491. # Set the assignee by its email
  492. msg = pagure.lib.add_issue_assignee(
  493. session=self.session,
  494. issue=issue,
  495. assignee='foo@bar.com',
  496. user='foo@pingou.com',
  497. ticketfolder=None)
  498. self.session.commit()
  499. self.assertEqual(msg, 'Issue assigned')
  500. # Change the assignee to someone else by its username
  501. msg = pagure.lib.add_issue_assignee(
  502. session=self.session,
  503. issue=issue,
  504. assignee='pingou',
  505. user='pingou',
  506. ticketfolder=None)
  507. self.session.commit()
  508. self.assertEqual(msg, 'Issue assigned')
  509. # After -- Searches by assignee
  510. issues = pagure.lib.search_issues(
  511. self.session, repo, assignee='pingou')
  512. self.assertEqual(len(issues), 1)
  513. self.assertEqual(issues[0].id, 2)
  514. self.assertEqual(issues[0].project_id, 1)
  515. self.assertEqual(issues[0].status, 'Open')
  516. self.assertEqual(issues[0].tags, [])
  517. issues = pagure.lib.search_issues(
  518. self.session, repo, assignee=True)
  519. self.assertEqual(len(issues), 1)
  520. self.assertEqual(issues[0].id, 2)
  521. self.assertEqual(issues[0].title, 'Test issue #2')
  522. self.assertEqual(issues[0].project_id, 1)
  523. self.assertEqual(issues[0].status, 'Open')
  524. self.assertEqual(issues[0].tags, [])
  525. issues = pagure.lib.search_issues(
  526. self.session, repo, assignee=False)
  527. self.assertEqual(len(issues), 1)
  528. self.assertEqual(issues[0].id, 1)
  529. self.assertEqual(issues[0].title, 'Test issue')
  530. self.assertEqual(issues[0].project_id, 1)
  531. self.assertEqual(issues[0].status, 'Open')
  532. self.assertEqual(issues[0].tags, [])
  533. # Reset the assignee to no-one
  534. msg = pagure.lib.add_issue_assignee(
  535. session=self.session,
  536. issue=issue,
  537. assignee=None,
  538. user='pingou',
  539. ticketfolder=None)
  540. self.session.commit()
  541. self.assertEqual(msg, 'Assignee reset')
  542. issues = pagure.lib.search_issues(
  543. self.session, repo, assignee=False)
  544. self.assertEqual(len(issues), 2)
  545. self.assertEqual(issues[0].id, 1)
  546. self.assertEqual(issues[1].id, 2)
  547. issues = pagure.lib.search_issues(
  548. self.session, repo, assignee=True)
  549. self.assertEqual(len(issues), 0)
  550. @patch('pagure.lib.git.update_git')
  551. @patch('pagure.lib.notify.send_email')
  552. def test_add_issue_comment(self, p_send_email, p_ugt):
  553. """ Test the add_issue_comment of pagure.lib. """
  554. p_send_email.return_value = True
  555. p_ugt.return_value = True
  556. self.test_new_issue()
  557. repo = pagure.lib.get_project(self.session, 'test')
  558. # Before
  559. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  560. self.assertEqual(len(issue.comments), 0)
  561. # Set the assignee by its email
  562. msg = pagure.lib.add_issue_assignee(
  563. session=self.session,
  564. issue=issue,
  565. assignee='foo@bar.com',
  566. user='foo@pingou.com',
  567. ticketfolder=None)
  568. self.session.commit()
  569. self.assertEqual(msg, 'Issue assigned')
  570. # Add a comment to that issue
  571. msg = pagure.lib.add_issue_comment(
  572. session=self.session,
  573. issue=issue,
  574. comment='Hey look a comment!',
  575. user='foo',
  576. ticketfolder=None
  577. )
  578. self.session.commit()
  579. self.assertEqual(msg, 'Comment added')
  580. # After
  581. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  582. self.assertEqual(len(issue.comments), 1)
  583. self.assertEqual(issue.comments[0].comment, 'Hey look a comment!')
  584. self.assertEqual(issue.comments[0].user.user, 'foo')
  585. @patch('pagure.lib.notify.send_email')
  586. def test_add_user_to_project(self, p_send_email):
  587. """ Test the add_user_to_project of pagure.lib. """
  588. p_send_email.return_value = True
  589. tests.create_projects(self.session)
  590. # Before
  591. repo = pagure.lib.get_project(self.session, 'test')
  592. self.assertEqual(len(repo.users), 0)
  593. # Add an user to a project
  594. self.assertRaises(
  595. pagure.exceptions.PagureException,
  596. pagure.lib.add_user_to_project,
  597. session=self.session,
  598. project=repo,
  599. new_user='foobar',
  600. user='pingou',
  601. )
  602. msg = pagure.lib.add_user_to_project(
  603. session=self.session,
  604. project=repo,
  605. new_user='foo',
  606. user='pingou',
  607. )
  608. self.session.commit()
  609. self.assertEqual(msg, 'User added')
  610. # After
  611. repo = pagure.lib.get_project(self.session, 'test')
  612. self.assertEqual(len(repo.users), 1)
  613. self.assertEqual(repo.users[0].user, 'foo')
  614. def test_new_project(self):
  615. """ Test the new_project of pagure.lib. """
  616. gitfolder = os.path.join(self.path, 'repos')
  617. docfolder = os.path.join(self.path, 'docs')
  618. ticketfolder = os.path.join(self.path, 'tickets')
  619. requestfolder = os.path.join(self.path, 'requests')
  620. os.mkdir(gitfolder)
  621. os.mkdir(docfolder)
  622. os.mkdir(ticketfolder)
  623. os.mkdir(requestfolder)
  624. # Try creating a blacklisted project
  625. self.assertRaises(
  626. pagure.exceptions.PagureException,
  627. pagure.lib.new_project,
  628. session=self.session,
  629. user='pingou',
  630. name='static',
  631. blacklist=['static'],
  632. allowed_prefix=[],
  633. gitfolder=gitfolder,
  634. docfolder=docfolder,
  635. ticketfolder=ticketfolder,
  636. requestfolder=requestfolder,
  637. description='description for static',
  638. parent_id=None,
  639. )
  640. # Create a new project
  641. msg = pagure.lib.new_project(
  642. session=self.session,
  643. user='pingou',
  644. name='testproject',
  645. blacklist=[],
  646. allowed_prefix=[],
  647. gitfolder=gitfolder,
  648. docfolder=docfolder,
  649. ticketfolder=ticketfolder,
  650. requestfolder=requestfolder,
  651. description='description for testproject',
  652. parent_id=None,
  653. )
  654. self.session.commit()
  655. self.assertEqual(msg, 'Project "testproject" created')
  656. repo = pagure.lib.get_project(self.session, 'testproject')
  657. self.assertEqual(repo.path, 'testproject.git')
  658. gitrepo = os.path.join(gitfolder, repo.path)
  659. docrepo = os.path.join(docfolder, repo.path)
  660. ticketrepo = os.path.join(ticketfolder, repo.path)
  661. requestrepo = os.path.join(requestfolder, repo.path)
  662. self.assertTrue(os.path.exists(gitrepo))
  663. self.assertTrue(os.path.exists(docrepo))
  664. self.assertTrue(os.path.exists(ticketrepo))
  665. self.assertTrue(os.path.exists(requestrepo))
  666. # Try re-creating it but all repos are existing
  667. self.assertRaises(
  668. pagure.exceptions.PagureException,
  669. pagure.lib.new_project,
  670. session=self.session,
  671. user='pingou',
  672. name='testproject',
  673. blacklist=[],
  674. allowed_prefix=[],
  675. gitfolder=gitfolder,
  676. docfolder=docfolder,
  677. ticketfolder=ticketfolder,
  678. requestfolder=requestfolder,
  679. description='description for testproject',
  680. parent_id=None
  681. )
  682. self.session.rollback()
  683. self.assertTrue(os.path.exists(gitrepo))
  684. self.assertTrue(os.path.exists(docrepo))
  685. self.assertTrue(os.path.exists(ticketrepo))
  686. self.assertTrue(os.path.exists(requestrepo))
  687. # Drop the main git repo and try again
  688. shutil.rmtree(gitrepo)
  689. self.assertRaises(
  690. pagure.exceptions.PagureException,
  691. pagure.lib.new_project,
  692. session=self.session,
  693. user='pingou',
  694. name='testproject',
  695. blacklist=[],
  696. allowed_prefix=[],
  697. gitfolder=gitfolder,
  698. docfolder=docfolder,
  699. ticketfolder=ticketfolder,
  700. requestfolder=requestfolder,
  701. description='description for testproject',
  702. parent_id=None
  703. )
  704. self.session.rollback()
  705. self.assertFalse(os.path.exists(gitrepo))
  706. self.assertTrue(os.path.exists(docrepo))
  707. self.assertTrue(os.path.exists(ticketrepo))
  708. self.assertTrue(os.path.exists(requestrepo))
  709. # Drop the doc repo and try again
  710. shutil.rmtree(docrepo)
  711. self.assertRaises(
  712. pagure.exceptions.PagureException,
  713. pagure.lib.new_project,
  714. session=self.session,
  715. user='pingou',
  716. name='testproject',
  717. blacklist=[],
  718. allowed_prefix=[],
  719. gitfolder=gitfolder,
  720. docfolder=docfolder,
  721. ticketfolder=ticketfolder,
  722. requestfolder=requestfolder,
  723. description='description for testproject',
  724. parent_id=None
  725. )
  726. self.session.rollback()
  727. self.assertFalse(os.path.exists(gitrepo))
  728. self.assertFalse(os.path.exists(docrepo))
  729. self.assertTrue(os.path.exists(ticketrepo))
  730. self.assertTrue(os.path.exists(requestrepo))
  731. # Drop the request repo and try again
  732. shutil.rmtree(ticketrepo)
  733. self.assertRaises(
  734. pagure.exceptions.PagureException,
  735. pagure.lib.new_project,
  736. session=self.session,
  737. user='pingou',
  738. name='testproject',
  739. blacklist=[],
  740. allowed_prefix=[],
  741. gitfolder=gitfolder,
  742. docfolder=docfolder,
  743. ticketfolder=ticketfolder,
  744. requestfolder=requestfolder,
  745. description='description for testproject',
  746. parent_id=None
  747. )
  748. self.session.rollback()
  749. self.assertFalse(os.path.exists(gitrepo))
  750. self.assertFalse(os.path.exists(docrepo))
  751. self.assertFalse(os.path.exists(ticketrepo))
  752. self.assertTrue(os.path.exists(requestrepo))
  753. def test_update_project_settings(self):
  754. """ Test the update_project_settings of pagure.lib. """
  755. tests.create_projects(self.session)
  756. # Before
  757. repo = pagure.lib.get_project(self.session, 'test2')
  758. self.assertTrue(repo.settings['issue_tracker'])
  759. self.assertFalse(repo.settings['project_documentation'])
  760. msg = pagure.lib.update_project_settings(
  761. session=self.session,
  762. repo=repo,
  763. settings={
  764. 'issue_tracker': True,
  765. 'project_documentation': False,
  766. 'pull_requests': True,
  767. 'Only_assignee_can_merge_pull-request': False,
  768. 'Minimum_score_to_merge_pull-request': -1,
  769. 'Web-hooks': None,
  770. 'Enforce_signed-off_commits_in_pull-request': False,
  771. 'always_merge': False,
  772. },
  773. user='pingou',
  774. )
  775. self.assertEqual(msg, 'No settings to change')
  776. msg = pagure.lib.update_project_settings(
  777. session=self.session,
  778. repo=repo,
  779. settings={
  780. 'issue_tracker': False,
  781. 'project_documentation': True,
  782. 'pull_requests': False,
  783. 'Only_assignee_can_merge_pull-request': None,
  784. 'Minimum_score_to_merge_pull-request': None,
  785. 'Web-hooks': '',
  786. 'Enforce_signed-off_commits_in_pull-request': False,
  787. },
  788. user='pingou',
  789. )
  790. self.assertEqual(msg, 'Edited successfully settings of repo: test2')
  791. # After
  792. repo = pagure.lib.get_project(self.session, 'test2')
  793. self.assertFalse(repo.settings['issue_tracker'])
  794. self.assertTrue(repo.settings['project_documentation'])
  795. self.assertFalse(repo.settings['pull_requests'])
  796. def test_search_projects(self):
  797. """ Test the search_projects of pagure.lib. """
  798. tests.create_projects(self.session)
  799. projects = pagure.lib.search_projects(self.session)
  800. self.assertEqual(len(projects), 2)
  801. self.assertEqual(projects[0].id, 1)
  802. self.assertEqual(projects[1].id, 2)
  803. projects = pagure.lib.search_projects(self.session, username='foo')
  804. self.assertEqual(len(projects), 0)
  805. projects = pagure.lib.search_projects(self.session, username='pingou')
  806. self.assertEqual(len(projects), 2)
  807. self.assertEqual(projects[0].id, 1)
  808. self.assertEqual(projects[1].id, 2)
  809. projects = pagure.lib.search_projects(self.session, start=1)
  810. self.assertEqual(len(projects), 1)
  811. self.assertEqual(projects[0].id, 2)
  812. projects = pagure.lib.search_projects(self.session, limit=1)
  813. self.assertEqual(len(projects), 1)
  814. self.assertEqual(projects[0].id, 1)
  815. projects = pagure.lib.search_projects(self.session, count=True)
  816. self.assertEqual(projects, 2)
  817. def test_search_project_forked(self):
  818. """ Test the search_project for forked projects in pagure.lib. """
  819. tests.create_projects(self.session)
  820. # Create two forked repo
  821. item = pagure.lib.model.Project(
  822. user_id=2, # foo
  823. name='test',
  824. description='test project #1',
  825. parent_id=1,
  826. hook_token='aaabbbttt',
  827. )
  828. self.session.add(item)
  829. item = pagure.lib.model.Project(
  830. user_id=2, # foo
  831. name='test2',
  832. description='test project #2',
  833. parent_id=2,
  834. hook_token='aaabbbuuu',
  835. )
  836. self.session.add(item)
  837. # Since we have two forks, let's search them
  838. projects = pagure.lib.search_projects(self.session, fork=True)
  839. self.assertEqual(len(projects), 2)
  840. projects = pagure.lib.search_projects(self.session, fork=False)
  841. self.assertEqual(len(projects), 2)
  842. def test_get_tags_of_project(self):
  843. """ Test the get_tags_of_project of pagure.lib. """
  844. self.test_add_tag_obj()
  845. repo = pagure.lib.get_project(self.session, 'test')
  846. tags = pagure.lib.get_tags_of_project(self.session, repo)
  847. self.assertEqual([tag.tag for tag in tags], ['tag1'])
  848. tags = pagure.lib.get_tags_of_project(
  849. self.session, repo, pattern='T*')
  850. self.assertEqual([tag.tag for tag in tags], ['tag1'])
  851. repo = pagure.lib.get_project(self.session, 'test2')
  852. tags = pagure.lib.get_tags_of_project(self.session, repo)
  853. self.assertEqual([tag.tag for tag in tags], [])
  854. def test_get_issue_statuses(self):
  855. """ Test the get_issue_statuses of pagure.lib. """
  856. statuses = pagure.lib.get_issue_statuses(self.session)
  857. self.assertEqual(
  858. sorted(statuses),
  859. ['Duplicate', 'Fixed', 'Insufficient data', 'Invalid', 'Open'])
  860. def test_set_up_user(self):
  861. """ Test the set_up_user of pagure.lib. """
  862. items = pagure.lib.search_user(self.session)
  863. self.assertEqual(2, len(items))
  864. self.assertEqual(2, items[0].id)
  865. self.assertEqual('foo', items[0].user)
  866. self.assertEqual(1, items[1].id)
  867. self.assertEqual('pingou', items[1].user)
  868. pagure.lib.set_up_user(
  869. session=self.session,
  870. username='skvidal',
  871. fullname='Seth',
  872. default_email='skvidal@fp.o',
  873. keydir=pagure.APP.config.get('GITOLITE_KEYDIR', None),
  874. )
  875. self.session.commit()
  876. items = pagure.lib.search_user(self.session)
  877. self.assertEqual(3, len(items))
  878. self.assertEqual(2, items[0].id)
  879. self.assertEqual('foo', items[0].user)
  880. self.assertEqual(1, items[1].id)
  881. self.assertEqual('pingou', items[1].user)
  882. self.assertEqual(3, items[2].id)
  883. self.assertEqual('skvidal', items[2].user)
  884. self.assertEqual('Seth', items[2].fullname)
  885. self.assertEqual(
  886. ['skvidal@fp.o'], [email.email for email in items[2].emails])
  887. # Add the user a second time
  888. pagure.lib.set_up_user(
  889. session=self.session,
  890. username='skvidal',
  891. fullname='Seth V',
  892. default_email='skvidal@fp.o',
  893. keydir=pagure.APP.config.get('GITOLITE_KEYDIR', None),
  894. )
  895. self.session.commit()
  896. # Nothing changed
  897. items = pagure.lib.search_user(self.session)
  898. self.assertEqual(3, len(items))
  899. self.assertEqual('skvidal', items[2].user)
  900. self.assertEqual('Seth V', items[2].fullname)
  901. self.assertEqual(
  902. ['skvidal@fp.o'], [email.email for email in items[2].emails])
  903. # Add the user a third time with a different email
  904. pagure.lib.set_up_user(
  905. session=self.session,
  906. username='skvidal',
  907. fullname='Seth',
  908. default_email='svidal@fp.o',
  909. keydir=pagure.APP.config.get('GITOLITE_KEYDIR', None),
  910. )
  911. self.session.commit()
  912. # Email added
  913. items = pagure.lib.search_user(self.session)
  914. self.assertEqual(3, len(items))
  915. self.assertEqual('skvidal', items[2].user)
  916. self.assertEqual(
  917. ['skvidal@fp.o', 'svidal@fp.o'],
  918. [email.email for email in items[2].emails])
  919. def test_update_user_ssh(self):
  920. """ Test the update_user_ssh of pagure.lib. """
  921. # Before
  922. user = pagure.lib.search_user(self.session, username='foo')
  923. self.assertEqual(user.public_ssh_key, None)
  924. msg = pagure.lib.update_user_ssh(self.session, user, 'blah', keydir=None)
  925. user = pagure.lib.search_user(self.session, username='foo')
  926. self.assertEqual(user.public_ssh_key, 'blah')
  927. msg = pagure.lib.update_user_ssh(self.session, user, 'blah', keydir=None)
  928. user = pagure.lib.search_user(self.session, username='foo')
  929. self.assertEqual(user.public_ssh_key, 'blah')
  930. msg = pagure.lib.update_user_ssh(self.session, 'foo', None, keydir=None)
  931. user = pagure.lib.search_user(self.session, username='foo')
  932. self.assertEqual(user.public_ssh_key, None)
  933. def test_avatar_url_from_openid(self):
  934. """ Test the avatar_url_from_openid of pagure.lib. """
  935. output = pagure.lib.avatar_url_from_openid('pingou@fedoraproject.org')
  936. self.assertEqual(
  937. output,
  938. 'https://seccdn.libravatar.org/avatar/'
  939. 'b3ee7bb4de70b6522c2478df3b4cd6322b5ec5d62ac7ceb1128e3d4ff42f6928'
  940. '?s=64&d=retro')
  941. def test_fork_project(self):
  942. """ Test the fork_project of pagure.lib. """
  943. gitfolder = os.path.join(self.path, 'repos')
  944. docfolder = os.path.join(self.path, 'docs')
  945. ticketfolder = os.path.join(self.path, 'tickets')
  946. requestfolder = os.path.join(self.path, 'requests')
  947. os.mkdir(gitfolder)
  948. os.mkdir(docfolder)
  949. os.mkdir(ticketfolder)
  950. projects = pagure.lib.search_projects(self.session)
  951. self.assertEqual(len(projects), 0)
  952. # Create a new project
  953. msg = pagure.lib.new_project(
  954. session=self.session,
  955. user='pingou',
  956. name='testproject',
  957. blacklist=[],
  958. allowed_prefix=[],
  959. gitfolder=gitfolder,
  960. docfolder=docfolder,
  961. ticketfolder=ticketfolder,
  962. requestfolder=requestfolder,
  963. description='description for testproject',
  964. parent_id=None,
  965. )
  966. self.session.commit()
  967. self.assertEqual(msg, 'Project "testproject" created')
  968. projects = pagure.lib.search_projects(self.session)
  969. self.assertEqual(len(projects), 1)
  970. repo = pagure.lib.get_project(self.session, 'testproject')
  971. gitrepo = os.path.join(gitfolder, repo.path)
  972. docrepo = os.path.join(docfolder, repo.path)
  973. ticketrepo = os.path.join(ticketfolder, repo.path)
  974. requestrepo = os.path.join(requestfolder, repo.path)
  975. self.assertTrue(os.path.exists(gitrepo))
  976. self.assertTrue(os.path.exists(docrepo))
  977. self.assertTrue(os.path.exists(ticketrepo))
  978. self.assertTrue(os.path.exists(requestrepo))
  979. # Git repo exists
  980. grepo = '%s.git' % os.path.join(
  981. gitfolder, 'forks', 'foo', 'testproject')
  982. os.makedirs(grepo)
  983. self.assertRaises(
  984. pagure.exceptions.PagureException,
  985. pagure.lib.fork_project,
  986. session=self.session,
  987. user='foo',
  988. repo=repo,
  989. gitfolder=gitfolder,
  990. docfolder=docfolder,
  991. ticketfolder=ticketfolder,
  992. requestfolder=requestfolder,
  993. )
  994. self.session.rollback()
  995. shutil.rmtree(grepo)
  996. # Doc repo exists
  997. grepo = '%s.git' % os.path.join(
  998. docfolder, 'forks', 'foo', 'testproject')
  999. os.makedirs(grepo)
  1000. self.assertRaises(
  1001. pagure.exceptions.PagureException,
  1002. pagure.lib.fork_project,
  1003. session=self.session,
  1004. user='foo',
  1005. repo=repo,
  1006. gitfolder=gitfolder,
  1007. docfolder=docfolder,
  1008. ticketfolder=ticketfolder,
  1009. requestfolder=requestfolder,
  1010. )
  1011. self.session.rollback()
  1012. shutil.rmtree(grepo)
  1013. # Ticket repo exists
  1014. grepo = '%s.git' % os.path.join(
  1015. ticketfolder, 'forks', 'foo', 'testproject')
  1016. os.makedirs(grepo)
  1017. self.assertRaises(
  1018. pagure.exceptions.PagureException,
  1019. pagure.lib.fork_project,
  1020. session=self.session,
  1021. user='foo',
  1022. repo=repo,
  1023. gitfolder=gitfolder,
  1024. docfolder=docfolder,
  1025. ticketfolder=ticketfolder,
  1026. requestfolder=requestfolder,
  1027. )
  1028. self.session.rollback()
  1029. shutil.rmtree(grepo)
  1030. # Request repo exists
  1031. grepo = '%s.git' % os.path.join(
  1032. requestfolder, 'forks', 'foo', 'testproject')
  1033. os.makedirs(grepo)
  1034. self.assertRaises(
  1035. pagure.exceptions.PagureException,
  1036. pagure.lib.fork_project,
  1037. session=self.session,
  1038. user='foo',
  1039. repo=repo,
  1040. gitfolder=gitfolder,
  1041. docfolder=docfolder,
  1042. ticketfolder=ticketfolder,
  1043. requestfolder=requestfolder,
  1044. )
  1045. self.session.rollback()
  1046. shutil.rmtree(grepo)
  1047. projects = pagure.lib.search_projects(self.session)
  1048. self.assertEqual(len(projects), 1)
  1049. # Fork worked
  1050. msg = pagure.lib.fork_project(
  1051. session=self.session,
  1052. user='foo',
  1053. repo=repo,
  1054. gitfolder=gitfolder,
  1055. docfolder=docfolder,
  1056. ticketfolder=ticketfolder,
  1057. requestfolder=requestfolder,
  1058. )
  1059. self.session.commit()
  1060. self.assertEqual(
  1061. msg, 'Repo "testproject" cloned to "foo/testproject"')
  1062. projects = pagure.lib.search_projects(self.session)
  1063. self.assertEqual(len(projects), 2)
  1064. # Fork a fork
  1065. repo = pagure.lib.get_project(
  1066. self.session, 'testproject', user='foo')
  1067. msg = pagure.lib.fork_project(
  1068. session=self.session,
  1069. user='pingou',
  1070. repo=repo,
  1071. gitfolder=gitfolder,
  1072. docfolder=docfolder,
  1073. ticketfolder=ticketfolder,
  1074. requestfolder=requestfolder,
  1075. )
  1076. self.session.commit()
  1077. self.assertEqual(
  1078. msg, 'Repo "testproject" cloned to "pingou/testproject"')
  1079. projects = pagure.lib.search_projects(self.session)
  1080. self.assertEqual(len(projects), 3)
  1081. @patch('pagure.lib.notify.send_email')
  1082. def test_new_pull_request(self, mockemail):
  1083. """ test new_pull_request of pagure.lib. """
  1084. mockemail.return_value = True
  1085. tests.create_projects(self.session)
  1086. # Create a forked repo
  1087. item = pagure.lib.model.Project(
  1088. user_id=1, # pingou
  1089. name='test',
  1090. description='test project #1',
  1091. parent_id=1,
  1092. hook_token='aaabbbrrr',
  1093. )
  1094. self.session.commit()
  1095. self.session.add(item)
  1096. # Add an extra user to project `foo`
  1097. repo = pagure.lib.get_project(self.session, 'test')
  1098. self.assertEqual(repo.open_requests, 0)
  1099. msg = pagure.lib.add_user_to_project(
  1100. session=self.session,
  1101. project=repo,
  1102. new_user='foo',
  1103. user='pingou',
  1104. )
  1105. self.session.commit()
  1106. self.assertEqual(msg, 'User added')
  1107. repo = pagure.lib.get_project(self.session, 'test')
  1108. forked_repo = pagure.lib.get_project(
  1109. self.session, 'test', user='pingou')
  1110. req = pagure.lib.new_pull_request(
  1111. session=self.session,
  1112. repo_from=forked_repo,
  1113. branch_from='master',
  1114. repo_to=repo,
  1115. branch_to='master',
  1116. title='test pull-request',
  1117. user='pingou',
  1118. requestfolder=None,
  1119. )
  1120. self.session.commit()
  1121. self.assertEqual(req.id, 1)
  1122. self.assertEqual(req.title, 'test pull-request')
  1123. self.assertEqual(repo.open_requests, 1)
  1124. @patch('pagure.lib.notify.send_email')
  1125. def test_add_pull_request_comment(self, mockemail):
  1126. """ Test add_pull_request_comment of pagure.lib. """
  1127. mockemail.return_value = True
  1128. self.test_new_pull_request()
  1129. request = pagure.lib.search_pull_requests(self.session, requestid=1)
  1130. msg = pagure.lib.add_pull_request_comment(
  1131. session=self.session,
  1132. request=request,
  1133. commit='commithash',
  1134. tree_id=None,
  1135. filename='file',
  1136. row=None,
  1137. comment='This is awesome, I got to remember it!',
  1138. user='foo',
  1139. requestfolder=None,
  1140. )
  1141. self.assertEqual(msg, 'Comment added')
  1142. self.session.commit()
  1143. self.assertEqual(len(request.discussion), 0)
  1144. self.assertEqual(len(request.comments), 1)
  1145. self.assertEqual(request.score, 0)
  1146. @patch('pagure.lib.notify.send_email')
  1147. def test_add_pull_request_flag(self, mockemail):
  1148. """ Test add_pull_request_flag of pagure.lib. """
  1149. mockemail.return_value = True
  1150. self.test_new_pull_request()
  1151. request = pagure.lib.search_pull_requests(self.session, requestid=1)
  1152. self.assertEqual(len(request.flags), 0)
  1153. msg = pagure.lib.add_pull_request_flag(
  1154. session=self.session,
  1155. request=request,
  1156. username="jenkins",
  1157. percent=100,
  1158. comment="Build passes",
  1159. url="http://jenkins.cloud.fedoraproject.org",
  1160. uid="jenkins_build_pagure_34",
  1161. user='foo',
  1162. requestfolder=None,
  1163. )
  1164. self.assertEqual(msg, 'Flag added')
  1165. self.session.commit()
  1166. self.assertEqual(len(request.flags), 1)
  1167. def test_search_pull_requests(self):
  1168. """ Test search_pull_requests of pagure.lib. """
  1169. self.test_new_pull_request()
  1170. prs = pagure.lib.search_pull_requests(
  1171. session=self.session
  1172. )
  1173. self.assertEqual(len(prs), 1)
  1174. prs = pagure.lib.search_pull_requests(
  1175. session=self.session,
  1176. project_id=1
  1177. )
  1178. self.assertEqual(len(prs), 1)
  1179. prs = pagure.lib.search_pull_requests(
  1180. session=self.session,
  1181. project_id_from=3
  1182. )
  1183. self.assertEqual(len(prs), 1)
  1184. prs = pagure.lib.search_pull_requests(
  1185. session=self.session,
  1186. status=False
  1187. )
  1188. self.assertEqual(len(prs), 0)
  1189. # All non-assigned PR
  1190. prs = pagure.lib.search_pull_requests(
  1191. session=self.session,
  1192. assignee=False
  1193. )
  1194. self.assertEqual(len(prs), 1)
  1195. prs[0].assignee_id = 1
  1196. self.session.add(prs[0])
  1197. self.session.commit()
  1198. # All the PR assigned
  1199. prs = pagure.lib.search_pull_requests(
  1200. session=self.session,
  1201. assignee=True
  1202. )
  1203. self.assertEqual(len(prs), 1)
  1204. # Basically the same as above but then for a specific user
  1205. prs = pagure.lib.search_pull_requests(
  1206. session=self.session,
  1207. assignee='pingou'
  1208. )
  1209. self.assertEqual(len(prs), 1)
  1210. # All PR except those assigned to pingou
  1211. prs = pagure.lib.search_pull_requests(
  1212. session=self.session,
  1213. assignee='!pingou'
  1214. )
  1215. self.assertEqual(len(prs), 0)
  1216. # All PR created by the specified author
  1217. prs = pagure.lib.search_pull_requests(
  1218. session=self.session,
  1219. author='pingou'
  1220. )
  1221. self.assertEqual(len(prs), 1)
  1222. # Count the PR instead of listing them
  1223. prs = pagure.lib.search_pull_requests(
  1224. session=self.session,
  1225. author='pingou',
  1226. count=True
  1227. )
  1228. self.assertEqual(prs, 1)
  1229. @patch('pagure.lib.notify.send_email')
  1230. def test_close_pull_request(self, send_email):
  1231. """ Test close_pull_request of pagure.lib. """
  1232. send_email.return_value = True
  1233. self.test_new_pull_request()
  1234. repo = pagure.lib.get_project(self.session, 'test')
  1235. self.assertEqual(repo.open_requests, 1)
  1236. request = pagure.lib.search_pull_requests(self.session, requestid=1)
  1237. pagure.lib.close_pull_request(
  1238. session=self.session,
  1239. request=request,
  1240. user='pingou',
  1241. requestfolder=None,
  1242. merged=True,
  1243. )
  1244. self.session.commit()
  1245. repo = pagure.lib.get_project(self.session, 'test')
  1246. self.assertEqual(repo.open_requests, 0)
  1247. prs = pagure.lib.search_pull_requests(
  1248. session=self.session,
  1249. status=False
  1250. )
  1251. self.assertEqual(len(prs), 1)
  1252. # Does not change much, just the notification sent
  1253. pagure.lib.close_pull_request(
  1254. session=self.session,
  1255. request=request,
  1256. user='pingou',
  1257. requestfolder=None,
  1258. merged=False,
  1259. )
  1260. self.session.commit()
  1261. prs = pagure.lib.search_pull_requests(
  1262. session=self.session,
  1263. status=False
  1264. )
  1265. self.assertEqual(len(prs), 1)
  1266. @patch('pagure.lib.git.update_git')
  1267. @patch('pagure.lib.notify.send_email')
  1268. def test_remove_issue_dependency(self, p_send_email, p_ugt):
  1269. """ Test remove_issue_dependency of pagure.lib. """
  1270. p_send_email.return_value = True
  1271. p_ugt.return_value = True
  1272. self.test_add_issue_dependency()
  1273. repo = pagure.lib.get_project(self.session, 'test')
  1274. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1275. issue_blocked = pagure.lib.search_issues(
  1276. self.session, repo, issueid=2)
  1277. # Before
  1278. self.assertEqual(len(issue.parents), 1)
  1279. self.assertEqual(issue.parents[0].id, 2)
  1280. self.assertEqual(len(issue.children), 0)
  1281. self.assertEqual(issue.children, [])
  1282. self.assertEqual(len(issue_blocked.parents), 0)
  1283. self.assertEqual(issue_blocked.parents, [])
  1284. self.assertEqual(len(issue_blocked.children), 1)
  1285. self.assertEqual(issue_blocked.children[0].id, 1)
  1286. self.assertRaises(
  1287. pagure.exceptions.PagureException,
  1288. pagure.lib.remove_issue_dependency,
  1289. session=self.session,
  1290. issue=issue,
  1291. issue_blocked=issue,
  1292. user='pingou',
  1293. ticketfolder=None)
  1294. # Wrong order of issues
  1295. msg = pagure.lib.remove_issue_dependency(
  1296. session=self.session,
  1297. issue=issue,
  1298. issue_blocked=issue_blocked,
  1299. user='pingou',
  1300. ticketfolder=None)
  1301. self.session.commit()
  1302. self.assertEqual(msg, None)
  1303. # Drop deps
  1304. msg = pagure.lib.remove_issue_dependency(
  1305. session=self.session,
  1306. issue=issue_blocked,
  1307. issue_blocked=issue,
  1308. user='pingou',
  1309. ticketfolder=None)
  1310. self.session.commit()
  1311. self.assertEqual(msg, 'Dependency removed')
  1312. # After
  1313. self.assertEqual(issue.parents, [])
  1314. self.assertEqual(issue.children, [])
  1315. self.assertEqual(issue_blocked.parents, [])
  1316. self.assertEqual(issue_blocked.children, [])
  1317. @patch('pagure.lib.git.update_git')
  1318. @patch('pagure.lib.notify.send_email')
  1319. def test_get_issue_comment(self, p_send_email, p_ugt):
  1320. """ Test the get_issue_comment of pagure.lib. """
  1321. p_send_email.return_value = True
  1322. p_ugt.return_value = True
  1323. self.test_add_issue_comment()
  1324. repo = pagure.lib.get_project(self.session, 'test')
  1325. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1326. self.assertEqual(
  1327. pagure.lib.get_issue_comment(self.session, issue.uid, 10),
  1328. None
  1329. )
  1330. comment = pagure.lib.get_issue_comment(self.session, issue.uid, 1)
  1331. self.assertEqual(comment.comment, 'Hey look a comment!')
  1332. @patch('pagure.lib.git.update_git')
  1333. @patch('pagure.lib.notify.send_email')
  1334. def test_get_issue_by_uid(self, p_send_email, p_ugt):
  1335. """ Test the get_issue_by_uid of pagure.lib. """
  1336. p_send_email.return_value = True
  1337. p_ugt.return_value = True
  1338. self.test_new_issue()
  1339. repo = pagure.lib.get_project(self.session, 'test')
  1340. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1341. self.assertEqual(
  1342. pagure.lib.get_issue_by_uid(self.session, 'foobar'),
  1343. None
  1344. )
  1345. new_issue = pagure.lib.get_issue_by_uid(self.session, issue.uid)
  1346. self.assertEqual(issue, new_issue)
  1347. @patch('pagure.lib.git.update_git')
  1348. @patch('pagure.lib.notify.send_email')
  1349. def test_update_tags(self, p_send_email, p_ugt):
  1350. """ Test the update_tags of pagure.lib. """
  1351. p_send_email.return_value = True
  1352. p_ugt.return_value = True
  1353. self.test_new_issue()
  1354. repo = pagure.lib.get_project(self.session, 'test')
  1355. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1356. # before
  1357. self.assertEqual(issue.tags_text, [])
  1358. messages = pagure.lib.update_tags(
  1359. self.session, issue, 'tag', 'pingou', ticketfolder=None)
  1360. self.assertEqual(messages, ['Tag added: tag'])
  1361. messages = pagure.lib.update_tags(
  1362. self.session, issue, ['tag2', 'tag3'], 'pingou',
  1363. ticketfolder=None)
  1364. self.assertEqual(
  1365. messages, ['Tag added: tag2, tag3', 'Removed tag: tag'])
  1366. # after
  1367. self.assertEqual(issue.tags_text, ['tag2', 'tag3'])
  1368. @patch('pagure.lib.git.update_git')
  1369. @patch('pagure.lib.notify.send_email')
  1370. def test_update_dependency_issue(self, p_send_email, p_ugt):
  1371. """ Test the update_dependency_issue of pagure.lib. """
  1372. p_send_email.return_value = True
  1373. p_ugt.return_value = True
  1374. self.test_new_issue()
  1375. repo = pagure.lib.get_project(self.session, 'test')
  1376. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1377. self.assertEqual(repo.open_tickets, 2)
  1378. self.assertEqual(repo.open_tickets_public, 2)
  1379. # Create issues to play with
  1380. msg = pagure.lib.new_issue(
  1381. session=self.session,
  1382. repo=repo,
  1383. title='Test issue #3',
  1384. content='We should work on this (3rd time!)',
  1385. user='pingou',
  1386. ticketfolder=None,
  1387. private=True,
  1388. )
  1389. self.session.commit()
  1390. self.assertEqual(msg.title, 'Test issue #3')
  1391. self.assertEqual(repo.open_tickets, 3)
  1392. self.assertEqual(repo.open_tickets_public, 2)
  1393. # before
  1394. self.assertEqual(issue.tags_text, [])
  1395. self.assertEqual(issue.depends_text, [])
  1396. self.assertEqual(issue.blocks_text, [])
  1397. messages = pagure.lib.update_dependency_issue(
  1398. self.session, repo, issue, '2', 'pingou', ticketfolder=None)
  1399. self.assertEqual(messages, ['Dependency added'])
  1400. messages = pagure.lib.update_dependency_issue(
  1401. self.session, repo, issue, ['3', '4', 5], 'pingou',
  1402. ticketfolder=None)
  1403. self.assertEqual(
  1404. messages, ['Dependency added', 'Dependency removed'])
  1405. # after
  1406. self.assertEqual(issue.tags_text, [])
  1407. self.assertEqual(issue.depends_text, [3])
  1408. self.assertEqual(issue.blocks_text, [])
  1409. @patch('pagure.lib.git.update_git')
  1410. @patch('pagure.lib.notify.send_email')
  1411. def test_update_blocked_issue(self, p_send_email, p_ugt):
  1412. """ Test the update_blocked_issue of pagure.lib. """
  1413. p_send_email.return_value = True
  1414. p_ugt.return_value = True
  1415. self.test_new_issue()
  1416. repo = pagure.lib.get_project(self.session, 'test')
  1417. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1418. # Create issues to play with
  1419. msg = pagure.lib.new_issue(
  1420. session=self.session,
  1421. repo=repo,
  1422. title='Test issue #3',
  1423. content='We should work on this (3rd time!)',
  1424. user='pingou',
  1425. ticketfolder=None,
  1426. private=True,
  1427. )
  1428. self.session.commit()
  1429. self.assertEqual(msg.title, 'Test issue #3')
  1430. # before
  1431. self.assertEqual(issue.tags_text, [])
  1432. self.assertEqual(issue.depends_text, [])
  1433. self.assertEqual(issue.blocks_text, [])
  1434. messages = pagure.lib.update_blocked_issue(
  1435. self.session, repo, issue, '2', 'pingou', ticketfolder=None)
  1436. self.assertEqual(messages, ['Dependency added'])
  1437. messages = pagure.lib.update_blocked_issue(
  1438. self.session, repo, issue, ['3', '4', 5], 'pingou',
  1439. ticketfolder=None)
  1440. self.assertEqual(
  1441. messages, ['Dependency added', 'Dependency removed'])
  1442. # after
  1443. self.assertEqual(issue.tags_text, [])
  1444. self.assertEqual(issue.depends_text, [])
  1445. self.assertEqual(issue.blocks_text, [3])
  1446. @patch('pagure.lib.notify.send_email')
  1447. def test_add_pull_request_assignee(self, mockemail):
  1448. """ Test add_pull_request_assignee of pagure.lib. """
  1449. mockemail.return_value = True
  1450. self.test_new_pull_request()
  1451. request = pagure.lib.search_pull_requests(self.session, requestid=1)
  1452. self.assertRaises(
  1453. pagure.exceptions.PagureException,
  1454. pagure.lib.add_pull_request_assignee,
  1455. session=self.session,
  1456. request=request,
  1457. assignee='bar',
  1458. user='foo',
  1459. requestfolder=None,
  1460. )
  1461. # Assign
  1462. msg = pagure.lib.add_pull_request_assignee(
  1463. session=self.session,
  1464. request=request,
  1465. assignee='pingou',
  1466. user='foo',
  1467. requestfolder=None,
  1468. )
  1469. self.assertEqual(msg, 'Request assigned')
  1470. # Reset
  1471. msg = pagure.lib.add_pull_request_assignee(
  1472. session=self.session,
  1473. request=request,
  1474. assignee=None,
  1475. user='foo',
  1476. requestfolder=None,
  1477. )
  1478. self.assertEqual(msg, 'Request reset')
  1479. # Try resetting again
  1480. msg = pagure.lib.add_pull_request_assignee(
  1481. session=self.session,
  1482. request=request,
  1483. assignee=None,
  1484. user='foo',
  1485. requestfolder=None,
  1486. )
  1487. self.assertEqual(msg, None)
  1488. def test_search_pending_email(self):
  1489. """ Test search_pending_email of pagure.lib. """
  1490. self.assertEqual(
  1491. pagure.lib.search_pending_email(self.session), None)
  1492. user = pagure.lib.search_user(self.session, username='pingou')
  1493. email_pend = pagure.lib.model.UserEmailPending(
  1494. user_id=user.id,
  1495. email='foo@fp.o',
  1496. token='abcdef',
  1497. )
  1498. self.session.add(email_pend)
  1499. self.session.commit()
  1500. self.assertNotEqual(
  1501. pagure.lib.search_pending_email(self.session), None)
  1502. self.assertNotEqual(
  1503. pagure.lib.search_pending_email(self.session, token='abcdef'),
  1504. None)
  1505. pend = pagure.lib.search_pending_email(self.session, token='abcdef')
  1506. self.assertEqual(pend.user.username, 'pingou')
  1507. self.assertEqual(pend.email, 'foo@fp.o')
  1508. self.assertEqual(pend.token, 'abcdef')
  1509. pend = pagure.lib.search_pending_email(self.session, email='foo@fp.o')
  1510. self.assertEqual(pend.user.username, 'pingou')
  1511. self.assertEqual(pend.email, 'foo@fp.o')
  1512. self.assertEqual(pend.token, 'abcdef')
  1513. def test_generate_hook_token(self):
  1514. """ Test generate_hook_token of pagure.lib. """
  1515. tests.create_projects(self.session)
  1516. projects = pagure.lib.search_projects(self.session)
  1517. for proj in projects:
  1518. self.assertIn(proj.hook_token, ['aaabbbccc', 'aaabbbddd'])
  1519. pagure.lib.generate_hook_token(self.session)
  1520. projects = pagure.lib.search_projects(self.session)
  1521. for proj in projects:
  1522. self.assertNotIn(proj.hook_token, ['aaabbbccc', 'aaabbbddd'])
  1523. @patch('pagure.lib.notify.send_email')
  1524. def test_pull_request_score(self, mockemail):
  1525. """ Test PullRequest.score of pagure.lib.model. """
  1526. mockemail.return_value = True
  1527. self.test_new_pull_request()
  1528. request = pagure.lib.search_pull_requests(self.session, requestid=1)
  1529. msg = pagure.lib.add_pull_request_comment(
  1530. session=self.session,
  1531. request=request,
  1532. commit=None,
  1533. tree_id=None,
  1534. filename=None,
  1535. row=None,
  1536. comment='This looks great :thumbsup:',
  1537. user='foo',
  1538. requestfolder=None,
  1539. )
  1540. self.session.commit()
  1541. self.assertEqual(msg, 'Comment added')
  1542. msg = pagure.lib.add_pull_request_comment(
  1543. session=self.session,
  1544. request=request,
  1545. commit=None,
  1546. tree_id=None,
  1547. filename=None,
  1548. row=None,
  1549. comment='I disagree -1',
  1550. user='pingou',
  1551. requestfolder=None,
  1552. )
  1553. self.session.commit()
  1554. self.assertEqual(msg, 'Comment added')
  1555. msg = pagure.lib.add_pull_request_comment(
  1556. session=self.session,
  1557. request=request,
  1558. commit=None,
  1559. tree_id=None,
  1560. filename=None,
  1561. row=None,
  1562. comment='NM this looks great now +1000',
  1563. user='pingou',
  1564. requestfolder=None,
  1565. )
  1566. self.session.commit()
  1567. self.assertEqual(msg, 'Comment added')
  1568. self.assertEqual(len(request.discussion), 3)
  1569. self.assertEqual(request.score, 1)
  1570. def test_add_group(self):
  1571. """ Test the add_group method of pagure.lib. """
  1572. groups = pagure.lib.search_groups(self.session)
  1573. self.assertEqual(len(groups), 0)
  1574. self.assertEqual(groups, [])
  1575. # Invalid type
  1576. self.assertRaises(
  1577. pagure.exceptions.PagureException,
  1578. pagure.lib.add_group,
  1579. self.session,
  1580. group_name='foo',
  1581. group_type='bar',
  1582. user='pingou',
  1583. is_admin=True,
  1584. blacklist=[],
  1585. )
  1586. groups = pagure.lib.search_groups(self.session)
  1587. self.assertEqual(len(groups), 0)
  1588. self.assertEqual(groups, [])
  1589. # Invalid user
  1590. self.assertRaises(
  1591. pagure.exceptions.PagureException,
  1592. pagure.lib.add_group,
  1593. self.session,
  1594. group_name='foo',
  1595. group_type='user',
  1596. user='test',
  1597. is_admin=False,
  1598. blacklist=[],
  1599. )
  1600. groups = pagure.lib.search_groups(self.session)
  1601. self.assertEqual(len(groups), 0)
  1602. self.assertEqual(groups, [])
  1603. msg = pagure.lib.add_group(
  1604. self.session,
  1605. group_name='foo',
  1606. group_type='bar',
  1607. user='pingou',
  1608. is_admin=False,
  1609. blacklist=[],
  1610. )
  1611. self.session.commit()
  1612. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  1613. groups = pagure.lib.search_groups(self.session)
  1614. self.assertEqual(len(groups), 1)
  1615. self.assertEqual(groups[0].group_name, 'foo')
  1616. # Group with this name already exists
  1617. self.assertRaises(
  1618. pagure.exceptions.PagureException,
  1619. pagure.lib.add_group,
  1620. self.session,
  1621. group_name='foo',
  1622. group_type='bar',
  1623. user='pingou',
  1624. is_admin=False,
  1625. blacklist=[],
  1626. )
  1627. # Group with a blacklisted prefix
  1628. self.assertRaises(
  1629. pagure.exceptions.PagureException,
  1630. pagure.lib.add_group,
  1631. self.session,
  1632. group_name='forks',
  1633. group_type='bar',
  1634. user='pingou',
  1635. is_admin=False,
  1636. blacklist=['forks'],
  1637. )
  1638. def test_add_user_to_group(self):
  1639. """ Test the add_user_to_group method of pagure.lib. """
  1640. self.test_add_group()
  1641. group = pagure.lib.search_groups(self.session, group_name='foo')
  1642. self.assertNotEqual(group, None)
  1643. self.assertEqual(group.group_name, 'foo')
  1644. # Invalid new user
  1645. self.assertRaises(
  1646. pagure.exceptions.PagureException,
  1647. pagure.lib.add_user_to_group,
  1648. self.session,
  1649. username='foobar',
  1650. group=group,
  1651. user='foo',
  1652. is_admin=False,
  1653. )
  1654. # Invalid user
  1655. self.assertRaises(
  1656. pagure.exceptions.PagureException,
  1657. pagure.lib.add_user_to_group,
  1658. self.session,
  1659. username='foo',
  1660. group=group,
  1661. user='foobar',
  1662. is_admin=False,
  1663. )
  1664. # User not allowed
  1665. self.assertRaises(
  1666. pagure.exceptions.PagureException,
  1667. pagure.lib.add_user_to_group,
  1668. self.session,
  1669. username='foo',
  1670. group=group,
  1671. user='foo',
  1672. is_admin=False,
  1673. )
  1674. msg = pagure.lib.add_user_to_group(
  1675. self.session,
  1676. username='foo',
  1677. group=group,
  1678. user='pingou',
  1679. is_admin=False,
  1680. )
  1681. self.session.commit()
  1682. self.assertEqual(msg, 'User `foo` added to the group `foo`.')
  1683. msg = pagure.lib.add_user_to_group(
  1684. self.session,
  1685. username='foo',
  1686. group=group,
  1687. user='pingou',
  1688. is_admin=False,
  1689. )
  1690. self.session.commit()
  1691. self.assertEqual(
  1692. msg, 'User `foo` already in the group, nothing to change.')
  1693. def test_is_group_member(self):
  1694. """ Test the is_group_member method of pagure.lib. """
  1695. self.test_add_group()
  1696. self.assertFalse(
  1697. pagure.lib.is_group_member(self.session, None, 'foo'))
  1698. self.assertFalse(
  1699. pagure.lib.is_group_member(self.session, 'bar', 'foo'))
  1700. self.assertFalse(
  1701. pagure.lib.is_group_member(self.session, 'foo', 'foo'))
  1702. self.assertTrue(
  1703. pagure.lib.is_group_member(self.session, 'pingou', 'foo'))
  1704. def test_get_user_group(self):
  1705. """ Test the get_user_group method of pagure.lib. """
  1706. self.test_add_group()
  1707. item = pagure.lib.get_user_group(self.session, 1, 1)
  1708. self.assertEqual(item.user_id, 1)
  1709. self.assertEqual(item.group_id, 1)
  1710. item = pagure.lib.get_user_group(self.session, 1, 2)
  1711. self.assertEqual(item, None)
  1712. item = pagure.lib.get_user_group(self.session, 2, 1)
  1713. self.assertEqual(item, None)
  1714. def test_get_group_types(self):
  1715. """ Test the get_group_types method of pagure.lib. """
  1716. self.test_add_group()
  1717. groups = pagure.lib.get_group_types(self.session, 'user')
  1718. self.assertEqual(len(groups), 1)
  1719. self.assertEqual(groups[0].group_type, 'user')
  1720. groups = pagure.lib.get_group_types(self.session)
  1721. self.assertEqual(len(groups), 2)
  1722. self.assertEqual(groups[0].group_type, 'admin')
  1723. self.assertEqual(groups[1].group_type, 'user')
  1724. def test_search_groups(self):
  1725. """ Test the search_groups method of pagure.lib. """
  1726. self.assertEqual(pagure.lib.search_groups(self.session), [])
  1727. msg = pagure.lib.add_group(
  1728. self.session,
  1729. group_name='foo',
  1730. group_type='bar',
  1731. user='pingou',
  1732. is_admin=False,
  1733. blacklist=[],
  1734. )
  1735. self.session.commit()
  1736. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  1737. groups = pagure.lib.search_groups(self.session)
  1738. self.assertEqual(len(groups), 1)
  1739. self.assertEqual(groups[0].group_name, 'foo')
  1740. msg = pagure.lib.add_group(
  1741. self.session,
  1742. group_name='bar',
  1743. group_type='admin',
  1744. user='pingou',
  1745. is_admin=True,
  1746. blacklist=[],
  1747. )
  1748. self.session.commit()
  1749. self.assertEqual(msg, 'User `pingou` added to the group `bar`.')
  1750. groups = pagure.lib.search_groups(self.session)
  1751. self.assertEqual(len(groups), 2)
  1752. self.assertEqual(groups[0].group_name, 'bar')
  1753. self.assertEqual(groups[1].group_name, 'foo')
  1754. groups = pagure.lib.search_groups(self.session, group_type='user')
  1755. self.assertEqual(len(groups), 1)
  1756. self.assertEqual(groups[0].group_name, 'foo')
  1757. groups = pagure.lib.search_groups(self.session, group_type='admin')
  1758. self.assertEqual(len(groups), 1)
  1759. self.assertEqual(groups[0].group_name, 'bar')
  1760. groups = pagure.lib.search_groups(self.session, group_name='foo')
  1761. self.assertEqual(groups.group_name, 'foo')
  1762. def test_delete_user_of_group(self):
  1763. """ Test the delete_user_of_group method of pagure.lib. """
  1764. self.test_add_user_to_group()
  1765. groups = pagure.lib.search_groups(self.session)
  1766. self.assertEqual(len(groups), 1)
  1767. self.assertEqual(groups[0].group_name, 'foo')
  1768. # Invalid username
  1769. self.assertRaises(
  1770. pagure.exceptions.PagureException,
  1771. pagure.lib.delete_user_of_group,
  1772. self.session,
  1773. username='bar',
  1774. groupname='foo',
  1775. user='pingou',
  1776. is_admin=False,
  1777. )
  1778. # Invalid groupname
  1779. self.assertRaises(
  1780. pagure.exceptions.PagureException,
  1781. pagure.lib.delete_user_of_group,
  1782. self.session,
  1783. username='foo',
  1784. groupname='bar',
  1785. user='pingou',
  1786. is_admin=False,
  1787. )
  1788. # Invalid user
  1789. self.assertRaises(
  1790. pagure.exceptions.PagureException,
  1791. pagure.lib.delete_user_of_group,
  1792. self.session,
  1793. username='foo',
  1794. groupname='foo',
  1795. user='test',
  1796. is_admin=False,
  1797. )
  1798. # User not in the group
  1799. item = pagure.lib.model.User(
  1800. user='bar',
  1801. fullname='bar',
  1802. password='foo',
  1803. default_email='bar@bar.com',
  1804. )
  1805. self.session.add(item)
  1806. item = pagure.lib.model.UserEmail(
  1807. user_id=3,
  1808. email='bar@bar.com')
  1809. self.session.add(item)
  1810. self.session.commit()
  1811. self.assertRaises(
  1812. pagure.exceptions.PagureException,
  1813. pagure.lib.delete_user_of_group,
  1814. self.session,
  1815. username='bar',
  1816. groupname='foo',
  1817. user='pingou',
  1818. is_admin=False,
  1819. )
  1820. # User is not allowed to remove the username
  1821. self.assertRaises(
  1822. pagure.exceptions.PagureException,
  1823. pagure.lib.delete_user_of_group,
  1824. self.session,
  1825. username='foo',
  1826. groupname='foo',
  1827. user='bar',
  1828. is_admin=False,
  1829. )
  1830. # Username is the creator of the group
  1831. self.assertRaises(
  1832. pagure.exceptions.PagureException,
  1833. pagure.lib.delete_user_of_group,
  1834. self.session,
  1835. username='pingou',
  1836. groupname='foo',
  1837. user='pingou',
  1838. is_admin=False,
  1839. )
  1840. # All good
  1841. group = pagure.lib.search_groups(self.session, group_name='foo')
  1842. self.assertEqual(len(group.users), 2)
  1843. pagure.lib.delete_user_of_group(
  1844. self.session,
  1845. username='foo',
  1846. groupname='foo',
  1847. user='pingou',
  1848. is_admin=False,
  1849. )
  1850. self.session.commit()
  1851. group = pagure.lib.search_groups(self.session, group_name='foo')
  1852. self.assertEqual(len(group.users), 1)
  1853. def test_add_group_to_project(self):
  1854. """ Test the add_group_to_project method of pagure.lib. """
  1855. tests.create_projects(self.session)
  1856. self.test_add_group()
  1857. project = pagure.lib.get_project(self.session, 'test2')
  1858. # Group does not exist
  1859. self.assertRaises(
  1860. pagure.exceptions.PagureException,
  1861. pagure.lib.add_group_to_project,
  1862. session=self.session,
  1863. project=project,
  1864. new_group='bar',
  1865. user='foo',
  1866. )
  1867. # User does not exist
  1868. self.assertRaises(
  1869. pagure.exceptions.PagureException,
  1870. pagure.lib.add_group_to_project,
  1871. session=self.session,
  1872. project=project,
  1873. new_group='foo',
  1874. user='bar',
  1875. )
  1876. # User not allowed
  1877. self.assertRaises(
  1878. pagure.exceptions.PagureException,
  1879. pagure.lib.add_group_to_project,
  1880. session=self.session,
  1881. project=project,
  1882. new_group='foo',
  1883. user='foo',
  1884. )
  1885. # All good
  1886. msg = pagure.lib.add_group_to_project(
  1887. session=self.session,
  1888. project=project,
  1889. new_group='foo',
  1890. user='pingou',
  1891. )
  1892. self.session.commit()
  1893. self.assertEqual(msg, 'Group added')
  1894. # Group already associated with the project
  1895. self.assertRaises(
  1896. pagure.exceptions.PagureException,
  1897. pagure.lib.add_group_to_project,
  1898. session=self.session,
  1899. project=project,
  1900. new_group='foo',
  1901. user='pingou',
  1902. )
  1903. def test_update_watch_status(self):
  1904. """ Test the update_watch_status method of pagure.lib. """
  1905. tests.create_projects(self.session)
  1906. project = pagure.lib.get_project(self.session, 'test')
  1907. # User does not exist
  1908. self.assertRaises(
  1909. pagure.exceptions.PagureException,
  1910. pagure.lib.update_watch_status,
  1911. session=self.session,
  1912. project=project,
  1913. user='aavrug',
  1914. watch='1',
  1915. )
  1916. # All good and when user seleted watch option.
  1917. msg = pagure.lib.update_watch_status(
  1918. session=self.session,
  1919. project=project,
  1920. user='pingou',
  1921. watch='1',
  1922. )
  1923. self.session.commit()
  1924. self.assertEqual(msg, 'You are now watching this repo.')
  1925. # All good and when user selected unwatch option.
  1926. msg = pagure.lib.update_watch_status(
  1927. session=self.session,
  1928. project=project,
  1929. user='pingou',
  1930. watch='0',
  1931. )
  1932. self.session.commit()
  1933. self.assertEqual(msg, 'You are no longer watching this repo.')
  1934. def test_is_watching(self):
  1935. """ Test the is_watching method of pagure.lib. """
  1936. tests.create_projects(self.session)
  1937. self.test_add_group()
  1938. project = pagure.lib.get_project(self.session, 'test')
  1939. # If user not logged in
  1940. watch = pagure.lib.is_watching(
  1941. session=self.session,
  1942. user=None,
  1943. reponame='test',
  1944. )
  1945. self.assertFalse(watch)
  1946. # User does not exist
  1947. user = tests.FakeUser()
  1948. user.username = 'aavrug'
  1949. watch = pagure.lib.is_watching(
  1950. session=self.session,
  1951. user=user,
  1952. reponame='test',
  1953. )
  1954. self.assertFalse(watch)
  1955. pagure.lib.add_group_to_project(
  1956. session=self.session,
  1957. project=project,
  1958. new_group='foo',
  1959. user='pingou',
  1960. )
  1961. self.session.commit()
  1962. group = pagure.lib.search_groups(self.session, group_name='foo')
  1963. pagure.lib.add_user_to_group(
  1964. self.session,
  1965. username='foo',
  1966. group=group,
  1967. user='pingou',
  1968. is_admin=False,
  1969. )
  1970. self.session.commit()
  1971. group = pagure.lib.search_groups(self.session, group_name='foo')
  1972. # If user belongs to any group of that project
  1973. user.username = 'foo'
  1974. watch = pagure.lib.is_watching(
  1975. session=self.session,
  1976. user=user,
  1977. reponame='test',
  1978. )
  1979. self.assertTrue(watch)
  1980. # If user is the creator
  1981. user.username = 'pingou'
  1982. watch = pagure.lib.is_watching(
  1983. session=self.session,
  1984. user=user,
  1985. reponame='test',
  1986. )
  1987. self.assertTrue(watch)
  1988. # Entry into watchers table
  1989. pagure.lib.update_watch_status(
  1990. session=self.session,
  1991. project=project,
  1992. user='pingou',
  1993. watch='1',
  1994. )
  1995. self.session.commit()
  1996. # From watchers table
  1997. watch = pagure.lib.is_watching(
  1998. session=self.session,
  1999. user=user,
  2000. reponame='test',
  2001. )
  2002. self.assertTrue(watch)
  2003. # Entry into watchers table
  2004. pagure.lib.update_watch_status(
  2005. session=self.session,
  2006. project=project,
  2007. user='pingou',
  2008. watch='0',
  2009. )
  2010. self.session.commit()
  2011. # From watchers table
  2012. watch = pagure.lib.is_watching(
  2013. session=self.session,
  2014. user=user,
  2015. reponame='test',
  2016. )
  2017. self.assertFalse(watch)
  2018. if __name__ == '__main__':
  2019. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibtests)
  2020. unittest.TextTestRunner(verbosity=2).run(SUITE)