test_pagure_flask_ui_app.py 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import unittest
  10. import shutil
  11. import sys
  12. import tempfile
  13. import os
  14. import six
  15. import json
  16. import pygit2
  17. from mock import patch, MagicMock
  18. sys.path.insert(
  19. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  20. )
  21. import pagure.lib.query
  22. import tests
  23. class PagureFlaskApptests(tests.Modeltests):
  24. """ Tests for flask app controller of pagure """
  25. def test_watch_list(self):
  26. """ Test for watch list of a user """
  27. user = tests.FakeUser(username="pingou")
  28. with tests.user_set(self.app.application, user):
  29. output = self.app.get("/", follow_redirects=True)
  30. output_text = output.get_data(as_text=True)
  31. self.assertIn(
  32. '<div class="text-center">You have no Projects</div>',
  33. output_text,
  34. )
  35. tests.create_projects(self.session)
  36. output = self.app.get("/", follow_redirects=True)
  37. output_text = output.get_data(as_text=True)
  38. self.assertIn(
  39. '<h4 class="font-weight-bold mb-0">My Projects</h4>',
  40. output_text,
  41. )
  42. def test_view_users(self):
  43. """ Test the view_users endpoint. """
  44. output = self.app.get("/users/?page=abc")
  45. self.assertEqual(output.status_code, 200)
  46. output_text = output.get_data(as_text=True)
  47. self.assertIn(
  48. '<h3 class="mb-3 font-weight-bold">\n Users '
  49. '<span class="badge badge-secondary">2</span>',
  50. output_text,
  51. )
  52. self.assertIn(
  53. '<a href="/user/pingou">\n '
  54. '<div class="nowrap"><strong>pingou</strong>',
  55. output_text,
  56. )
  57. self.assertIn(
  58. '<a href="/user/foo">\n '
  59. '<div class="nowrap"><strong>foo</strong>',
  60. output_text,
  61. )
  62. @patch.dict("pagure.config.config", {"ITEM_PER_PAGE": 2})
  63. def test_view_user_repo_cnt(self):
  64. """ Test the repo counts on the view_user endpoint. """
  65. tests.create_projects(self.session)
  66. self.gitrepos = tests.create_projects_git(
  67. pagure.config.config["GIT_FOLDER"]
  68. )
  69. output = self.app.get("/user/pingou")
  70. self.assertEqual(output.status_code, 200)
  71. output_text = output.get_data(as_text=True)
  72. self.assertIn(
  73. """<span>
  74. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  75. <span class="d-none d-md-inline">Projects&nbsp;</span>
  76. </span>
  77. <div class="ml-auto">
  78. <span class="badge badge-secondary">
  79. 3
  80. </span>
  81. </div>""",
  82. output_text,
  83. )
  84. self.assertIn(
  85. """<span>
  86. <i class="fa fa-fw text-muted fa-code-fork"></i>
  87. <span class="d-none d-md-inline">Forks&nbsp;</span>
  88. </span>
  89. <div class="ml-auto">
  90. <span class="badge badge-secondary">
  91. 0
  92. </span>
  93. </div>""",
  94. output_text,
  95. )
  96. def test_view_user(self):
  97. """ Test the view_user endpoint. """
  98. output = self.app.get("/user/pingou?repopage=abc&forkpage=def")
  99. self.assertEqual(output.status_code, 200)
  100. output_text = output.get_data(as_text=True)
  101. self.assertIn(
  102. """<span>
  103. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  104. <span class="d-none d-md-inline">Projects&nbsp;</span>
  105. </span>
  106. <div class="ml-auto">
  107. <span class="badge badge-secondary">
  108. 0
  109. </span>
  110. </div>""",
  111. output_text,
  112. )
  113. self.assertIn(
  114. """<span>
  115. <i class="fa fa-fw text-muted fa-code-fork"></i>
  116. <span class="d-none d-md-inline">Forks&nbsp;</span>
  117. </span>
  118. <div class="ml-auto">
  119. <span class="badge badge-secondary">
  120. 0
  121. </span>
  122. </div>""",
  123. output_text,
  124. )
  125. tests.create_projects(self.session)
  126. self.gitrepos = tests.create_projects_git(
  127. pagure.config.config["GIT_FOLDER"]
  128. )
  129. output = self.app.get("/user/pingou?repopage=abc&forkpage=def")
  130. self.assertEqual(output.status_code, 200)
  131. output_text = output.get_data(as_text=True)
  132. self.assertIn(
  133. """<span>
  134. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  135. <span class="d-none d-md-inline">Projects&nbsp;</span>
  136. </span>
  137. <div class="ml-auto">
  138. <span class="badge badge-secondary">
  139. 3
  140. </span>
  141. </div>""",
  142. output_text,
  143. )
  144. self.assertIn(
  145. """<span>
  146. <i class="fa fa-fw text-muted fa-code-fork"></i>
  147. <span class="d-none d-md-inline">Forks&nbsp;</span>
  148. </span>
  149. <div class="ml-auto">
  150. <span class="badge badge-secondary">
  151. 0
  152. </span>
  153. </div>""",
  154. output_text,
  155. )
  156. self.assertNotIn(
  157. '<a class="page-link" href="#" tabindex="-1">page 1 of 2</a>',
  158. output_text,
  159. )
  160. @patch.dict(
  161. "pagure.config.config",
  162. {
  163. "PAGURE_ADMIN_USERS": ["pingou"],
  164. "ALLOW_ADMIN_IGNORE_EXISTING_REPOS": True,
  165. },
  166. )
  167. def test_adopt_repos(self):
  168. """ Test the new_project endpoint with existing git repo. """
  169. # Before
  170. projects = pagure.lib.query.search_projects(self.session)
  171. self.assertEqual(len(projects), 0)
  172. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  173. tests.add_content_git_repo(
  174. os.path.join(self.path, "repos", "test.git")
  175. )
  176. user = tests.FakeUser(username="pingou")
  177. with tests.user_set(self.app.application, user):
  178. data = {
  179. "csrf_token": self.get_csrf(),
  180. "name": "test",
  181. "description": "Project #1",
  182. }
  183. output = self.app.post("/new/", data=data, follow_redirects=True)
  184. self.assertEqual(output.status_code, 200)
  185. output_text = output.get_data(as_text=True)
  186. self.assertIn("The main repo test.git already exists", output_text)
  187. data["ignore_existing_repos"] = "y"
  188. output = self.app.post("/new/", data=data, follow_redirects=True)
  189. self.assertEqual(output.status_code, 200)
  190. output_text = output.get_data(as_text=True)
  191. self.assertIn("Alice Author", output_text)
  192. @patch.dict(
  193. "pagure.config.config",
  194. {"PAGURE_ADMIN_USERS": [], "USERS_IGNORE_EXISTING_REPOS": ["pingou"]},
  195. )
  196. def test_adopt_repos_non_admin(self):
  197. """ Test the new_project endpoint with existing git repo for non-admins. """
  198. # Before
  199. projects = pagure.lib.query.search_projects(self.session)
  200. self.assertEqual(len(projects), 0)
  201. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  202. tests.add_content_git_repo(
  203. os.path.join(self.path, "repos", "test.git")
  204. )
  205. user = tests.FakeUser(username="pingou")
  206. with tests.user_set(self.app.application, user):
  207. data = {
  208. "csrf_token": self.get_csrf(),
  209. "name": "test",
  210. "description": "Project #1",
  211. }
  212. output = self.app.post("/new/", data=data, follow_redirects=True)
  213. self.assertEqual(output.status_code, 200)
  214. output_text = output.get_data(as_text=True)
  215. self.assertIn("The main repo test.git already exists", output_text)
  216. data["ignore_existing_repos"] = "y"
  217. output = self.app.post("/new/", data=data, follow_redirects=True)
  218. self.assertEqual(output.status_code, 200)
  219. output_text = output.get_data(as_text=True)
  220. self.assertIn("Alice Author", output_text)
  221. @patch.dict(
  222. "pagure.config.config",
  223. {"PAGURE_ADMIN_USERS": [], "USERS_IGNORE_EXISTING_REPOS": []},
  224. )
  225. def test_adopt_repos_not_allowed(self):
  226. """ Test the new_project endpoint with existing git repo for no access. """
  227. # Before
  228. projects = pagure.lib.query.search_projects(self.session)
  229. self.assertEqual(len(projects), 0)
  230. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  231. tests.add_content_git_repo(
  232. os.path.join(self.path, "repos", "test.git")
  233. )
  234. user = tests.FakeUser(username="pingou")
  235. with tests.user_set(self.app.application, user):
  236. data = {
  237. "csrf_token": self.get_csrf(),
  238. "name": "test",
  239. "description": "Project #1",
  240. }
  241. output = self.app.post("/new/", data=data, follow_redirects=True)
  242. self.assertEqual(output.status_code, 200)
  243. output_text = output.get_data(as_text=True)
  244. self.assertIn("The main repo test.git already exists", output_text)
  245. data["ignore_existing_repos"] = "y"
  246. output = self.app.post("/new/", data=data, follow_redirects=True)
  247. self.assertEqual(output.status_code, 200)
  248. output_text = output.get_data(as_text=True)
  249. self.assertIn("The main repo test.git already exists", output_text)
  250. @patch.dict("pagure.config.config", {"PROJECT_NAME_REGEX": "^1[a-z]*$"})
  251. def test_new_project_diff_regex(self):
  252. """ Test the new_project endpoint with a different regex. """
  253. # Before
  254. projects = pagure.lib.query.search_projects(self.session)
  255. self.assertEqual(len(projects), 0)
  256. user = tests.FakeUser(username="foo")
  257. with tests.user_set(self.app.application, user):
  258. output = self.app.get("/new/")
  259. self.assertEqual(output.status_code, 200)
  260. output_text = output.get_data(as_text=True)
  261. self.assertIn("<strong>Create new Project</strong>", output_text)
  262. csrf_token = self.get_csrf(output=output)
  263. data = {
  264. "description": "Project #1",
  265. "name": "project-1",
  266. "csrf_token": csrf_token,
  267. }
  268. output = self.app.post("/new/", data=data, follow_redirects=True)
  269. self.assertEqual(output.status_code, 200)
  270. output_text = output.get_data(as_text=True)
  271. self.assertIn("<title>New project - Pagure</title>", output_text)
  272. self.assertIn("<strong>Create new Project</strong>", output_text)
  273. self.assertIn(
  274. "<small>\n Invalid input.&nbsp;\n"
  275. " </small>",
  276. output_text,
  277. )
  278. @patch.dict("pagure.config.config", {"PRIVATE_PROJECTS": True})
  279. def test_new_project_private(self):
  280. """ Test the new_project endpoint for a private project. """
  281. # Before
  282. projects = pagure.lib.query.search_projects(self.session)
  283. self.assertEqual(len(projects), 0)
  284. self.assertFalse(
  285. os.path.exists(
  286. os.path.join(self.path, "repos", "foo", "project#1.git")
  287. )
  288. )
  289. self.assertFalse(
  290. os.path.exists(
  291. os.path.join(
  292. self.path, "repos", "tickets", "foo", "project#1.git"
  293. )
  294. )
  295. )
  296. self.assertFalse(
  297. os.path.exists(
  298. os.path.join(
  299. self.path, "repos", "docs", "foo", "project#1.git"
  300. )
  301. )
  302. )
  303. self.assertFalse(
  304. os.path.exists(
  305. os.path.join(
  306. self.path, "repos", "requests", "foo", "project#1.git"
  307. )
  308. )
  309. )
  310. user = tests.FakeUser()
  311. with tests.user_set(self.app.application, user):
  312. output = self.app.get("/new/")
  313. self.assertEqual(output.status_code, 200)
  314. self.assertIn(
  315. "<strong>Create new Project</strong>",
  316. output.get_data(as_text=True),
  317. )
  318. csrf_token = self.get_csrf(output=output)
  319. data = {"description": "Project #1", "private": True}
  320. output = self.app.post("/new/", data=data)
  321. self.assertEqual(output.status_code, 200)
  322. output_text = output.get_data(as_text=True)
  323. self.assertIn("<strong>Create new Project</strong>", output_text)
  324. self.assertIn(
  325. "<small>\n This field is required.&nbsp;\n"
  326. " </small>",
  327. output_text,
  328. )
  329. data["name"] = "project-1"
  330. output = self.app.post("/new/", data=data)
  331. self.assertEqual(output.status_code, 200)
  332. output_text = output.get_data(as_text=True)
  333. self.assertIn("<strong>Create new Project</strong>", output_text)
  334. self.assertNotIn(
  335. "<small>\n This field is required.&nbsp;\n"
  336. " </small>",
  337. output_text,
  338. )
  339. data["csrf_token"] = csrf_token
  340. output = self.app.post("/new/", data=data)
  341. self.assertEqual(output.status_code, 200)
  342. output_text = output.get_data(as_text=True)
  343. self.assertIn("<strong>Create new Project</strong>", output_text)
  344. self.assertIn("No user " "&#34;username&#34; found", output_text)
  345. user.username = "foo"
  346. with tests.user_set(self.app.application, user):
  347. data["csrf_token"] = csrf_token
  348. output = self.app.post("/new/", data=data, follow_redirects=True)
  349. self.assertEqual(output.status_code, 200)
  350. output_text = output.get_data(as_text=True)
  351. self.assertIn(
  352. '<div class="projectinfo my-3">\nProject #1', output_text
  353. )
  354. self.assertIn("<p>This repo is brand new!</p>", output_text)
  355. self.assertIn(
  356. "<title>Overview - foo/project-1 - Pagure</title>", output_text
  357. )
  358. # After
  359. projects = pagure.lib.query.search_projects(self.session)
  360. self.assertEqual(len(projects), 0)
  361. projects = pagure.lib.query.search_projects(self.session, private=True)
  362. self.assertEqual(len(projects), 1)
  363. self.assertTrue(
  364. os.path.exists(
  365. os.path.join(self.path, "repos", "foo", "project-1.git")
  366. )
  367. )
  368. self.assertTrue(
  369. os.path.exists(
  370. os.path.join(
  371. self.path, "repos", "tickets", "foo", "project-1.git"
  372. )
  373. )
  374. )
  375. self.assertTrue(
  376. os.path.exists(
  377. os.path.join(
  378. self.path, "repos", "docs", "foo", "project-1.git"
  379. )
  380. )
  381. )
  382. self.assertTrue(
  383. os.path.exists(
  384. os.path.join(
  385. self.path, "repos", "requests", "foo", "project-1.git"
  386. )
  387. )
  388. )
  389. def test_non_ascii_new_project(self):
  390. """ Test the new_project endpoint with a non-ascii project. """
  391. # Before
  392. projects = pagure.lib.query.search_projects(self.session)
  393. self.assertEqual(len(projects), 0)
  394. self.assertFalse(
  395. os.path.exists(os.path.join(self.path, "repos", "project-1.git"))
  396. )
  397. self.assertFalse(
  398. os.path.exists(
  399. os.path.join(self.path, "repos", "tickets", "project-1.git")
  400. )
  401. )
  402. self.assertFalse(
  403. os.path.exists(
  404. os.path.join(self.path, "repos", "docs", "project-1.git")
  405. )
  406. )
  407. self.assertFalse(
  408. os.path.exists(
  409. os.path.join(self.path, "repos", "requests", "project-1.git")
  410. )
  411. )
  412. user = tests.FakeUser()
  413. user.username = "foo"
  414. with tests.user_set(self.app.application, user):
  415. output = self.app.get("/new/")
  416. self.assertEqual(output.status_code, 200)
  417. output_text = output.get_data(as_text=True)
  418. self.assertIn("<strong>Create new Project</strong>", output_text)
  419. csrf_token = output_text.split(
  420. 'name="csrf_token" type="hidden" value="'
  421. )[1].split('">')[0]
  422. data = {
  423. "description": "Prõjéctö #1",
  424. "name": "project-1",
  425. "csrf_token": csrf_token,
  426. "create_readme": True,
  427. }
  428. output = self.app.post("/new/", data=data, follow_redirects=True)
  429. self.assertEqual(output.status_code, 200)
  430. output_text = output.get_data(as_text=True)
  431. self.assertIn(
  432. '<div class="projectinfo my-3">\nPrõjéctö #1', output_text
  433. )
  434. self.assertIn(
  435. """<section class="readme">
  436. <div class="markdown"><h1>project-1</h1>
  437. <p>Prõjéctö #1</p></div>
  438. </section>""",
  439. output_text,
  440. )
  441. data = {
  442. "description": "Мой первый суперский репозиторий",
  443. "name": "project-2",
  444. "csrf_token": csrf_token,
  445. "create_readme": True,
  446. }
  447. output = self.app.post("/new/", data=data, follow_redirects=True)
  448. self.assertEqual(output.status_code, 200)
  449. output_text = output.get_data(as_text=True)
  450. self.assertIn(
  451. '<div class="projectinfo my-3">\nМой первый суперский репозиторий',
  452. output_text,
  453. )
  454. self.assertIn(
  455. """<section class="readme">
  456. <div class="markdown"><h1>project-2</h1>
  457. <p>Мой первый суперский репозиторий</p></div>
  458. </section>""",
  459. output_text,
  460. )
  461. # After
  462. projects = pagure.lib.query.search_projects(self.session)
  463. self.assertEqual(len(projects), 2)
  464. for project in ["project-1", "project-2"]:
  465. self.assertTrue(
  466. os.path.exists(
  467. os.path.join(self.path, "repos", "%s.git" % project)
  468. )
  469. )
  470. self.assertTrue(
  471. os.path.exists(
  472. os.path.join(
  473. self.path, "repos", "tickets", "%s.git" % project
  474. )
  475. )
  476. )
  477. self.assertTrue(
  478. os.path.exists(
  479. os.path.join(
  480. self.path, "repos", "docs", "%s.git" % project
  481. )
  482. )
  483. )
  484. self.assertTrue(
  485. os.path.exists(
  486. os.path.join(
  487. self.path, "repos", "requests", "%s.git" % project
  488. )
  489. )
  490. )
  491. @patch("pygit2.init_repository", wraps=pygit2.init_repository)
  492. def test_new_project_with_template(self, pygit2init):
  493. """ Test the new_project endpoint for a new project with a template set.
  494. """
  495. # Before
  496. projects = pagure.lib.query.search_projects(self.session)
  497. self.assertEqual(len(projects), 0)
  498. self.assertFalse(
  499. os.path.exists(os.path.join(self.path, "repos", "project-1.git"))
  500. )
  501. self.assertFalse(
  502. os.path.exists(
  503. os.path.join(self.path, "repos", "tickets", "project-1.git")
  504. )
  505. )
  506. self.assertFalse(
  507. os.path.exists(
  508. os.path.join(self.path, "repos", "docs", "project-1.git")
  509. )
  510. )
  511. self.assertFalse(
  512. os.path.exists(
  513. os.path.join(self.path, "repos", "requests", "project-1.git")
  514. )
  515. )
  516. user = tests.FakeUser()
  517. user.username = "foo"
  518. with tests.user_set(self.app.application, user):
  519. output = self.app.get("/new/")
  520. self.assertEqual(output.status_code, 200)
  521. self.assertIn(
  522. "<strong>Create new Project</strong>",
  523. output.get_data(as_text=True),
  524. )
  525. csrf_token = self.get_csrf(output=output)
  526. data = {
  527. "description": "test",
  528. "name": "project-1",
  529. "csrf_token": csrf_token,
  530. "create_readme": True,
  531. }
  532. output = self.app.post("/new/", data=data, follow_redirects=True)
  533. self.assertEqual(output.status_code, 200)
  534. self.assertIn(
  535. '<div class="projectinfo my-3">\ntest',
  536. output.get_data(as_text=True),
  537. )
  538. self.assertEqual(pygit2init.call_count, 4)
  539. pygit2init.assert_any_call(
  540. "%s/repos/project-1.git" % self.path,
  541. bare=True,
  542. template_path=None,
  543. )
  544. path = os.path.join(self.path, "repos", "project-1.git")
  545. with patch.dict(
  546. "pagure.config.config", {"PROJECT_TEMPLATE_PATH": path}
  547. ):
  548. data = {
  549. "description": "test2",
  550. "name": "project-2",
  551. "csrf_token": csrf_token,
  552. "create_readme": True,
  553. }
  554. output = self.app.post(
  555. "/new/", data=data, follow_redirects=True
  556. )
  557. self.assertEqual(output.status_code, 200)
  558. self.assertIn(
  559. '<div class="projectinfo my-3">\ntest2',
  560. output.get_data(as_text=True),
  561. )
  562. self.assertEqual(pygit2init.call_count, 8)
  563. pygit2init.assert_any_call(
  564. "%s/repos/project-2.git" % self.path,
  565. bare=True,
  566. template_path="%s/repos/project-1.git" % self.path,
  567. )
  568. # After
  569. projects = pagure.lib.query.search_projects(self.session)
  570. self.assertEqual(len(projects), 2)
  571. for project in ["project-1", "project-2"]:
  572. self.assertTrue(
  573. os.path.exists(
  574. os.path.join(self.path, "repos", "%s.git" % project)
  575. )
  576. )
  577. self.assertTrue(
  578. os.path.exists(
  579. os.path.join(
  580. self.path, "repos", "tickets", "%s.git" % project
  581. )
  582. )
  583. )
  584. self.assertTrue(
  585. os.path.exists(
  586. os.path.join(
  587. self.path, "repos", "docs", "%s.git" % project
  588. )
  589. )
  590. )
  591. self.assertTrue(
  592. os.path.exists(
  593. os.path.join(
  594. self.path, "repos", "requests", "%s.git" % project
  595. )
  596. )
  597. )
  598. @patch("pagure.ui.app.admin_session_timedout")
  599. def test_user_settings(self, ast):
  600. """ Test the user_settings endpoint. """
  601. ast.return_value = False
  602. user = tests.FakeUser()
  603. with tests.user_set(self.app.application, user):
  604. output = self.app.get("/settings/")
  605. self.assertEqual(output.status_code, 404)
  606. self.assertIn(
  607. "<h2>Page not found (404)</h2>", output.get_data(as_text=True)
  608. )
  609. user.username = "foo"
  610. with tests.user_set(self.app.application, user):
  611. output = self.app.get("/settings/")
  612. self.assertEqual(output.status_code, 200)
  613. output_text = output.get_data(as_text=True)
  614. self.assertIn(
  615. "<title>foo's settings - Pagure</title>", output_text
  616. )
  617. ast.return_value = True
  618. output = self.app.get("/settings/")
  619. self.assertEqual(output.status_code, 302)
  620. @patch("pagure.decorators.admin_session_timedout")
  621. def test_add_user_sshkey(self, ast):
  622. """ Test the add_user_sshkey endpoint. """
  623. ast.return_value = False
  624. # User not logged in
  625. output = self.app.get("/settings/")
  626. self.assertEqual(output.status_code, 302)
  627. ast.return_value = False
  628. user = tests.FakeUser(username="pingou")
  629. with tests.user_set(self.app.application, user):
  630. output = self.app.get("/settings", follow_redirects=True)
  631. self.assertEqual(output.status_code, 200)
  632. output_text = output.get_data(as_text=True)
  633. self.assertIn("<strong>Add SSH key", output_text)
  634. csrf_token = self.get_csrf(output=output)
  635. data = {"ssh_key": "asdf"}
  636. # No CSRF token
  637. output = self.app.post(
  638. "/settings/usersettings/addkey",
  639. data=data,
  640. follow_redirects=True,
  641. )
  642. self.assertEqual(output.status_code, 200)
  643. output_text = output.get_data(as_text=True)
  644. self.assertIn("<strong>Add SSH key", output_text)
  645. data["csrf_token"] = csrf_token
  646. # First, invalid SSH key
  647. output = self.app.post(
  648. "/settings/usersettings/addkey",
  649. data=data,
  650. follow_redirects=True,
  651. )
  652. self.assertEqual(output.status_code, 200)
  653. output_text = output.get_data(as_text=True)
  654. self.assertIn("<strong>Add SSH key", output_text)
  655. self.assertIn("SSH key invalid", output_text)
  656. # Next up, multiple SSH keys
  657. data[
  658. "ssh_key"
  659. ] = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q=="
  660. output = self.app.post(
  661. "/settings/usersettings/addkey",
  662. data=data,
  663. follow_redirects=True,
  664. )
  665. self.assertEqual(output.status_code, 200)
  666. output_text = output.get_data(as_text=True)
  667. self.assertIn("Please add single SSH keys.", output_text)
  668. # Now, a valid SSH key
  669. data[
  670. "ssh_key"
  671. ] = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q=="
  672. output = self.app.post(
  673. "/settings/usersettings/addkey",
  674. data=data,
  675. follow_redirects=True,
  676. )
  677. self.assertEqual(output.status_code, 200)
  678. output_text = output.get_data(as_text=True)
  679. self.assertIn(
  680. "<title>pingou's settings - Pagure</title>", output_text
  681. )
  682. self.assertIn("SSH key added", output_text)
  683. self.assertNotIn("Push Access", output_text)
  684. # And now, adding the same key
  685. output = self.app.post(
  686. "/settings/usersettings/addkey",
  687. data=data,
  688. follow_redirects=True,
  689. )
  690. self.assertEqual(output.status_code, 200)
  691. output_text = output.get_data(as_text=True)
  692. self.assertIn("SSH key already exists", output_text)
  693. # And next, a key with push access
  694. data[
  695. "ssh_key"
  696. ] = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC9Xwc2RDzPBhlEDARfHldGjudIVoa04tqT1JVKGQmyllTFz7Rb8CngQL3e7zyNzotnhwYKHdoiLlPkVEiDee4dWMUe48ilqId+FJZQGhyv8fu4BoFdE1AJUVylzmltbLg14VqG5gjTpXgtlrEva9arKwBMHJjRYc8ScaSn3OgyQw=="
  697. output = self.app.post(
  698. "/settings/usersettings/addkey",
  699. data=data,
  700. follow_redirects=True,
  701. )
  702. self.assertEqual(output.status_code, 200)
  703. output_text = output.get_data(as_text=True)
  704. self.assertIn(
  705. "<title>pingou's settings - Pagure</title>", output_text
  706. )
  707. self.assertIn("SSH key added", output_text)
  708. @patch("pagure.decorators.admin_session_timedout")
  709. def test_remove_user_sshkey(self, ast):
  710. """ Test the remove_sshkey endpoint. """
  711. ast.return_value = False
  712. user = tests.FakeUser()
  713. # User not logged in
  714. output = self.app.post("/settings/usersettings/removekey/1")
  715. self.assertEqual(output.status_code, 302)
  716. user.username = "pingou"
  717. with tests.user_set(self.app.application, user):
  718. data = {"csrf_token": self.get_csrf()}
  719. output = self.app.post(
  720. "/settings/usersettings/removekey/1",
  721. data=data,
  722. follow_redirects=True,
  723. )
  724. self.assertEqual(output.status_code, 200)
  725. output_text = output.get_data(as_text=True)
  726. self.assertIn(
  727. "<title>pingou's settings - Pagure</title>", output_text
  728. )
  729. self.assertIn("SSH key does not exist", output_text)
  730. # Add a deploy key to a project
  731. pingou = pagure.lib.query.get_user(self.session, "pingou")
  732. msg = pagure.lib.query.add_sshkey_to_project_or_user(
  733. session=self.session,
  734. ssh_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==",
  735. user=pingou,
  736. pushaccess=True,
  737. creator=pingou,
  738. )
  739. self.session.commit()
  740. self.assertEqual(msg, "SSH key added")
  741. with tests.user_set(self.app.application, user):
  742. output = self.app.post(
  743. "/settings/usersettings/removekey/1", follow_redirects=True
  744. )
  745. self.assertEqual(output.status_code, 200)
  746. output_text = output.get_data(as_text=True)
  747. self.assertIn(
  748. "<title>pingou's settings - Pagure</title>", output_text
  749. )
  750. self.assertNotIn("SSH key removed", output_text)
  751. data = {"csrf_token": self.get_csrf()}
  752. output = self.app.post(
  753. "/settings/usersettings/removekey/1",
  754. data=data,
  755. follow_redirects=True,
  756. )
  757. self.assertEqual(output.status_code, 200)
  758. output_text = output.get_data(as_text=True)
  759. self.assertIn(
  760. "<title>pingou's settings - Pagure</title>", output_text
  761. )
  762. self.assertIn("SSH key removed", output_text)
  763. def patched_commit_exists(user, namespace, repo, githash):
  764. """ Patched version of pagure.pfmarkdown._commit_exists to enforce
  765. returning true on some given hash without having us actually check
  766. the git repos.
  767. """
  768. if githash in [
  769. "9364354",
  770. "9364354a",
  771. "9364354a4555ba17aa60f0dc844d70b74eb1aecd",
  772. ]:
  773. return True
  774. else:
  775. return False
  776. @patch(
  777. "pagure.pfmarkdown._commit_exists",
  778. MagicMock(side_effect=patched_commit_exists),
  779. )
  780. def test_patched_markdown_preview(self):
  781. """ Test the markdown_preview endpoint. """
  782. data = {"content": "test\n----\n\n * 1\n * item 2"}
  783. # CSRF missing
  784. output = self.app.post("/markdown/", data=data)
  785. self.assertEqual(output.status_code, 400)
  786. user = tests.FakeUser()
  787. user.username = "foo"
  788. with tests.user_set(self.app.application, user):
  789. output = self.app.get("/settings/")
  790. self.assertEqual(output.status_code, 200)
  791. output_text = output.get_data(as_text=True)
  792. self.assertIn(
  793. "<title>foo's settings - Pagure</title>", output_text
  794. )
  795. csrf_token = self.get_csrf(output=output)
  796. # With CSRF
  797. data["csrf_token"] = csrf_token
  798. output = self.app.post("/markdown/", data=data)
  799. self.assertEqual(output.status_code, 200)
  800. exp = """<div class="markdown"><h2>test</h2>
  801. <ul>
  802. <li>1</li>
  803. <li>item 2</li>
  804. </ul></div>"""
  805. self.assertEqual(output.get_data(as_text=True), exp)
  806. tests.create_projects(self.session)
  807. texts = [
  808. "pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd",
  809. "Cf commit 936435", # 6 chars - not long enough
  810. "Cf commit 9364354", # 7 chars - long enough
  811. "Cf commit 9364354a", # 8 chars - still long enough
  812. "Cf commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd", # 40 chars
  813. ]
  814. expected = [
  815. # 'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd',
  816. '<div class="markdown"><p>pingou committed on <a href="/test/c/9364354a4555ba17aa60f0dc844d70b74eb1aecd" '
  817. 'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
  818. ">test#9364354a4555ba17aa60f0dc844d70b74eb1aecd</a></p></div>",
  819. # 'Cf commit 936435',
  820. '<div class="markdown"><p>Cf commit 936435</p></div>',
  821. # 'Cf commit 9364354',
  822. #'<p>Cf commit 9364354</p>',
  823. '<div class="markdown"><p>Cf commit <a href="/test/c/9364354" '
  824. 'title="Commit 9364354">9364354</a></p></div>',
  825. # 'Cf commit 9364354a',
  826. '<div class="markdown"><p>Cf commit <a href="/test/c/9364354a" '
  827. 'title="Commit 9364354a">9364354</a></p></div>',
  828. # 'Cf commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd',
  829. '<div class="markdown"><p>Cf commit <a href="/test/c/9364354a4555ba17aa60f0dc844d70b74eb1aecd" '
  830. 'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
  831. ">9364354</a></p></div>",
  832. ]
  833. with self.app.application.app_context():
  834. for idx, text in enumerate(texts):
  835. data = {"content": text, "csrf_token": csrf_token}
  836. output = self.app.post("/markdown/?repo=test", data=data)
  837. self.assertEqual(output.status_code, 200)
  838. self.assertEqual(expected[idx], output.get_data(as_text=True))
  839. def test_markdown_preview(self):
  840. """ Test the markdown_preview endpoint with a non-existing commit.
  841. """
  842. user = tests.FakeUser()
  843. user.username = "foo"
  844. with tests.user_set(self.app.application, user):
  845. output = self.app.get("/settings/")
  846. self.assertEqual(output.status_code, 200)
  847. output_text = output.get_data(as_text=True)
  848. self.assertIn(
  849. "<title>foo's settings - Pagure</title>", output_text
  850. )
  851. csrf_token = self.get_csrf(output=output)
  852. tests.create_projects(self.session)
  853. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  854. text = "Cf commit 9364354a4555ba17aa60f0d"
  855. exp = '<div class="markdown"><p>Cf commit 9364354a4555ba17aa60f0d</p></div>'
  856. with self.app.application.app_context():
  857. data = {"content": text, "csrf_token": csrf_token}
  858. output = self.app.post("/markdown/?repo=test", data=data)
  859. self.assertEqual(output.status_code, 200)
  860. self.assertEqual(exp, output.get_data(as_text=True))
  861. def test_markdown_preview_valid_commit(self):
  862. """ Test the markdown_preview endpoint with an existing commit. """
  863. user = tests.FakeUser()
  864. user.username = "foo"
  865. with tests.user_set(self.app.application, user):
  866. output = self.app.get("/settings/")
  867. self.assertEqual(output.status_code, 200)
  868. output_text = output.get_data(as_text=True)
  869. self.assertIn(
  870. "<title>foo's settings - Pagure</title>", output_text
  871. )
  872. csrf_token = self.get_csrf(output=output)
  873. tests.create_projects(self.session)
  874. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  875. repopath = os.path.join(self.path, "repos", "test.git")
  876. tests.add_content_git_repo(repopath)
  877. repo = pygit2.Repository(repopath)
  878. first_commit = repo.revparse_single("HEAD")
  879. text = "Cf commit %s" % first_commit.oid.hex
  880. exp = (
  881. '<div class="markdown"><p>Cf commit <a href="/test/c/{0}" title="Commit {0}">{1}'
  882. "</a></p></div>".format(
  883. first_commit.oid.hex, first_commit.oid.hex[:7]
  884. )
  885. )
  886. with self.app.application.app_context():
  887. data = {"content": text, "csrf_token": csrf_token}
  888. output = self.app.post("/markdown/?repo=test", data=data)
  889. self.assertEqual(output.status_code, 200)
  890. self.assertEqual(exp, output.get_data(as_text=True))
  891. @patch("pagure.ui.app.admin_session_timedout")
  892. def test_remove_user_email(self, ast):
  893. """ Test the remove_user_email endpoint. """
  894. ast.return_value = False
  895. user = tests.FakeUser()
  896. with tests.user_set(self.app.application, user):
  897. output = self.app.post("/settings/email/drop")
  898. self.assertEqual(output.status_code, 404)
  899. self.assertIn(
  900. "<h2>Page not found (404)</h2>", output.get_data(as_text=True)
  901. )
  902. user.username = "foo"
  903. with tests.user_set(self.app.application, user):
  904. output = self.app.get("/settings/")
  905. self.assertEqual(output.status_code, 200)
  906. output_text = output.get_data(as_text=True)
  907. self.assertIn(
  908. "<title>foo's settings - Pagure</title>", output_text
  909. )
  910. csrf_token = self.get_csrf(output=output)
  911. data = {"email": "foo@pingou.com"}
  912. output = self.app.post(
  913. "/settings/email/drop", data=data, follow_redirects=True
  914. )
  915. self.assertEqual(output.status_code, 200)
  916. output_text = output.get_data(as_text=True)
  917. self.assertIn(
  918. "<title>foo's settings - Pagure</title>", output_text
  919. )
  920. self.assertIn(
  921. "You must always have at " "least one email", output_text
  922. )
  923. user.username = "pingou"
  924. with tests.user_set(self.app.application, user):
  925. output = self.app.get("/settings/")
  926. self.assertEqual(output.status_code, 200)
  927. output_text = output.get_data(as_text=True)
  928. self.assertIn(
  929. "<title>pingou's settings - Pagure</title>", output_text
  930. )
  931. csrf_token = self.get_csrf(output=output)
  932. data = {"email": "foo@pingou.com"}
  933. output = self.app.post(
  934. "/settings/email/drop", data=data, follow_redirects=True
  935. )
  936. self.assertEqual(output.status_code, 200)
  937. output_text = output.get_data(as_text=True)
  938. self.assertIn(
  939. "<title>pingou's settings - Pagure</title>", output_text
  940. )
  941. self.assertEqual(output_text.count("foo@pingou.com"), 4)
  942. data = {"csrf_token": csrf_token, "email": "foobar@pingou.com"}
  943. output = self.app.post(
  944. "/settings/email/drop", data=data, follow_redirects=True
  945. )
  946. self.assertEqual(output.status_code, 200)
  947. output_text = output.get_data(as_text=True)
  948. self.assertIn(
  949. "<title>pingou's settings - Pagure</title>", output_text
  950. )
  951. self.assertIn(
  952. "You do not have the "
  953. "email: foobar@pingou.com, nothing to remove",
  954. output_text,
  955. )
  956. data = {"csrf_token": csrf_token, "email": "foo@pingou.com"}
  957. output = self.app.post(
  958. "/settings/email/drop", data=data, follow_redirects=True
  959. )
  960. self.assertEqual(output.status_code, 200)
  961. output_text = output.get_data(as_text=True)
  962. self.assertEqual(output_text.count("foo@pingou.com"), 0)
  963. self.assertEqual(output_text.count("bar@pingou.com"), 3)
  964. output = self.app.post(
  965. "/settings/email/drop", data=data, follow_redirects=True
  966. )
  967. self.assertEqual(output.status_code, 200)
  968. output_text = output.get_data(as_text=True)
  969. self.assertEqual(output_text.count("foo@pingou.com"), 0)
  970. self.assertEqual(output_text.count("bar@pingou.com"), 3)
  971. ast.return_value = True
  972. output = self.app.post("/settings/email/drop", data=data)
  973. self.assertEqual(output.status_code, 302)
  974. @patch("pagure.lib.notify.send_email")
  975. @patch("pagure.ui.app.admin_session_timedout")
  976. def test_add_api_user_email(self, ast, send_email):
  977. """ Test the add_api_user_email endpoint. """
  978. send_email.return_value = True
  979. ast.return_value = False
  980. user = tests.FakeUser()
  981. with tests.user_set(self.app.application, user):
  982. output = self.app.post("/settings/email/add")
  983. self.assertEqual(output.status_code, 404)
  984. self.assertIn(
  985. "<h2>Page not found (404)</h2>", output.get_data(as_text=True)
  986. )
  987. user.username = "foo"
  988. with tests.user_set(self.app.application, user):
  989. output = self.app.post("/settings/email/add")
  990. self.assertEqual(output.status_code, 200)
  991. output_text = output.get_data(as_text=True)
  992. self.assertIn("<strong>Add new email</strong>", output_text)
  993. if self.get_wtforms_version() >= (2, 2):
  994. self.assertIn(
  995. '<input class="form-control form-control-error" id="email" '
  996. 'name="email" required type="text" value="">',
  997. output_text,
  998. )
  999. else:
  1000. self.assertIn(
  1001. '<input class="form-control form-control-error" id="email" '
  1002. 'name="email" type="text" value="">',
  1003. output_text,
  1004. )
  1005. user.username = "pingou"
  1006. with tests.user_set(self.app.application, user):
  1007. output = self.app.post("/settings/email/add")
  1008. self.assertEqual(output.status_code, 200)
  1009. output_text = output.get_data(as_text=True)
  1010. self.assertIn("<strong>Add new email</strong>", output_text)
  1011. if self.get_wtforms_version() >= (2, 2):
  1012. self.assertIn(
  1013. '<input class="form-control form-control-error" id="email" '
  1014. 'name="email" required type="text" value="">',
  1015. output_text,
  1016. )
  1017. else:
  1018. self.assertIn(
  1019. '<input class="form-control form-control-error" id="email" '
  1020. 'name="email" type="text" value="">',
  1021. output_text,
  1022. )
  1023. csrf_token = output_text.split(
  1024. 'name="csrf_token" type="hidden" value="'
  1025. )[1].split('">')[0]
  1026. data = {"email": "foo2@pingou.com"}
  1027. output = self.app.post(
  1028. "/settings/email/add", data=data, follow_redirects=True
  1029. )
  1030. self.assertEqual(output.status_code, 200)
  1031. output_text = output.get_data(as_text=True)
  1032. self.assertIn("<strong>Add new email</strong>", output_text)
  1033. self.assertEqual(output_text.count("foo2@pingou.com"), 1)
  1034. # New email
  1035. data = {"csrf_token": csrf_token, "email": "foðbar@pingou.com"}
  1036. output = self.app.post(
  1037. "/settings/email/add", data=data, follow_redirects=True
  1038. )
  1039. self.assertEqual(output.status_code, 200)
  1040. output_text = output.get_data(as_text=True)
  1041. self.assertIn(
  1042. "<title>pingou's settings - Pagure</title>", output_text
  1043. )
  1044. self.assertIn("Email pending validation", output_text)
  1045. self.assertEqual(output_text.count("foo@pingou.com"), 4)
  1046. self.assertEqual(output_text.count("bar@pingou.com"), 5)
  1047. self.assertEqual(output_text.count("foðbar@pingou.com"), 2)
  1048. # Email already pending
  1049. output = self.app.post(
  1050. "/settings/email/add", data=data, follow_redirects=True
  1051. )
  1052. self.assertEqual(output.status_code, 200)
  1053. output_text = output.get_data(as_text=True)
  1054. self.assertIn(
  1055. '<div class="card-header">\n '
  1056. "<strong>Add new email</strong>",
  1057. output_text,
  1058. )
  1059. self.assertIn(
  1060. "This email is already " "pending confirmation", output_text
  1061. )
  1062. # User already has this email
  1063. data = {"csrf_token": csrf_token, "email": "foo@pingou.com"}
  1064. output = self.app.post(
  1065. "/settings/email/add", data=data, follow_redirects=True
  1066. )
  1067. self.assertEqual(output.status_code, 200)
  1068. output_text = output.get_data(as_text=True)
  1069. self.assertTrue("<strong>Add new email</strong>" in output_text)
  1070. self.assertTrue(
  1071. "Invalid value, can&#39;t be any of: bar@pingou.com, "
  1072. "foo@pingou.com.&nbsp;" in output_text
  1073. or "Invalid value, can&#39;t be any of: foo@pingou.com, "
  1074. "bar@pingou.com.&nbsp;" in output_text
  1075. )
  1076. self.assertEqual(output_text.count("foo@pingou.com"), 6)
  1077. self.assertEqual(output_text.count("bar@pingou.com"), 5)
  1078. self.assertEqual(output_text.count("foðbar@pingou.com"), 0)
  1079. # Email registered by someone else
  1080. data = {"csrf_token": csrf_token, "email": "foo@bar.com"}
  1081. output = self.app.post(
  1082. "/settings/email/add", data=data, follow_redirects=True
  1083. )
  1084. self.assertEqual(output.status_code, 200)
  1085. output_text = output.get_data(as_text=True)
  1086. self.assertTrue("<strong>Add new email</strong>" in output_text)
  1087. self.assertIn(
  1088. "Invalid value, can&#39;t be any of: foo@bar.com.&nbsp;",
  1089. output_text,
  1090. )
  1091. ast.return_value = True
  1092. output = self.app.post("/settings/email/add", data=data)
  1093. self.assertEqual(output.status_code, 302)
  1094. @patch("pagure.lib.notify.send_email")
  1095. @patch("pagure.ui.app.admin_session_timedout")
  1096. def test_set_default_email(self, ast, send_email):
  1097. """ Test the set_default_email endpoint. """
  1098. send_email.return_value = True
  1099. ast.return_value = False
  1100. user = tests.FakeUser()
  1101. with tests.user_set(self.app.application, user):
  1102. output = self.app.post("/settings/email/default")
  1103. self.assertEqual(output.status_code, 404)
  1104. self.assertTrue(
  1105. "<h2>Page not found (404)</h2>"
  1106. in output.get_data(as_text=True)
  1107. )
  1108. user.username = "pingou"
  1109. with tests.user_set(self.app.application, user):
  1110. output = self.app.get("/settings/")
  1111. self.assertEqual(output.status_code, 200)
  1112. output_text = output.get_data(as_text=True)
  1113. self.assertIn(
  1114. "<title>pingou's settings - Pagure</title>", output_text
  1115. )
  1116. csrf_token = self.get_csrf(output=output)
  1117. data = {"email": "foo@pingou.com"}
  1118. output = self.app.post(
  1119. "/settings/email/default", data=data, follow_redirects=True
  1120. )
  1121. self.assertEqual(output.status_code, 200)
  1122. output_text = output.get_data(as_text=True)
  1123. self.assertIn(
  1124. "<title>pingou's settings - Pagure</title>", output_text
  1125. )
  1126. self.assertEqual(output_text.count("foo@pingou.com"), 4)
  1127. # Set invalid default email
  1128. data = {"csrf_token": csrf_token, "email": "foobar@pingou.com"}
  1129. output = self.app.post(
  1130. "/settings/email/default", data=data, follow_redirects=True
  1131. )
  1132. self.assertEqual(output.status_code, 200)
  1133. output_text = output.get_data(as_text=True)
  1134. self.assertIn(
  1135. "<title>pingou's settings - Pagure</title>", output_text
  1136. )
  1137. self.assertEqual(output_text.count("foo@pingou.com"), 4)
  1138. self.assertIn(
  1139. "You do not have the "
  1140. "email: foobar@pingou.com, nothing to set",
  1141. output_text,
  1142. )
  1143. # Set default email
  1144. data = {"csrf_token": csrf_token, "email": "foo@pingou.com"}
  1145. output = self.app.post(
  1146. "/settings/email/default", data=data, follow_redirects=True
  1147. )
  1148. self.assertEqual(output.status_code, 200)
  1149. output_text = output.get_data(as_text=True)
  1150. self.assertIn(
  1151. "<title>pingou's settings - Pagure</title>", output_text
  1152. )
  1153. self.assertEqual(output_text.count("foo@pingou.com"), 4)
  1154. self.assertIn(
  1155. "Default email set to: " "foo@pingou.com", output_text
  1156. )
  1157. ast.return_value = True
  1158. output = self.app.post("/settings/email/default", data=data)
  1159. self.assertEqual(output.status_code, 302)
  1160. @patch("pagure.lib.notify.send_email")
  1161. @patch("pagure.ui.app.admin_session_timedout")
  1162. def test_reconfirm_email(self, ast, send_email):
  1163. """ Test the reconfirm_email endpoint. """
  1164. send_email.return_value = True
  1165. ast.return_value = False
  1166. # Add a pending email to pingou
  1167. userobj = pagure.lib.query.search_user(self.session, username="pingou")
  1168. self.assertEqual(len(userobj.emails), 2)
  1169. email_pend = pagure.lib.model.UserEmailPending(
  1170. user_id=userobj.id, email="foo@fp.o", token="abcdef"
  1171. )
  1172. self.session.add(email_pend)
  1173. self.session.commit()
  1174. user = tests.FakeUser()
  1175. with tests.user_set(self.app.application, user):
  1176. output = self.app.post("/settings/email/resend")
  1177. self.assertEqual(output.status_code, 404)
  1178. self.assertTrue(
  1179. "<h2>Page not found (404)</h2>"
  1180. in output.get_data(as_text=True)
  1181. )
  1182. user.username = "pingou"
  1183. with tests.user_set(self.app.application, user):
  1184. output = self.app.get("/settings/")
  1185. self.assertEqual(output.status_code, 200)
  1186. output_text = output.get_data(as_text=True)
  1187. self.assertIn(
  1188. "<title>pingou's settings - Pagure</title>", output_text
  1189. )
  1190. csrf_token = self.get_csrf(output=output)
  1191. data = {"email": "foo@pingou.com"}
  1192. output = self.app.post(
  1193. "/settings/email/resend", data=data, follow_redirects=True
  1194. )
  1195. self.assertEqual(output.status_code, 200)
  1196. output_text = output.get_data(as_text=True)
  1197. self.assertIn(
  1198. "<title>pingou's settings - Pagure</title>", output_text
  1199. )
  1200. self.assertEqual(output_text.count("foo@pingou.com"), 4)
  1201. # Set invalid default email
  1202. data = {"csrf_token": csrf_token, "email": "foobar@pingou.com"}
  1203. output = self.app.post(
  1204. "/settings/email/resend", data=data, follow_redirects=True
  1205. )
  1206. self.assertEqual(output.status_code, 200)
  1207. output_text = output.get_data(as_text=True)
  1208. self.assertIn(
  1209. "<title>pingou's settings - Pagure</title>", output_text
  1210. )
  1211. self.assertEqual(output_text.count("foo@pingou.com"), 4)
  1212. self.assertIn(
  1213. "This email address has " "already been confirmed", output_text
  1214. )
  1215. # Validate a non-validated email
  1216. data = {"csrf_token": csrf_token, "email": "foo@fp.o"}
  1217. output = self.app.post(
  1218. "/settings/email/resend", data=data, follow_redirects=True
  1219. )
  1220. self.assertEqual(output.status_code, 200)
  1221. output_text = output.get_data(as_text=True)
  1222. self.assertIn(
  1223. "<title>pingou's settings - Pagure</title>", output_text
  1224. )
  1225. self.assertEqual(output_text.count("foo@pingou.com"), 4)
  1226. self.assertIn("Confirmation email re-sent", output_text)
  1227. ast.return_value = True
  1228. output = self.app.post("/settings/email/resend", data=data)
  1229. self.assertEqual(output.status_code, 302)
  1230. @patch("pagure.ui.app.admin_session_timedout")
  1231. def test_confirm_email(self, ast):
  1232. """ Test the confirm_email endpoint. """
  1233. output = self.app.get("/settings/email/confirm/foobar")
  1234. self.assertEqual(output.status_code, 302)
  1235. ast.return_value = False
  1236. # Add a pending email to pingou
  1237. userobj = pagure.lib.query.search_user(self.session, username="pingou")
  1238. self.assertEqual(len(userobj.emails), 2)
  1239. email_pend = pagure.lib.model.UserEmailPending(
  1240. user_id=userobj.id, email="foo@fp.o", token="abcdef"
  1241. )
  1242. self.session.add(email_pend)
  1243. self.session.commit()
  1244. user = tests.FakeUser()
  1245. user.username = "pingou"
  1246. with tests.user_set(self.app.application, user):
  1247. # Wrong token
  1248. output = self.app.get(
  1249. "/settings/email/confirm/foobar", follow_redirects=True
  1250. )
  1251. self.assertEqual(output.status_code, 200)
  1252. output_text = output.get_data(as_text=True)
  1253. self.assertIn(
  1254. "<title>pingou's settings - Pagure</title>", output_text
  1255. )
  1256. self.assertIn("No email associated with this token.", output_text)
  1257. # Confirm email
  1258. output = self.app.get(
  1259. "/settings/email/confirm/abcdef", follow_redirects=True
  1260. )
  1261. self.assertEqual(output.status_code, 200)
  1262. output_text = output.get_data(as_text=True)
  1263. self.assertIn(
  1264. "<title>pingou's settings - Pagure</title>", output_text
  1265. )
  1266. self.assertIn("Email validated", output_text)
  1267. userobj = pagure.lib.query.search_user(self.session, username="pingou")
  1268. self.assertEqual(len(userobj.emails), 3)
  1269. ast.return_value = True
  1270. output = self.app.get("/settings/email/confirm/foobar")
  1271. self.assertEqual(output.status_code, 302)
  1272. def test_view_my_requests_no_user(self):
  1273. """Test the view_user_requests endpoint."""
  1274. output = self.app.get("/user/somenonexistentuser/requests")
  1275. self.assertEqual(output.status_code, 404)
  1276. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1277. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1278. def test_view_my_requests(self):
  1279. """Test the view_user_requests endpoint. """
  1280. # Create the PR
  1281. tests.create_projects(self.session)
  1282. repo = pagure.lib.query._get_project(self.session, "test")
  1283. req = pagure.lib.query.new_pull_request(
  1284. session=self.session,
  1285. repo_from=repo,
  1286. branch_from="dev",
  1287. repo_to=repo,
  1288. branch_to="master",
  1289. title="test pull-request #1",
  1290. user="pingou",
  1291. )
  1292. self.session.commit()
  1293. self.assertEqual(req.id, 1)
  1294. self.assertEqual(req.title, "test pull-request #1")
  1295. output = self.app.get("/user/pingou/requests")
  1296. self.assertEqual(output.status_code, 200)
  1297. output_text = output.get_data(as_text=True)
  1298. self.assertIn("test pull-request #1", output_text)
  1299. self.assertEqual(output_text.count('pr-status pr-status-open"'), 1)
  1300. # Add a PR in a fork
  1301. item = pagure.lib.model.Project(
  1302. user_id=1, # pingou
  1303. name="test_fork",
  1304. description="test project #1",
  1305. is_fork=True,
  1306. parent_id=1,
  1307. hook_token="aaabbbttt",
  1308. )
  1309. self.session.add(item)
  1310. repo = pagure.lib.query._get_project(
  1311. self.session, "test_fork", user="pingou"
  1312. )
  1313. req = pagure.lib.query.new_pull_request(
  1314. session=self.session,
  1315. repo_from=repo,
  1316. branch_from="dev",
  1317. repo_to=repo,
  1318. branch_to="master",
  1319. title="tést pull-request #2",
  1320. user="pingou",
  1321. )
  1322. self.session.commit()
  1323. self.assertEqual(req.id, 1)
  1324. self.assertEqual(req.title, "tést pull-request #2")
  1325. output = self.app.get("/user/pingou/requests")
  1326. self.assertEqual(output.status_code, 200)
  1327. output_text = output.get_data(as_text=True)
  1328. self.assertIn("test pull-request #1", output_text)
  1329. self.assertIn("tést pull-request #2", output_text)
  1330. self.assertEqual(output_text.count('pr-status pr-status-open"'), 2)
  1331. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1332. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1333. def test_view_my_requests_pr_in_another_project(self):
  1334. """Test the view_user_requests endpoint when the user opened a PR
  1335. in another project. """
  1336. # Pingou creates the PR on test
  1337. tests.create_projects(self.session)
  1338. repo = pagure.lib.query._get_project(self.session, "test")
  1339. req = pagure.lib.query.new_pull_request(
  1340. session=self.session,
  1341. repo_from=repo,
  1342. branch_from="dev",
  1343. repo_to=repo,
  1344. branch_to="master",
  1345. title="test pull-request #1",
  1346. user="pingou",
  1347. )
  1348. self.session.commit()
  1349. self.assertEqual(req.id, 1)
  1350. self.assertEqual(req.title, "test pull-request #1")
  1351. # foo creates the PR on test
  1352. repo = pagure.lib.query._get_project(self.session, "test")
  1353. req = pagure.lib.query.new_pull_request(
  1354. session=self.session,
  1355. repo_from=repo,
  1356. branch_from="dev",
  1357. repo_to=repo,
  1358. branch_to="master",
  1359. title="test pull-request #2",
  1360. user="foo",
  1361. )
  1362. self.session.commit()
  1363. self.assertEqual(req.id, 2)
  1364. self.assertEqual(req.title, "test pull-request #2")
  1365. # Check pingou's PR list
  1366. output = self.app.get("/user/pingou/requests")
  1367. self.assertEqual(output.status_code, 200)
  1368. output_text = output.get_data(as_text=True)
  1369. self.assertIn("test pull-request #1", output_text)
  1370. self.assertIn("test pull-request #2", output_text)
  1371. self.assertEqual(output_text.count('pr-status pr-status-open"'), 2)
  1372. # Check foo's PR list
  1373. output = self.app.get("/user/foo/requests")
  1374. self.assertEqual(output.status_code, 200)
  1375. output_text = output.get_data(as_text=True)
  1376. self.assertNotIn("test pull-request #1", output_text)
  1377. self.assertIn("test pull-request #2", output_text)
  1378. self.assertEqual(output_text.count('pr-status pr-status-open"'), 1)
  1379. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1380. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1381. def test_view_my_requests_against_another_project(self):
  1382. """Test the view_user_requests endpoint when there is a PR opened
  1383. by me against a project I do not have rights on. """
  1384. # Create the PR
  1385. tests.create_projects(self.session)
  1386. repo = pagure.lib.query._get_project(self.session, "test")
  1387. req = pagure.lib.query.new_pull_request(
  1388. session=self.session,
  1389. repo_from=repo,
  1390. branch_from="dev",
  1391. repo_to=repo,
  1392. branch_to="master",
  1393. title="test pull-request #1",
  1394. user="foo",
  1395. )
  1396. self.session.commit()
  1397. self.assertEqual(req.id, 1)
  1398. self.assertEqual(req.title, "test pull-request #1")
  1399. output = self.app.get("/user/foo/requests")
  1400. self.assertEqual(output.status_code, 200)
  1401. output_text = output.get_data(as_text=True)
  1402. self.assertIn("test pull-request #1", output_text)
  1403. self.assertEqual(output_text.count('pr-status pr-status-open"'), 1)
  1404. def test_view_my_issues_no_user(self):
  1405. """Test the view_user_issues endpoint with a missing user."""
  1406. output = self.app.get("/user/somenonexistentuser/issues")
  1407. self.assertEqual(output.status_code, 404)
  1408. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1409. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1410. def test_view_my_issues(self):
  1411. """Test the view_user_issues endpoint when the user exists."""
  1412. # Create the issue
  1413. tests.create_projects(self.session)
  1414. repo = pagure.lib.query._get_project(self.session, "test")
  1415. msg = pagure.lib.query.new_issue(
  1416. session=self.session,
  1417. repo=repo,
  1418. title="Test issue #1",
  1419. content="We should work on this for the second time",
  1420. user="pingou",
  1421. status="Open",
  1422. )
  1423. self.session.commit()
  1424. self.assertEqual(msg.title, "Test issue #1")
  1425. output = self.app.get("/user/pingou/issues")
  1426. self.assertEqual(output.status_code, 200)
  1427. output_text = output.get_data(as_text=True)
  1428. self.assertIn("Test issue #1", output_text)
  1429. self.assertEqual(
  1430. output_text.count("issue-status issue-status-open"), 1
  1431. )
  1432. # Add an issue in a fork
  1433. item = pagure.lib.model.Project(
  1434. user_id=2, # foo
  1435. name="test_fork",
  1436. description="test project #1",
  1437. is_fork=True,
  1438. parent_id=1,
  1439. hook_token="aaabbbttt",
  1440. )
  1441. self.session.add(item)
  1442. repo = pagure.lib.query._get_project(
  1443. self.session, "test_fork", user="foo"
  1444. )
  1445. msg = pagure.lib.query.new_issue(
  1446. session=self.session,
  1447. repo=repo,
  1448. title="Test issue #2",
  1449. content="We should work on this for the second time",
  1450. user="pingou",
  1451. status="Open",
  1452. )
  1453. self.session.commit()
  1454. self.assertEqual(msg.title, "Test issue #2")
  1455. # Test the assigned issue table. Create issue then set the assignee
  1456. msg = pagure.lib.query.new_issue(
  1457. session=self.session,
  1458. repo=repo,
  1459. title="Test issue #3",
  1460. content="This issue created by foo, but assigned to pingou",
  1461. user="foo",
  1462. status="Open",
  1463. )
  1464. self.session.commit()
  1465. self.assertEqual(msg.title, "Test issue #3")
  1466. msg = pagure.lib.query.add_issue_assignee(
  1467. session=self.session, issue=msg, assignee="pingou", user="foo"
  1468. )
  1469. self.session.commit()
  1470. self.assertEqual(msg, "Issue assigned to pingou")
  1471. output = self.app.get("/user/pingou/issues")
  1472. self.assertEqual(output.status_code, 200)
  1473. output_text = output.get_data(as_text=True)
  1474. self.assertIn("Test issue #1", output_text)
  1475. self.assertIn("Test issue #2", output_text)
  1476. self.assertIn("Test issue #3", output_text)
  1477. self.assertEqual(
  1478. output_text.count("issue-status issue-status-open"), 3
  1479. )
  1480. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1481. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1482. def test_view_my_issues_disabled(self):
  1483. """Test the view_user_issues endpoint when the project disabled issue
  1484. tracking."""
  1485. # Create the issue
  1486. tests.create_projects(self.session)
  1487. repo = pagure.lib.query._get_project(self.session, "test")
  1488. msg = pagure.lib.query.new_issue(
  1489. session=self.session,
  1490. repo=repo,
  1491. title="Test issue #1",
  1492. content="We should work on this for the second time",
  1493. user="pingou",
  1494. status="Open",
  1495. )
  1496. self.session.commit()
  1497. self.assertEqual(msg.title, "Test issue #1")
  1498. # Before
  1499. output = self.app.get("/user/pingou/issues")
  1500. self.assertEqual(output.status_code, 200)
  1501. output_text = output.get_data(as_text=True)
  1502. self.assertIn("Test issue #1", output_text)
  1503. self.assertEqual(
  1504. output_text.count("issue-status issue-status-open"), 1
  1505. )
  1506. # Disable issue tracking
  1507. repo = pagure.lib.query._get_project(self.session, "test")
  1508. settings = repo.settings
  1509. settings["issue_tracker"] = False
  1510. repo.settings = settings
  1511. self.session.add(repo)
  1512. self.session.commit()
  1513. # After
  1514. output = self.app.get("/user/pingou/issues")
  1515. self.assertEqual(output.status_code, 200)
  1516. output_text = output.get_data(as_text=True)
  1517. self.assertNotIn("Test issue #1", output_text)
  1518. self.assertEqual(
  1519. output_text.count("issue-status issue-status-open"), 0
  1520. )
  1521. def test_view_my_issues_tickets_turned_off(self):
  1522. """Test the view_user_issues endpoint when the user exists and
  1523. and ENABLE_TICKETS is False """
  1524. # Turn off the tickets instance wide
  1525. pagure.config.config["ENABLE_TICKETS"] = False
  1526. output = self.app.get("/user/pingou/issues")
  1527. self.assertEqual(output.status_code, 404)
  1528. pagure.config.config["ENABLE_TICKETS"] = True
  1529. @patch("pagure.ui.app.admin_session_timedout")
  1530. def test_add_user_token(self, ast):
  1531. """ Test the add_user_token endpoint. """
  1532. ast.return_value = False
  1533. user = tests.FakeUser()
  1534. with tests.user_set(self.app.application, user):
  1535. output = self.app.get("/settings/token/new/")
  1536. self.assertEqual(output.status_code, 404)
  1537. self.assertIn(
  1538. "<h2>Page not found (404)</h2>", output.get_data(as_text=True)
  1539. )
  1540. user.username = "foo"
  1541. with tests.user_set(self.app.application, user):
  1542. output = self.app.get("/settings/token/new")
  1543. self.assertEqual(output.status_code, 200)
  1544. output_text = output.get_data(as_text=True)
  1545. self.assertIn(
  1546. '<div class="card-header">\n <strong>'
  1547. "Create a new token</strong>\n",
  1548. output_text,
  1549. )
  1550. self.assertIn(
  1551. '<input type="checkbox" name="acls" value="create_project">',
  1552. output_text,
  1553. )
  1554. csrf_token = output_text.split(
  1555. 'name="csrf_token" type="hidden" value="'
  1556. )[1].split('">')[0]
  1557. data = {"acls": ["create_project", "fork_project"]}
  1558. # missing CSRF
  1559. output = self.app.post("/settings/token/new", data=data)
  1560. self.assertEqual(output.status_code, 200)
  1561. output_text = output.get_data(as_text=True)
  1562. self.assertIn("<title>Create token - Pagure</title>", output_text)
  1563. self.assertIn(
  1564. '<div class="card-header">\n <strong>'
  1565. "Create a new token</strong>\n",
  1566. output_text,
  1567. )
  1568. self.assertIn(
  1569. '<input type="checkbox" name="acls" value="create_project">',
  1570. output_text,
  1571. )
  1572. data = {"acls": ["new_project"], "csrf_token": csrf_token}
  1573. # Invalid ACLs
  1574. output = self.app.post("/settings/token/new", data=data)
  1575. self.assertEqual(output.status_code, 200)
  1576. output_text = output.get_data(as_text=True)
  1577. self.assertIn("<title>Create token - Pagure</title>", output_text)
  1578. self.assertIn(
  1579. '<div class="card-header">\n <strong>'
  1580. "Create a new token</strong>\n",
  1581. output_text,
  1582. )
  1583. self.assertIn(
  1584. '<input type="checkbox" name="acls" value="create_project">',
  1585. output_text,
  1586. )
  1587. data = {
  1588. "acls": ["create_project", "fork_project"],
  1589. "csrf_token": csrf_token,
  1590. }
  1591. # All good
  1592. output = self.app.post(
  1593. "/settings/token/new", data=data, follow_redirects=True
  1594. )
  1595. self.assertEqual(output.status_code, 200)
  1596. output_text = output.get_data(as_text=True)
  1597. self.assertIn(
  1598. "<title>foo's settings - Pagure</title>", output_text
  1599. )
  1600. self.assertIn("Token created", output_text)
  1601. self.assertEqual(
  1602. output_text.count(
  1603. '<small class="font-weight-bold">Active until'
  1604. ),
  1605. 1,
  1606. )
  1607. ast.return_value = True
  1608. output = self.app.get("/settings/token/new")
  1609. self.assertEqual(output.status_code, 302)
  1610. @patch("pagure.ui.app.admin_session_timedout")
  1611. def test_revoke_api_user_token(self, ast):
  1612. """ Test the revoke_api_user_token endpoint. """
  1613. ast.return_value = False
  1614. user = tests.FakeUser()
  1615. with tests.user_set(self.app.application, user):
  1616. # Token doesn't exist
  1617. output = self.app.post("/settings/token/revoke/foobar")
  1618. self.assertEqual(output.status_code, 404)
  1619. self.assertTrue(
  1620. "<h2>Page not found (404)</h2>"
  1621. in output.get_data(as_text=True)
  1622. )
  1623. # Create the foobar API token but associated w/ the user 'foo'
  1624. item = pagure.lib.model.Token(
  1625. id="foobar",
  1626. user_id=2, # foo
  1627. expiration=datetime.datetime.utcnow()
  1628. + datetime.timedelta(days=30),
  1629. )
  1630. self.session.add(item)
  1631. self.session.commit()
  1632. # Token not associated w/ this user
  1633. output = self.app.post("/settings/token/revoke/foobar")
  1634. self.assertEqual(output.status_code, 404)
  1635. self.assertTrue(
  1636. "<h2>Page not found (404)</h2>"
  1637. in output.get_data(as_text=True)
  1638. )
  1639. user.username = "foo"
  1640. with tests.user_set(self.app.application, user):
  1641. # Missing CSRF token
  1642. output = self.app.post(
  1643. "/settings/token/revoke/foobar", follow_redirects=True
  1644. )
  1645. self.assertEqual(output.status_code, 200)
  1646. output_text = output.get_data(as_text=True)
  1647. self.assertIn(
  1648. "<title>foo's settings - Pagure</title>", output_text
  1649. )
  1650. self.assertEqual(
  1651. output_text.count(
  1652. '<small class="font-weight-bold">Active until'
  1653. ),
  1654. 1,
  1655. )
  1656. csrf_token = output_text.split(
  1657. 'name="csrf_token" type="hidden" value="'
  1658. )[1].split('">')[0]
  1659. data = {"csrf_token": csrf_token}
  1660. # All good - token is deleted
  1661. output = self.app.post(
  1662. "/settings/token/revoke/foobar",
  1663. data=data,
  1664. follow_redirects=True,
  1665. )
  1666. self.assertEqual(output.status_code, 200)
  1667. output_text = output.get_data(as_text=True)
  1668. self.assertIn(
  1669. "<title>foo's settings - Pagure</title>", output_text
  1670. )
  1671. self.assertEqual(
  1672. output_text.count(
  1673. '<small class="font-weight-bold">Active until'
  1674. ),
  1675. 0,
  1676. )
  1677. user = pagure.lib.query.get_user(self.session, key="foo")
  1678. self.assertEqual(len(user.tokens), 1)
  1679. expiration_dt = user.tokens[0].expiration
  1680. # Token was already deleted - no changes
  1681. output = self.app.post(
  1682. "/settings/token/revoke/foobar",
  1683. data=data,
  1684. follow_redirects=True,
  1685. )
  1686. self.assertEqual(output.status_code, 200)
  1687. output_text = output.get_data(as_text=True)
  1688. self.assertIn(
  1689. "<title>foo's settings - Pagure</title>", output_text
  1690. )
  1691. self.assertEqual(
  1692. output_text.count(
  1693. '<small class="font-weight-bold">Active until'
  1694. ),
  1695. 0,
  1696. )
  1697. # Ensure the expiration date did not change
  1698. user = pagure.lib.query.get_user(self.session, key="foo")
  1699. self.assertEqual(len(user.tokens), 1)
  1700. self.assertEqual(expiration_dt, user.tokens[0].expiration)
  1701. ast.return_value = True
  1702. output = self.app.get("/settings/token/new")
  1703. self.assertEqual(output.status_code, 302)
  1704. @patch.dict("pagure.config.config", {"PAGURE_AUTH": "fas"})
  1705. @patch.dict("pagure.utils.pagure_config", {"PAGURE_AUTH": "fas"})
  1706. def test_create_project_auth_FAS_no_FPCA(self):
  1707. """ Test creating a project when auth is FAS and the user did not
  1708. sign the FPCA. """
  1709. user = tests.FakeUser(username="foo", cla_done=False)
  1710. with tests.user_set(self.app.application, user):
  1711. output = self.app.get("/new/", follow_redirects=True)
  1712. self.assertEqual(output.status_code, 200)
  1713. output_text = output.get_data(as_text=True)
  1714. self.assertIn("<title>Home - Pagure</title>", output_text)
  1715. self.assertIn(
  1716. '</i> You must <a href="https://admin.fedoraproject.org/accounts/'
  1717. '">sign the FPCA</a> (Fedora Project Contributor Agreement) '
  1718. "to use pagure</div>",
  1719. output_text,
  1720. )
  1721. class PagureFlaskAppAboutPagetests(tests.Modeltests):
  1722. """ Unit-tests for the about page. """
  1723. def test_about_page(self):
  1724. """ Test the about page when an admin_email is set. """
  1725. output = self.app.get("/about/")
  1726. self.assertEqual(output.status_code, 200)
  1727. output_text = output.get_data(as_text=True)
  1728. self.assertIn("<title>About - Pagure</title>", output_text)
  1729. self.assertIn(
  1730. "by emailing:\n "
  1731. '<a href="mailto:root@localhost.localdomain">',
  1732. output_text,
  1733. )
  1734. self.assertIn(
  1735. 'href="https://pagure.io/pagure/issues">open a ticket</a>',
  1736. output_text,
  1737. )
  1738. @patch.dict("pagure.config.config", {"ADMIN_EMAIL": "admin@fp.o"})
  1739. def test_about_page_admin_email(self):
  1740. """ Test the about page when an admin_email is set. """
  1741. output = self.app.get("/about/")
  1742. self.assertEqual(output.status_code, 200)
  1743. output_text = output.get_data(as_text=True)
  1744. self.assertIn("<title>About - Pagure</title>", output_text)
  1745. self.assertIn(
  1746. 'by emailing:\n <a href="mailto:admin@fp.o">', output_text
  1747. )
  1748. self.assertIn(
  1749. 'href="https://pagure.io/pagure/issues">open a ticket</a>',
  1750. output_text,
  1751. )
  1752. class PagureFlaskAppNoDocstests(tests.Modeltests):
  1753. """ Tests for flask app controller of pagure """
  1754. config_values = {"enable_docs": False, "docs_folder": None}
  1755. def test_new_project_no_docs_folder(self):
  1756. """ Test the new_project endpoint with DOCS_FOLDER is None. """
  1757. # Before
  1758. projects = pagure.lib.query.search_projects(self.session)
  1759. self.assertEqual(len(projects), 0)
  1760. self.assertFalse(
  1761. os.path.exists(os.path.join(self.path, "repos", "project#1.git"))
  1762. )
  1763. self.assertFalse(
  1764. os.path.exists(
  1765. os.path.join(self.path, "repos", "tickets", "project#1.git")
  1766. )
  1767. )
  1768. self.assertFalse(
  1769. os.path.exists(
  1770. os.path.join(self.path, "repos", "docs", "project#1.git")
  1771. )
  1772. )
  1773. self.assertFalse(
  1774. os.path.exists(
  1775. os.path.join(self.path, "repos", "requests", "project#1.git")
  1776. )
  1777. )
  1778. user = tests.FakeUser(username="foo")
  1779. with tests.user_set(self.app.application, user):
  1780. csrf_token = self.get_csrf()
  1781. data = {
  1782. "description": "Project #1",
  1783. "name": "project-1",
  1784. "csrf_token": csrf_token,
  1785. }
  1786. output = self.app.post("/new/", data=data, follow_redirects=True)
  1787. self.assertEqual(output.status_code, 200)
  1788. output_text = output.get_data(as_text=True)
  1789. self.assertIn(
  1790. '<div class="projectinfo my-3">\nProject #1', output_text
  1791. )
  1792. self.assertIn("<p>This repo is brand new!</p>", output_text)
  1793. self.assertIn(
  1794. "<title>Overview - project-1 - Pagure</title>", output_text
  1795. )
  1796. # After
  1797. projects = pagure.lib.query.search_projects(self.session)
  1798. self.assertEqual(len(projects), 1)
  1799. self.assertTrue(
  1800. os.path.exists(os.path.join(self.path, "repos", "project-1.git"))
  1801. )
  1802. self.assertTrue(
  1803. os.path.exists(
  1804. os.path.join(self.path, "repos", "tickets", "project-1.git")
  1805. )
  1806. )
  1807. self.assertFalse(
  1808. os.path.exists(
  1809. os.path.join(self.path, "repos", "docs", "project-1.git")
  1810. )
  1811. )
  1812. self.assertTrue(
  1813. os.path.exists(
  1814. os.path.join(self.path, "repos", "requests", "project-1.git")
  1815. )
  1816. )
  1817. class PagureFlaskAppNoTicketstests(tests.Modeltests):
  1818. """ Tests for flask app controller of pagure """
  1819. config_values = {"enable_tickets": False, "tickets_folder": None}
  1820. def test_new_project_no_tickets_folder(self):
  1821. """ Test the new_project endpoint with TICKETS_FOLDER is None. """
  1822. # Before
  1823. projects = pagure.lib.query.search_projects(self.session)
  1824. self.assertEqual(len(projects), 0)
  1825. self.assertFalse(
  1826. os.path.exists(os.path.join(self.path, "repos", "project#1.git"))
  1827. )
  1828. self.assertFalse(
  1829. os.path.exists(
  1830. os.path.join(self.path, "repos", "tickets", "project#1.git")
  1831. )
  1832. )
  1833. self.assertFalse(
  1834. os.path.exists(
  1835. os.path.join(self.path, "repos", "docs", "project#1.git")
  1836. )
  1837. )
  1838. self.assertFalse(
  1839. os.path.exists(
  1840. os.path.join(self.path, "repos", "requests", "project#1.git")
  1841. )
  1842. )
  1843. user = tests.FakeUser(username="foo")
  1844. with tests.user_set(self.app.application, user):
  1845. csrf_token = self.get_csrf()
  1846. data = {
  1847. "description": "Project #1",
  1848. "name": "project-1",
  1849. "csrf_token": csrf_token,
  1850. }
  1851. output = self.app.post("/new/", data=data, follow_redirects=True)
  1852. self.assertEqual(output.status_code, 200)
  1853. output_text = output.get_data(as_text=True)
  1854. self.assertIn(
  1855. '<div class="projectinfo my-3">\nProject #1', output_text
  1856. )
  1857. self.assertIn("<p>This repo is brand new!</p>", output_text)
  1858. self.assertIn(
  1859. "<title>Overview - project-1 - Pagure</title>", output_text
  1860. )
  1861. # After
  1862. projects = pagure.lib.query.search_projects(self.session)
  1863. self.assertEqual(len(projects), 1)
  1864. self.assertTrue(
  1865. os.path.exists(os.path.join(self.path, "repos", "project-1.git"))
  1866. )
  1867. self.assertFalse(
  1868. os.path.exists(
  1869. os.path.join(self.path, "repos", "tickets", "project-1.git")
  1870. )
  1871. )
  1872. self.assertTrue(
  1873. os.path.exists(
  1874. os.path.join(self.path, "repos", "docs", "project-1.git")
  1875. )
  1876. )
  1877. self.assertTrue(
  1878. os.path.exists(
  1879. os.path.join(self.path, "repos", "requests", "project-1.git")
  1880. )
  1881. )
  1882. class PagureFlaskAppRenewUserApiTokentests(tests.Modeltests):
  1883. @patch("pagure.decorators.admin_session_timedout")
  1884. def setUp(self, ast):
  1885. """ Constructor """
  1886. super(PagureFlaskAppRenewUserApiTokentests, self).setUp()
  1887. self.ast = ast
  1888. self.ast.return_value = False
  1889. user = tests.FakeUser(username="pingou")
  1890. with tests.user_set(self.app.application, user):
  1891. output = self.app.get("/settings/token/new")
  1892. self.assertEqual(output.status_code, 200)
  1893. output_text = output.get_data(as_text=True)
  1894. self.assertIn("<strong>Create a new token</strong>", output_text)
  1895. self.csrf_token = self.get_csrf(output=output)
  1896. # Create a token to renew
  1897. data = {"csrf_token": self.csrf_token, "acls": ["modify_project"]}
  1898. output = self.app.post(
  1899. "/settings/token/new/", data=data, follow_redirects=True
  1900. )
  1901. self.assertEqual(output.status_code, 200)
  1902. output_text = output.get_data(as_text=True)
  1903. self.assertIn("Token created", output_text)
  1904. # 1 token associated with the user, expires in 60 days
  1905. userobj = pagure.lib.query.search_user(self.session, username="pingou")
  1906. self.assertEqual(len(userobj.tokens), 1)
  1907. self.assertEqual(
  1908. userobj.tokens[0].expiration.date(),
  1909. datetime.datetime.utcnow().date()
  1910. + datetime.timedelta(days=(30 * 6)),
  1911. )
  1912. self.token = userobj.tokens[0].id
  1913. def test_renew_api_token_not_in(self):
  1914. """ Test the renew_api_token endpoint. """
  1915. # User not logged in
  1916. output = self.app.post("/settings/token/renew/123")
  1917. self.assertEqual(output.status_code, 302)
  1918. @patch("pagure.ui.app.admin_session_timedout")
  1919. def test_renew_api_token_session_old(self, ast):
  1920. """ Test the renew_api_token endpoint. """
  1921. ast.return_value = True
  1922. user = tests.FakeUser(username="pingou")
  1923. with tests.user_set(self.app.application, user):
  1924. data = {"csrf_token": self.csrf_token}
  1925. # Test when the session timed-out
  1926. output = self.app.post("/settings/token/renew/123", data=data)
  1927. self.assertEqual(output.status_code, 302)
  1928. output = self.app.get("/", follow_redirects=True)
  1929. self.assertEqual(output.status_code, 200)
  1930. output_text = output.get_data(as_text=True)
  1931. self.assertIn("Action canceled, try it again", output_text)
  1932. def test_renew_api_token_invalid_token(self):
  1933. """ Test the renew_api_token endpoint. """
  1934. user = tests.FakeUser(username="pingou")
  1935. with tests.user_set(self.app.application, user):
  1936. output = self.app.post(
  1937. "/settings/token/renew/123",
  1938. data={"csrf_token": self.csrf_token},
  1939. )
  1940. self.assertEqual(output.status_code, 404)
  1941. output_text = output.get_data(as_text=True)
  1942. self.assertIn("<p>Token not found</p>", output_text)
  1943. def test_renew_api_token(self):
  1944. """ Test the renew_api_token endpoint. """
  1945. user = tests.FakeUser(username="pingou")
  1946. with tests.user_set(self.app.application, user):
  1947. output = self.app.post(
  1948. "/settings/token/renew/%s" % self.token,
  1949. data={"csrf_token": self.csrf_token},
  1950. follow_redirects=True,
  1951. )
  1952. output_text = output.get_data(as_text=True)
  1953. self.assertIn(
  1954. "<title>pingou's settings - Pagure</title>", output_text
  1955. )
  1956. self.assertIn("Token created", output_text)
  1957. self.assertEqual(output_text.count('title="Revoke token">'), 2)
  1958. self.assertEqual(output_text.count('title="Renew token">'), 2)
  1959. self.session.commit()
  1960. # Existing token has been renewed
  1961. userobj = pagure.lib.query.search_user(
  1962. self.session, username="pingou"
  1963. )
  1964. self.assertEqual(len(userobj.tokens), 2)
  1965. self.assertEqual(
  1966. userobj.tokens[0].expiration.date(),
  1967. userobj.tokens[1].expiration.date(),
  1968. )
  1969. self.assertEqual(
  1970. userobj.tokens[0].created.date(),
  1971. userobj.tokens[1].created.date(),
  1972. )
  1973. self.assertEqual(userobj.tokens[0].acls, userobj.tokens[1].acls)
  1974. self.assertEqual(
  1975. userobj.tokens[0].description, userobj.tokens[1].description
  1976. )
  1977. class PagureFlaskAppNewProjecttests(tests.Modeltests):
  1978. """ Tests creating new project via the flask app controller of pagure """
  1979. def setUp(self):
  1980. """ Setup the environment. """
  1981. super(PagureFlaskAppNewProjecttests, self).setUp()
  1982. # Before
  1983. projects = pagure.lib.query.search_projects(self.session)
  1984. self.assertEqual(len(projects), 0)
  1985. self.assertFalse(
  1986. os.path.exists(os.path.join(self.path, "repos", "project#1.git"))
  1987. )
  1988. self.assertFalse(
  1989. os.path.exists(
  1990. os.path.join(self.path, "repos", "tickets", "project#1.git")
  1991. )
  1992. )
  1993. self.assertFalse(
  1994. os.path.exists(
  1995. os.path.join(self.path, "repos", "docs", "project#1.git")
  1996. )
  1997. )
  1998. self.assertFalse(
  1999. os.path.exists(
  2000. os.path.join(self.path, "repos", "requests", "project#1.git")
  2001. )
  2002. )
  2003. @patch.dict("pagure.config.config", {"CASE_SENSITIVE": True})
  2004. def test_new_project_case_sensitive(self):
  2005. tests.create_projects(self.session)
  2006. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2007. output = self.app.get("/test")
  2008. self.assertEqual(output.status_code, 200)
  2009. output = self.app.get("/TEST")
  2010. self.assertEqual(output.status_code, 404)
  2011. user = tests.FakeUser()
  2012. user.username = "foo"
  2013. with tests.user_set(self.app.application, user):
  2014. output = self.app.get("/new/")
  2015. self.assertEqual(output.status_code, 200)
  2016. csrf_token = self.get_csrf(output=output)
  2017. data = {
  2018. "description": "TEST",
  2019. "name": "TEST",
  2020. "csrf_token": csrf_token,
  2021. "create_readme": True,
  2022. }
  2023. self.app.post("/new/", data=data, follow_redirects=True)
  2024. self.assertEqual(output.status_code, 200)
  2025. output = self.app.get("/TEST")
  2026. self.assertEqual(output.status_code, 200)
  2027. def test_new_project_readme(self):
  2028. # Before
  2029. projects = pagure.lib.query.search_projects(self.session)
  2030. self.assertEqual(len(projects), 0)
  2031. user = tests.FakeUser(username="foo")
  2032. with tests.user_set(self.app.application, user):
  2033. csrf_token = self.get_csrf()
  2034. data = {
  2035. "description": "testproject",
  2036. "name": "testproject",
  2037. "csrf_token": csrf_token,
  2038. "create_readme": True,
  2039. }
  2040. output = self.app.post("/new/", data=data, follow_redirects=True)
  2041. self.assertEqual(output.status_code, 200)
  2042. output_text = output.get_data(as_text=True)
  2043. self.assertIn(
  2044. "<title>Overview - testproject - Pagure</title>", output_text
  2045. )
  2046. self.assertIn(
  2047. '<a href="/testproject"><strong>testproject</strong></a>',
  2048. output_text,
  2049. )
  2050. self.assertIn(
  2051. '<code class="py-1 px-2 font-weight-bold '
  2052. 'commit_branch">master</code>',
  2053. output_text,
  2054. )
  2055. # After
  2056. projects = pagure.lib.query.search_projects(self.session)
  2057. self.assertEqual(len(projects), 1)
  2058. project = pagure.lib.query._get_project(self.session, "testproject")
  2059. repo = pygit2.Repository(project.repopath("main"))
  2060. self.assertEqual(repo.listall_branches(), ["master"])
  2061. @patch.dict("pagure.config.config", {"ENABLE_UI_NEW_PROJECTS": False})
  2062. def test_new_project_when_turned_off_in_the_ui(self):
  2063. """ Test the new_project endpoint when new project creation is
  2064. not allowed in the UI of this pagure instance. """
  2065. user = tests.FakeUser(username="foo")
  2066. with tests.user_set(self.app.application, user):
  2067. output = self.app.get("/new/")
  2068. self.assertEqual(output.status_code, 404)
  2069. data = {"description": "Project #1", "name": "project-1"}
  2070. output = self.app.post("/new/", data=data, follow_redirects=True)
  2071. self.assertEqual(output.status_code, 404)
  2072. @patch.dict("pagure.config.config", {"ENABLE_UI_NEW_PROJECTS": False})
  2073. def test_new_project_button_when_turned_off_in_the_ui_no_project(self):
  2074. """ Test the index endpoint when new project creation is
  2075. not allowed in the UI of this pagure instance. """
  2076. user = tests.FakeUser(username="foo")
  2077. with tests.user_set(self.app.application, user):
  2078. output = self.app.get("/", follow_redirects=True)
  2079. self.assertEqual(output.status_code, 200)
  2080. output_text = output.get_data(as_text=True)
  2081. self.assertIn(
  2082. '<h4 class="font-weight-bold mb-0">My Projects</h4>',
  2083. output_text,
  2084. )
  2085. # master template
  2086. self.assertNotIn(
  2087. '<span class="oi" data-glyph="plus" title="Create New"',
  2088. output_text,
  2089. )
  2090. # index_auth template
  2091. self.assertNotIn(
  2092. 'title="Create New Project" aria-hidden="true">', output_text
  2093. )
  2094. @patch.dict("pagure.config.config", {"ENABLE_UI_NEW_PROJECTS": False})
  2095. def test_new_project_button_when_turned_off_in_the_ui_w_project(self):
  2096. """ Test the index endpoint when new project creation is
  2097. not allowed in the UI of this pagure instance. """
  2098. tests.create_projects(self.session)
  2099. user = tests.FakeUser(username="pingou")
  2100. with tests.user_set(self.app.application, user):
  2101. output = self.app.get("/", follow_redirects=True)
  2102. self.assertEqual(output.status_code, 200)
  2103. output_text = output.get_data(as_text=True)
  2104. self.assertIn(
  2105. '<h4 class="font-weight-bold mb-0">My Projects</h4>',
  2106. output_text,
  2107. )
  2108. # master template
  2109. self.assertNotIn(
  2110. '<span class="oi" data-glyph="plus" title="Create New"',
  2111. output_text,
  2112. )
  2113. # index_auth template
  2114. self.assertNotIn(
  2115. 'title="Create New Project" aria-hidden="true">', output_text
  2116. )
  2117. def test_new_project_with_dot(self):
  2118. """ Test the new_project endpoint when new project contains a dot.
  2119. """
  2120. # Before
  2121. projects = pagure.lib.query.search_projects(self.session)
  2122. self.assertEqual(len(projects), 0)
  2123. user = tests.FakeUser(username="foo")
  2124. with tests.user_set(self.app.application, user):
  2125. csrf_token = self.get_csrf()
  2126. data = {
  2127. "description": "Project #1.",
  2128. "name": "project.1",
  2129. "csrf_token": csrf_token,
  2130. }
  2131. output = self.app.post("/new/", data=data, follow_redirects=True)
  2132. self.assertEqual(output.status_code, 200)
  2133. output_text = output.get_data(as_text=True)
  2134. self.assertIn(
  2135. "<title>Overview - project.1 - Pagure</title>", output_text
  2136. )
  2137. self.assertIn(
  2138. '<a href="/project.1"><strong>project.1</strong></a>',
  2139. output_text,
  2140. )
  2141. # After
  2142. projects = pagure.lib.query.search_projects(self.session)
  2143. self.assertEqual(len(projects), 1)
  2144. def test_new_project_with_plus(self):
  2145. """ Test the new_project endpoint when new project contains a plus sign.
  2146. """
  2147. # Before
  2148. projects = pagure.lib.query.search_projects(self.session)
  2149. self.assertEqual(len(projects), 0)
  2150. user = tests.FakeUser(username="foo")
  2151. with tests.user_set(self.app.application, user):
  2152. csrf_token = self.get_csrf()
  2153. data = {
  2154. "description": "Project #1.",
  2155. "name": "project+1",
  2156. "csrf_token": csrf_token,
  2157. }
  2158. output = self.app.post("/new/", data=data, follow_redirects=True)
  2159. self.assertEqual(output.status_code, 200)
  2160. output_text = output.get_data(as_text=True)
  2161. self.assertIn(
  2162. "<title>Overview - project+1 - Pagure</title>", output_text
  2163. )
  2164. self.assertTrue(
  2165. '<a href="/project+1"><strong>project+1</strong></a>'
  2166. in output_text
  2167. or '<a href="/project%2B1"><strong>project+1</strong></a>'
  2168. in output_text
  2169. )
  2170. # After
  2171. projects = pagure.lib.query.search_projects(self.session)
  2172. self.assertEqual(len(projects), 1)
  2173. repo = pygit2.Repository(projects[0].repopath("main"))
  2174. self.assertEqual(repo.listall_branches(), [])
  2175. def test_new_project_with_default_branch(self):
  2176. """ Test the new_project endpoint when new project contains a plus sign.
  2177. """
  2178. # Before
  2179. projects = pagure.lib.query.search_projects(self.session)
  2180. self.assertEqual(len(projects), 0)
  2181. user = tests.FakeUser(username="foo")
  2182. with tests.user_set(self.app.application, user):
  2183. csrf_token = self.get_csrf()
  2184. data = {
  2185. "description": "Project #1.",
  2186. "name": "project_main",
  2187. "csrf_token": csrf_token,
  2188. "default_branch": "main",
  2189. "create_readme": True,
  2190. }
  2191. output = self.app.post("/new/", data=data, follow_redirects=True)
  2192. self.assertEqual(output.status_code, 200)
  2193. output_text = output.get_data(as_text=True)
  2194. self.assertIn(
  2195. "<title>Overview - project_main - Pagure</title>", output_text
  2196. )
  2197. self.assertIn(
  2198. '<a href="/project_main"><strong>project_main</strong></a>',
  2199. output_text,
  2200. )
  2201. self.assertIn(
  2202. '<code class="py-1 px-2 font-weight-bold '
  2203. 'commit_branch">main</code>',
  2204. output_text,
  2205. )
  2206. # After
  2207. projects = pagure.lib.query.search_projects(self.session)
  2208. self.assertEqual(len(projects), 1)
  2209. repo = pygit2.Repository(projects[0].repopath("main"))
  2210. self.assertEqual(repo.listall_branches(), ["main"])
  2211. def test_new_project_when_turned_off(self):
  2212. """ Test the new_project endpoint when new project creation is
  2213. not allowed in the pagure instance. """
  2214. # turn the project creation off
  2215. pagure.config.config["ENABLE_NEW_PROJECTS"] = False
  2216. # Before
  2217. projects = pagure.lib.query.search_projects(self.session)
  2218. self.assertEqual(len(projects), 0)
  2219. self.assertFalse(
  2220. os.path.exists(os.path.join(self.path, "repos", "project-1.git"))
  2221. )
  2222. self.assertFalse(
  2223. os.path.exists(
  2224. os.path.join(self.path, "repos", "tickets", "project-1.git")
  2225. )
  2226. )
  2227. self.assertFalse(
  2228. os.path.exists(
  2229. os.path.join(self.path, "repos", "docs", "project-1.git")
  2230. )
  2231. )
  2232. self.assertFalse(
  2233. os.path.exists(
  2234. os.path.join(self.path, "repos", "requests", "project-1.git")
  2235. )
  2236. )
  2237. user = tests.FakeUser()
  2238. with tests.user_set(self.app.application, user):
  2239. output = self.app.get("/new/")
  2240. self.assertEqual(output.status_code, 404)
  2241. # just get the csrf token
  2242. pagure.config.config["ENABLE_NEW_PROJECTS"] = True
  2243. output = self.app.get("/new/")
  2244. pagure.config.config["ENABLE_NEW_PROJECTS"] = False
  2245. csrf_token = (
  2246. output.get_data(as_text=True)
  2247. .split('name="csrf_token" type="hidden" value="')[1]
  2248. .split('">')[0]
  2249. )
  2250. data = {"description": "Project #1", "name": "project-1"}
  2251. user.username = "foo"
  2252. with tests.user_set(self.app.application, user):
  2253. data["csrf_token"] = csrf_token
  2254. output = self.app.post("/new/", data=data, follow_redirects=True)
  2255. self.assertEqual(output.status_code, 404)
  2256. # After
  2257. projects = pagure.lib.query.search_projects(self.session)
  2258. self.assertEqual(len(projects), 0)
  2259. self.assertFalse(
  2260. os.path.exists(os.path.join(self.path, "repos", "project-1.git"))
  2261. )
  2262. self.assertFalse(
  2263. os.path.exists(
  2264. os.path.join(self.path, "repos", "tickets", "project-1.git")
  2265. )
  2266. )
  2267. self.assertFalse(
  2268. os.path.exists(
  2269. os.path.join(self.path, "repos", "docs", "project-1.git")
  2270. )
  2271. )
  2272. self.assertFalse(
  2273. os.path.exists(
  2274. os.path.join(self.path, "repos", "requests", "project-1.git")
  2275. )
  2276. )
  2277. pagure.config.config["ENABLE_NEW_PROJECTS"] = True
  2278. def test_new_project_mirrored_invalid_url(self):
  2279. """ Test the new_project with a mirrored repo but an invalid URL. """
  2280. user = tests.FakeUser(username="foo")
  2281. with tests.user_set(self.app.application, user):
  2282. output = self.app.get("/new/")
  2283. self.assertEqual(output.status_code, 200)
  2284. csrf_token = self.get_csrf(output=output)
  2285. data = {
  2286. "description": "Project #1",
  2287. "name": "project-1",
  2288. "mirrored_from": "abcd",
  2289. "csrf_token": csrf_token,
  2290. }
  2291. output = self.app.post("/new/", data=data, follow_redirects=True)
  2292. self.assertEqual(output.status_code, 200)
  2293. output_text = output.get_data(as_text=True)
  2294. self.assertIn("<title>New project - Pagure</title>", output_text)
  2295. self.assertIn("Invalid input.&nbsp;", output_text)
  2296. def test_new_project_mirrored_invalid_sshurl(self):
  2297. """ Test the new_project with a mirrored repo but an invalid
  2298. SSH-like url.
  2299. """
  2300. user = tests.FakeUser(username="foo")
  2301. with tests.user_set(self.app.application, user):
  2302. output = self.app.get("/new/")
  2303. self.assertEqual(output.status_code, 200)
  2304. csrf_token = self.get_csrf(output=output)
  2305. data = {
  2306. "description": "Project #1",
  2307. "name": "project-1",
  2308. "mirrored_from": "ssh://git@server.org/foo/bar.git",
  2309. "csrf_token": csrf_token,
  2310. }
  2311. output = self.app.post("/new/", data=data, follow_redirects=True)
  2312. self.assertEqual(output.status_code, 200)
  2313. output_text = output.get_data(as_text=True)
  2314. self.assertIn("<title>New project - Pagure</title>", output_text)
  2315. self.assertIn("Invalid input.&nbsp;", output_text)
  2316. def test_new_project_mirrored_valid_url(self):
  2317. """ Test the new_project with a mirrored repo with a valid url. """
  2318. user = tests.FakeUser(username="foo")
  2319. with tests.user_set(self.app.application, user):
  2320. output = self.app.get("/new/")
  2321. self.assertEqual(output.status_code, 200)
  2322. output_text = output.get_data(as_text=True)
  2323. self.assertIn(
  2324. '<strong><label for="mirrored_from">Mirror from URL'
  2325. "</label></strong>",
  2326. output_text,
  2327. )
  2328. csrf_token = self.get_csrf(output=output)
  2329. data = {
  2330. "description": "Project #1",
  2331. "name": "project-1",
  2332. "mirrored_from": "https://example.com/foo/bar.git",
  2333. "csrf_token": csrf_token,
  2334. }
  2335. output = self.app.post("/new/", data=data, follow_redirects=True)
  2336. self.assertEqual(output.status_code, 200)
  2337. output_text = output.get_data(as_text=True)
  2338. self.assertIn(
  2339. "<title>Overview - project-1 - Pagure</title>", output_text
  2340. )
  2341. self.assertIn(
  2342. "<p>This repo is brand new and meant to be mirrored from "
  2343. "https://example.com/foo/bar.git !</p>",
  2344. output_text,
  2345. )
  2346. @patch.dict("pagure.config.config", {"DISABLE_MIRROR_IN": True})
  2347. def test_new_project_mirrored_mirror_disabled(self):
  2348. """ Test the new_project with a mirrored repo when that feature is
  2349. disabled.
  2350. """
  2351. user = tests.FakeUser(username="foo")
  2352. with tests.user_set(self.app.application, user):
  2353. output = self.app.get("/new/")
  2354. self.assertEqual(output.status_code, 200)
  2355. output_text = output.get_data(as_text=True)
  2356. self.assertNotIn(
  2357. '<strong><label for="mirrored_from">Mirror from URL'
  2358. "</label></strong>",
  2359. output_text,
  2360. )
  2361. csrf_token = self.get_csrf(output=output)
  2362. data = {
  2363. "description": "Project #1",
  2364. "name": "project-1",
  2365. "mirrored_from": "https://example.com/foo/bar.git",
  2366. "csrf_token": csrf_token,
  2367. }
  2368. output = self.app.post("/new/", data=data, follow_redirects=True)
  2369. self.assertEqual(output.status_code, 200)
  2370. output_text = output.get_data(as_text=True)
  2371. self.assertIn("<title>New project - Pagure</title>", output_text)
  2372. self.assertIn(
  2373. "</i> Mirroring in projects has been disabled in "
  2374. "this instance</div>",
  2375. output_text,
  2376. )
  2377. def test_new_project(self):
  2378. """ Test the new_project endpoint. """
  2379. user = tests.FakeUser()
  2380. with tests.user_set(self.app.application, user):
  2381. output = self.app.get("/new/")
  2382. self.assertEqual(output.status_code, 200)
  2383. output_text = output.get_data(as_text=True)
  2384. self.assertIn("<strong>Create new Project</strong>", output_text)
  2385. csrf_token = output_text.split(
  2386. 'name="csrf_token" type="hidden" value="'
  2387. )[1].split('">')[0]
  2388. data = {"description": "Project #1"}
  2389. output = self.app.post("/new/", data=data)
  2390. self.assertEqual(output.status_code, 200)
  2391. output_text = output.get_data(as_text=True)
  2392. self.assertIn("<strong>Create new Project</strong>", output_text)
  2393. self.assertIn(
  2394. "<small>\n This field is required.&nbsp;\n"
  2395. " </small>",
  2396. output_text,
  2397. )
  2398. data["name"] = "project-1"
  2399. output = self.app.post("/new/", data=data)
  2400. self.assertEqual(output.status_code, 200)
  2401. output_text = output.get_data(as_text=True)
  2402. self.assertIn("<strong>Create new Project</strong>", output_text)
  2403. self.assertNotIn(
  2404. "<small>\n This field is required.&nbsp;\n"
  2405. " </small>",
  2406. output_text,
  2407. )
  2408. data["csrf_token"] = csrf_token
  2409. output = self.app.post("/new/", data=data)
  2410. self.assertEqual(output.status_code, 200)
  2411. output_text = output.get_data(as_text=True)
  2412. self.assertIn("<strong>Create new Project</strong>", output_text)
  2413. self.assertIn("No user " "&#34;username&#34; found", output_text)
  2414. user.username = "foo"
  2415. with tests.user_set(self.app.application, user):
  2416. data["csrf_token"] = csrf_token
  2417. output = self.app.post("/new/", data=data, follow_redirects=True)
  2418. self.assertEqual(output.status_code, 200)
  2419. output_text = output.get_data(as_text=True)
  2420. self.assertIn(
  2421. '<div class="projectinfo my-3">\nProject #1', output_text
  2422. )
  2423. self.assertIn("<p>This repo is brand new!</p>", output_text)
  2424. self.assertIn(
  2425. "<title>Overview - project-1 - Pagure</title>", output_text
  2426. )
  2427. # After
  2428. projects = pagure.lib.query.search_projects(self.session)
  2429. self.assertEqual(len(projects), 1)
  2430. self.assertTrue(
  2431. os.path.exists(os.path.join(self.path, "repos", "project-1.git"))
  2432. )
  2433. self.assertTrue(
  2434. os.path.exists(
  2435. os.path.join(self.path, "repos", "tickets", "project-1.git")
  2436. )
  2437. )
  2438. self.assertTrue(
  2439. os.path.exists(
  2440. os.path.join(self.path, "repos", "docs", "project-1.git")
  2441. )
  2442. )
  2443. self.assertTrue(
  2444. os.path.exists(
  2445. os.path.join(self.path, "repos", "requests", "project-1.git")
  2446. )
  2447. )
  2448. @patch.dict("pagure.config.config", {"USER_NAMESPACE": True})
  2449. def test_new_project_user_namespaced(self):
  2450. """ Test the new_project with a user namespaced enabled.
  2451. """
  2452. user = tests.FakeUser(username="foo")
  2453. with tests.user_set(self.app.application, user):
  2454. output = self.app.get("/new/")
  2455. self.assertEqual(output.status_code, 200)
  2456. output_text = output.get_data(as_text=True)
  2457. csrf_token = self.get_csrf(output=output)
  2458. data = {
  2459. "description": "Project #1",
  2460. "name": "project-1",
  2461. "csrf_token": csrf_token,
  2462. }
  2463. output = self.app.post("/new/", data=data, follow_redirects=True)
  2464. self.assertEqual(output.status_code, 200)
  2465. output_text = output.get_data(as_text=True)
  2466. self.assertIn(
  2467. "<title>Overview - foo/project-1 - Pagure</title>", output_text
  2468. )
  2469. self.assertIn(
  2470. '<div class="projectinfo my-3">\nProject #1', output_text
  2471. )
  2472. self.assertIn("<p>This repo is brand new!</p>", output_text)
  2473. # After
  2474. projects = pagure.lib.query.search_projects(self.session)
  2475. self.assertEqual(len(projects), 1)
  2476. self.assertTrue(
  2477. os.path.exists(
  2478. os.path.join(self.path, "repos", "foo", "project-1.git")
  2479. )
  2480. )
  2481. self.assertTrue(
  2482. os.path.exists(
  2483. os.path.join(
  2484. self.path, "repos", "tickets", "foo", "project-1.git"
  2485. )
  2486. )
  2487. )
  2488. self.assertTrue(
  2489. os.path.exists(
  2490. os.path.join(
  2491. self.path, "repos", "docs", "foo", "project-1.git"
  2492. )
  2493. )
  2494. )
  2495. self.assertTrue(
  2496. os.path.exists(
  2497. os.path.join(
  2498. self.path, "repos", "requests", "foo", "project-1.git"
  2499. )
  2500. )
  2501. )
  2502. @patch.dict("pagure.config.config", {"USER_NAMESPACE": True})
  2503. def test_new_project_user_namespaced_invalid_user(self):
  2504. """ Test the new_project with a user namespaced enabled.
  2505. """
  2506. tests.create_user(self.session, "docs", "evil docs", ["docs@bar.com"])
  2507. user = tests.FakeUser(username="docs")
  2508. with tests.user_set(self.app.application, user):
  2509. output = self.app.get("/new/")
  2510. self.assertEqual(output.status_code, 200)
  2511. output_text = output.get_data(as_text=True)
  2512. csrf_token = self.get_csrf(output=output)
  2513. data = {
  2514. "description": "Project #1",
  2515. "name": "project-1",
  2516. "csrf_token": csrf_token,
  2517. }
  2518. output = self.app.post("/new/", data=data, follow_redirects=True)
  2519. self.assertEqual(output.status_code, 200)
  2520. output_text = output.get_data(as_text=True)
  2521. self.assertIn("<title>New project - Pagure</title>", output_text)
  2522. self.assertIn(
  2523. "</i> No project &#34;docs/project-1&#34; are allowed to be "
  2524. "created due to potential conflicts in URLs with pagure "
  2525. "itself</div>",
  2526. output_text,
  2527. )
  2528. # After
  2529. projects = pagure.lib.query.search_projects(self.session)
  2530. self.assertEqual(len(projects), 0)
  2531. if __name__ == "__main__":
  2532. unittest.main(verbosity=2)