def test_user_public_attributes(self): """Test public attributes works.""" user = User(email_addr="*****@*****.**", name="johndoe", pro=1, fullname="John Doe", locale="en") public_attributes = [ 'created', 'name', 'fullname', 'locale', 'info', 'n_answers', 'registered_ago', 'rank', 'score' ] user.set_password("juandiso") print(sorted(public_attributes)) print(sorted(user.public_attributes())) assert sorted(public_attributes) == sorted(user.public_attributes()) data = user.to_public_json() err_msg = "There are some keys that should not be public" assert sorted(list(data.keys())) == sorted(public_attributes), err_msg all_attributes = list(user.dictize().keys()) s = set(public_attributes) private_attributes = [x for x in all_attributes if x not in s] for attr in private_attributes: err_msg = "This attribute should be private %s" % attr assert data.get(attr) is None, err_msg
def test_info_public_keys(self): """Test DomainObject to_public_json method works.""" user = User() user.name = 'daniel' user.info = dict(container='3', avatar='img.png', token='secret') user_dict = user.dictize() json = user.to_public_json() err_msg = "Wrong value" assert json['name'] == user.name, err_msg err_msg = "Missing fields" assert json.keys().sort() == user.public_attributes().sort(), err_msg err_msg = "There should be info keys" assert json['info']['container'] == '3', err_msg assert json['info']['avatar'] == 'img.png', err_msg err_msg = "This key should be missing" assert json['info'].get('token') is None, err_msg json = user.to_public_json(data=user_dict) err_msg = "Wrong value" assert json['name'] == user.name, err_msg err_msg = "Missing fields" assert json.keys().sort() == user.public_attributes().sort(), err_msg err_msg = "There should be info keys" assert json['info']['container'] == '3', err_msg assert json['info']['avatar'] == 'img.png', err_msg err_msg = "This key should be missing" assert json['info'].get('token') is None, err_msg
def test_to_public_json(self): """Test DomainObject to_public_json method works.""" user = User() user.name = 'daniel' user_dict = user.dictize() json = user.to_public_json() err_msg = "Wrong value" assert json['name'] == user.name, err_msg err_msg = "Missing fields" assert json.keys().sort() == user.public_attributes().sort(), err_msg json = user.to_public_json(data=user_dict) err_msg = "Wrong value" assert json['name'] == user.name, err_msg err_msg = "Missing fields" assert json.keys().sort() == user.public_attributes().sort(), err_msg
def test_info_public_keys(self): """Test DomainObject to_public_json method works.""" user = User() user.name = 'daniel' user.info = dict(container='3', avatar='img.png', token='secret', badges=['awesome.png', 'incredible.png'], hidden=True) user_dict = user.dictize() json = user.to_public_json() err_msg = "Wrong value" assert json['name'] == user.name, err_msg err_msg = "Missing fields" assert list( json.keys()).sort() == user.public_attributes().sort(), err_msg err_msg = "There should be info keys" assert json['info']['container'] == '3', err_msg assert json['info']['avatar'] == 'img.png', err_msg err_msg = "This key should be missing" assert json['info'].get('token') is None, err_msg json = user.to_public_json(data=user_dict) err_msg = "Wrong value" assert json['name'] == user.name, err_msg err_msg = "Missing fields" assert list( json.keys()).sort() == user.public_attributes().sort(), err_msg err_msg = "There should be info keys" assert json['info']['container'] == '3', err_msg assert json['info']['avatar'] == 'img.png', err_msg err_msg = "This key should be missing" assert json['info'].get('token') is None, err_msg with patch.dict(self.flask_app.config, {'USER_INFO_PUBLIC_FIELDS': ['badges']}): json = user.to_public_json() assert list(json['info'].keys()).sort() == User().public_info_keys( ).sort(), err_msg assert 'badges' in list(json['info'].keys()) assert 'hidden' not in list(json['info'].keys())
def test_user_public_attributes(self): """Test public attributes works.""" user = User( email_addr="*****@*****.**", name="johndoe", pro=1, fullname="John Doe", locale="en") public_attributes = ['created', 'name', 'fullname', 'locale', 'info', 'task_runs', 'registered_ago', 'rank', 'score'] user.set_password("juandiso") assert public_attributes.sort() == user.public_attributes().sort() data = user.to_public_json() err_msg = "There are some keys that should not be public" assert data.keys().sort() == public_attributes.sort(), err_msg all_attributes = user.dictize().keys() s = set(public_attributes) private_attributes = [x for x in all_attributes if x not in s] for attr in private_attributes: err_msg = "This attribute should be private %s" % attr assert data.get(attr) is None, err_msg
def test_domain_object_error(self): """Test DomainObject errors work.""" user = User() user.name = "John" d = user.dictize() user.undictize(d)