def test_50_user_attributes(self): user = User(login="******", realm=self.realm1) r = user.set_attribute("hans", "wurst") self.assertTrue(r > 0) r = user.set_attribute("hugen", "dubel") self.assertTrue(r > 1) attrs = user.attributes self.assertEqual(attrs.get("hans"), "wurst") self.assertEqual(attrs.get("hugen"), "dubel") # Now we can overwrite attributes r = user.set_attribute("hans", "meiser") attrs = user.attributes self.assertEqual(attrs.get("hans"), "meiser") self.assertEqual(attrs.get("hugen"), "dubel") # now delete some attributes of the user r = user.delete_attribute("hans") self.assertTrue(r == 1) attrs = user.attributes self.assertEqual(attrs.get("hans"), None) self.assertEqual(attrs.get("hugen"), "dubel") # delete all attributes user.set_attribute("key", "value") r = user.delete_attribute() self.assertTrue(r == 2) attrs = user.attributes self.assertEqual(attrs.get("hans"), None) self.assertEqual(attrs.get("hugen"), None) self.assertEqual(attrs.get("key"), None)
def do(self, action, options=None): """ This method executes the defined action in the given event. :param action: The action to perform :type action: str :param options: Contains the flask parameters g, attrkey and attrvalue :type options: dict :return: """ g = options.get("g") request = options.get("request") handler_def = options.get("handler_def") handler_options = handler_def.get("options", {}) user_type = handler_options.get("user", USER_TYPE.TOKENOWNER) tokenowner = self._get_tokenowner(request) if user_type == USER_TYPE.TOKENOWNER and not tokenowner.is_empty(): user = tokenowner elif user_type == USER_TYPE.LOGGED_IN_USER and hasattr( g, 'logged_in_user'): user = User(login=g.logged_in_user.get('username'), realm=g.logged_in_user.get('realm')) else: log.warning("Unable to determine the user for handling the custom " "attribute! action: {0!s}, handler: {1!s}".format( action, handler_def)) return False attrkey = handler_options.get("attrkey") attrvalue = handler_options.get("attrvalue") if action.lower() == "set_custom_user_attributes": ret = user.set_attribute(attrkey, attrvalue) elif action.lower() == "delete_custom_user_attributes": ret = user.delete_attribute(attrkey) else: log.warning('Unknown action value: {0!s}'.format(action)) ret = False return ret
def test_02_user_attribute_with_handler_logged_in_user(self): user = User('cornelius', realm=self.realm1) self.assertNotIn('foo', user.attributes, user.attributes) # get the auth-token for the user with self.app.test_request_context('/auth', data={ "username": '******', "password": '******' }, method='POST'): res = self.app.full_dispatch_request() self.assertEqual(200, res.status_code, res) result = res.json.get("result") self.assertTrue(result.get("status"), result) user_token = result.get("value").get("token") # try to delete a non-existing attribute eid = set_event("user_atts", event=["token_list"], action=ACTION_TYPE.DELETE_CUSTOM_USER_ATTRIBUTES, handlermodule="CustomUserAttributes", conditions={}, options={ 'user': USER_TYPE.LOGGED_IN_USER, 'attrkey': 'foo' }) with self.app.test_request_context( '/token/', method='GET', headers={'Authorization': user_token}): res = self.app.full_dispatch_request() self.assertTrue(res.status_code == 200, res) self.assertNotIn('foo', user.attributes, user.attributes) # delete an existing attribute user.set_attribute('foo', 'bar') self.assertIn('foo', user.attributes, user.attributes) with self.app.test_request_context( '/token/', method='GET', headers={'Authorization': user_token}): res = self.app.full_dispatch_request() self.assertTrue(res.status_code == 200, res) self.assertNotIn('foo', user.attributes, user.attributes) # add an attribute eid = set_event("user_atts", event=["token_list"], id=eid, action=ACTION_TYPE.SET_CUSTOM_USER_ATTRIBUTES, handlermodule="CustomUserAttributes", conditions={}, options={ 'user': USER_TYPE.LOGGED_IN_USER, 'attrkey': 'foo', 'attrvalue': 'bar' }) with self.app.test_request_context( '/token/', method='GET', headers={'Authorization': user_token}): res = self.app.full_dispatch_request() self.assertTrue(res.status_code == 200, res) self.assertEqual('bar', user.attributes['foo'], user.attributes) # overwrite an attribute eid = set_event("user_atts", event=["token_list"], id=eid, action=ACTION_TYPE.SET_CUSTOM_USER_ATTRIBUTES, handlermodule="CustomUserAttributes", conditions={}, options={ 'user': USER_TYPE.LOGGED_IN_USER, 'attrkey': 'foo', 'attrvalue': 'baz' }) with self.app.test_request_context( '/token/', method='GET', headers={'Authorization': user_token}): res = self.app.full_dispatch_request() self.assertTrue(res.status_code == 200, res) self.assertEqual('baz', user.attributes['foo'], user.attributes) delete_event(eid) user.delete_attribute('foo')