test_pagure_lib.py 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  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. 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. # Try creating a 40 chars project
  641. self.assertRaises(
  642. pagure.exceptions.PagureException,
  643. pagure.lib.new_project,
  644. session=self.session,
  645. user='pingou',
  646. name='pingou/' + 's' * 40,
  647. blacklist=['static'],
  648. allowed_prefix=['pingou'],
  649. gitfolder=gitfolder,
  650. docfolder=docfolder,
  651. ticketfolder=ticketfolder,
  652. requestfolder=requestfolder,
  653. description='description for 40 chars length project',
  654. parent_id=None,
  655. prevent_40_chars=True,
  656. )
  657. # Create a new project
  658. msg = pagure.lib.new_project(
  659. session=self.session,
  660. user='pingou',
  661. name='testproject',
  662. blacklist=[],
  663. allowed_prefix=[],
  664. gitfolder=gitfolder,
  665. docfolder=docfolder,
  666. ticketfolder=ticketfolder,
  667. requestfolder=requestfolder,
  668. description='description for testproject',
  669. parent_id=None,
  670. )
  671. self.session.commit()
  672. self.assertEqual(msg, 'Project "testproject" created')
  673. repo = pagure.lib.get_project(self.session, 'testproject')
  674. self.assertEqual(repo.path, 'testproject.git')
  675. gitrepo = os.path.join(gitfolder, repo.path)
  676. docrepo = os.path.join(docfolder, repo.path)
  677. ticketrepo = os.path.join(ticketfolder, repo.path)
  678. requestrepo = os.path.join(requestfolder, repo.path)
  679. self.assertTrue(os.path.exists(gitrepo))
  680. self.assertTrue(os.path.exists(docrepo))
  681. self.assertTrue(os.path.exists(ticketrepo))
  682. self.assertTrue(os.path.exists(requestrepo))
  683. # Try re-creating it but all repos are existing
  684. self.assertRaises(
  685. pagure.exceptions.PagureException,
  686. pagure.lib.new_project,
  687. session=self.session,
  688. user='pingou',
  689. name='testproject',
  690. blacklist=[],
  691. allowed_prefix=[],
  692. gitfolder=gitfolder,
  693. docfolder=docfolder,
  694. ticketfolder=ticketfolder,
  695. requestfolder=requestfolder,
  696. description='description for testproject',
  697. parent_id=None
  698. )
  699. self.session.rollback()
  700. self.assertTrue(os.path.exists(gitrepo))
  701. self.assertTrue(os.path.exists(docrepo))
  702. self.assertTrue(os.path.exists(ticketrepo))
  703. self.assertTrue(os.path.exists(requestrepo))
  704. # Drop the main git repo and try again
  705. shutil.rmtree(gitrepo)
  706. self.assertRaises(
  707. pagure.exceptions.PagureException,
  708. pagure.lib.new_project,
  709. session=self.session,
  710. user='pingou',
  711. name='testproject',
  712. blacklist=[],
  713. allowed_prefix=[],
  714. gitfolder=gitfolder,
  715. docfolder=docfolder,
  716. ticketfolder=ticketfolder,
  717. requestfolder=requestfolder,
  718. description='description for testproject',
  719. parent_id=None
  720. )
  721. self.session.rollback()
  722. self.assertFalse(os.path.exists(gitrepo))
  723. self.assertTrue(os.path.exists(docrepo))
  724. self.assertTrue(os.path.exists(ticketrepo))
  725. self.assertTrue(os.path.exists(requestrepo))
  726. # Drop the doc repo and try again
  727. shutil.rmtree(docrepo)
  728. self.assertRaises(
  729. pagure.exceptions.PagureException,
  730. pagure.lib.new_project,
  731. session=self.session,
  732. user='pingou',
  733. name='testproject',
  734. blacklist=[],
  735. allowed_prefix=[],
  736. gitfolder=gitfolder,
  737. docfolder=docfolder,
  738. ticketfolder=ticketfolder,
  739. requestfolder=requestfolder,
  740. description='description for testproject',
  741. parent_id=None
  742. )
  743. self.session.rollback()
  744. self.assertFalse(os.path.exists(gitrepo))
  745. self.assertFalse(os.path.exists(docrepo))
  746. self.assertTrue(os.path.exists(ticketrepo))
  747. self.assertTrue(os.path.exists(requestrepo))
  748. # Drop the request repo and try again
  749. shutil.rmtree(ticketrepo)
  750. self.assertRaises(
  751. pagure.exceptions.PagureException,
  752. pagure.lib.new_project,
  753. session=self.session,
  754. user='pingou',
  755. name='testproject',
  756. blacklist=[],
  757. allowed_prefix=[],
  758. gitfolder=gitfolder,
  759. docfolder=docfolder,
  760. ticketfolder=ticketfolder,
  761. requestfolder=requestfolder,
  762. description='description for testproject',
  763. parent_id=None
  764. )
  765. self.session.rollback()
  766. self.assertFalse(os.path.exists(gitrepo))
  767. self.assertFalse(os.path.exists(docrepo))
  768. self.assertFalse(os.path.exists(ticketrepo))
  769. self.assertTrue(os.path.exists(requestrepo))
  770. # Re-Try creating a 40 chars project this time allowing it
  771. msg = pagure.lib.new_project(
  772. session=self.session,
  773. user='pingou',
  774. name='pingou/' + 's' * 40,
  775. blacklist=['static'],
  776. allowed_prefix=['pingou'],
  777. gitfolder=gitfolder,
  778. docfolder=docfolder,
  779. ticketfolder=ticketfolder,
  780. requestfolder=requestfolder,
  781. description='description for 40 chars length project',
  782. parent_id=None,
  783. )
  784. self.session.commit()
  785. self.assertEqual(
  786. msg,
  787. 'Project "pingou/ssssssssssssssssssssssssssssssssssssssss" '
  788. 'created')
  789. def test_update_project_settings(self):
  790. """ Test the update_project_settings of pagure.lib. """
  791. tests.create_projects(self.session)
  792. # Before
  793. repo = pagure.lib.get_project(self.session, 'test2')
  794. self.assertTrue(repo.settings['issue_tracker'])
  795. self.assertFalse(repo.settings['project_documentation'])
  796. msg = pagure.lib.update_project_settings(
  797. session=self.session,
  798. repo=repo,
  799. settings={
  800. 'issue_tracker': True,
  801. 'project_documentation': False,
  802. 'pull_requests': True,
  803. 'Only_assignee_can_merge_pull-request': False,
  804. 'Minimum_score_to_merge_pull-request': -1,
  805. 'Web-hooks': None,
  806. 'Enforce_signed-off_commits_in_pull-request': False,
  807. 'always_merge': False,
  808. },
  809. user='pingou',
  810. )
  811. self.assertEqual(msg, 'No settings to change')
  812. msg = pagure.lib.update_project_settings(
  813. session=self.session,
  814. repo=repo,
  815. settings={
  816. 'issue_tracker': False,
  817. 'project_documentation': True,
  818. 'pull_requests': False,
  819. 'Only_assignee_can_merge_pull-request': None,
  820. 'Minimum_score_to_merge_pull-request': None,
  821. 'Web-hooks': '',
  822. 'Enforce_signed-off_commits_in_pull-request': False,
  823. },
  824. user='pingou',
  825. )
  826. self.assertEqual(msg, 'Edited successfully settings of repo: test2')
  827. # After
  828. repo = pagure.lib.get_project(self.session, 'test2')
  829. self.assertFalse(repo.settings['issue_tracker'])
  830. self.assertTrue(repo.settings['project_documentation'])
  831. self.assertFalse(repo.settings['pull_requests'])
  832. def test_search_projects(self):
  833. """ Test the search_projects of pagure.lib. """
  834. tests.create_projects(self.session)
  835. projects = pagure.lib.search_projects(self.session)
  836. self.assertEqual(len(projects), 2)
  837. self.assertEqual(projects[0].id, 1)
  838. self.assertEqual(projects[1].id, 2)
  839. projects = pagure.lib.search_projects(self.session, username='foo')
  840. self.assertEqual(len(projects), 0)
  841. projects = pagure.lib.search_projects(self.session, username='pingou')
  842. self.assertEqual(len(projects), 2)
  843. self.assertEqual(projects[0].id, 1)
  844. self.assertEqual(projects[1].id, 2)
  845. projects = pagure.lib.search_projects(self.session, start=1)
  846. self.assertEqual(len(projects), 1)
  847. self.assertEqual(projects[0].id, 2)
  848. projects = pagure.lib.search_projects(self.session, limit=1)
  849. self.assertEqual(len(projects), 1)
  850. self.assertEqual(projects[0].id, 1)
  851. projects = pagure.lib.search_projects(self.session, count=True)
  852. self.assertEqual(projects, 2)
  853. def test_search_project_forked(self):
  854. """ Test the search_project for forked projects in pagure.lib. """
  855. tests.create_projects(self.session)
  856. # Create two forked repo
  857. item = pagure.lib.model.Project(
  858. user_id=2, # foo
  859. name='test',
  860. description='test project #1',
  861. parent_id=1,
  862. hook_token='aaabbbttt',
  863. )
  864. self.session.add(item)
  865. item = pagure.lib.model.Project(
  866. user_id=2, # foo
  867. name='test2',
  868. description='test project #2',
  869. parent_id=2,
  870. hook_token='aaabbbuuu',
  871. )
  872. self.session.add(item)
  873. # Since we have two forks, let's search them
  874. projects = pagure.lib.search_projects(self.session, fork=True)
  875. self.assertEqual(len(projects), 2)
  876. projects = pagure.lib.search_projects(self.session, fork=False)
  877. self.assertEqual(len(projects), 2)
  878. def test_get_tags_of_project(self):
  879. """ Test the get_tags_of_project of pagure.lib. """
  880. self.test_add_tag_obj()
  881. repo = pagure.lib.get_project(self.session, 'test')
  882. tags = pagure.lib.get_tags_of_project(self.session, repo)
  883. self.assertEqual([tag.tag for tag in tags], ['tag1'])
  884. tags = pagure.lib.get_tags_of_project(
  885. self.session, repo, pattern='T*')
  886. self.assertEqual([tag.tag for tag in tags], ['tag1'])
  887. repo = pagure.lib.get_project(self.session, 'test2')
  888. tags = pagure.lib.get_tags_of_project(self.session, repo)
  889. self.assertEqual([tag.tag for tag in tags], [])
  890. def test_get_issue_statuses(self):
  891. """ Test the get_issue_statuses of pagure.lib. """
  892. statuses = pagure.lib.get_issue_statuses(self.session)
  893. self.assertEqual(
  894. sorted(statuses),
  895. ['Duplicate', 'Fixed', 'Insufficient data', 'Invalid', 'Open'])
  896. def test_set_up_user(self):
  897. """ Test the set_up_user of pagure.lib. """
  898. items = pagure.lib.search_user(self.session)
  899. self.assertEqual(2, len(items))
  900. self.assertEqual(2, items[0].id)
  901. self.assertEqual('foo', items[0].user)
  902. self.assertEqual(1, items[1].id)
  903. self.assertEqual('pingou', items[1].user)
  904. pagure.lib.set_up_user(
  905. session=self.session,
  906. username='skvidal',
  907. fullname='Seth',
  908. default_email='skvidal@fp.o',
  909. keydir=pagure.APP.config.get('GITOLITE_KEYDIR', None),
  910. )
  911. self.session.commit()
  912. items = pagure.lib.search_user(self.session)
  913. self.assertEqual(3, len(items))
  914. self.assertEqual(2, items[0].id)
  915. self.assertEqual('foo', items[0].user)
  916. self.assertEqual(1, items[1].id)
  917. self.assertEqual('pingou', items[1].user)
  918. self.assertEqual(3, items[2].id)
  919. self.assertEqual('skvidal', items[2].user)
  920. self.assertEqual('Seth', items[2].fullname)
  921. self.assertEqual(
  922. ['skvidal@fp.o'], [email.email for email in items[2].emails])
  923. # Add the user a second time
  924. pagure.lib.set_up_user(
  925. session=self.session,
  926. username='skvidal',
  927. fullname='Seth V',
  928. default_email='skvidal@fp.o',
  929. keydir=pagure.APP.config.get('GITOLITE_KEYDIR', None),
  930. )
  931. self.session.commit()
  932. # Nothing changed
  933. items = pagure.lib.search_user(self.session)
  934. self.assertEqual(3, len(items))
  935. self.assertEqual('skvidal', items[2].user)
  936. self.assertEqual('Seth V', items[2].fullname)
  937. self.assertEqual(
  938. ['skvidal@fp.o'], [email.email for email in items[2].emails])
  939. # Add the user a third time with a different email
  940. pagure.lib.set_up_user(
  941. session=self.session,
  942. username='skvidal',
  943. fullname='Seth',
  944. default_email='svidal@fp.o',
  945. keydir=pagure.APP.config.get('GITOLITE_KEYDIR', None),
  946. )
  947. self.session.commit()
  948. # Email added
  949. items = pagure.lib.search_user(self.session)
  950. self.assertEqual(3, len(items))
  951. self.assertEqual('skvidal', items[2].user)
  952. self.assertEqual(
  953. ['skvidal@fp.o', 'svidal@fp.o'],
  954. [email.email for email in items[2].emails])
  955. def test_update_user_ssh(self):
  956. """ Test the update_user_ssh of pagure.lib. """
  957. # Before
  958. user = pagure.lib.search_user(self.session, username='foo')
  959. self.assertEqual(user.public_ssh_key, None)
  960. msg = pagure.lib.update_user_ssh(self.session, user, 'blah', keydir=None)
  961. user = pagure.lib.search_user(self.session, username='foo')
  962. self.assertEqual(user.public_ssh_key, 'blah')
  963. msg = pagure.lib.update_user_ssh(self.session, user, 'blah', keydir=None)
  964. user = pagure.lib.search_user(self.session, username='foo')
  965. self.assertEqual(user.public_ssh_key, 'blah')
  966. msg = pagure.lib.update_user_ssh(self.session, 'foo', None, keydir=None)
  967. user = pagure.lib.search_user(self.session, username='foo')
  968. self.assertEqual(user.public_ssh_key, None)
  969. def test_avatar_url_from_openid(self):
  970. """ Test the avatar_url_from_openid of pagure.lib. """
  971. output = pagure.lib.avatar_url_from_openid('pingou@fedoraproject.org')
  972. self.assertEqual(
  973. output,
  974. 'https://seccdn.libravatar.org/avatar/'
  975. 'b3ee7bb4de70b6522c2478df3b4cd6322b5ec5d62ac7ceb1128e3d4ff42f6928'
  976. '?s=64&d=retro')
  977. def test_fork_project(self):
  978. """ Test the fork_project of pagure.lib. """
  979. gitfolder = os.path.join(self.path, 'repos')
  980. docfolder = os.path.join(self.path, 'docs')
  981. ticketfolder = os.path.join(self.path, 'tickets')
  982. requestfolder = os.path.join(self.path, 'requests')
  983. os.mkdir(gitfolder)
  984. os.mkdir(docfolder)
  985. os.mkdir(ticketfolder)
  986. projects = pagure.lib.search_projects(self.session)
  987. self.assertEqual(len(projects), 0)
  988. # Create a new project
  989. msg = pagure.lib.new_project(
  990. session=self.session,
  991. user='pingou',
  992. name='testproject',
  993. blacklist=[],
  994. allowed_prefix=[],
  995. gitfolder=gitfolder,
  996. docfolder=docfolder,
  997. ticketfolder=ticketfolder,
  998. requestfolder=requestfolder,
  999. description='description for testproject',
  1000. parent_id=None,
  1001. )
  1002. self.session.commit()
  1003. self.assertEqual(msg, 'Project "testproject" created')
  1004. projects = pagure.lib.search_projects(self.session)
  1005. self.assertEqual(len(projects), 1)
  1006. repo = pagure.lib.get_project(self.session, 'testproject')
  1007. gitrepo = os.path.join(gitfolder, repo.path)
  1008. docrepo = os.path.join(docfolder, repo.path)
  1009. ticketrepo = os.path.join(ticketfolder, repo.path)
  1010. requestrepo = os.path.join(requestfolder, repo.path)
  1011. self.assertTrue(os.path.exists(gitrepo))
  1012. self.assertTrue(os.path.exists(docrepo))
  1013. self.assertTrue(os.path.exists(ticketrepo))
  1014. self.assertTrue(os.path.exists(requestrepo))
  1015. # Git repo exists
  1016. grepo = '%s.git' % os.path.join(
  1017. gitfolder, 'forks', 'foo', 'testproject')
  1018. os.makedirs(grepo)
  1019. self.assertRaises(
  1020. pagure.exceptions.PagureException,
  1021. pagure.lib.fork_project,
  1022. session=self.session,
  1023. user='foo',
  1024. repo=repo,
  1025. gitfolder=gitfolder,
  1026. docfolder=docfolder,
  1027. ticketfolder=ticketfolder,
  1028. requestfolder=requestfolder,
  1029. )
  1030. self.session.rollback()
  1031. shutil.rmtree(grepo)
  1032. # Doc repo exists
  1033. grepo = '%s.git' % os.path.join(
  1034. docfolder, 'forks', 'foo', 'testproject')
  1035. os.makedirs(grepo)
  1036. self.assertRaises(
  1037. pagure.exceptions.PagureException,
  1038. pagure.lib.fork_project,
  1039. session=self.session,
  1040. user='foo',
  1041. repo=repo,
  1042. gitfolder=gitfolder,
  1043. docfolder=docfolder,
  1044. ticketfolder=ticketfolder,
  1045. requestfolder=requestfolder,
  1046. )
  1047. self.session.rollback()
  1048. shutil.rmtree(grepo)
  1049. # Ticket repo exists
  1050. grepo = '%s.git' % os.path.join(
  1051. ticketfolder, 'forks', 'foo', 'testproject')
  1052. os.makedirs(grepo)
  1053. self.assertRaises(
  1054. pagure.exceptions.PagureException,
  1055. pagure.lib.fork_project,
  1056. session=self.session,
  1057. user='foo',
  1058. repo=repo,
  1059. gitfolder=gitfolder,
  1060. docfolder=docfolder,
  1061. ticketfolder=ticketfolder,
  1062. requestfolder=requestfolder,
  1063. )
  1064. self.session.rollback()
  1065. shutil.rmtree(grepo)
  1066. # Request repo exists
  1067. grepo = '%s.git' % os.path.join(
  1068. requestfolder, 'forks', 'foo', 'testproject')
  1069. os.makedirs(grepo)
  1070. self.assertRaises(
  1071. pagure.exceptions.PagureException,
  1072. pagure.lib.fork_project,
  1073. session=self.session,
  1074. user='foo',
  1075. repo=repo,
  1076. gitfolder=gitfolder,
  1077. docfolder=docfolder,
  1078. ticketfolder=ticketfolder,
  1079. requestfolder=requestfolder,
  1080. )
  1081. self.session.rollback()
  1082. shutil.rmtree(grepo)
  1083. projects = pagure.lib.search_projects(self.session)
  1084. self.assertEqual(len(projects), 1)
  1085. # Fork worked
  1086. msg = pagure.lib.fork_project(
  1087. session=self.session,
  1088. user='foo',
  1089. repo=repo,
  1090. gitfolder=gitfolder,
  1091. docfolder=docfolder,
  1092. ticketfolder=ticketfolder,
  1093. requestfolder=requestfolder,
  1094. )
  1095. self.session.commit()
  1096. self.assertEqual(
  1097. msg, 'Repo "testproject" cloned to "foo/testproject"')
  1098. projects = pagure.lib.search_projects(self.session)
  1099. self.assertEqual(len(projects), 2)
  1100. # Fork a fork
  1101. repo = pagure.lib.get_project(
  1102. self.session, 'testproject', user='foo')
  1103. msg = pagure.lib.fork_project(
  1104. session=self.session,
  1105. user='pingou',
  1106. repo=repo,
  1107. gitfolder=gitfolder,
  1108. docfolder=docfolder,
  1109. ticketfolder=ticketfolder,
  1110. requestfolder=requestfolder,
  1111. )
  1112. self.session.commit()
  1113. self.assertEqual(
  1114. msg, 'Repo "testproject" cloned to "pingou/testproject"')
  1115. projects = pagure.lib.search_projects(self.session)
  1116. self.assertEqual(len(projects), 3)
  1117. @patch('pagure.lib.notify.send_email')
  1118. def test_new_pull_request(self, mockemail):
  1119. """ test new_pull_request of pagure.lib. """
  1120. mockemail.return_value = True
  1121. tests.create_projects(self.session)
  1122. # Create a forked repo
  1123. item = pagure.lib.model.Project(
  1124. user_id=1, # pingou
  1125. name='test',
  1126. description='test project #1',
  1127. parent_id=1,
  1128. hook_token='aaabbbrrr',
  1129. )
  1130. self.session.commit()
  1131. self.session.add(item)
  1132. # Add an extra user to project `foo`
  1133. repo = pagure.lib.get_project(self.session, 'test')
  1134. self.assertEqual(repo.open_requests, 0)
  1135. msg = pagure.lib.add_user_to_project(
  1136. session=self.session,
  1137. project=repo,
  1138. new_user='foo',
  1139. user='pingou',
  1140. )
  1141. self.session.commit()
  1142. self.assertEqual(msg, 'User added')
  1143. repo = pagure.lib.get_project(self.session, 'test')
  1144. forked_repo = pagure.lib.get_project(
  1145. self.session, 'test', user='pingou')
  1146. req = pagure.lib.new_pull_request(
  1147. session=self.session,
  1148. repo_from=forked_repo,
  1149. branch_from='master',
  1150. repo_to=repo,
  1151. branch_to='master',
  1152. title='test pull-request',
  1153. user='pingou',
  1154. requestfolder=None,
  1155. )
  1156. self.session.commit()
  1157. self.assertEqual(req.id, 1)
  1158. self.assertEqual(req.title, 'test pull-request')
  1159. self.assertEqual(repo.open_requests, 1)
  1160. @patch('pagure.lib.notify.send_email')
  1161. def test_add_pull_request_comment(self, mockemail):
  1162. """ Test add_pull_request_comment of pagure.lib. """
  1163. mockemail.return_value = True
  1164. self.test_new_pull_request()
  1165. request = pagure.lib.search_pull_requests(self.session, requestid=1)
  1166. msg = pagure.lib.add_pull_request_comment(
  1167. session=self.session,
  1168. request=request,
  1169. commit='commithash',
  1170. tree_id=None,
  1171. filename='file',
  1172. row=None,
  1173. comment='This is awesome, I got to remember it!',
  1174. user='foo',
  1175. requestfolder=None,
  1176. )
  1177. self.assertEqual(msg, 'Comment added')
  1178. self.session.commit()
  1179. self.assertEqual(len(request.discussion), 0)
  1180. self.assertEqual(len(request.comments), 1)
  1181. self.assertEqual(request.score, 0)
  1182. @patch('pagure.lib.notify.send_email')
  1183. def test_add_pull_request_flag(self, mockemail):
  1184. """ Test add_pull_request_flag of pagure.lib. """
  1185. mockemail.return_value = True
  1186. self.test_new_pull_request()
  1187. request = pagure.lib.search_pull_requests(self.session, requestid=1)
  1188. self.assertEqual(len(request.flags), 0)
  1189. msg = pagure.lib.add_pull_request_flag(
  1190. session=self.session,
  1191. request=request,
  1192. username="jenkins",
  1193. percent=100,
  1194. comment="Build passes",
  1195. url="http://jenkins.cloud.fedoraproject.org",
  1196. uid="jenkins_build_pagure_34",
  1197. user='foo',
  1198. requestfolder=None,
  1199. )
  1200. self.assertEqual(msg, 'Flag added')
  1201. self.session.commit()
  1202. self.assertEqual(len(request.flags), 1)
  1203. def test_search_pull_requests(self):
  1204. """ Test search_pull_requests of pagure.lib. """
  1205. self.test_new_pull_request()
  1206. prs = pagure.lib.search_pull_requests(
  1207. session=self.session
  1208. )
  1209. self.assertEqual(len(prs), 1)
  1210. prs = pagure.lib.search_pull_requests(
  1211. session=self.session,
  1212. project_id=1
  1213. )
  1214. self.assertEqual(len(prs), 1)
  1215. prs = pagure.lib.search_pull_requests(
  1216. session=self.session,
  1217. project_id_from=3
  1218. )
  1219. self.assertEqual(len(prs), 1)
  1220. prs = pagure.lib.search_pull_requests(
  1221. session=self.session,
  1222. status=False
  1223. )
  1224. self.assertEqual(len(prs), 0)
  1225. # All non-assigned PR
  1226. prs = pagure.lib.search_pull_requests(
  1227. session=self.session,
  1228. assignee=False
  1229. )
  1230. self.assertEqual(len(prs), 1)
  1231. prs[0].assignee_id = 1
  1232. self.session.add(prs[0])
  1233. self.session.commit()
  1234. # All the PR assigned
  1235. prs = pagure.lib.search_pull_requests(
  1236. session=self.session,
  1237. assignee=True
  1238. )
  1239. self.assertEqual(len(prs), 1)
  1240. # Basically the same as above but then for a specific user
  1241. prs = pagure.lib.search_pull_requests(
  1242. session=self.session,
  1243. assignee='pingou'
  1244. )
  1245. self.assertEqual(len(prs), 1)
  1246. # All PR except those assigned to pingou
  1247. prs = pagure.lib.search_pull_requests(
  1248. session=self.session,
  1249. assignee='!pingou'
  1250. )
  1251. self.assertEqual(len(prs), 0)
  1252. # All PR created by the specified author
  1253. prs = pagure.lib.search_pull_requests(
  1254. session=self.session,
  1255. author='pingou'
  1256. )
  1257. self.assertEqual(len(prs), 1)
  1258. # Count the PR instead of listing them
  1259. prs = pagure.lib.search_pull_requests(
  1260. session=self.session,
  1261. author='pingou',
  1262. count=True
  1263. )
  1264. self.assertEqual(prs, 1)
  1265. @patch('pagure.lib.notify.send_email')
  1266. def test_close_pull_request(self, send_email):
  1267. """ Test close_pull_request of pagure.lib. """
  1268. send_email.return_value = True
  1269. self.test_new_pull_request()
  1270. repo = pagure.lib.get_project(self.session, 'test')
  1271. self.assertEqual(repo.open_requests, 1)
  1272. request = pagure.lib.search_pull_requests(self.session, requestid=1)
  1273. pagure.lib.close_pull_request(
  1274. session=self.session,
  1275. request=request,
  1276. user='pingou',
  1277. requestfolder=None,
  1278. merged=True,
  1279. )
  1280. self.session.commit()
  1281. repo = pagure.lib.get_project(self.session, 'test')
  1282. self.assertEqual(repo.open_requests, 0)
  1283. prs = pagure.lib.search_pull_requests(
  1284. session=self.session,
  1285. status=False
  1286. )
  1287. self.assertEqual(len(prs), 1)
  1288. # Does not change much, just the notification sent
  1289. pagure.lib.close_pull_request(
  1290. session=self.session,
  1291. request=request,
  1292. user='pingou',
  1293. requestfolder=None,
  1294. merged=False,
  1295. )
  1296. self.session.commit()
  1297. prs = pagure.lib.search_pull_requests(
  1298. session=self.session,
  1299. status=False
  1300. )
  1301. self.assertEqual(len(prs), 1)
  1302. @patch('pagure.lib.git.update_git')
  1303. @patch('pagure.lib.notify.send_email')
  1304. def test_remove_issue_dependency(self, p_send_email, p_ugt):
  1305. """ Test remove_issue_dependency of pagure.lib. """
  1306. p_send_email.return_value = True
  1307. p_ugt.return_value = True
  1308. self.test_add_issue_dependency()
  1309. repo = pagure.lib.get_project(self.session, 'test')
  1310. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1311. issue_blocked = pagure.lib.search_issues(
  1312. self.session, repo, issueid=2)
  1313. # Before
  1314. self.assertEqual(len(issue.parents), 1)
  1315. self.assertEqual(issue.parents[0].id, 2)
  1316. self.assertEqual(len(issue.children), 0)
  1317. self.assertEqual(issue.children, [])
  1318. self.assertEqual(len(issue_blocked.parents), 0)
  1319. self.assertEqual(issue_blocked.parents, [])
  1320. self.assertEqual(len(issue_blocked.children), 1)
  1321. self.assertEqual(issue_blocked.children[0].id, 1)
  1322. self.assertRaises(
  1323. pagure.exceptions.PagureException,
  1324. pagure.lib.remove_issue_dependency,
  1325. session=self.session,
  1326. issue=issue,
  1327. issue_blocked=issue,
  1328. user='pingou',
  1329. ticketfolder=None)
  1330. # Wrong order of issues
  1331. msg = pagure.lib.remove_issue_dependency(
  1332. session=self.session,
  1333. issue=issue,
  1334. issue_blocked=issue_blocked,
  1335. user='pingou',
  1336. ticketfolder=None)
  1337. self.session.commit()
  1338. self.assertEqual(msg, None)
  1339. # Drop deps
  1340. msg = pagure.lib.remove_issue_dependency(
  1341. session=self.session,
  1342. issue=issue_blocked,
  1343. issue_blocked=issue,
  1344. user='pingou',
  1345. ticketfolder=None)
  1346. self.session.commit()
  1347. self.assertEqual(msg, 'Dependency removed')
  1348. # After
  1349. self.assertEqual(issue.parents, [])
  1350. self.assertEqual(issue.children, [])
  1351. self.assertEqual(issue_blocked.parents, [])
  1352. self.assertEqual(issue_blocked.children, [])
  1353. @patch('pagure.lib.git.update_git')
  1354. @patch('pagure.lib.notify.send_email')
  1355. def test_get_issue_comment(self, p_send_email, p_ugt):
  1356. """ Test the get_issue_comment of pagure.lib. """
  1357. p_send_email.return_value = True
  1358. p_ugt.return_value = True
  1359. self.test_add_issue_comment()
  1360. repo = pagure.lib.get_project(self.session, 'test')
  1361. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1362. self.assertEqual(
  1363. pagure.lib.get_issue_comment(self.session, issue.uid, 10),
  1364. None
  1365. )
  1366. comment = pagure.lib.get_issue_comment(self.session, issue.uid, 1)
  1367. self.assertEqual(comment.comment, 'Hey look a comment!')
  1368. @patch('pagure.lib.git.update_git')
  1369. @patch('pagure.lib.notify.send_email')
  1370. def test_get_issue_by_uid(self, p_send_email, p_ugt):
  1371. """ Test the get_issue_by_uid 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(
  1378. pagure.lib.get_issue_by_uid(self.session, 'foobar'),
  1379. None
  1380. )
  1381. new_issue = pagure.lib.get_issue_by_uid(self.session, issue.uid)
  1382. self.assertEqual(issue, new_issue)
  1383. @patch('pagure.lib.git.update_git')
  1384. @patch('pagure.lib.notify.send_email')
  1385. def test_update_tags(self, p_send_email, p_ugt):
  1386. """ Test the update_tags of pagure.lib. """
  1387. p_send_email.return_value = True
  1388. p_ugt.return_value = True
  1389. self.test_new_issue()
  1390. repo = pagure.lib.get_project(self.session, 'test')
  1391. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1392. # before
  1393. self.assertEqual(issue.tags_text, [])
  1394. messages = pagure.lib.update_tags(
  1395. self.session, issue, 'tag', 'pingou', ticketfolder=None)
  1396. self.assertEqual(messages, ['Tag added: tag'])
  1397. messages = pagure.lib.update_tags(
  1398. self.session, issue, ['tag2', 'tag3'], 'pingou',
  1399. ticketfolder=None)
  1400. self.assertEqual(
  1401. messages, ['Tag added: tag2, tag3', 'Removed tag: tag'])
  1402. # after
  1403. self.assertEqual(issue.tags_text, ['tag2', 'tag3'])
  1404. @patch('pagure.lib.git.update_git')
  1405. @patch('pagure.lib.notify.send_email')
  1406. def test_update_dependency_issue(self, p_send_email, p_ugt):
  1407. """ Test the update_dependency_issue of pagure.lib. """
  1408. p_send_email.return_value = True
  1409. p_ugt.return_value = True
  1410. self.test_new_issue()
  1411. repo = pagure.lib.get_project(self.session, 'test')
  1412. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1413. self.assertEqual(repo.open_tickets, 2)
  1414. self.assertEqual(repo.open_tickets_public, 2)
  1415. # Create issues to play with
  1416. msg = pagure.lib.new_issue(
  1417. session=self.session,
  1418. repo=repo,
  1419. title='Test issue #3',
  1420. content='We should work on this (3rd time!)',
  1421. user='pingou',
  1422. ticketfolder=None,
  1423. private=True,
  1424. )
  1425. self.session.commit()
  1426. self.assertEqual(msg.title, 'Test issue #3')
  1427. self.assertEqual(repo.open_tickets, 3)
  1428. self.assertEqual(repo.open_tickets_public, 2)
  1429. # before
  1430. self.assertEqual(issue.tags_text, [])
  1431. self.assertEqual(issue.depends_text, [])
  1432. self.assertEqual(issue.blocks_text, [])
  1433. messages = pagure.lib.update_dependency_issue(
  1434. self.session, repo, issue, '2', 'pingou', ticketfolder=None)
  1435. self.assertEqual(messages, ['Dependency added'])
  1436. messages = pagure.lib.update_dependency_issue(
  1437. self.session, repo, issue, ['3', '4', 5], 'pingou',
  1438. ticketfolder=None)
  1439. self.assertEqual(
  1440. messages, ['Dependency added', 'Dependency removed'])
  1441. # after
  1442. self.assertEqual(issue.tags_text, [])
  1443. self.assertEqual(issue.depends_text, [3])
  1444. self.assertEqual(issue.blocks_text, [])
  1445. @patch('pagure.lib.git.update_git')
  1446. @patch('pagure.lib.notify.send_email')
  1447. def test_update_blocked_issue(self, p_send_email, p_ugt):
  1448. """ Test the update_blocked_issue of pagure.lib. """
  1449. p_send_email.return_value = True
  1450. p_ugt.return_value = True
  1451. self.test_new_issue()
  1452. repo = pagure.lib.get_project(self.session, 'test')
  1453. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1454. # Create issues to play with
  1455. msg = pagure.lib.new_issue(
  1456. session=self.session,
  1457. repo=repo,
  1458. title='Test issue #3',
  1459. content='We should work on this (3rd time!)',
  1460. user='pingou',
  1461. ticketfolder=None,
  1462. private=True,
  1463. )
  1464. self.session.commit()
  1465. self.assertEqual(msg.title, 'Test issue #3')
  1466. # before
  1467. self.assertEqual(issue.tags_text, [])
  1468. self.assertEqual(issue.depends_text, [])
  1469. self.assertEqual(issue.blocks_text, [])
  1470. messages = pagure.lib.update_blocked_issue(
  1471. self.session, repo, issue, '2', 'pingou', ticketfolder=None)
  1472. self.assertEqual(messages, ['Dependency added'])
  1473. messages = pagure.lib.update_blocked_issue(
  1474. self.session, repo, issue, ['3', '4', 5], 'pingou',
  1475. ticketfolder=None)
  1476. self.assertEqual(
  1477. messages, ['Dependency added', 'Dependency removed'])
  1478. # after
  1479. self.assertEqual(issue.tags_text, [])
  1480. self.assertEqual(issue.depends_text, [])
  1481. self.assertEqual(issue.blocks_text, [3])
  1482. @patch('pagure.lib.notify.send_email')
  1483. def test_add_pull_request_assignee(self, mockemail):
  1484. """ Test add_pull_request_assignee of pagure.lib. """
  1485. mockemail.return_value = True
  1486. self.test_new_pull_request()
  1487. request = pagure.lib.search_pull_requests(self.session, requestid=1)
  1488. self.assertRaises(
  1489. pagure.exceptions.PagureException,
  1490. pagure.lib.add_pull_request_assignee,
  1491. session=self.session,
  1492. request=request,
  1493. assignee='bar',
  1494. user='foo',
  1495. requestfolder=None,
  1496. )
  1497. # Assign
  1498. msg = pagure.lib.add_pull_request_assignee(
  1499. session=self.session,
  1500. request=request,
  1501. assignee='pingou',
  1502. user='foo',
  1503. requestfolder=None,
  1504. )
  1505. self.assertEqual(msg, 'Request assigned')
  1506. # Reset
  1507. msg = pagure.lib.add_pull_request_assignee(
  1508. session=self.session,
  1509. request=request,
  1510. assignee=None,
  1511. user='foo',
  1512. requestfolder=None,
  1513. )
  1514. self.assertEqual(msg, 'Request reset')
  1515. # Try resetting again
  1516. msg = pagure.lib.add_pull_request_assignee(
  1517. session=self.session,
  1518. request=request,
  1519. assignee=None,
  1520. user='foo',
  1521. requestfolder=None,
  1522. )
  1523. self.assertEqual(msg, None)
  1524. def test_search_pending_email(self):
  1525. """ Test search_pending_email of pagure.lib. """
  1526. self.assertEqual(
  1527. pagure.lib.search_pending_email(self.session), None)
  1528. user = pagure.lib.search_user(self.session, username='pingou')
  1529. email_pend = pagure.lib.model.UserEmailPending(
  1530. user_id=user.id,
  1531. email='foo@fp.o',
  1532. token='abcdef',
  1533. )
  1534. self.session.add(email_pend)
  1535. self.session.commit()
  1536. self.assertNotEqual(
  1537. pagure.lib.search_pending_email(self.session), None)
  1538. self.assertNotEqual(
  1539. pagure.lib.search_pending_email(self.session, token='abcdef'),
  1540. None)
  1541. pend = pagure.lib.search_pending_email(self.session, token='abcdef')
  1542. self.assertEqual(pend.user.username, 'pingou')
  1543. self.assertEqual(pend.email, 'foo@fp.o')
  1544. self.assertEqual(pend.token, 'abcdef')
  1545. pend = pagure.lib.search_pending_email(self.session, email='foo@fp.o')
  1546. self.assertEqual(pend.user.username, 'pingou')
  1547. self.assertEqual(pend.email, 'foo@fp.o')
  1548. self.assertEqual(pend.token, 'abcdef')
  1549. def test_generate_hook_token(self):
  1550. """ Test generate_hook_token of pagure.lib. """
  1551. tests.create_projects(self.session)
  1552. projects = pagure.lib.search_projects(self.session)
  1553. for proj in projects:
  1554. self.assertIn(proj.hook_token, ['aaabbbccc', 'aaabbbddd'])
  1555. pagure.lib.generate_hook_token(self.session)
  1556. projects = pagure.lib.search_projects(self.session)
  1557. for proj in projects:
  1558. self.assertNotIn(proj.hook_token, ['aaabbbccc', 'aaabbbddd'])
  1559. @patch('pagure.lib.notify.send_email')
  1560. def test_pull_request_score(self, mockemail):
  1561. """ Test PullRequest.score of pagure.lib.model. """
  1562. mockemail.return_value = True
  1563. self.test_new_pull_request()
  1564. request = pagure.lib.search_pull_requests(self.session, requestid=1)
  1565. msg = pagure.lib.add_pull_request_comment(
  1566. session=self.session,
  1567. request=request,
  1568. commit=None,
  1569. tree_id=None,
  1570. filename=None,
  1571. row=None,
  1572. comment='This looks great :thumbsup:',
  1573. user='foo',
  1574. requestfolder=None,
  1575. )
  1576. self.session.commit()
  1577. self.assertEqual(msg, 'Comment added')
  1578. msg = pagure.lib.add_pull_request_comment(
  1579. session=self.session,
  1580. request=request,
  1581. commit=None,
  1582. tree_id=None,
  1583. filename=None,
  1584. row=None,
  1585. comment='I disagree -1',
  1586. user='pingou',
  1587. requestfolder=None,
  1588. )
  1589. self.session.commit()
  1590. self.assertEqual(msg, 'Comment added')
  1591. msg = pagure.lib.add_pull_request_comment(
  1592. session=self.session,
  1593. request=request,
  1594. commit=None,
  1595. tree_id=None,
  1596. filename=None,
  1597. row=None,
  1598. comment='NM this looks great now +1000',
  1599. user='pingou',
  1600. requestfolder=None,
  1601. )
  1602. self.session.commit()
  1603. self.assertEqual(msg, 'Comment added')
  1604. self.assertEqual(len(request.discussion), 3)
  1605. self.assertEqual(request.score, 1)
  1606. def test_add_group(self):
  1607. """ Test the add_group method of pagure.lib. """
  1608. groups = pagure.lib.search_groups(self.session)
  1609. self.assertEqual(len(groups), 0)
  1610. self.assertEqual(groups, [])
  1611. # Invalid type
  1612. self.assertRaises(
  1613. pagure.exceptions.PagureException,
  1614. pagure.lib.add_group,
  1615. self.session,
  1616. group_name='foo',
  1617. group_type='bar',
  1618. user='pingou',
  1619. is_admin=True,
  1620. blacklist=[],
  1621. )
  1622. groups = pagure.lib.search_groups(self.session)
  1623. self.assertEqual(len(groups), 0)
  1624. self.assertEqual(groups, [])
  1625. # Invalid user
  1626. self.assertRaises(
  1627. pagure.exceptions.PagureException,
  1628. pagure.lib.add_group,
  1629. self.session,
  1630. group_name='foo',
  1631. group_type='user',
  1632. user='test',
  1633. is_admin=False,
  1634. blacklist=[],
  1635. )
  1636. groups = pagure.lib.search_groups(self.session)
  1637. self.assertEqual(len(groups), 0)
  1638. self.assertEqual(groups, [])
  1639. msg = pagure.lib.add_group(
  1640. self.session,
  1641. group_name='foo',
  1642. group_type='bar',
  1643. user='pingou',
  1644. is_admin=False,
  1645. blacklist=[],
  1646. )
  1647. self.session.commit()
  1648. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  1649. groups = pagure.lib.search_groups(self.session)
  1650. self.assertEqual(len(groups), 1)
  1651. self.assertEqual(groups[0].group_name, 'foo')
  1652. # Group with this name already exists
  1653. self.assertRaises(
  1654. pagure.exceptions.PagureException,
  1655. pagure.lib.add_group,
  1656. self.session,
  1657. group_name='foo',
  1658. group_type='bar',
  1659. user='pingou',
  1660. is_admin=False,
  1661. blacklist=[],
  1662. )
  1663. # Group with a blacklisted prefix
  1664. self.assertRaises(
  1665. pagure.exceptions.PagureException,
  1666. pagure.lib.add_group,
  1667. self.session,
  1668. group_name='forks',
  1669. group_type='bar',
  1670. user='pingou',
  1671. is_admin=False,
  1672. blacklist=['forks'],
  1673. )
  1674. def test_add_user_to_group(self):
  1675. """ Test the add_user_to_group method of pagure.lib. """
  1676. self.test_add_group()
  1677. group = pagure.lib.search_groups(self.session, group_name='foo')
  1678. self.assertNotEqual(group, None)
  1679. self.assertEqual(group.group_name, 'foo')
  1680. # Invalid new user
  1681. self.assertRaises(
  1682. pagure.exceptions.PagureException,
  1683. pagure.lib.add_user_to_group,
  1684. self.session,
  1685. username='foobar',
  1686. group=group,
  1687. user='foo',
  1688. is_admin=False,
  1689. )
  1690. # Invalid user
  1691. self.assertRaises(
  1692. pagure.exceptions.PagureException,
  1693. pagure.lib.add_user_to_group,
  1694. self.session,
  1695. username='foo',
  1696. group=group,
  1697. user='foobar',
  1698. is_admin=False,
  1699. )
  1700. # User not allowed
  1701. self.assertRaises(
  1702. pagure.exceptions.PagureException,
  1703. pagure.lib.add_user_to_group,
  1704. self.session,
  1705. username='foo',
  1706. group=group,
  1707. user='foo',
  1708. is_admin=False,
  1709. )
  1710. msg = pagure.lib.add_user_to_group(
  1711. self.session,
  1712. username='foo',
  1713. group=group,
  1714. user='pingou',
  1715. is_admin=False,
  1716. )
  1717. self.session.commit()
  1718. self.assertEqual(msg, 'User `foo` added to the group `foo`.')
  1719. msg = pagure.lib.add_user_to_group(
  1720. self.session,
  1721. username='foo',
  1722. group=group,
  1723. user='pingou',
  1724. is_admin=False,
  1725. )
  1726. self.session.commit()
  1727. self.assertEqual(
  1728. msg, 'User `foo` already in the group, nothing to change.')
  1729. def test_is_group_member(self):
  1730. """ Test the is_group_member method of pagure.lib. """
  1731. self.test_add_group()
  1732. self.assertFalse(
  1733. pagure.lib.is_group_member(self.session, None, 'foo'))
  1734. self.assertFalse(
  1735. pagure.lib.is_group_member(self.session, 'bar', 'foo'))
  1736. self.assertFalse(
  1737. pagure.lib.is_group_member(self.session, 'foo', 'foo'))
  1738. self.assertTrue(
  1739. pagure.lib.is_group_member(self.session, 'pingou', 'foo'))
  1740. def test_get_user_group(self):
  1741. """ Test the get_user_group method of pagure.lib. """
  1742. self.test_add_group()
  1743. item = pagure.lib.get_user_group(self.session, 1, 1)
  1744. self.assertEqual(item.user_id, 1)
  1745. self.assertEqual(item.group_id, 1)
  1746. item = pagure.lib.get_user_group(self.session, 1, 2)
  1747. self.assertEqual(item, None)
  1748. item = pagure.lib.get_user_group(self.session, 2, 1)
  1749. self.assertEqual(item, None)
  1750. def test_get_group_types(self):
  1751. """ Test the get_group_types method of pagure.lib. """
  1752. self.test_add_group()
  1753. groups = pagure.lib.get_group_types(self.session, 'user')
  1754. self.assertEqual(len(groups), 1)
  1755. self.assertEqual(groups[0].group_type, 'user')
  1756. groups = pagure.lib.get_group_types(self.session)
  1757. self.assertEqual(len(groups), 2)
  1758. self.assertEqual(groups[0].group_type, 'admin')
  1759. self.assertEqual(groups[1].group_type, 'user')
  1760. def test_search_groups(self):
  1761. """ Test the search_groups method of pagure.lib. """
  1762. self.assertEqual(pagure.lib.search_groups(self.session), [])
  1763. msg = pagure.lib.add_group(
  1764. self.session,
  1765. group_name='foo',
  1766. group_type='bar',
  1767. user='pingou',
  1768. is_admin=False,
  1769. blacklist=[],
  1770. )
  1771. self.session.commit()
  1772. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  1773. groups = pagure.lib.search_groups(self.session)
  1774. self.assertEqual(len(groups), 1)
  1775. self.assertEqual(groups[0].group_name, 'foo')
  1776. msg = pagure.lib.add_group(
  1777. self.session,
  1778. group_name='bar',
  1779. group_type='admin',
  1780. user='pingou',
  1781. is_admin=True,
  1782. blacklist=[],
  1783. )
  1784. self.session.commit()
  1785. self.assertEqual(msg, 'User `pingou` added to the group `bar`.')
  1786. groups = pagure.lib.search_groups(self.session)
  1787. self.assertEqual(len(groups), 2)
  1788. self.assertEqual(groups[0].group_name, 'bar')
  1789. self.assertEqual(groups[1].group_name, 'foo')
  1790. groups = pagure.lib.search_groups(self.session, group_type='user')
  1791. self.assertEqual(len(groups), 1)
  1792. self.assertEqual(groups[0].group_name, 'foo')
  1793. groups = pagure.lib.search_groups(self.session, group_type='admin')
  1794. self.assertEqual(len(groups), 1)
  1795. self.assertEqual(groups[0].group_name, 'bar')
  1796. groups = pagure.lib.search_groups(self.session, group_name='foo')
  1797. self.assertEqual(groups.group_name, 'foo')
  1798. def test_delete_user_of_group(self):
  1799. """ Test the delete_user_of_group method of pagure.lib. """
  1800. self.test_add_user_to_group()
  1801. groups = pagure.lib.search_groups(self.session)
  1802. self.assertEqual(len(groups), 1)
  1803. self.assertEqual(groups[0].group_name, 'foo')
  1804. # Invalid username
  1805. self.assertRaises(
  1806. pagure.exceptions.PagureException,
  1807. pagure.lib.delete_user_of_group,
  1808. self.session,
  1809. username='bar',
  1810. groupname='foo',
  1811. user='pingou',
  1812. is_admin=False,
  1813. )
  1814. # Invalid groupname
  1815. self.assertRaises(
  1816. pagure.exceptions.PagureException,
  1817. pagure.lib.delete_user_of_group,
  1818. self.session,
  1819. username='foo',
  1820. groupname='bar',
  1821. user='pingou',
  1822. is_admin=False,
  1823. )
  1824. # Invalid user
  1825. self.assertRaises(
  1826. pagure.exceptions.PagureException,
  1827. pagure.lib.delete_user_of_group,
  1828. self.session,
  1829. username='foo',
  1830. groupname='foo',
  1831. user='test',
  1832. is_admin=False,
  1833. )
  1834. # User not in the group
  1835. item = pagure.lib.model.User(
  1836. user='bar',
  1837. fullname='bar',
  1838. password='foo',
  1839. default_email='bar@bar.com',
  1840. )
  1841. self.session.add(item)
  1842. item = pagure.lib.model.UserEmail(
  1843. user_id=3,
  1844. email='bar@bar.com')
  1845. self.session.add(item)
  1846. self.session.commit()
  1847. self.assertRaises(
  1848. pagure.exceptions.PagureException,
  1849. pagure.lib.delete_user_of_group,
  1850. self.session,
  1851. username='bar',
  1852. groupname='foo',
  1853. user='pingou',
  1854. is_admin=False,
  1855. )
  1856. # User is not allowed to remove the username
  1857. self.assertRaises(
  1858. pagure.exceptions.PagureException,
  1859. pagure.lib.delete_user_of_group,
  1860. self.session,
  1861. username='foo',
  1862. groupname='foo',
  1863. user='bar',
  1864. is_admin=False,
  1865. )
  1866. # Username is the creator of the group
  1867. self.assertRaises(
  1868. pagure.exceptions.PagureException,
  1869. pagure.lib.delete_user_of_group,
  1870. self.session,
  1871. username='pingou',
  1872. groupname='foo',
  1873. user='pingou',
  1874. is_admin=False,
  1875. )
  1876. # All good
  1877. group = pagure.lib.search_groups(self.session, group_name='foo')
  1878. self.assertEqual(len(group.users), 2)
  1879. pagure.lib.delete_user_of_group(
  1880. self.session,
  1881. username='foo',
  1882. groupname='foo',
  1883. user='pingou',
  1884. is_admin=False,
  1885. )
  1886. self.session.commit()
  1887. group = pagure.lib.search_groups(self.session, group_name='foo')
  1888. self.assertEqual(len(group.users), 1)
  1889. def test_add_group_to_project(self):
  1890. """ Test the add_group_to_project method of pagure.lib. """
  1891. tests.create_projects(self.session)
  1892. self.test_add_group()
  1893. project = pagure.lib.get_project(self.session, 'test2')
  1894. # Group does not exist
  1895. self.assertRaises(
  1896. pagure.exceptions.PagureException,
  1897. pagure.lib.add_group_to_project,
  1898. session=self.session,
  1899. project=project,
  1900. new_group='bar',
  1901. user='foo',
  1902. )
  1903. # User does not exist
  1904. self.assertRaises(
  1905. pagure.exceptions.PagureException,
  1906. pagure.lib.add_group_to_project,
  1907. session=self.session,
  1908. project=project,
  1909. new_group='foo',
  1910. user='bar',
  1911. )
  1912. # User not allowed
  1913. self.assertRaises(
  1914. pagure.exceptions.PagureException,
  1915. pagure.lib.add_group_to_project,
  1916. session=self.session,
  1917. project=project,
  1918. new_group='foo',
  1919. user='foo',
  1920. )
  1921. # All good
  1922. msg = pagure.lib.add_group_to_project(
  1923. session=self.session,
  1924. project=project,
  1925. new_group='foo',
  1926. user='pingou',
  1927. )
  1928. self.session.commit()
  1929. self.assertEqual(msg, 'Group added')
  1930. # Group already associated with the project
  1931. self.assertRaises(
  1932. pagure.exceptions.PagureException,
  1933. pagure.lib.add_group_to_project,
  1934. session=self.session,
  1935. project=project,
  1936. new_group='foo',
  1937. user='pingou',
  1938. )
  1939. def test_update_watch_status(self):
  1940. """ Test the update_watch_status method of pagure.lib. """
  1941. tests.create_projects(self.session)
  1942. project = pagure.lib.get_project(self.session, 'test')
  1943. # User does not exist
  1944. self.assertRaises(
  1945. pagure.exceptions.PagureException,
  1946. pagure.lib.update_watch_status,
  1947. session=self.session,
  1948. project=project,
  1949. user='aavrug',
  1950. watch='1',
  1951. )
  1952. # All good and when user seleted watch option.
  1953. msg = pagure.lib.update_watch_status(
  1954. session=self.session,
  1955. project=project,
  1956. user='pingou',
  1957. watch='1',
  1958. )
  1959. self.session.commit()
  1960. self.assertEqual(msg, 'You are now watching this repo.')
  1961. # All good and when user selected unwatch option.
  1962. msg = pagure.lib.update_watch_status(
  1963. session=self.session,
  1964. project=project,
  1965. user='pingou',
  1966. watch='0',
  1967. )
  1968. self.session.commit()
  1969. self.assertEqual(msg, 'You are no longer watching this repo.')
  1970. def test_is_watching(self):
  1971. """ Test the is_watching method of pagure.lib. """
  1972. tests.create_projects(self.session)
  1973. self.test_add_group()
  1974. project = pagure.lib.get_project(self.session, 'test')
  1975. # If user not logged in
  1976. watch = pagure.lib.is_watching(
  1977. session=self.session,
  1978. user=None,
  1979. reponame='test',
  1980. )
  1981. self.assertFalse(watch)
  1982. # User does not exist
  1983. user = tests.FakeUser()
  1984. user.username = 'aavrug'
  1985. watch = pagure.lib.is_watching(
  1986. session=self.session,
  1987. user=user,
  1988. reponame='test',
  1989. )
  1990. self.assertFalse(watch)
  1991. pagure.lib.add_group_to_project(
  1992. session=self.session,
  1993. project=project,
  1994. new_group='foo',
  1995. user='pingou',
  1996. )
  1997. self.session.commit()
  1998. group = pagure.lib.search_groups(self.session, group_name='foo')
  1999. pagure.lib.add_user_to_group(
  2000. self.session,
  2001. username='foo',
  2002. group=group,
  2003. user='pingou',
  2004. is_admin=False,
  2005. )
  2006. self.session.commit()
  2007. group = pagure.lib.search_groups(self.session, group_name='foo')
  2008. # If user belongs to any group of that project
  2009. user.username = 'foo'
  2010. watch = pagure.lib.is_watching(
  2011. session=self.session,
  2012. user=user,
  2013. reponame='test',
  2014. )
  2015. self.assertTrue(watch)
  2016. # If user is the creator
  2017. user.username = 'pingou'
  2018. watch = pagure.lib.is_watching(
  2019. session=self.session,
  2020. user=user,
  2021. reponame='test',
  2022. )
  2023. self.assertTrue(watch)
  2024. # Entry into watchers table
  2025. pagure.lib.update_watch_status(
  2026. session=self.session,
  2027. project=project,
  2028. user='pingou',
  2029. watch='1',
  2030. )
  2031. self.session.commit()
  2032. # From watchers table
  2033. watch = pagure.lib.is_watching(
  2034. session=self.session,
  2035. user=user,
  2036. reponame='test',
  2037. )
  2038. self.assertTrue(watch)
  2039. # Entry into watchers table
  2040. pagure.lib.update_watch_status(
  2041. session=self.session,
  2042. project=project,
  2043. user='pingou',
  2044. watch='0',
  2045. )
  2046. self.session.commit()
  2047. # From watchers table
  2048. watch = pagure.lib.is_watching(
  2049. session=self.session,
  2050. user=user,
  2051. reponame='test',
  2052. )
  2053. self.assertFalse(watch)
  2054. if __name__ == '__main__':
  2055. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibtests)
  2056. unittest.TextTestRunner(verbosity=2).run(SUITE)