def test_example(self): """Tests the example set in the acl module.""" # Set a dict of roles with an 'admin' role that has full access and assign # users to it. Each role maps to a list of rules. Each rule, a tuple # (topic, name, flag), where flag, as bool to allow or disallow access. # Wildcard '*' can be used to match all topics and/or names. Acl.roles_map = { 'admin': [ ('*', '*', True), ], } # Assign users 'user_1' and 'user_2' to the 'admin' role. AclRules.insert_or_update(area='my_area', user='******', roles=['admin']) AclRules.insert_or_update(area='my_area', user='******', roles=['admin']) # Restrict 'user_2' from accessing a specific resource, adding a new rule # with flag set to False. Now this user has access to everything except this # resource. user_acl = AclRules.get_by_area_and_user('my_area', 'user_2') user_acl.rules.append(('UserAdmin', '*', False)) user_acl.put() # Check 'user_2' permission. acl = Acl(area='my_area', user='******') self.assertEqual(acl.has_access(topic='UserAdmin', name='save'), False) self.assertEqual(acl.has_access(topic='UserAdmin', name='get'), False) self.assertEqual(acl.has_access(topic='AnythingElse', name='put'), True)
def test_example(self): """Tests the example set in the acl module.""" # Set a dict of roles with an 'admin' role that has full access and assign # users to it. Each role maps to a list of rules. Each rule, a tuple # (topic, name, flag), where flag, as bool to allow or disallow access. # Wildcard '*' can be used to match all topics and/or names. Acl.roles_map = { 'admin': [ ('*', '*', True), ], } # Assign users 'user_1' and 'user_2' to the 'admin' role. AclRules.insert_or_update(area='my_area', user='******', roles=['admin']) AclRules.insert_or_update(area='my_area', user='******', roles=['admin']) # Restrict 'user_2' from accessing a specific resource, adding a new rule # with flag set to False. Now this user has access to everything except this # resource. user_acl = AclRules.get_by_area_and_user('my_area', 'user_2') user_acl.rules.append(('UserAdmin', '*', False)) user_acl.put() # Check 'user_2' permission. acl = Acl(area='my_area', user='******') self.assertEqual(acl.has_access(topic='UserAdmin', name='save'), False) self.assertEqual(acl.has_access(topic='UserAdmin', name='get'), False) self.assertEqual(acl.has_access(topic='AnythingElse', name='put'), True)
def test_has_access(self): AclRules.insert_or_update(area='my_area', user='******', rules=[('*', '*', True)]) AclRules.insert_or_update(area='my_area', user='******', rules=[('content', '*', True), ('content', 'delete', False)]) AclRules.insert_or_update(area='my_area', user='******', rules=[('content', 'read', True)]) acl1 = Acl(area='my_area', user='******') acl2 = Acl(area='my_area', user='******') acl3 = Acl(area='my_area', user='******') self.assertEqual(acl1.has_access('content', 'read'), True) self.assertEqual(acl1.has_access('content', 'update'), True) self.assertEqual(acl1.has_access('content', 'delete'), True) self.assertEqual(acl2.has_access('content', 'read'), True) self.assertEqual(acl2.has_access('content', 'update'), True) self.assertEqual(acl2.has_access('content', 'delete'), False) self.assertEqual(acl3.has_access('content', 'read'), True) self.assertEqual(acl3.has_access('content', 'update'), False) self.assertEqual(acl3.has_access('content', 'delete'), False)
def test_has_access(self): AclRules.insert_or_update(area='my_area', user='******', rules=[('*', '*', True)]) AclRules.insert_or_update(area='my_area', user='******', rules=[('content', '*', True), ('content', 'delete', False)]) AclRules.insert_or_update(area='my_area', user='******', rules=[('content', 'read', True)]) acl1 = Acl(area='my_area', user='******') acl2 = Acl(area='my_area', user='******') acl3 = Acl(area='my_area', user='******') self.assertEqual(acl1.has_access('content', 'read'), True) self.assertEqual(acl1.has_access('content', 'update'), True) self.assertEqual(acl1.has_access('content', 'delete'), True) self.assertEqual(acl2.has_access('content', 'read'), True) self.assertEqual(acl2.has_access('content', 'update'), True) self.assertEqual(acl2.has_access('content', 'delete'), False) self.assertEqual(acl3.has_access('content', 'read'), True) self.assertEqual(acl3.has_access('content', 'update'), False) self.assertEqual(acl3.has_access('content', 'delete'), False)
def test_roles_lock_changed(self): roles_map1 = { 'editor': [('content', '*', True),], 'contributor': [('content', '*', True), ('content', 'delete', False)], } Acl.roles_map = roles_map1 Acl.roles_lock = 'initial' AclRules.insert_or_update(area='my_area', user='******', roles=['editor']) acl1 = Acl(area='my_area', user='******') AclRules.insert_or_update(area='my_area', user='******', roles=['contributor']) acl2 = Acl(area='my_area', user='******') self.assertEqual(acl1.has_access('content', 'add'), True) self.assertEqual(acl1.has_access('content', 'edit'), True) self.assertEqual(acl1.has_access('content', 'delete'), True) self.assertEqual(acl2.has_access('content', 'add'), True) self.assertEqual(acl2.has_access('content', 'edit'), True) self.assertEqual(acl2.has_access('content', 'delete'), False) roles_map2 = { 'editor': [('content', '*', True),], 'contributor': [('content', '*', True), ('content', 'delete', False), ('content', 'add', False)], } Acl.roles_map = roles_map2 Acl.roles_lock = 'changed' acl1 = Acl(area='my_area', user='******') acl2 = Acl(area='my_area', user='******') self.assertEqual(acl1.has_access('content', 'add'), True) self.assertEqual(acl1.has_access('content', 'edit'), True) self.assertEqual(acl1.has_access('content', 'delete'), True) self.assertEqual(acl2.has_access('content', 'add'), False) self.assertEqual(acl2.has_access('content', 'edit'), True) self.assertEqual(acl2.has_access('content', 'delete'), False)
def test_has_access_with_roles(self): Acl.roles_map = { 'admin': [('*', '*', True),], 'editor': [('content', '*', True),], 'contributor': [('content', '*', True), ('content', 'delete', False)], 'designer': [('design', '*', True),], } AclRules.insert_or_update(area='my_area', user='******', roles=['admin']) acl1 = Acl(area='my_area', user='******') AclRules.insert_or_update(area='my_area', user='******', roles=['admin'], rules=[('ManageUsers', '*', False)]) acl2 = Acl(area='my_area', user='******') AclRules.insert_or_update(area='my_area', user='******', roles=['editor']) acl3 = Acl(area='my_area', user='******') AclRules.insert_or_update(area='my_area', user='******', roles=['contributor'], rules=[('design', '*', True),]) acl4 = Acl(area='my_area', user='******') self.assertEqual(acl1.has_access('ApproveUsers', 'save'), True) self.assertEqual(acl1.has_access('ManageUsers', 'edit'), True) self.assertEqual(acl1.has_access('ManageUsers', 'delete'), True) self.assertEqual(acl1.has_access('ApproveUsers', 'save'), True) self.assertEqual(acl2.has_access('ManageUsers', 'edit'), False) self.assertEqual(acl2.has_access('ManageUsers', 'delete'), False) self.assertEqual(acl3.has_access('ApproveUsers', 'save'), False) self.assertEqual(acl3.has_access('ManageUsers', 'edit'), False) self.assertEqual(acl3.has_access('ManageUsers', 'delete'), False) self.assertEqual(acl3.has_access('content', 'edit'), True) self.assertEqual(acl3.has_access('content', 'delete'), True) self.assertEqual(acl3.has_access('content', 'save'), True) self.assertEqual(acl3.has_access('design', 'edit'), False) self.assertEqual(acl3.has_access('design', 'delete'), False) self.assertEqual(acl4.has_access('ApproveUsers', 'save'), False) self.assertEqual(acl4.has_access('ManageUsers', 'edit'), False) self.assertEqual(acl4.has_access('ManageUsers', 'delete'), False) self.assertEqual(acl4.has_access('content', 'edit'), True) self.assertEqual(acl4.has_access('content', 'delete'), False) self.assertEqual(acl4.has_access('content', 'save'), True) self.assertEqual(acl4.has_access('design', 'edit'), True) self.assertEqual(acl4.has_access('design', 'delete'), True)
def test_roles_lock_changed(self): roles_map1 = { 'editor': [ ('content', '*', True), ], 'contributor': [('content', '*', True), ('content', 'delete', False)], } Acl.roles_map = roles_map1 Acl.roles_lock = 'initial' AclRules.insert_or_update(area='my_area', user='******', roles=['editor']) acl1 = Acl(area='my_area', user='******') AclRules.insert_or_update(area='my_area', user='******', roles=['contributor']) acl2 = Acl(area='my_area', user='******') self.assertEqual(acl1.has_access('content', 'add'), True) self.assertEqual(acl1.has_access('content', 'edit'), True) self.assertEqual(acl1.has_access('content', 'delete'), True) self.assertEqual(acl2.has_access('content', 'add'), True) self.assertEqual(acl2.has_access('content', 'edit'), True) self.assertEqual(acl2.has_access('content', 'delete'), False) roles_map2 = { 'editor': [ ('content', '*', True), ], 'contributor': [('content', '*', True), ('content', 'delete', False), ('content', 'add', False)], } Acl.roles_map = roles_map2 Acl.roles_lock = 'changed' acl1 = Acl(area='my_area', user='******') acl2 = Acl(area='my_area', user='******') self.assertEqual(acl1.has_access('content', 'add'), True) self.assertEqual(acl1.has_access('content', 'edit'), True) self.assertEqual(acl1.has_access('content', 'delete'), True) self.assertEqual(acl2.has_access('content', 'add'), False) self.assertEqual(acl2.has_access('content', 'edit'), True) self.assertEqual(acl2.has_access('content', 'delete'), False)
def test_has_access_with_roles(self): Acl.roles_map = { 'admin': [ ('*', '*', True), ], 'editor': [ ('content', '*', True), ], 'contributor': [('content', '*', True), ('content', 'delete', False)], 'designer': [ ('design', '*', True), ], } AclRules.insert_or_update(area='my_area', user='******', roles=['admin']) acl1 = Acl(area='my_area', user='******') AclRules.insert_or_update(area='my_area', user='******', roles=['admin'], rules=[('ManageUsers', '*', False)]) acl2 = Acl(area='my_area', user='******') AclRules.insert_or_update(area='my_area', user='******', roles=['editor']) acl3 = Acl(area='my_area', user='******') AclRules.insert_or_update(area='my_area', user='******', roles=['contributor'], rules=[ ('design', '*', True), ]) acl4 = Acl(area='my_area', user='******') self.assertEqual(acl1.has_access('ApproveUsers', 'save'), True) self.assertEqual(acl1.has_access('ManageUsers', 'edit'), True) self.assertEqual(acl1.has_access('ManageUsers', 'delete'), True) self.assertEqual(acl1.has_access('ApproveUsers', 'save'), True) self.assertEqual(acl2.has_access('ManageUsers', 'edit'), False) self.assertEqual(acl2.has_access('ManageUsers', 'delete'), False) self.assertEqual(acl3.has_access('ApproveUsers', 'save'), False) self.assertEqual(acl3.has_access('ManageUsers', 'edit'), False) self.assertEqual(acl3.has_access('ManageUsers', 'delete'), False) self.assertEqual(acl3.has_access('content', 'edit'), True) self.assertEqual(acl3.has_access('content', 'delete'), True) self.assertEqual(acl3.has_access('content', 'save'), True) self.assertEqual(acl3.has_access('design', 'edit'), False) self.assertEqual(acl3.has_access('design', 'delete'), False) self.assertEqual(acl4.has_access('ApproveUsers', 'save'), False) self.assertEqual(acl4.has_access('ManageUsers', 'edit'), False) self.assertEqual(acl4.has_access('ManageUsers', 'delete'), False) self.assertEqual(acl4.has_access('content', 'edit'), True) self.assertEqual(acl4.has_access('content', 'delete'), False) self.assertEqual(acl4.has_access('content', 'save'), True) self.assertEqual(acl4.has_access('design', 'edit'), True) self.assertEqual(acl4.has_access('design', 'delete'), True)