test_pagure_flask_ui_app.py 105 KB

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