def test_filter_fields_for_file(module_file):
    file, *_ = module_file

    response = helper.get_resources_response(params=dict(path=file, fields='size'))

    check.response_has_status_code(response, 200)
    check.response_has_only_fields(response, 'size')
def test_filter_fields_for_folder(module_folder):
    folder, *_ = module_folder

    response = helper.get_resources_response(params=dict(path=folder, fields='_embedded'))

    check.response_has_status_code(response, 200)
    check.response_has_only_fields(response, '_embedded')
def test_delete_published_folder(published_folder):
    path, folder_name, *_ = published_folder

    response = helper.delete_resources_response(params=dict(path=path))

    check.response_has_status_code(response, 202, 204)

    with allure.step("Waiting when folder will be deleted"):
        wait(predicate=lambda: helper.get_resources_response(params=dict(
            path=path)).status_code == 404,
             timeout_seconds=30,
             sleep_seconds=(1, 2, 4))

    with allure.step("Deleted folder should be in trash folder"):
        folder_in_trash = helper.get_trash_resources(params=dict(
            path=folder_name))
        assert folder_in_trash[
            'name'] == folder_name, 'Unable to find deleted folder in trash'

    with allure.step("Deleted folder should be unpublished"):
        items = helper.get_public_resources().get('items')
        items_by_name = [
            item for item in items if item.get('name') == folder_name
        ]
        assert len(items_by_name) == 0, "Deleted file still published"
def test_bad_request(module_folder, limit, offset):
    folder, *_ = module_folder

    response = helper.get_resources_response(params=dict(path=folder, limit=limit, offset=offset))

    check.response_has_status_code(response, 400)
    check.response_has_only_fields(response, 'message', 'description', 'error')
    check.response_has_field_with_value(response, field='error', value='FieldValidationError')
def test_expired_user(module_folder):
    folder, *_ = module_folder

    response = helper.get_resources_response(params=dict(path=folder), by_user='******')

    check.response_has_status_code(response, 401)
    check.response_has_only_fields(response, 'message', 'description', 'error')
    check.response_has_field_with_value(response, field='error', value='UnauthorizedError')
def test_uploaded_file_with_same_name_should_have_postfix(base_folder):
    file_path = f'{base_folder}/same_name.txt'
    link = config.data.TEST_DATA_URL['test_txt.txt']
    helper.upload_and_wait_status(params=dict(path=file_path, url=link),
                                  status='success')
    helper.upload_and_wait_status(params=dict(path=file_path, url=link),
                                  status='success')

    with allure.step("First file was uploaded with original name"):
        response = helper.get_resources_response(params=dict(path=file_path))
        check.response_has_status_code(response, 200)
        check.response_has_field_with_value(response, "name", 'same_name.txt')

    with allure.step("Second file was uploaded with postfix"):
        response = helper.get_resources_response(params=dict(
            path=f'{base_folder}/same_name (1).txt'))
        check.response_has_status_code(response, 200)
        check.response_has_field_with_value(response, "name",
                                            'same_name (1).txt')
def test_meta_for_folder(module_folder):
    path, folder_name = module_folder

    response = helper.get_resources_response(params=dict(path=path))

    check.response_has_status_code(response, 200)
    check.response_has_only_fields(
        response,
        '_embedded', 'name', 'exif', 'created', 'resource_id', 'modified', 'path', 'comment_ids', 'type', 'revision'
    )
    check.response_has_field_with_value(response, "type", 'dir')
    check.response_has_field_with_value(response, "name", folder_name)
def test_sort_resources(folder_with_two_files, sorting_type):
    folder, *_ = folder_with_two_files

    response = helper.get_resources_response(params=dict(path=folder, sort=sorting_type))
    check.response_has_status_code(response, 200)
    check.response_has_fields(response, '_embedded')

    with allure.step("Check sorting is correct"):
        json = response.json()
        embedded = json['_embedded']
        items = embedded['items']
        expected_items = sorted(items, key=lambda item: item[sorting_type])
        assert items == expected_items
