test_pagure_lib.py 92 KB

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