test_pagure_exclude_group_index.py 5.0 KB

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