Łukasz Fidosz avatar Łukasz Fidosz committed 2a649ce

added tests for deleting user, user's __repr__, and resources with perms with given resources ids, fixed bug with filtering by resurces ids there

Comments (0)

Files changed (2)

ziggurat_foundations/models.py

         
         
     
-    def resources_with_perms(self, perms, resource_ids=None,
-                             cache='default',
+    def resources_with_perms(self, perms, resource_ids=None, cache='default',
                              invalidate=False, db_session=None):
         """ returns all resources that user has perms for,
             note that at least one perm needs to be met,
-            resource_ids restricts the search to  specific resources"""
+            resource_ids restricts the search tospecific resources"""
         # owned entities have ALL permissions so we return those resources too
         # even without explict perms set
         # TODO: implement admin superrule perm - maybe return all apps
         q2 = q2.filter(self.UserResourcePermission.user_name == self.user_name)
         q2 = q2.filter(self.Resource.resource_id == self.UserResourcePermission.resource_id)
         q2 = q2.filter(self.UserResourcePermission.perm_name.in_(perms))
+        q = q.union(q2)
         if resource_ids:
             q = q.filter(self.Resource.resource_id.in_(resource_ids))
-        q = q.union(q2)
         q = q.order_by(self.Resource.resource_name)
         if invalidate:
             return True

ziggurat_foundations/tests.py

         self.assertEqual(user.user_name, u'username')
         self.assertEqual(user.email, u'email')
         self.assertEqual(user.status, 0)
+    
+    def test_delete_user(self):
+        self._addUser()
+        to_delete = User.by_user_name(u'username', db_session=self.session)
+        self.session.delete(to_delete)
+        self.session.commit()
+
+    def test_user_repr(self):
+        user = self._addUser()
+        self.assertEqual(repr(user), '<User: username>')
 
     def test_by_user_name_existing(self):
         created_user = self._addUser()
-        queried_user = User.by_user_name(u'username',db_session=self.session)
+        queried_user = User.by_user_name(u'username', db_session=self.session)
 
         self.assertEqual(created_user, queried_user)
 
         resource = self._addResource(1, u'test_resource')
         permission = UserResourcePermission(perm_name=u'test_perm',
                                             user_name=created_user.user_name,
-                                            resource_id = resource.resource_id
-                                                )
+                                            resource_id=resource.resource_id)
         resource.user_permissions.append(permission)
         self.session.flush()
         resources = created_user.resources_with_perms([u'test_perm'],
         resources = created_user.resources_with_perms([u'test_perm'],
                                         db_session=self.session).all()
         self.assertEqual(resources, [resource,resource2])
-        
+    
+    def test_resources_ids_with_perm(self):
+        created_user = self._addUser()
+        resource1 = self._addResource(1, u'test_resource1')
+        resource2 = self._addResource(2, u'test_resource2')
+        resource3 = self._addResource(3, u'test_resource3')
+
+        permission1 = UserResourcePermission(perm_name=u'test_perm',
+                                            user_name=created_user.user_name,
+                                            resource_id=resource1.resource_id)
+        permission2 = UserResourcePermission(perm_name=u'test_perm',
+                                            user_name=created_user.user_name,
+                                            resource_id=resource2.resource_id)
+        permission3 = UserResourcePermission(perm_name=u'test_perm',
+                                            user_name=created_user.user_name,
+                                            resource_id=resource3.resource_id)
+
+        resource1.user_permissions.append(permission1)
+        resource2.user_permissions.append(permission2)
+        resource3.user_permissions.append(permission3)
+
+        self.session.flush()
+        resources = created_user.resources_with_perms([u'test_perm'],
+                                                resource_ids=[1, 3],
+                                                db_session=self.session).all()
+        self.assertEqual(resources, [resource1, resource3])
+
     def test_resources_with_wrong_group_permission(self):
         with self.assertRaises(AssertionError):
             created_user = self._addUser()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.