test_pagure_flask_ui_groups.py 20 KB

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