test_pagure_lib.py 82 KB

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