def test_limit_nested_resources(folder_with_two_files, limit, expected_items, expected_limit):
    folder, *_ = folder_with_two_files

    response = helper.get_resources_response(params=dict(path=folder, limit=limit))

    check.response_has_status_code(response, 200)
    check.response_has_fields(response, '_embedded')

    with allure.step("Check expected amount of resources were given"):
        json = response.json()
        embedded = json['_embedded']
        assert len(embedded.get('items')) == expected_items
        assert embedded.get('limit') == expected_limit
def test_meta_for_file(module_file):
    path, file_name = module_file

    response = helper.get_resources_response(params=dict(path=path))

    check.response_has_status_code(response, 200)
    check.response_has_fields(
        response,
        'antivirus_status', 'file', 'sha256', 'name', 'exif', 'created', 'resource_id', 'modified', 'path',
        'comment_ids', 'type', 'revision', 'media_type', 'md5', 'mime_type', 'size'
    )
    check.response_has_field_with_value(response, "type", 'file')
    check.response_has_field_with_value(response, "mime_type", 'text/plain')
    check.response_has_field_with_value(response, "name", file_name)
def test_offset_nested_resources(folder_with_two_files):
    folder, file_1, file_2, *_ = folder_with_two_files

    response = helper.get_resources_response(params=dict(path=folder, offset=1, sort='created'))
    check.response_has_status_code(response, 200)
    check.response_has_fields(response, '_embedded')

    with allure.step("Check expected amount of files"):
        json = response.json()
        embedded = json['_embedded']
        items = embedded.get('items')
        assert len(items) == 1, "Should be only one file (2 files in directory and offset is 1)"

    with allure.step("Check expected file is taken"):
        item = items[0]
        assert file_2 in item['path'], "Files should be sorted by created date (default sorting type)"
def test_delete_file(temp_file):
    path, file_name, *_ = temp_file()

    response = helper.delete_resources_response(params=dict(path=path))

    check.response_has_status_code(response, 202, 204)

    with allure.step("Waiting when file will be deleted"):
        wait(predicate=lambda: helper.get_resources_response(params=dict(
            path=path)).status_code == 404,
             timeout_seconds=30,
             sleep_seconds=(1, 2, 4))

    with allure.step("Deleted file should be in trash folder"):
        file_in_trash = helper.get_trash_resources(params=dict(path=file_name))
        assert file_in_trash[
            'name'] == file_name, 'Unable to find deleted file in trash'
def test_delete_file_permanently(temp_file):
    path, file_name, *_ = temp_file()

    response = helper.delete_resources_response(
        params=dict(path=path, permanently=True))
    check.response_has_status_code(response, 202, 204)

    with allure.step("Waiting when file will be deleted"):
        wait(predicate=lambda: helper.get_resources_response(params=dict(
            path=path)).status_code == 404,
             timeout_seconds=30,
             sleep_seconds=(1, 2, 4))

    with allure.step("Deleted permanently file should not be in trash folder"):
        response = helper.get_trash_resources_response(params=dict(
            path=file_name))
        assert response.status_code == 404, 'Deleted permanently file should not be in trash'
def test_upload_file_with_type(base_folder, file_name, link, mime_type):
    """ Temporary folder is created, upload in it image, operation should be successful, file should be available and
        have expected name and mime type """
    file_path = f'{base_folder}/{file_name}'
    response = helper.post_upload_resource_response(
        params=dict(path=file_path, url=link))

    check.response_has_status_code(response, 202)
    check.response_has_fields(response, 'href')
    href = response.json().get('href')
    operation_id = href.split('/')[-1]

    helper.when_operation_status(operation_id, 'success')

    with allure.step("File information should be available"):
        response = helper.get_resources_response(params=dict(path=file_path))
        check.response_has_status_code(response, 200)
        check.response_has_field_with_value(response, "mime_type", mime_type)
        check.response_has_field_with_value(response, "name", file_name)
def test_not_found(base_folder):
    response = helper.get_resources_response(params=dict(path=f'{base_folder}/nonexistent.txt'))

    check.response_has_status_code(response, 404)
    check.response_has_only_fields(response, 'message', 'description', 'error')
    check.response_has_field_with_value(response, field='error', value='DiskNotFoundError')