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