test_pagure_flask_ui_app.py 97 KB

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