test_pagure_exclude_group_index.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - 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 mock
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure.lib.query
  16. import pagure.lib.model
  17. import tests
  18. class PagureExcludeGroupIndex(tests.Modeltests):
  19. """ Tests the EXCLUDE_GROUP_INDEX configuration key in pagure """
  20. def setUp(self):
  21. """ Set up the environnment, ran before every tests. """
  22. super(PagureExcludeGroupIndex, self).setUp()
  23. tests.create_projects(self.session)
  24. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  25. # Create a ``provenpackger`` group:
  26. msg = pagure.lib.query.add_group(
  27. self.session,
  28. group_name='provenpackager',
  29. display_name='Proven Packagers',
  30. description='Packagers having access to all the repo',
  31. group_type='user',
  32. user='pingou',
  33. is_admin=False,
  34. blacklist=[],
  35. )
  36. self.session.commit()
  37. self.assertEqual(
  38. msg, 'User `pingou` added to the group `provenpackager`.')
  39. # Add the `provenpackager` group to the test2 project
  40. project = pagure.lib.query._get_project(self.session, 'test2')
  41. msg = pagure.lib.query.add_group_to_project(
  42. session=self.session,
  43. project=project,
  44. new_group='provenpackager',
  45. user='pingou',
  46. )
  47. self.session.commit()
  48. self.assertEqual(msg, 'Group added')
  49. def test_defaults_pingou(self):
  50. """ Test which repo pingou has by default. """
  51. repos = pagure.lib.query.search_projects(
  52. self.session,
  53. username='pingou',
  54. fork=False,
  55. )
  56. self.assertEqual(len(repos), 3)
  57. for idx, name in enumerate(['test', 'test2', 'test3']):
  58. self.assertEqual(repos[idx].name, name)
  59. def test_defaults_foo(self):
  60. """ Test which repo foo has by default. """
  61. repos = pagure.lib.query.search_projects(
  62. self.session,
  63. username='foo',
  64. fork=False,
  65. )
  66. self.assertEqual(len(repos), 0)
  67. def test_add_foo_test(self):
  68. """ Test adding foo to the test project. """
  69. group = pagure.lib.query.search_groups(
  70. self.session, group_name='provenpackager')
  71. self.assertEqual(group.group_name, 'provenpackager')
  72. # List all foo's project before (ie: there should be none)
  73. repos = pagure.lib.query.search_projects(
  74. self.session,
  75. username='foo',
  76. fork=False,
  77. )
  78. self.assertEqual(len(repos), 0)
  79. # Adding `foo` to the `provenpackager` group
  80. msg = pagure.lib.query.add_user_to_group(
  81. self.session,
  82. username='foo',
  83. group=group,
  84. user='pingou',
  85. is_admin=False,
  86. )
  87. self.assertEqual(
  88. msg, 'User `foo` added to the group `provenpackager`.')
  89. # Test that foo has now one project, via the provenpackager group
  90. repos = pagure.lib.query.search_projects(
  91. self.session,
  92. username='foo',
  93. fork=False,
  94. )
  95. self.assertEqual(len(repos), 1)
  96. self.assertEqual(repos[0].name, 'test2')
  97. def test_excluding_provenpackager(self):
  98. """ Test retrieving user's repo with a group excluded. """
  99. # Add `foo` to `provenpackager`
  100. group = pagure.lib.query.search_groups(
  101. self.session, group_name='provenpackager')
  102. self.assertEqual(group.group_name, 'provenpackager')
  103. msg = pagure.lib.query.add_user_to_group(
  104. self.session,
  105. username='foo',
  106. group=group,
  107. user='pingou',
  108. is_admin=False,
  109. )
  110. self.assertEqual(
  111. msg, 'User `foo` added to the group `provenpackager`.')
  112. # Get foo's project outside of proven packager
  113. repos = pagure.lib.query.search_projects(
  114. self.session,
  115. username='foo',
  116. exclude_groups=['provenpackager'],
  117. fork=False,
  118. )
  119. self.assertEqual(len(repos), 0)
  120. # Get pingou's project outside of proven packager (nothing changes)
  121. repos = pagure.lib.query.search_projects(
  122. self.session,
  123. username='pingou',
  124. exclude_groups=['provenpackager'],
  125. fork=False,
  126. )
  127. repos2 = pagure.lib.query.search_projects(
  128. self.session,
  129. username='pingou',
  130. fork=False,
  131. )
  132. self.assertEqual(repos, repos2)
  133. self.assertEqual(len(repos), 3)
  134. for idx, name in enumerate(['test', 'test2', 'test3']):
  135. self.assertEqual(repos[idx].name, name)
  136. if __name__ == '__main__':
  137. unittest.main(verbosity=2)