def test_create_package_managed():
    package_id = 'devtest_createmanaged'
    notes = 'devtest_createmanaged description'

    with ckan_editor_utils.CKANEditorSession(CKAN_URL, CKAN_KEY) as cm:
        res_put1 = cm.put_dataset(
            {
                'name': package_id,
                'extra:identifier': package_id,
                'notes': notes,
                'owner_org': CKAN_ORG,
            },
            skip_existing=False)
        res_put2 = cm.put_dataset(
            {
                'name': package_id,
                'extra:identifier': package_id,
                'notes': notes,
                'owner_org': CKAN_ORG,
            },
            skip_existing=True)

    res_show = requests.get(CKAN_URL + 'package_show',
                            params={'id': package_id},
                            headers=dict(Authorization=CKAN_KEY))

    requests.post(CKAN_URL + 'dataset_purge',
                  data={'id': package_id},
                  headers=dict(Authorization=CKAN_KEY))

    assert res_put1.ok
    assert res_show.json()['result']['notes'] == notes
    assert res_put2.result['notes'] == notes
def _test_resource_upload():
    s3_obj_summary = s3r.ObjectSummary(bucket_name=BUCKET_NAME,
                                       key='Dev/_DSC5121_stitcha.jpg')

    filename = os.path.basename(s3_obj_summary.key)
    package_id = 'devtest_resourceupload_direct'

    ckan_editor_utils.package_create(
        CKAN_URL, CKAN_KEY, {
            'name': package_id,
            'extra:identifier': package_id,
            'notes': 'some description',
            'owner_org': CKAN_ORG
        })

    res_create = ckan_editor_utils.resource_create(
        CKAN_URL, CKAN_KEY, {
            'package_id': package_id,
            'url': filename,
            'name': filename,
            'notes': 'my photo',
            'multipart_name': filename,
            'url_type': 'upload',
        })

    resource_id = res_create.json()['result']['id']

    # upload to ckan
    with ckan_editor_utils.CKANEditorSession(CKAN_URL, CKAN_KEY) as cm:
        res_put = cm._upload_s3_resource(resource_id, s3_obj_summary)

    # download from s3 to get md5
    md5_orig = hashlib.md5(s3_obj_summary.get()["Body"].read()).hexdigest()
    # logger.info('md5 from s3 ' + md5_orig)

    # download from ckan to get md5
    res_show = requests.get(CKAN_URL + 'package_show',
                            params={'id': package_id},
                            headers=dict(Authorization=CKAN_KEY))
    # logger.info(res_show.json())
    resource_data = res_show.json()['result']['resources'][-1]

    ckan_obj = requests.get(resource_data['url'])

    md5_ckan = hashlib.md5(ckan_obj.content).hexdigest()

    requests.post(CKAN_URL + 'resource_delete',
                  data={'id': resource_data['id']},
                  headers=dict(Authorization=CKAN_KEY))
    requests.post(CKAN_URL + 'dataset_purge',
                  data={'id': package_id},
                  headers=dict(Authorization=CKAN_KEY))
    assert md5_ckan == md5_orig
def test_managed_delete():
    package_id = 'devtest_manageddelete'
    with ckan_editor_utils.CKANEditorSession(CKAN_URL, CKAN_KEY) as cm:
        cm.put_dataset({
            'name': package_id,
            'extra:identifier': package_id,
            'notes': 'some description',
            'owner_org': CKAN_ORG
        })
        cm.delete_dataset(package_id)

    res_show = ckan_editor_utils.package_show(CKAN_URL, CKAN_KEY, package_id)
    assert res_show.status_code == 404
def _test_create_resource_managed_one():
    package_id = 'devtest_managedcreateresourceone'
    s3_obj_summary = s3r.ObjectSummary(bucket_name=BUCKET_NAME,
                                       key='Dev/_DSC5121_stitcha.jpg')
    with ckan_editor_utils.CKANEditorSession(CKAN_URL, CKAN_KEY) as cm:
        cm.put_dataset({
            'name': package_id,
            'extra:identifier': package_id,
            'notes': 'some description',
            'owner_org': CKAN_ORG
        })
        resp_res = cm.put_resource_from_s3(
            {
                'name': package_id,
                'resource:name': 'myphoto',
                'resource:description': 'myphoto description'
            },
            skip_existing=True,
            s3_path='s3://sra-data-extract-copy/Dev/_DSC5121_stitcha.jpg')
    md5_orig = hashlib.md5(s3_obj_summary.get()["Body"].read()).hexdigest()
    # logger.info('md5 from s3 ' + md5_orig)

    # download from ckan to get md5
    res_show = requests.get(CKAN_URL + 'package_show',
                            params={'id': package_id},
                            headers=dict(Authorization=CKAN_KEY))
    # logger.info(res_show.json())
    resource_data = res_show.json()['result']['resources'][-1]

    ckan_obj = requests.get(resource_data['url'])

    md5_ckan = hashlib.md5(ckan_obj.content).hexdigest()

    requests.post(CKAN_URL + 'resource_delete',
                  data={'id': resource_data['id']},
                  headers=dict(Authorization=CKAN_KEY))
    requests.post(CKAN_URL + 'dataset_purge',
                  data={'id': package_id},
                  headers=dict(Authorization=CKAN_KEY))

    assert md5_ckan == md5_orig
def _test_create_resource_managed_two():
    package_id = 'devtest_managedcreateresourcetwo'
    s3_obj_summary = s3r.ObjectSummary(bucket_name=BUCKET_NAME,
                                       key='Dev/_DSC5121_stitcha.jpg')
    with ckan_editor_utils.CKANEditorSession(CKAN_URL, CKAN_KEY) as cm:
        cm.put_dataset({
            'name': package_id,
            'extra:identifier': package_id,
            'notes': 'some description',
            'owner_org': CKAN_ORG
        })
        cm.put_resource_from_s3(
            {
                'name': package_id,
                'resource:name': 'myphoto',
                'resource:description': 'myphoto description'
            },
            skip_existing=True,
            s3_path='s3://sra-data-extract-copy/Dev/_DSC5121_stitcha.jpg')
        res_put = cm.put_resource_from_s3(
            {
                'name': package_id,
                'resource:name': 'myphoto',
                'resource:description': 'myphoto description'
            },
            skip_existing=True,
            s3_path='s3://sra-data-extract-copy/Dev/_DSC5121_stitcha.jpg')

    requests.post(CKAN_URL + 'resource_delete',
                  data={'id': res_put.result['resources'][-1]['id']},
                  headers=dict(Authorization=CKAN_KEY))
    requests.post(CKAN_URL + 'dataset_purge',
                  data={'id': package_id},
                  headers=dict(Authorization=CKAN_KEY))

    assert res_put.result['num_resources'] == 1