test_pagure_flask_ui_app.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import unittest
  10. import shutil
  11. import sys
  12. import os
  13. import six
  14. import json
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.lib
  19. import tests
  20. class PagureFlaskApptests(tests.Modeltests):
  21. """ Tests for flask app controller of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskApptests, self).setUp()
  25. pagure.APP.config['TESTING'] = True
  26. pagure.SESSION = self.session
  27. pagure.ui.SESSION = self.session
  28. pagure.ui.app.SESSION = self.session
  29. pagure.ui.filters.SESSION = self.session
  30. pagure.ui.repo.SESSION = self.session
  31. pagure.APP.config['GIT_FOLDER'] = self.path
  32. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  33. self.path, 'forks')
  34. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  35. self.path, 'tickets')
  36. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  37. self.path, 'docs')
  38. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  39. self.path, 'requests')
  40. self.app = pagure.APP.test_client()
  41. def test_index(self):
  42. """ Test the index endpoint. """
  43. output = self.app.get('/')
  44. self.assertEqual(output.status_code, 200)
  45. self.assertIn(
  46. '<h2 class="m-b-1">All Projects '
  47. '<span class="label label-default">0</span></h2>', output.get_data(as_text=True))
  48. tests.create_projects(self.session)
  49. output = self.app.get('/?page=abc')
  50. self.assertEqual(output.status_code, 200)
  51. self.assertIn(
  52. '<h2 class="m-b-1">All Projects '
  53. '<span class="label label-default">2</span></h2>', output.get_data(as_text=True))
  54. # Add a 3rd project with a long description
  55. item = pagure.lib.model.Project(
  56. user_id=2, # foo
  57. name='test3',
  58. description='test project #3 with a very long description',
  59. hook_token='aaabbbeee',
  60. )
  61. self.session.add(item)
  62. self.session.commit()
  63. user = tests.FakeUser(username='foo')
  64. with tests.user_set(pagure.APP, user):
  65. output = self.app.get('/?repopage=abc&forkpage=def')
  66. self.assertIn(
  67. 'Projects <span class="label label-default">1</span>',
  68. output.get_data(as_text=True))
  69. self.assertIn(
  70. 'Forks <span class="label label-default">0</span>',
  71. output.get_data(as_text=True))
  72. self.assertEqual(
  73. output.get_data(as_text=True).count('<p>No group found</p>'), 1)
  74. self.assertEqual(
  75. output.get_data(as_text=True).count('<div class="card-header">'),
  76. 4)
  77. def test_watch_list(self):
  78. ''' Test for watch list of a user '''
  79. user = tests.FakeUser(username='pingou')
  80. with tests.user_set(pagure.APP, user):
  81. output = self.app.get('/')
  82. self.assertIn(
  83. '<div class="text-xs-center">You have no projects</div>',
  84. output.data.decode('utf-8'))
  85. self.assertIn(
  86. '<p>You have no forks</p>',
  87. output.data.decode('utf-8'))
  88. self.assertIn(
  89. '<p>No project in watch list</p>',
  90. output.data.decode('utf-8'))
  91. tests.create_projects(self.session)
  92. output = self.app.get('/')
  93. self.assertIn(
  94. 'My Projects <span class="label label-default">2</span>',
  95. output.data.decode('utf-8'))
  96. self.assertIn(
  97. 'My Forks <span class="label label-default">0</span>',
  98. output.data.decode('utf-8'))
  99. self.assertIn(
  100. 'My Watch List <span class="label label-default">2</span>',
  101. output.data.decode('utf-8'))
  102. def test_view_users(self):
  103. """ Test the view_users endpoint. """
  104. output = self.app.get('/users/?page=abc')
  105. self.assertEqual(output.status_code, 200)
  106. self.assertIn(
  107. '<h2 class="m-b-1">\n Users '
  108. '<span class="label label-default">2</span></h2>', output.get_data(as_text=True))
  109. self.assertIn(
  110. '<a class="project_link logo_link" href="/user/pingou">',
  111. output.get_data(as_text=True))
  112. self.assertIn(
  113. '<a class="project_link logo_link" href="/user/foo">',
  114. output.get_data(as_text=True))
  115. def test_view_user(self):
  116. """ Test the view_user endpoint. """
  117. output = self.app.get('/user/pingou?repopage=abc&forkpage=def')
  118. self.assertEqual(output.status_code, 200)
  119. self.assertIn(
  120. 'Projects <span class="label label-default">0</span>',
  121. output.get_data(as_text=True))
  122. self.assertIn(
  123. 'Forks <span class="label label-default">0</span>',
  124. output.get_data(as_text=True))
  125. tests.create_projects(self.session)
  126. self.gitrepos = tests.create_projects_git(
  127. pagure.APP.config['GIT_FOLDER'])
  128. output = self.app.get('/user/pingou?repopage=abc&forkpage=def')
  129. self.assertEqual(output.status_code, 200)
  130. self.assertIn(
  131. b'Projects <span class="label label-default">2</span>',
  132. output.data)
  133. self.assertIn(
  134. b'Forks <span class="label label-default">0</span>', output.data)
  135. def test_new_project_when_turned_off(self):
  136. """ Test the new_project endpoint when new project creation is
  137. not allowed in the pagure instance. """
  138. #turn the project creation off
  139. pagure.APP.config['ENABLE_NEW_PROJECTS'] = False
  140. # Before
  141. projects = pagure.lib.search_projects(self.session)
  142. self.assertEqual(len(projects), 0)
  143. self.assertFalse(os.path.exists(
  144. os.path.join(self.path, 'project-1.git')))
  145. self.assertFalse(os.path.exists(
  146. os.path.join(self.path, 'tickets', 'project-1.git')))
  147. self.assertFalse(os.path.exists(
  148. os.path.join(self.path, 'docs', 'project-1.git')))
  149. self.assertFalse(os.path.exists(
  150. os.path.join(self.path, 'requests', 'project-1.git')))
  151. user = tests.FakeUser()
  152. with tests.user_set(pagure.APP, user):
  153. output = self.app.get('/new/')
  154. self.assertEqual(output.status_code, 404)
  155. #just get the csrf token
  156. pagure.APP.config['ENABLE_NEW_PROJECTS'] = True
  157. output = self.app.get('/new/')
  158. pagure.APP.config['ENABLE_NEW_PROJECTS'] = False
  159. csrf_token = output.data.split(
  160. b'name="csrf_token" type="hidden" value="')[1].split(b'">')[0]
  161. data = {
  162. 'description': 'Project #1',
  163. 'name': 'project-1',
  164. }
  165. user.username = 'foo'
  166. with tests.user_set(pagure.APP, user):
  167. data['csrf_token'] = csrf_token
  168. output = self.app.post('/new/', data=data, follow_redirects=True)
  169. self.assertEqual(output.status_code, 404)
  170. #After
  171. projects = pagure.lib.search_projects(self.session)
  172. self.assertEqual(len(projects), 0)
  173. self.assertFalse(os.path.exists(
  174. os.path.join(self.path, 'project-1.git')))
  175. self.assertFalse(os.path.exists(
  176. os.path.join(self.path, 'tickets', 'project-1.git')))
  177. self.assertFalse(os.path.exists(
  178. os.path.join(self.path, 'docs', 'project-1.git')))
  179. self.assertFalse(os.path.exists(
  180. os.path.join(self.path, 'requests', 'project-1.git')))
  181. pagure.APP.config['ENABLE_NEW_PROJECTS'] = True
  182. def test_new_project(self):
  183. """ Test the new_project endpoint. """
  184. # Before
  185. projects = pagure.lib.search_projects(self.session)
  186. self.assertEqual(len(projects), 0)
  187. self.assertFalse(os.path.exists(
  188. os.path.join(self.path, 'project#1.git')))
  189. self.assertFalse(os.path.exists(
  190. os.path.join(self.path, 'tickets', 'project#1.git')))
  191. self.assertFalse(os.path.exists(
  192. os.path.join(self.path, 'docs', 'project#1.git')))
  193. self.assertFalse(os.path.exists(
  194. os.path.join(self.path, 'requests', 'project#1.git')))
  195. user = tests.FakeUser()
  196. with tests.user_set(pagure.APP, user):
  197. output = self.app.get('/new/')
  198. self.assertEqual(output.status_code, 200)
  199. self.assertIn(
  200. b'<strong>Create new Project</strong>', output.data)
  201. csrf_token = output.get_data(as_text=True).split(
  202. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  203. data = {
  204. 'description': 'Project #1',
  205. }
  206. output = self.app.post('/new/', data=data)
  207. self.assertEqual(output.status_code, 200)
  208. self.assertIn(
  209. b'<strong>Create new Project</strong>', output.data)
  210. self.assertIn(
  211. b'<small>\n This field is required.&nbsp;\n'
  212. b' </small>', output.data)
  213. data['name'] = 'project-1'
  214. output = self.app.post('/new/', data=data)
  215. self.assertEqual(output.status_code, 200)
  216. self.assertIn(b'<strong>Create new Project</strong>', output.data)
  217. self.assertNotIn(
  218. b'<small>\n This field is required.&nbsp;\n'
  219. b' </small>', output.data)
  220. data['csrf_token'] = csrf_token
  221. output = self.app.post('/new/', data=data)
  222. self.assertEqual(output.status_code, 200)
  223. self.assertIn(b'<strong>Create new Project</strong>', output.data)
  224. self.assertIn(
  225. b'</button>\n No user '
  226. b'&#34;username&#34; found\n </div>',
  227. output.data)
  228. user.username = 'foo'
  229. with tests.user_set(pagure.APP, user):
  230. data['csrf_token'] = csrf_token
  231. output = self.app.post('/new/', data=data, follow_redirects=True)
  232. self.assertEqual(output.status_code, 200)
  233. self.assertIn(
  234. b'<div class="projectinfo m-t-1 m-b-1">\nProject #1 </div>',
  235. output.data)
  236. self.assertIn(b'<p>This repo is brand new!</p>', output.data)
  237. self.assertIn(
  238. b'<title>Overview - project-1 - Pagure</title>', output.data)
  239. # After
  240. projects = pagure.lib.search_projects(self.session)
  241. self.assertEqual(len(projects), 1)
  242. self.assertTrue(os.path.exists(
  243. os.path.join(self.path, 'project-1.git')))
  244. self.assertTrue(os.path.exists(
  245. os.path.join(self.path, 'tickets', 'project-1.git')))
  246. self.assertTrue(os.path.exists(
  247. os.path.join(self.path, 'docs', 'project-1.git')))
  248. self.assertTrue(os.path.exists(
  249. os.path.join(self.path, 'requests', 'project-1.git')))
  250. def test_non_ascii_new_project(self):
  251. """ Test the new_project endpoint with a non-ascii project. """
  252. # Before
  253. projects = pagure.lib.search_projects(self.session)
  254. self.assertEqual(len(projects), 0)
  255. self.assertFalse(os.path.exists(
  256. os.path.join(self.path, 'project-1.git')))
  257. self.assertFalse(os.path.exists(
  258. os.path.join(self.path, 'tickets', 'project-1.git')))
  259. self.assertFalse(os.path.exists(
  260. os.path.join(self.path, 'docs', 'project-1.git')))
  261. self.assertFalse(os.path.exists(
  262. os.path.join(self.path, 'requests', 'project-1.git')))
  263. user = tests.FakeUser()
  264. user.username = 'foo'
  265. with tests.user_set(pagure.APP, user):
  266. output = self.app.get('/new/')
  267. self.assertEqual(output.status_code, 200)
  268. self.assertIn(
  269. b'<strong>Create new Project</strong>', output.data)
  270. csrf_token = output.get_data(as_text=True).split(
  271. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  272. data = {
  273. 'description': 'Prõjéctö #1',
  274. 'name': 'project-1',
  275. 'csrf_token': csrf_token,
  276. 'create_readme': True,
  277. }
  278. output = self.app.post('/new/', data=data, follow_redirects=True)
  279. self.assertEqual(output.status_code, 200)
  280. self.assertIn(
  281. '<div class="projectinfo m-t-1 m-b-1">\nPrõjéctö #1 </div>',
  282. output.data if six.PY2 else output.get_data(as_text=True))
  283. self.assertIn(
  284. '''<section class="readme">
  285. <h1>project-1</h1>
  286. <p>Prõjéctö #1</p>
  287. </section>''', output.data if six.PY2 else output.get_data(as_text=True))
  288. data = {
  289. 'description': 'Мой первый суперский репозиторий',
  290. 'name': 'project-2',
  291. 'csrf_token': csrf_token,
  292. 'create_readme': True,
  293. }
  294. output = self.app.post('/new/', data=data, follow_redirects=True)
  295. self.assertEqual(output.status_code, 200)
  296. self.assertIn(
  297. '<div class="projectinfo m-t-1 m-b-1">\nМой первый суперский репозиторий </div>',
  298. output.data if six.PY2 else output.get_data(as_text=True))
  299. self.assertIn(
  300. '''<section class="readme">
  301. <h1>project-2</h1>
  302. <p>Мой первый суперский репозиторий</p>
  303. </section>''', output.data if six.PY2 else output.get_data(as_text=True))
  304. # After
  305. projects = pagure.lib.search_projects(self.session)
  306. self.assertEqual(len(projects), 2)
  307. for project in ['project-1', 'project-2']:
  308. self.assertTrue(os.path.exists(
  309. os.path.join(self.path, '%s.git' % project)))
  310. self.assertTrue(os.path.exists(
  311. os.path.join(self.path, 'tickets', '%s.git' % project)))
  312. self.assertTrue(os.path.exists(
  313. os.path.join(self.path, 'docs', '%s.git' % project)))
  314. self.assertTrue(os.path.exists(
  315. os.path.join(self.path, 'requests', '%s.git' % project)))
  316. @patch('pagure.ui.app.admin_session_timedout')
  317. def test_user_settings(self, ast):
  318. """ Test the user_settings endpoint. """
  319. ast.return_value = False
  320. self.test_new_project()
  321. user = tests.FakeUser()
  322. with tests.user_set(pagure.APP, user):
  323. output = self.app.get('/settings/')
  324. self.assertEqual(output.status_code, 404)
  325. self.assertIn(b'<h2>Page not found (404)</h2>', output.data)
  326. user.username = 'foo'
  327. with tests.user_set(pagure.APP, user):
  328. output = self.app.get('/settings/')
  329. self.assertEqual(output.status_code, 200)
  330. self.assertIn(
  331. b'<div class="card-header">\n Basic Information\n'
  332. b' </div>', output.data)
  333. self.assertIn(
  334. b'<textarea class="form-control" id="ssh_key" name="ssh_key">'
  335. b'</textarea>', output.data)
  336. csrf_token = output.get_data(as_text=True).split(
  337. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  338. data = {
  339. 'ssh_key': 'blah'
  340. }
  341. output = self.app.post('/settings/', data=data)
  342. self.assertEqual(output.status_code, 200)
  343. self.assertIn(
  344. '<div class="card-header">\n Basic Information\n'
  345. ' </div>', output.get_data(as_text=True))
  346. data['csrf_token'] = csrf_token
  347. output = self.app.post(
  348. '/settings/', data=data, follow_redirects=True)
  349. self.assertEqual(output.status_code, 200)
  350. self.assertIn(b'Invalid SSH keys', output.data)
  351. self.assertIn(
  352. b'<div class="card-header">\n Basic Information\n'
  353. b' </div>', output.data)
  354. self.assertIn(b'>blah</textarea>', output.data)
  355. csrf_token = output.data.split(
  356. b'name="csrf_token" type="hidden" value="')[1].split(b'">')[0]
  357. data = {
  358. 'ssh_key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDUkub32fZnNI'
  359. '1zJYs43vhhx3c6IcYo4yzhw1gQ37BLhrrNeS6x8l5PKX4J8ZP5'
  360. '1XhViPaLbeOpl94Vm5VSCbLy0xtY9KwLhMkbKj7g6vvfxLm2sT'
  361. 'Osb15j4jzIkUYYgIE7cHhZMCLWR6UA1c1HEzo6mewMDsvpQ9wk'
  362. 'cDnAuXjK3Q==',
  363. 'csrf_token': csrf_token
  364. }
  365. output = self.app.post(
  366. '/settings/', data=data, follow_redirects=True)
  367. self.assertEqual(output.status_code, 200)
  368. self.assertIn(b'Public ssh key updated', output.data)
  369. self.assertIn(
  370. b'<textarea class="form-control" id="ssh_key" name="ssh_key">'
  371. b'ssh-rsa AAAA', output.data)
  372. ast.return_value = True
  373. output = self.app.get('/settings/')
  374. self.assertEqual(output.status_code, 302)
  375. def test_markdown_preview(self):
  376. """ Test the markdown_preview endpoint. """
  377. data = {
  378. 'content': 'test\n----\n\n * 1\n * item 2'
  379. }
  380. # CSRF missing
  381. output = self.app.post('/markdown/', data=data)
  382. self.assertEqual(output.status_code, 400)
  383. user = tests.FakeUser()
  384. user.username = 'foo'
  385. with tests.user_set(pagure.APP, user):
  386. output = self.app.get('/settings/')
  387. self.assertEqual(output.status_code, 200)
  388. self.assertIn(
  389. '<div class="card-header">\n Basic Information\n'
  390. ' </div>', output.get_data(as_text=True))
  391. self.assertIn(
  392. '<textarea class="form-control" id="ssh_key" name="ssh_key">'
  393. '</textarea>', output.get_data(as_text=True))
  394. csrf_token = output.get_data(as_text=True).split(
  395. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  396. # With CSRF
  397. data['csrf_token'] = csrf_token
  398. output = self.app.post('/markdown/', data=data)
  399. self.assertEqual(output.status_code, 200)
  400. exp = """<h2>test</h2>
  401. <ul>
  402. <li>1</li>
  403. <li>item 2</li>
  404. </ul>"""
  405. self.assertEqual(output.get_data(as_text=True), exp)
  406. @patch('pagure.ui.app.admin_session_timedout')
  407. def test_remove_user_email(self, ast):
  408. """ Test the remove_user_email endpoint. """
  409. ast.return_value = False
  410. self.test_new_project()
  411. user = tests.FakeUser()
  412. with tests.user_set(pagure.APP, user):
  413. output = self.app.post('/settings/email/drop')
  414. self.assertEqual(output.status_code, 404)
  415. self.assertIn(b'<h2>Page not found (404)</h2>', output.data)
  416. user.username = 'foo'
  417. with tests.user_set(pagure.APP, user):
  418. output = self.app.post('/settings/')
  419. self.assertEqual(output.status_code, 200)
  420. self.assertIn(
  421. b'<div class="card-header">\n Basic Information\n'
  422. b' </div>', output.data)
  423. self.assertIn(
  424. b'<textarea class="form-control form-control-error" id="ssh_key" name="ssh_key">'
  425. b'</textarea>', output.data)
  426. csrf_token = output.get_data(as_text=True).split(
  427. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  428. data = {
  429. 'email': 'foo@pingou.com',
  430. }
  431. output = self.app.post(
  432. '/settings/email/drop', data=data, follow_redirects=True)
  433. self.assertEqual(output.status_code, 200)
  434. self.assertIn(
  435. b'<div class="card-header">\n Basic Information\n'
  436. b' </div>', output.data)
  437. self.assertIn(
  438. b'<textarea class="form-control" id="ssh_key" name="ssh_key">'
  439. b'</textarea>', output.data)
  440. self.assertIn(
  441. b'</button>\n You must always have '
  442. b'at least one email', output.data)
  443. user.username = 'pingou'
  444. with tests.user_set(pagure.APP, user):
  445. output = self.app.post('/settings/')
  446. self.assertEqual(output.status_code, 200)
  447. self.assertIn(
  448. b'<div class="card-header">\n Basic Information\n'
  449. b' </div>', output.data)
  450. self.assertIn(
  451. b'<textarea class="form-control form-control-error" id="ssh_key" name="ssh_key">'
  452. b'</textarea>', output.data)
  453. csrf_token = output.get_data(as_text=True).split(
  454. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  455. data = {
  456. 'email': 'foo@pingou.com',
  457. }
  458. output = self.app.post(
  459. '/settings/email/drop', data=data, follow_redirects=True)
  460. self.assertEqual(output.status_code, 200)
  461. self.assertIn(
  462. b'<div class="card-header">\n Basic Information\n'
  463. b' </div>', output.data)
  464. self.assertEqual(output.data.count(b'foo@pingou.com'), 4)
  465. data = {
  466. 'csrf_token': csrf_token,
  467. 'email': 'foobar@pingou.com',
  468. }
  469. output = self.app.post(
  470. '/settings/email/drop', data=data, follow_redirects=True)
  471. self.assertEqual(output.status_code, 200)
  472. self.assertIn(
  473. b'<div class="card-header">\n Basic Information\n'
  474. b' </div>', output.data)
  475. self.assertIn(
  476. b'</button>\n You do not have the '
  477. b'email: foobar@pingou.com, nothing to remove', output.data)
  478. data = {
  479. 'csrf_token': csrf_token,
  480. 'email': 'foo@pingou.com',
  481. }
  482. output = self.app.post(
  483. '/settings/email/drop', data=data, follow_redirects=True)
  484. self.assertEqual(output.status_code, 200)
  485. self.assertEqual(output.data.count(b'foo@pingou.com'), 0)
  486. self.assertEqual(output.data.count(b'bar@pingou.com'), 3)
  487. output = self.app.post(
  488. '/settings/email/drop', data=data, follow_redirects=True)
  489. self.assertEqual(output.status_code, 200)
  490. self.assertEqual(output.data.count(b'foo@pingou.com'), 0)
  491. self.assertEqual(output.data.count(b'bar@pingou.com'), 3)
  492. ast.return_value = True
  493. output = self.app.post('/settings/email/drop', data=data)
  494. self.assertEqual(output.status_code, 302)
  495. @patch('pagure.lib.notify.send_email')
  496. @patch('pagure.ui.app.admin_session_timedout')
  497. def test_add_user_email(self, ast, send_email):
  498. """ Test the add_user_email endpoint. """
  499. send_email.return_value = True
  500. ast.return_value = False
  501. self.test_new_project()
  502. user = tests.FakeUser()
  503. with tests.user_set(pagure.APP, user):
  504. output = self.app.post('/settings/email/add')
  505. self.assertEqual(output.status_code, 404)
  506. self.assertIn(b'<h2>Page not found (404)</h2>', output.data)
  507. user.username = 'foo'
  508. with tests.user_set(pagure.APP, user):
  509. output = self.app.post('/settings/email/add')
  510. self.assertEqual(output.status_code, 200)
  511. self.assertIn(b"<strong>Add new email</strong>", output.data)
  512. self.assertIn(
  513. b'<input class="form-control form-control-error" id="email" '
  514. b'name="email" type="text" value="">', output.data)
  515. user.username = 'pingou'
  516. with tests.user_set(pagure.APP, user):
  517. output = self.app.post('/settings/email/add')
  518. self.assertEqual(output.status_code, 200)
  519. self.assertIn(b"<strong>Add new email</strong>", output.data)
  520. self.assertIn(
  521. b'<input class="form-control form-control-error" id="email" '
  522. b'name="email" type="text" value="">', output.data)
  523. csrf_token = output.get_data(as_text=True).split(
  524. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  525. data = {
  526. 'email': 'foo2@pingou.com',
  527. }
  528. output = self.app.post(
  529. '/settings/email/add', data=data, follow_redirects=True)
  530. self.assertEqual(output.status_code, 200)
  531. self.assertIn(b"<strong>Add new email</strong>", output.data)
  532. self.assertEqual(output.data.count(b'foo2@pingou.com'), 1)
  533. # New email
  534. data = {
  535. 'csrf_token': csrf_token,
  536. 'email': 'foobar@pingou.com',
  537. }
  538. output = self.app.post(
  539. '/settings/email/add', data=data, follow_redirects=True)
  540. self.assertEqual(output.status_code, 200)
  541. self.assertIn(
  542. b'<div class="card-header">\n Basic Information\n'
  543. b' </div>', output.data)
  544. self.assertIn(
  545. b'</button>\n Email pending validation',
  546. output.data)
  547. self.assertEqual(output.data.count(b'foo@pingou.com'), 4)
  548. self.assertEqual(output.data.count(b'bar@pingou.com'), 5)
  549. self.assertEqual(output.data.count(b'foobar@pingou.com'), 2)
  550. # Email already pending
  551. output = self.app.post(
  552. '/settings/email/add', data=data, follow_redirects=True)
  553. self.assertEqual(output.status_code, 200)
  554. self.assertIn(
  555. b'<div class="card-header">\n '
  556. b'<strong>Add new email</strong>', output.data)
  557. self.assertIn(
  558. b'</button>\n This email is already '
  559. b'pending confirmation', output.data)
  560. # User already has this email
  561. data = {
  562. 'csrf_token': csrf_token,
  563. 'email': 'foo@pingou.com',
  564. }
  565. output = self.app.post(
  566. '/settings/email/add', data=data, follow_redirects=True)
  567. self.assertEqual(output.status_code, 200)
  568. self.assertIn(
  569. b"<strong>Add new email</strong>", output.data)
  570. self.assertTrue(
  571. b'Invalid value, can&#39;t be any of: bar@pingou.com, '
  572. b'foo@pingou.com.&nbsp;' in output.data
  573. or
  574. b'Invalid value, can&#39;t be any of: foo@pingou.com, '
  575. b'bar@pingou.com.&nbsp;' in output.data
  576. )
  577. self.assertEqual(output.data.count(b'foo@pingou.com'), 6)
  578. self.assertEqual(output.data.count(b'bar@pingou.com'), 5)
  579. self.assertEqual(output.data.count(b'foobar@pingou.com'), 0)
  580. # Email registered by someone else
  581. data = {
  582. 'csrf_token': csrf_token,
  583. 'email': 'foo@bar.com',
  584. }
  585. output = self.app.post(
  586. '/settings/email/add', data=data, follow_redirects=True)
  587. self.assertEqual(output.status_code, 200)
  588. self.assertIn(b"<strong>Add new email</strong>", output.data)
  589. self.assertIn(
  590. b'Invalid value, can&#39;t be any of: foo@bar.com.&nbsp;',
  591. output.data)
  592. ast.return_value = True
  593. output = self.app.post('/settings/email/add', data=data)
  594. self.assertEqual(output.status_code, 302)
  595. @patch('pagure.lib.notify.send_email')
  596. @patch('pagure.ui.app.admin_session_timedout')
  597. def test_set_default_email(self, ast, send_email):
  598. """ Test the set_default_email endpoint. """
  599. send_email.return_value = True
  600. ast.return_value = False
  601. self.test_new_project()
  602. user = tests.FakeUser()
  603. with tests.user_set(pagure.APP, user):
  604. output = self.app.post('/settings/email/default')
  605. self.assertEqual(output.status_code, 404)
  606. self.assertIn(b'<h2>Page not found (404)</h2>', output.data)
  607. user.username = 'pingou'
  608. with tests.user_set(pagure.APP, user):
  609. output = self.app.get('/settings/')
  610. self.assertEqual(output.status_code, 200)
  611. self.assertIn(
  612. b'<div class="card-header">\n Basic Information\n'
  613. b' </div>', output.data)
  614. self.assertIn(
  615. b'<textarea class="form-control" id="ssh_key" name="ssh_key">'
  616. b'</textarea>', output.data)
  617. csrf_token = output.get_data(as_text=True).split(
  618. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  619. data = {
  620. 'email': 'foo@pingou.com',
  621. }
  622. output = self.app.post(
  623. '/settings/email/default', data=data, follow_redirects=True)
  624. self.assertEqual(output.status_code, 200)
  625. self.assertIn(
  626. b'<div class="card-header">\n Basic Information\n'
  627. b' </div>', output.data)
  628. self.assertEqual(output.data.count(b'foo@pingou.com'), 4)
  629. # Set invalid default email
  630. data = {
  631. 'csrf_token': csrf_token,
  632. 'email': 'foobar@pingou.com',
  633. }
  634. output = self.app.post(
  635. '/settings/email/default', data=data, follow_redirects=True)
  636. self.assertEqual(output.status_code, 200)
  637. self.assertIn(
  638. b'<div class="card-header">\n Basic Information\n'
  639. b' </div>', output.data)
  640. self.assertEqual(output.data.count(b'foo@pingou.com'), 4)
  641. self.assertIn(
  642. b'</button>\n You do not have the '
  643. b'email: foobar@pingou.com, nothing to set',
  644. output.data)
  645. # Set default email
  646. data = {
  647. 'csrf_token': csrf_token,
  648. 'email': 'foo@pingou.com',
  649. }
  650. output = self.app.post(
  651. '/settings/email/default', data=data, follow_redirects=True)
  652. self.assertEqual(output.status_code, 200)
  653. self.assertIn(
  654. b'<div class="card-header">\n Basic Information\n'
  655. b' </div>', output.data)
  656. self.assertEqual(output.data.count(b'foo@pingou.com'), 4)
  657. self.assertIn(
  658. b'</button>\n Default email set to: '
  659. b'foo@pingou.com', output.data)
  660. ast.return_value = True
  661. output = self.app.post('/settings/email/default', data=data)
  662. self.assertEqual(output.status_code, 302)
  663. @patch('pagure.lib.notify.send_email')
  664. @patch('pagure.ui.app.admin_session_timedout')
  665. def test_reconfirm_email(self, ast, send_email):
  666. """ Test the reconfirm_email endpoint. """
  667. send_email.return_value = True
  668. ast.return_value = False
  669. self.test_new_project()
  670. # Add a pending email to pingou
  671. userobj = pagure.lib.search_user(self.session, username='pingou')
  672. self.assertEqual(len(userobj.emails), 2)
  673. email_pend = pagure.lib.model.UserEmailPending(
  674. user_id=userobj.id,
  675. email='foo@fp.o',
  676. token='abcdef',
  677. )
  678. self.session.add(email_pend)
  679. self.session.commit()
  680. user = tests.FakeUser()
  681. with tests.user_set(pagure.APP, user):
  682. output = self.app.post('/settings/email/resend')
  683. self.assertEqual(output.status_code, 404)
  684. self.assertIn(b'<h2>Page not found (404)</h2>', output.data)
  685. user.username = 'pingou'
  686. with tests.user_set(pagure.APP, user):
  687. output = self.app.get('/settings/')
  688. self.assertEqual(output.status_code, 200)
  689. self.assertIn(
  690. b'<div class="card-header">\n Basic Information\n'
  691. b' </div>', output.data)
  692. self.assertIn(
  693. b'<textarea class="form-control" id="ssh_key" name="ssh_key">'
  694. b'</textarea>', output.data)
  695. csrf_token = output.get_data(as_text=True).split(
  696. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  697. data = {
  698. 'email': 'foo@pingou.com',
  699. }
  700. output = self.app.post(
  701. '/settings/email/resend', data=data, follow_redirects=True)
  702. self.assertEqual(output.status_code, 200)
  703. self.assertIn(
  704. '<div class="card-header">\n Basic Information\n'
  705. ' </div>', output.get_data(as_text=True))
  706. self.assertEqual(output.get_data(as_text=True).count('foo@pingou.com'), 4)
  707. # Set invalid default email
  708. data = {
  709. 'csrf_token': csrf_token,
  710. 'email': 'foobar@pingou.com',
  711. }
  712. output = self.app.post(
  713. '/settings/email/resend', data=data, follow_redirects=True)
  714. self.assertEqual(output.status_code, 200)
  715. self.assertIn(
  716. b'<div class="card-header">\n Basic Information\n'
  717. b' </div>', output.data)
  718. self.assertEqual(output.get_data(as_text=True).count('foo@pingou.com'), 4)
  719. self.assertIn(
  720. b'</button>\n This email address has '
  721. b'already been confirmed', output.data)
  722. # Validate a non-validated email
  723. data = {
  724. 'csrf_token': csrf_token,
  725. 'email': 'foo@fp.o',
  726. }
  727. output = self.app.post(
  728. '/settings/email/resend', data=data, follow_redirects=True)
  729. self.assertEqual(output.status_code, 200)
  730. self.assertIn(
  731. b'<div class="card-header">\n Basic Information\n'
  732. b' </div>', output.data)
  733. self.assertEqual(output.data.count(b'foo@pingou.com'), 4)
  734. self.assertIn(
  735. b'</button>\n Confirmation email re-sent',
  736. output.data)
  737. ast.return_value = True
  738. output = self.app.post('/settings/email/resend', data=data)
  739. self.assertEqual(output.status_code, 302)
  740. @patch('pagure.ui.app.admin_session_timedout')
  741. def test_confirm_email(self, ast):
  742. """ Test the confirm_email endpoint. """
  743. output = self.app.get('/settings/email/confirm/foobar')
  744. self.assertEqual(output.status_code, 302)
  745. ast.return_value = False
  746. # Add a pending email to pingou
  747. userobj = pagure.lib.search_user(self.session, username='pingou')
  748. self.assertEqual(len(userobj.emails), 2)
  749. email_pend = pagure.lib.model.UserEmailPending(
  750. user_id=userobj.id,
  751. email='foo@fp.o',
  752. token='abcdef',
  753. )
  754. self.session.add(email_pend)
  755. self.session.commit()
  756. user = tests.FakeUser()
  757. user.username = 'pingou'
  758. with tests.user_set(pagure.APP, user):
  759. # Wrong token
  760. output = self.app.get(
  761. '/settings/email/confirm/foobar', follow_redirects=True)
  762. self.assertEqual(output.status_code, 200)
  763. self.assertIn(
  764. b'<div class="card-header">\n Basic Information\n'
  765. b' </div>', output.data)
  766. self.assertIn(
  767. b'</button>\n No email associated with this token.',
  768. output.data)
  769. # Confirm email
  770. output = self.app.get(
  771. '/settings/email/confirm/abcdef', follow_redirects=True)
  772. self.assertEqual(output.status_code, 200)
  773. self.assertIn(
  774. b'<div class="card-header">\n Basic Information\n'
  775. b' </div>', output.data)
  776. self.assertIn(
  777. b'</button>\n Email validated',
  778. output.data)
  779. userobj = pagure.lib.search_user(self.session, username='pingou')
  780. self.assertEqual(len(userobj.emails), 3)
  781. ast.return_value = True
  782. output = self.app.get('/settings/email/confirm/foobar')
  783. self.assertEqual(output.status_code, 302)
  784. if __name__ == '__main__':
  785. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskApptests)
  786. unittest.TextTestRunner(verbosity=2).run(SUITE)