test_pagure_flask_ui_groups.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import shutil
  10. import sys
  11. import os
  12. import json
  13. from mock import patch
  14. sys.path.insert(0, os.path.join(os.path.dirname(
  15. os.path.abspath(__file__)), '..'))
  16. import pagure.config
  17. import tests
  18. class PagureFlaskGroupstests(tests.Modeltests):
  19. """ Tests for flask groups controller of pagure """
  20. def test_group_lists(self):
  21. """ Test the group_lists endpoint. """
  22. output = self.app.get('/groups')
  23. self.assertIn(
  24. '<h3 class="font-weight-bold">\n'
  25. ' Groups <span class="badge badge-secondary">0</span>',
  26. output.get_data(as_text=True))
  27. def test_add_group(self):
  28. """ Test the add_group endpoint. """
  29. output = self.app.get('/group/add')
  30. self.assertEqual(output.status_code, 302)
  31. user = tests.FakeUser()
  32. with tests.user_set(self.app.application, user):
  33. output = self.app.get('/group/add')
  34. self.assertEqual(output.status_code, 403)
  35. user.username = 'pingou'
  36. with tests.user_set(self.app.application, user):
  37. output = self.app.get('/group/add')
  38. self.assertEqual(output.status_code, 200)
  39. self.assertIn('<strong>Create new group</strong>', output.get_data(as_text=True))
  40. self.assertNotIn(
  41. '<option value="admin">admin</option>', output.get_data(as_text=True))
  42. csrf_token = output.get_data(as_text=True).split(
  43. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  44. data = {
  45. }
  46. # Insufficient input
  47. output = self.app.post('/group/add', data=data)
  48. self.assertEqual(output.status_code, 200)
  49. self.assertIn('<strong>Create new group</strong>', output.get_data(as_text=True))
  50. self.assertEqual(output.get_data(as_text=True).count(
  51. 'This field is required.'), 3)
  52. data = {
  53. 'group_name': 'test_group',
  54. 'display_name': 'Test Group',
  55. 'description': 'This is a group for the tests',
  56. }
  57. # Missing CSRF
  58. output = self.app.post('/group/add', data=data)
  59. self.assertEqual(output.status_code, 200)
  60. self.assertIn('<strong>Create new group</strong>', output.get_data(as_text=True))
  61. self.assertEqual(output.get_data(as_text=True).count(
  62. 'This field is required.'), 0)
  63. data['csrf_token'] = csrf_token
  64. # All good
  65. output = self.app.post(
  66. '/group/add', data=data, follow_redirects=True)
  67. self.assertEqual(output.status_code, 200)
  68. self.assertIn(
  69. 'User `pingou` added to '
  70. 'the group `test_group`.', output.get_data(as_text=True))
  71. self.assertIn(
  72. 'Group `test_group` created.',
  73. output.get_data(as_text=True))
  74. self.assertIn(
  75. '<h3 class="font-weight-bold">\n'
  76. ' Groups <span class="badge badge-secondary">1</span>',
  77. output.get_data(as_text=True))
  78. user = tests.FakeUser(
  79. username='pingou',
  80. groups=pagure.config.config['ADMIN_GROUP'])
  81. with tests.user_set(self.app.application, user):
  82. output = self.app.get('/group/add')
  83. self.assertEqual(output.status_code, 200)
  84. self.assertIn('<strong>Create new group</strong>', output.get_data(as_text=True))
  85. self.assertIn('<option value="admin">admin</option>', output.get_data(as_text=True))
  86. data = {
  87. 'group_name': 'test_admin_group',
  88. 'group_type': 'admin',
  89. 'display_name': 'Test Admin Group',
  90. 'description': 'This is another group for the tests',
  91. 'csrf_token': csrf_token,
  92. }
  93. # All good
  94. output = self.app.post(
  95. '/group/add', data=data, follow_redirects=True)
  96. self.assertEqual(output.status_code, 200)
  97. self.assertIn(
  98. 'User `pingou` added to '
  99. 'the group `test_admin_group`.', output.get_data(as_text=True))
  100. self.assertIn(
  101. 'Group `test_admin_group` '
  102. 'created.',output.get_data(as_text=True))
  103. self.assertIn(
  104. '<h3 class="font-weight-bold">\n'
  105. ' Groups <span class="badge badge-secondary">2</span>',
  106. output.get_data(as_text=True))
  107. def test_edit_group(self):
  108. """ Test the edit_group endpoint. """
  109. output = self.app.get('/group/test_group/edit')
  110. self.assertEqual(output.status_code, 302)
  111. user = tests.FakeUser()
  112. with tests.user_set(self.app.application, user):
  113. output = self.app.get('/group/test_group/edit')
  114. self.assertEqual(output.status_code, 404)
  115. self.assertIn('<p>Group not found</p>', output.get_data(as_text=True))
  116. self.test_add_group()
  117. user.username = 'foo'
  118. with tests.user_set(self.app.application, user):
  119. output = self.app.get('/group/foo/edit')
  120. self.assertEqual(output.status_code, 404)
  121. self.assertIn('<p>Group not found</p>', output.get_data(as_text=True))
  122. output = self.app.get('/group/test_group/edit')
  123. self.assertEqual(output.status_code, 200)
  124. self.assertIn(
  125. '<title>Edit group: test_group - Pagure</title>',
  126. output.get_data(as_text=True))
  127. self.assertIn(
  128. '<form action="/group/test_group/edit" method="post">',
  129. output.get_data(as_text=True))
  130. self.assertIn(
  131. '<strong><label for="description">Description'
  132. '</label></strong>', output.get_data(as_text=True))
  133. csrf_token = output.get_data(as_text=True).split(
  134. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  135. # Missing CSRF
  136. data = {
  137. 'group_name': 'test_group',
  138. 'display_name': 'Test Group edited',
  139. 'description': 'This is a group for the tests edited',
  140. }
  141. output = self.app.post(
  142. '/group/test_group/edit', data=data, follow_redirects=True)
  143. self.assertEqual(output.status_code, 200)
  144. self.assertIn(
  145. '<title>Edit group: test_group - Pagure</title>',
  146. output.get_data(as_text=True))
  147. self.assertIn(
  148. '<form action="/group/test_group/edit" method="post">',
  149. output.get_data(as_text=True))
  150. self.assertIn(
  151. '<strong><label for="description">Description'
  152. '</label></strong>', output.get_data(as_text=True))
  153. # User not allowed
  154. data['csrf_token'] = csrf_token
  155. output = self.app.post(
  156. '/group/test_group/edit', data=data, follow_redirects=True)
  157. self.assertEqual(output.status_code, 200)
  158. self.assertIn(
  159. '<title>Group test_group - Pagure</title>',
  160. output.get_data(as_text=True))
  161. self.assertIn(
  162. 'You are not '
  163. 'allowed to edit this group', output.get_data(as_text=True))
  164. self.assertIn(
  165. '<h3 class="mb-0 font-weight-bold">Test Group</h3>',
  166. output.get_data(as_text=True))
  167. user.username = 'pingou'
  168. with tests.user_set(self.app.application, user):
  169. # Invalid repo
  170. output = self.app.post(
  171. '/group/bar/edit', data=data, follow_redirects=True)
  172. self.assertEqual(output.status_code, 404)
  173. self.assertIn('<p>Group not found</p>', output.get_data(as_text=True))
  174. output = self.app.post(
  175. '/group/test_group/edit', data=data, follow_redirects=True)
  176. self.assertEqual(output.status_code, 200)
  177. self.assertIn(
  178. '<title>Group test_group - Pagure</title>', output.get_data(as_text=True))
  179. self.assertIn(
  180. '<h3 class="mb-0 font-weight-bold">Test Group edited</h3>',
  181. output.get_data(as_text=True))
  182. self.assertIn(
  183. 'Group &#34;Test Group edited&#34; (test_group) edited',
  184. output.get_data(as_text=True))
  185. def test_give_group(self):
  186. """ Test the give_group endpoint. """
  187. output = self.app.post('/group/test_group/give')
  188. self.assertEqual(output.status_code, 302)
  189. user = tests.FakeUser()
  190. with tests.user_set(self.app.application, user):
  191. output = self.app.post('/group/test_group/give')
  192. self.assertEqual(output.status_code, 404)
  193. self.assertIn('<p>Group not found</p>', output.get_data(as_text=True))
  194. self.test_add_group()
  195. user.username = 'foo'
  196. with tests.user_set(self.app.application, user):
  197. output = self.app.post('/group/foo/give')
  198. self.assertEqual(output.status_code, 404)
  199. self.assertIn('<p>Group not found</p>', output.get_data(as_text=True))
  200. output = self.app.post('/group/test_group/give')
  201. self.assertEqual(output.status_code, 403)
  202. csrf_token = self.get_csrf()
  203. user.username = 'pingou'
  204. with tests.user_set(self.app.application, user):
  205. # Missing CSRF
  206. data = {
  207. 'username': 'invalid',
  208. }
  209. output = self.app.post(
  210. '/group/test_group/give', data=data, follow_redirects=True)
  211. self.assertEqual(output.status_code, 200)
  212. output_text = output.get_data(as_text=True)
  213. self.assertIn(
  214. '<title>Group test_group - Pagure</title>', output_text)
  215. self.assertIn(
  216. 'administered by <a href="/user/pingou">pingou</a>',
  217. output_text)
  218. # User not found
  219. data['csrf_token'] = csrf_token
  220. output = self.app.post(
  221. '/group/test_group/give', data=data, follow_redirects=True)
  222. self.assertEqual(output.status_code, 200)
  223. output_text = output.get_data(as_text=True)
  224. self.assertIn(
  225. '<title>Group test_group - Pagure</title>', output_text)
  226. self.assertIn(
  227. '</i> No user invalid found to give this group to</div>',
  228. output_text)
  229. self.assertIn(
  230. 'administered by <a href="/user/pingou">pingou</a>',
  231. output_text)
  232. # Working
  233. data["username"] = "foo"
  234. output = self.app.post(
  235. '/group/test_group/give', data=data, follow_redirects=True)
  236. self.assertEqual(output.status_code, 200)
  237. output_text = output.get_data(as_text=True)
  238. self.assertIn(
  239. '<title>Group test_group - Pagure</title>', output_text)
  240. self.assertIn('</i> Group given</div>', output_text)
  241. self.assertIn(
  242. 'administered by <a href="/user/foo">foo</a>', output_text)
  243. def test_group_delete(self):
  244. """ Test the group_delete endpoint. """
  245. output = self.app.post('/group/foo/delete')
  246. self.assertEqual(output.status_code, 302)
  247. user = tests.FakeUser()
  248. with tests.user_set(self.app.application, user):
  249. output = self.app.post('/group/foo/delete', follow_redirects=True)
  250. self.assertEqual(output.status_code, 200)
  251. self.assertIn(
  252. '<p>No groups have been created on this pagure instance '
  253. 'yet</p>', output.get_data(as_text=True))
  254. self.assertIn(
  255. '<h3 class="font-weight-bold">\n'
  256. ' Groups <span class="badge badge-secondary">0</span>',
  257. output.get_data(as_text=True))
  258. self.test_add_group()
  259. with tests.user_set(self.app.application, user):
  260. output = self.app.post('/group/foo/delete', follow_redirects=True)
  261. self.assertEqual(output.status_code, 200)
  262. self.assertIn(
  263. '<h3 class="font-weight-bold">\n'
  264. ' Groups <span class="badge badge-secondary">1</span>',
  265. output.get_data(as_text=True))
  266. output = self.app.get('/new/')
  267. csrf_token = output.get_data(as_text=True).split(
  268. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  269. user.username = 'foo'
  270. with tests.user_set(self.app.application, user):
  271. data = {
  272. 'csrf_token': csrf_token,
  273. }
  274. output = self.app.post(
  275. '/group/bar/delete', data=data, follow_redirects=True)
  276. self.assertEqual(output.status_code, 200)
  277. self.assertIn(
  278. 'No group `bar` found',
  279. output.get_data(as_text=True))
  280. self.assertIn(
  281. '<h3 class="font-weight-bold">\n'
  282. ' Groups <span class="badge badge-secondary">1</span>',
  283. output.get_data(as_text=True))
  284. output = self.app.post(
  285. '/group/test_group/delete', data=data, follow_redirects=True)
  286. self.assertEqual(output.status_code, 200)
  287. self.assertIn(
  288. 'You are not allowed to '
  289. 'delete the group test_group', output.get_data(as_text=True))
  290. self.assertIn(
  291. '<h3 class="font-weight-bold">\n'
  292. ' Groups <span class="badge badge-secondary">1</span>',
  293. output.get_data(as_text=True))
  294. user.username = 'bar'
  295. with tests.user_set(self.app.application, user):
  296. output = self.app.post(
  297. '/group/test_group/delete', data=data, follow_redirects=True)
  298. self.assertEqual(output.status_code, 404)
  299. user.username = 'pingou'
  300. with tests.user_set(self.app.application, user):
  301. output = self.app.post(
  302. '/group/test_group/delete', data=data, follow_redirects=True)
  303. self.assertEqual(output.status_code, 200)
  304. self.assertIn(
  305. 'Group `test_group` has '
  306. 'been deleted', output.get_data(as_text=True))
  307. self.assertIn(
  308. '<h3 class="font-weight-bold">\n'
  309. ' Groups <span class="badge badge-secondary">0</span>',
  310. output.get_data(as_text=True))
  311. def test_view_group(self):
  312. """ Test the view_group endpoint. """
  313. output = self.app.get('/group/foo')
  314. self.assertEqual(output.status_code, 404)
  315. self.test_add_group()
  316. user = tests.FakeUser()
  317. with tests.user_set(self.app.application, user):
  318. output = self.app.get('/group/test_group')
  319. self.assertEqual(output.status_code, 200)
  320. self.assertIn(
  321. '<h3 class="mb-0 font-weight-bold">Test Group</h3>',
  322. output.get_data(as_text=True))
  323. output = self.app.get('/group/test_admin_group')
  324. self.assertEqual(output.status_code, 404)
  325. user = tests.FakeUser(
  326. username='pingou',
  327. groups=pagure.config.config['ADMIN_GROUP'])
  328. with tests.user_set(self.app.application, user):
  329. # Admin can see group of type admins
  330. output = self.app.get('/group/test_admin_group')
  331. self.assertEqual(output.status_code, 200)
  332. self.assertIn(
  333. '<h3 class="mb-0 font-weight-bold">Test Admin Group</h3>',
  334. output.get_data(as_text=True))
  335. self.assertEqual(
  336. output.get_data(as_text=True).count('<a href="/user/'), 2)
  337. csrf_token = output.get_data(as_text=True).split(
  338. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  339. # No CSRF
  340. data = {
  341. 'user': 'bar'
  342. }
  343. output = self.app.post('/group/test_admin_group', data=data)
  344. self.assertEqual(output.status_code, 200)
  345. self.assertIn(
  346. '<h3 class="mb-0 font-weight-bold">Test Admin Group</h3>',
  347. output.get_data(as_text=True))
  348. self.assertEqual(
  349. output.get_data(as_text=True).count('<a href="/user/'), 2)
  350. # Invalid user
  351. data = {
  352. 'user': 'bar',
  353. 'csrf_token': csrf_token,
  354. }
  355. output = self.app.post(
  356. '/group/test_admin_group', data=data, follow_redirects=True)
  357. self.assertEqual(output.status_code, 200)
  358. self.assertIn(
  359. 'No user `bar` found',
  360. output.get_data(as_text=True))
  361. self.assertIn(
  362. '<h3 class="mb-0 font-weight-bold">Test Admin Group</h3>',
  363. output.get_data(as_text=True))
  364. self.assertEqual(
  365. output.get_data(as_text=True).count('<a href="/user/'), 2)
  366. # All good
  367. data = {
  368. 'user': 'foo',
  369. 'csrf_token': csrf_token,
  370. }
  371. output = self.app.post('/group/test_admin_group', data=data)
  372. self.assertEqual(output.status_code, 200)
  373. self.assertIn(
  374. 'User `foo` added to the '
  375. 'group `test_admin_group`.', output.get_data(as_text=True))
  376. self.assertIn(
  377. '<h3 class="mb-0 font-weight-bold">Test Admin Group</h3>',
  378. output.get_data(as_text=True))
  379. self.assertEqual(
  380. output.get_data(as_text=True).count('<a href="/user/'), 3)
  381. def test_group_user_delete(self):
  382. """ Test the group_user_delete endpoint. """
  383. output = self.app.post('/group/foo/bar/delete')
  384. self.assertEqual(output.status_code, 302)
  385. user = tests.FakeUser()
  386. with tests.user_set(self.app.application, user):
  387. output = self.app.post(
  388. '/group/foo/bar/delete', follow_redirects=True)
  389. self.assertEqual(output.status_code, 404)
  390. self.test_add_group()
  391. user = tests.FakeUser()
  392. with tests.user_set(self.app.application, user):
  393. output = self.app.post(
  394. '/group/test_group/bar/delete', follow_redirects=True)
  395. self.assertEqual(output.status_code, 200)
  396. self.assertIn(
  397. '<h3 class="mb-0 font-weight-bold">Test Group</h3>',
  398. output.get_data(as_text=True))
  399. self.assertEqual(
  400. output.get_data(as_text=True).count('<a href="/user/'), 2)
  401. output = self.app.get('/new/')
  402. csrf_token = output.get_data(as_text=True).split(
  403. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  404. data = {'csrf_token': csrf_token}
  405. output = self.app.post(
  406. '/group/test_group/bar/delete', data=data, follow_redirects=True)
  407. self.assertEqual(output.status_code, 200)
  408. self.assertIn(
  409. 'No user `bar` found',
  410. output.get_data(as_text=True))
  411. self.assertIn(
  412. '<h3 class="mb-0 font-weight-bold">Test Group</h3>',
  413. output.get_data(as_text=True))
  414. self.assertEqual(
  415. output.get_data(as_text=True).count('<a href="/user/'), 2)
  416. output = self.app.post(
  417. '/group/test_group/foo/delete', data=data, follow_redirects=True)
  418. self.assertEqual(output.status_code, 200)
  419. self.assertIn(
  420. 'Could not find user '
  421. 'username', output.get_data(as_text=True))
  422. self.assertIn(
  423. '<h3 class="mb-0 font-weight-bold">Test Group</h3>',
  424. output.get_data(as_text=True))
  425. self.assertEqual(
  426. output.get_data(as_text=True).count('<a href="/user/'), 2)
  427. user.username = 'pingou'
  428. with tests.user_set(self.app.application, user):
  429. # User not in the group
  430. output = self.app.post(
  431. '/group/test_group/foo/delete', data=data, follow_redirects=True)
  432. self.assertEqual(output.status_code, 200)
  433. self.assertIn(
  434. 'User `foo` could not be '
  435. 'found in the group `test_group`', output.get_data(as_text=True))
  436. self.assertIn(
  437. '<h3 class="mb-0 font-weight-bold">Test Group</h3>',
  438. output.get_data(as_text=True))
  439. self.assertEqual(
  440. output.get_data(as_text=True).count('<a href="/user/'), 2)
  441. # Cannot delete creator
  442. output = self.app.post(
  443. '/group/test_group/foo/delete', data=data, follow_redirects=True)
  444. self.assertEqual(output.status_code, 200)
  445. self.assertIn(
  446. 'User `foo` could not be '
  447. 'found in the group `test_group`', output.get_data(as_text=True))
  448. self.assertIn(
  449. '<h3 class="mb-0 font-weight-bold">Test Group</h3>',
  450. output.get_data(as_text=True))
  451. self.assertEqual(
  452. output.get_data(as_text=True).count('<a href="/user/'), 2)
  453. # Add user foo
  454. data = {
  455. 'user': 'foo',
  456. 'csrf_token': csrf_token,
  457. }
  458. output = self.app.post('/group/test_group', data=data)
  459. self.assertEqual(output.status_code, 200)
  460. self.assertIn(
  461. 'User `foo` added to the '
  462. 'group `test_group`.', output.get_data(as_text=True))
  463. self.assertIn(
  464. '<h3 class="mb-0 font-weight-bold">Test Group</h3>',
  465. output.get_data(as_text=True))
  466. self.assertEqual(
  467. output.get_data(as_text=True).count('<a href="/user/'), 3)
  468. output = self.app.post(
  469. '/group/test_group/foo/delete', data=data, follow_redirects=True)
  470. self.assertEqual(output.status_code, 200)
  471. self.assertIn(
  472. 'User `foo` removed from '
  473. 'the group `test_group`', output.get_data(as_text=True))
  474. self.assertIn(
  475. '<h3 class="mb-0 font-weight-bold">Test Group</h3>',
  476. output.get_data(as_text=True))
  477. self.assertEqual(
  478. output.get_data(as_text=True).count('<a href="/user/'), 2)
  479. if __name__ == '__main__':
  480. unittest.main(verbosity=2)