async def test_validate_attr_FILE_None(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_FILE', attr_type=ATTR.FILE(), attr_val=None, mode='create', )
async def test_validate_attr_FILE_None_allow_none(): attr_val = await validate_attr( attr_name='test_validate_attr_FILE', attr_type=ATTR.FILE(), attr_val=None, mode='update', ) assert attr_val == None
async def test_validate_attr_FILE_dict_invalid(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_FILE', attr_type=ATTR.FILE(), attr_val={'key': 'value'}, mode='create', )
async def test_validate_attr_FILE_default_None(): attr_type = ATTR.FILE() attr_type._default = 'test_validate_attr_FILE' attr_val = await validate_attr( attr_name='test_validate_attr_FILE', attr_type=attr_type, attr_val=None, mode='create', ) assert attr_val == 'test_validate_attr_FILE'
async def test_validate_attr_FILE_default_int_allow_none(): attr_type = ATTR.FILE() attr_type._default = 'test_validate_attr_FILE' attr_val = await validate_attr( attr_name='test_validate_attr_FILE', attr_type=attr_type, attr_val=1, mode='update', ) assert attr_val == None
def test_generate_attr_FILE_no_args(): attr_val = utils.generate_attr(attr_type=ATTR.FILE()) assert type(attr_val) == dict assert set(k for k in attr_val.keys()) == { 'name', 'lastModified', 'type', 'size', 'content', } assert attr_val['type'] == 'text/plain' assert attr_val['name'].endswith('.txt')
class File(BaseModule): '''`File` module provides functionality for `File Upload Workflow`.''' collection = 'files' attrs = { 'user': ATTR.ID(desc='`_id` of `User` doc file belongs to.'), 'file': ATTR.FILE(desc='File object.'), 'create_time': ATTR.DATETIME( desc='Python `datetime` ISO format of the doc creation.' ), } methods = { 'read': METHOD(permissions=[PERM(privilege='__sys')]), 'create': METHOD( permissions=[PERM(privilege='create')], post_method=True, ), 'delete': METHOD(permissions=[PERM(privilege='__sys')]), } async def on_read(self, results, skip_events, env, query, doc, payload): for i in range(len(results['docs'])): results['docs'][i]['file']['lastModified'] = int( results['docs'][i]['file']['lastModified'] ) return (results, skip_events, env, query, doc, payload) async def pre_create(self, skip_events, env, query, doc, payload): if Config.file_upload_limit != -1 and len(doc['file']) > Config.file_upload_limit: raise self.exception( status=400, msg=f'File size is beyond allowed limit.', args={ 'code': 'INVALID_SIZE', 'attr': doc['__attr'].decode('utf-8'), 'name': doc['name'].decode('utf-8'), }, ) if (module := doc['__module'].decode('utf-8')) not in Config.modules.keys(): raise self.exception( status=400, msg=f'Invalid module \'{module}\'', args={'code': 'INVALID_MODULE'}, ) try: attr_type = _extract_attr( scope=Registry.module(module).attrs, attr_path='$__' + (attr := doc['__attr'].decode('utf-8')), )
async def test_validate_attr_FILE_file_list(): file_attr_val = { 'name': '__filename', 'type': 'mime/type', 'lastModified': 0, 'size': 6, 'content': b'__file', } attr_val = await validate_attr( attr_name='test_validate_attr_FILE', attr_type=ATTR.FILE(), attr_val=[file_attr_val], mode='create', ) assert attr_val == file_attr_val
def test_generate_attr_FILE_types_extension(): attr_val = utils.generate_attr(attr_type=ATTR.FILE( types=['image/*', '*.png'])) assert attr_val['type'] == 'image/*' assert attr_val['name'].endswith('.png')
def test_generate_attr_FILE_types(): attr_val = utils.generate_attr(attr_type=ATTR.FILE(types=['image/*'])) assert attr_val['type'] == 'image/*' assert attr_val['name'].endswith('.txt')