test_pagure_exclude_group_index.py 4.9 KB

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