Beispiel #1
0
    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_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())