|
import os |
|
import time |
|
import requests |
|
from selenium import webdriver |
|
from selenium.webdriver.common.keys import Keys |
|
from selenium.webdriver.support.ui import WebDriverWait |
|
from selenium.webdriver.support import expected_conditions as EC |
|
from selenium.common.exceptions import TimeoutException |
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
|
BROWSER = f'{BASE_DIR}/chromedriver' |
|
DOMAIN = 'http://adr' |
|
USER = { |
|
'username': 'admin', |
|
'password': 'fjelltopp', |
|
'api_key': '6011357f-a7f8-4367-a47d-8c2ab8059520' |
|
} |
|
DATASET_PREFIX = 'test-dataset-' |
|
DATASET_TYPE = 'dataset-2' |
|
|
|
RESOURCE_TYPE = 'url' |
|
#RESOURCE_TYPE = 'file' |
|
|
|
driver = webdriver.Chrome(BROWSER) |
|
wait = WebDriverWait(driver, 10) |
|
|
|
|
|
def load_elements(xpaths): |
|
for name, xpath in xpaths.items(): |
|
try: |
|
wait.until(lambda driver: driver.find_element_by_xpath(xpath)) |
|
except TimeoutException: |
|
raise Exception(f'Could not find {name} using xpath {xpath}') |
|
return { |
|
name: driver.find_element_by_xpath(xpath) |
|
for name, xpath in xpaths.items() |
|
} |
|
|
|
|
|
print('Logging In') |
|
driver.get(f'{DOMAIN}/user/login') |
|
elements = load_elements({ |
|
'username_input': '//*[@id="field-login"]', |
|
'password_input': '//*[@id="field-password"]', |
|
'hide_debug_toolbar_button': '//*[@id="flHideToolBarButton"]', |
|
}) |
|
elements['hide_debug_toolbar_button'].click() |
|
elements['username_input'].send_keys(USER['username']) |
|
elements['password_input'].send_keys(USER['password']) |
|
elements['password_input'].send_keys(Keys.RETURN) |
|
wait.until(EC.url_to_be(f'{DOMAIN}/dashboard/')) |
|
|
|
print('Creating Dataset') |
|
dataset_name = f'{DATASET_PREFIX}{int(time.time())}' |
|
driver.get(f'{DOMAIN}/{DATASET_TYPE}/new?action=new&controller=package') |
|
elements = load_elements({ |
|
'dataset_title_input': '//*[@id="field-title"]', |
|
'edit_dataset_url_button': '//*[@id="dataset-edit"]/div[1]/div/div/button', |
|
'edit_dataset_form': '//*[@id="dataset-edit"]' |
|
}) |
|
elements['dataset_title_input'].send_keys(dataset_name) |
|
elements['edit_dataset_url_button'].click() |
|
elements['edit_dataset_form'].submit() |
|
wait.until(EC.url_contains(dataset_name)) |
|
|
|
print('Uploading Resource') |
|
upload_resource_url = f'{DOMAIN}/{DATASET_TYPE}/{dataset_name}/resource/new' |
|
resource_name = f'test-resource-{int(time.time())}' |
|
driver.get(upload_resource_url) |
|
if RESOURCE_TYPE == 'url': |
|
desired_resource_url = f'https://example.com?resource_name={resource_name}' |
|
elements = load_elements({ |
|
'link_button': '//*[@id="FileInputComponent"]/div/div/button[2]', |
|
'resource_name_input': '//*[@id="field-name"]', |
|
'add_button': '//*[@id="resource-edit"]/div[6]/button[2]' |
|
}) |
|
elements['link_button'].click() |
|
elements.update( |
|
load_elements({'link_input': '//*[@id="field-url"]'}) |
|
) |
|
elements['resource_name_input'].send_keys(resource_name) |
|
elements['link_input'].send_keys(desired_resource_url) |
|
else: |
|
elements = load_elements({ |
|
'file_uploader': '//*[@id="FileInputComponent"]/div/input', |
|
'resource_name_input': '//*[@id="field-name"]', |
|
'add_button': '//*[@id="resource-edit"]/div[6]/button[2]' |
|
}) |
|
example_file_name = 'example.csv' |
|
elements['file_uploader'].send_keys(f'{BASE_DIR}/{example_file_name}') |
|
load_elements({'uploaded_file': '//*[@id="FileInputComponent"]/h3/i[1]'}) |
|
elements['resource_name_input'].send_keys(resource_name) |
|
elements['add_button'].click() |
|
wait.until(EC.url_changes(upload_resource_url)) |
|
|
|
print('Validating Resource') |
|
api_response_json = requests.get( |
|
f'{DOMAIN}/api/3/action/package_show', |
|
params={'id': dataset_name}, |
|
headers={'Authorization': USER['api_key']}, |
|
).json() |
|
resource = api_response_json['result']['resources'][0] |
|
# TODO: replace with pytest |
|
if RESOURCE_TYPE == 'url': |
|
assert resource['size'] in [None, ''] |
|
assert resource['sha256'] in [None, ''] |
|
assert resource['lfs_prefix'] in [None, ''] |
|
assert resource['url_type'] in [None, ''] |
|
assert resource['url'] == desired_resource_url |
|
# TODO: confirm x-msdos-program is correct |
|
assert resource['format'] == 'application/x-msdos-program' |
|
else: |
|
assert resource['size'] > 0 |
|
assert resource['sha256'] not in [None, ''] |
|
assert resource['lfs_prefix'] not in [None, ''] |
|
assert resource['url_type'] == 'upload' |
|
assert example_file_name in resource['url'] |
|
assert resource['format'] == 'CSV' |
|
print('Booyah! All tests passed') |
|
|
|
print('Cleaning up (deleting all test datasets)') |
|
search_results = requests.get( |
|
f'{DOMAIN}/api/3/action/package_list' |
|
).json() |
|
test_dataset_ids = [ |
|
dataset_name |
|
for dataset_name in search_results['result'] |
|
if DATASET_PREFIX in dataset_name |
|
] |
|
for dataset_id in test_dataset_ids: |
|
requests.post( |
|
f'{DOMAIN}/api/3/action/package_delete', |
|
headers={'Authorization': USER['api_key']}, |
|
data={'id': dataset_id} |
|
).json() |
|
|
|
driver.quit() |