test_pagure_flask_ui_groups.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 setUp(self):
  22. """ Set up the environnment, ran before every tests. """
  23. super(PagureFlaskGroupstests, self).setUp()
  24. pagure.APP.config['TESTING'] = True
  25. pagure.SESSION = self.session
  26. pagure.ui.SESSION = self.session
  27. pagure.ui.app.SESSION = self.session
  28. pagure.ui.groups.SESSION = self.session
  29. pagure.ui.repo.SESSION = self.session
  30. pagure.ui.filters.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_group_lists(self):
  42. """ Test the group_lists endpoint. """
  43. output = self.app.get('/groups')
  44. self.assertIn(
  45. '<h2 class="m-b-1">\n'
  46. ' Groups <span class="label label-default">0</span>',
  47. output.data)
  48. def test_add_group(self):
  49. """ Test the add_group endpoint. """
  50. output = self.app.get('/group/add')
  51. self.assertEqual(output.status_code, 302)
  52. user = tests.FakeUser()
  53. with tests.user_set(pagure.APP, user):
  54. output = self.app.get('/group/add')
  55. self.assertEqual(output.status_code, 403)
  56. user.username = 'pingou'
  57. with tests.user_set(pagure.APP, user):
  58. output = self.app.get('/group/add')
  59. self.assertEqual(output.status_code, 200)
  60. self.assertIn('<h2>Create group</h2>', output.data)
  61. self.assertNotIn(
  62. '<option value="admin">admin</option>', output.data)
  63. csrf_token = output.data.split(
  64. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  65. data = {
  66. }
  67. # Insufficient input
  68. output = self.app.post('/group/add', data=data)
  69. self.assertEqual(output.status_code, 200)
  70. self.assertIn('<h2>Create group</h2>', output.data)
  71. self.assertEqual(output.data.count(
  72. 'This field is required.'), 3)
  73. data = {
  74. 'group_name': 'test_group',
  75. 'display_name': 'Test Group',
  76. 'description': 'This is a group for the tests',
  77. }
  78. # Missing CSRF
  79. output = self.app.post('/group/add', data=data)
  80. self.assertEqual(output.status_code, 200)
  81. self.assertIn('<h2>Create group</h2>', output.data)
  82. self.assertEqual(output.data.count(
  83. 'This field is required.'), 0)
  84. data['csrf_token'] = csrf_token
  85. # All good
  86. output = self.app.post(
  87. '/group/add', data=data, follow_redirects=True)
  88. self.assertEqual(output.status_code, 200)
  89. self.assertIn(
  90. '</button>\n User `pingou` added to '
  91. 'the group `test_group`.', output.data)
  92. self.assertIn(
  93. '</button>\n Group `test_group` created.',
  94. output.data)
  95. self.assertIn(
  96. '<h2 class="m-b-1">\n'
  97. ' Groups <span class="label label-default">1</span>',
  98. output.data)
  99. user = tests.FakeUser(
  100. username='pingou',
  101. groups=pagure.APP.config['ADMIN_GROUP'])
  102. with tests.user_set(pagure.APP, user):
  103. output = self.app.get('/group/add')
  104. self.assertEqual(output.status_code, 200)
  105. self.assertIn('<h2>Create group</h2>', output.data)
  106. self.assertIn('<option value="admin">admin</option>', output.data)
  107. data = {
  108. 'group_name': 'test_admin_group',
  109. 'group_type': 'admin',
  110. 'display_name': 'Test Admin Group',
  111. 'description': 'This is another group for the tests',
  112. 'csrf_token': csrf_token,
  113. }
  114. # All good
  115. output = self.app.post(
  116. '/group/add', data=data, follow_redirects=True)
  117. self.assertEqual(output.status_code, 200)
  118. self.assertIn(
  119. '</button>\n User `pingou` added to '
  120. 'the group `test_admin_group`.', output.data)
  121. self.assertIn(
  122. '</button>\n Group `test_admin_group` '
  123. 'created.',output.data)
  124. self.assertIn(
  125. '<h2 class="m-b-1">\n'
  126. ' Groups <span class="label label-default">2</span>',
  127. output.data)
  128. def test_edit_group(self):
  129. """ Test the edit_group endpoint. """
  130. output = self.app.get('/group/test_group/edit')
  131. self.assertEqual(output.status_code, 302)
  132. user = tests.FakeUser()
  133. with tests.user_set(pagure.APP, user):
  134. output = self.app.get('/group/test_group/edit')
  135. self.assertEqual(output.status_code, 404)
  136. self.assertIn('<p>Group not found</p>', output.data)
  137. self.test_add_group()
  138. user.username = 'foo'
  139. with tests.user_set(pagure.APP, user):
  140. output = self.app.get('/group/foo/edit')
  141. self.assertEqual(output.status_code, 404)
  142. self.assertIn('<p>Group not found</p>', output.data)
  143. output = self.app.get('/group/test_group/edit')
  144. self.assertEqual(output.status_code, 200)
  145. self.assertIn(
  146. '<title>Edit group: test_group - Pagure</title>',
  147. output.data)
  148. self.assertIn(
  149. '<form action="/group/test_group/edit" method="post">',
  150. output.data)
  151. self.assertIn(
  152. '<strong><label for="description">Description'
  153. '</label></strong>', output.data)
  154. csrf_token = output.data.split(
  155. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  156. # Missing CSRF
  157. data = {
  158. 'group_name': 'test_group',
  159. 'display_name': 'Test Group edited',
  160. 'description': 'This is a group for the tests edited',
  161. }
  162. output = self.app.post(
  163. '/group/test_group/edit', data=data, follow_redirects=True)
  164. #print output.data
  165. self.assertEqual(output.status_code, 200)
  166. self.assertIn(
  167. '<title>Edit group: test_group - Pagure</title>',
  168. output.data)
  169. self.assertIn(
  170. '<form action="/group/test_group/edit" method="post">',
  171. output.data)
  172. self.assertIn(
  173. '<strong><label for="description">Description'
  174. '</label></strong>', output.data)
  175. # User not allowed
  176. data['csrf_token'] = csrf_token
  177. output = self.app.post(
  178. '/group/test_group/edit', data=data, follow_redirects=True)
  179. self.assertEqual(output.status_code, 200)
  180. self.assertIn(
  181. '<title>Group test_group - Pagure</title>',
  182. output.data)
  183. self.assertIn(
  184. '</button>\n You are not '
  185. 'allowed to edit this group', output.data)
  186. self.assertIn(
  187. '<span class="oi" data-glyph="people"></span> '
  188. '&nbsp;Test Group', output.data)
  189. user.username = 'pingou'
  190. with tests.user_set(pagure.APP, user):
  191. # Invalid repo
  192. output = self.app.post(
  193. '/group/bar/edit', data=data, follow_redirects=True)
  194. self.assertEqual(output.status_code, 404)
  195. self.assertIn('<p>Group not found</p>', output.data)
  196. output = self.app.post(
  197. '/group/test_group/edit', data=data, follow_redirects=True)
  198. self.assertEqual(output.status_code, 200)
  199. self.assertIn(
  200. '<title>Group test_group - Pagure</title>', output.data)
  201. self.assertIn(
  202. '<span class="oi" data-glyph="people"></span> '
  203. '&nbsp;Test Group', output.data)
  204. self.assertIn(
  205. 'Group &#34;Test Group edited&#34; (test_group) edited',
  206. output.data)
  207. def test_group_delete(self):
  208. """ Test the group_delete endpoint. """
  209. output = self.app.post('/group/foo/delete')
  210. self.assertEqual(output.status_code, 302)
  211. user = tests.FakeUser()
  212. with tests.user_set(pagure.APP, user):
  213. output = self.app.post('/group/foo/delete', follow_redirects=True)
  214. self.assertEqual(output.status_code, 200)
  215. self.assertIn(
  216. '<p>No groups have been created on this pagure instance '
  217. 'yet</p>', output.data)
  218. self.assertIn(
  219. '<h2 class="m-b-1">\n'
  220. ' Groups <span class="label label-default">0</span>',
  221. output.data)
  222. self.test_add_group()
  223. with tests.user_set(pagure.APP, user):
  224. output = self.app.post('/group/foo/delete', follow_redirects=True)
  225. self.assertEqual(output.status_code, 200)
  226. self.assertIn(
  227. '<h2 class="m-b-1">\n'
  228. ' Groups <span class="label label-default">1</span>',
  229. output.data)
  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(pagure.APP, 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(pagure.APP, 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(pagure.APP, 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(pagure.APP, 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.APP.config['ADMIN_GROUP'])
  291. with tests.user_set(pagure.APP, 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(pagure.APP, 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(pagure.APP, 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(pagure.APP, 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. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  433. PagureFlaskGroupstests)
  434. unittest.TextTestRunner(verbosity=2).run(SUITE)