test_pagure_flask_ui_app.py 34 KB

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