You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by di...@apache.org on 2022/09/15 13:46:39 UTC

[allura] 04/05: fixup! fixup! [#8455] allura pytest - fix misc test failures that popped up during pytest conversion

This is an automated email from the ASF dual-hosted git repository.

dill0wn pushed a commit to branch dw/8455
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 8effcc6bde8e7685885f72955bfffc6d816af90c
Author: Dillon Walls <di...@slashdotmedia.com>
AuthorDate: Thu Sep 15 13:44:16 2022 +0000

    fixup! fixup! [#8455] allura pytest - fix misc test failures that popped up during pytest conversion
---
 Allura/allura/lib/macro.py          | 15 ++++++++-------
 Allura/allura/model/project.py      | 20 +++++++++++++++++++-
 Allura/allura/tests/test_globals.py | 25 ++++++++++++++++++++-----
 3 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/Allura/allura/lib/macro.py b/Allura/allura/lib/macro.py
index b1af50297..0e293037e 100644
--- a/Allura/allura/lib/macro.py
+++ b/Allura/allura/lib/macro.py
@@ -177,14 +177,15 @@ def project_blog_posts(max_number=5, sort='timestamp', summary=False, mount_poin
         'state': 'published',
     })
     posts = posts.sort(sort, pymongo.DESCENDING).limit(int(max_number)).all()
-    output = ((dict(
-        href=post.url(),
-        title=post.title,
-        author=post.author().display_name,
-        ago=h.ago(post.timestamp),
-        description=summary and '&nbsp;' or g.markdown.cached_convert(post, 'text')))
+    output = [
+        dict(href=post.url(),
+             title=post.title,
+             author=post.author().display_name,
+             ago=h.ago(post.timestamp),
+             description=summary and '&nbsp;' or g.markdown.cached_convert(post, 'text'))
         for post in posts if security.has_access(post, 'read', project=post.app.project)() and
-        security.has_access(post.app.project, 'read', project=post.app.project)())
+            security.has_access(post.app.project, 'read', project=post.app.project)()
+    ]
     posts = BlogPosts(posts=output)
     g.resource_manager.register(posts)
     response = posts.display(posts=output)
diff --git a/Allura/allura/model/project.py b/Allura/allura/model/project.py
index c0a010f5e..66b265e80 100644
--- a/Allura/allura/model/project.py
+++ b/Allura/allura/model/project.py
@@ -1102,12 +1102,30 @@ class Project(SearchIndexable, MappedClass, ActivityNode, ActivityObject):
             ThreadLocalORMSession.flush_all()
 
     def add_user(self, user, role_names):
-        'Convenience method to add member with the given role(s).'
+        '''Convenience method to add member with the given role(s).'''
         pr = ProjectRole.by_user(user, project=self, upsert=True)
         for role_name in role_names:
             r = ProjectRole.by_name(role_name, self)
             pr.roles.append(r._id)
 
+    def remove_user(self, user, role_names=None):
+        '''Convenience method to add member with the given role(s).'''
+        pr = ProjectRole.by_user(user, project=self)
+        if not pr:
+            return
+
+        if not role_names or not isinstance(role_names, Iterable):
+            ProjectRole.query.remove({'_id': pr._id})
+            return
+
+        for role_name in role_names:
+            r = ProjectRole.by_name(role_name, self)
+            if r._id in pr.roles:
+                pr.roles.remove(r._id)
+
+        if not pr.roles:
+            pr.remove()
+
     @property
     def twitter_handle(self):
         return self.social_account('Twitter').accounturl
diff --git a/Allura/allura/tests/test_globals.py b/Allura/allura/tests/test_globals.py
index 172c1c57b..389c7a228 100644
--- a/Allura/allura/tests/test_globals.py
+++ b/Allura/allura/tests/test_globals.py
@@ -49,11 +49,6 @@ from forgewiki import model as WM
 from forgeblog import model as BM
 
 
-def setup_module(module):
-    setup_basic_test()
-    setup_unit_test()
-
-
 def squish_spaces(text):
     # \s is whitespace
     # \xa0 is &nbsp; in unicode form
@@ -85,8 +80,28 @@ def get_projects_property_in_the_same_order(names, prop):
 @with_nose_compatibility
 class Test():
 
+    @classmethod
+    def setup_class(cls):
+        setup_basic_test()
+        setup_global_objects()
+
     def setup_method(self, method):
         setup_global_objects()
+        p_nbhd = M.Neighborhood.query.get(name='Projects')
+        p_test = M.Project.query.get(shortname='test', neighborhood_id=p_nbhd._id)
+        self.acl_bak = p_test.acl.copy()
+
+    def teardown_method(self, method):
+        user = M.User.by_username('test-admin')
+        user.display_name = 'Test Admin'
+
+        p_nbhd = M.Neighborhood.query.get(name='Projects')
+        p_test = M.Project.query.get(shortname='test', neighborhood_id=p_nbhd._id)
+        p_test.remove_user(M.User.by_username('test-user'))
+        p_test.remove_user(M.User.by_username('test-user-0'))
+        p_test.acl = self.acl_bak
+
+        ThreadLocalORMSession.flush_all()
 
     @td.with_wiki
     def test_app_globals(self):