test_pagure_exclude_group_index.py 4.9 KB

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