Skip to content

Commit

Permalink
update zenodo test
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasprobst committed Dec 13, 2023
1 parent b46cd6e commit 810a0cd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ on:
branches:
- main
- dev
- dev-zenodo

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
python-version: [ '3.8', '3.9' , '3.10', '3.11', '3.12']
mongodb-version: ['5.0', '6.0']
# os: [ ubuntu-latest, macos-latest, windows-latest ]
os: [ windows-latest ]
python-version: [ '3.8', ]
mongodb-version: ['5.0', ]
# python-version: [ '3.8', '3.9' , '3.10', '3.11', '3.12']
# mongodb-version: ['5.0', '6.0']

steps:
- name: Checkout repo
Expand Down Expand Up @@ -55,8 +59,9 @@ jobs:
mongodb-version: ${{ matrix.mongodb-version }}

- name: Run pytest coverage
run: |
pytest --cov --cov-report=xml
# env:
# ENV_SECRET: ${{ secrest.ZENODO_API_TOKEN }}
run: TEST_SECRET=${{ secrets.ZENODO_API_TOKEN }} pytest --cov --cov-report=xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
59 changes: 54 additions & 5 deletions h5rdmtoolbox/database/zenodo/config.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import appdirs
import configparser
import os
import pathlib

import requests
from typing import Union

from h5rdmtoolbox.utils import create_tbx_logger

def get_api_token(sandbox: bool,
zenodo_ini_filename: Union[str, pathlib.Path] = None):
"""Read the Zenodo API token from the config file."""
config = configparser.ConfigParser()
logger = create_tbx_logger('zenodo')


def _parse_ini_file(zenodo_ini_filename: Union[str, pathlib.Path]):
if zenodo_ini_filename is None:
zenodo_ini_filename = pathlib.Path(appdirs.user_data_dir('h5rdmtoolbox')) / 'zenodo.ini'
else:
zenodo_ini_filename = pathlib.Path(zenodo_ini_filename)
if not zenodo_ini_filename.exists():
raise FileNotFoundError(f'File {zenodo_ini_filename} not found.')
return zenodo_ini_filename


def get_api_token(sandbox: bool,
zenodo_ini_filename: Union[str, pathlib.Path] = None):
"""Read the Zenodo API token from the config file."""
env_token = os.environ.get('ZENODO_API_TOKEN', None)
if env_token is not None:
env_token = env_token.strip()
logger.debug('Took token from environment variable ZENODO_API_TOKEN.')
verify_token(sandbox, env_token)
return env_token
zenodo_ini_filename = _parse_ini_file(zenodo_ini_filename)
config = configparser.ConfigParser()
config.read(zenodo_ini_filename)
if sandbox:
api_token = config['zenodo:sandbox']['api_token']
Expand All @@ -24,4 +40,37 @@ def get_api_token(sandbox: bool,
raise ValueError(f'No API token found in {zenodo_ini_filename}. Please verify the correctness of the file '
f'{zenodo_ini_filename}. The api_token entry must be in the section [zenodo] or '
f'[zenodo:sandbox].')
verify_token(sandbox, api_token)
return api_token


def verify_token(sandbox: bool, api_token: str):
# validate the token
if sandbox:
url = 'https://sandbox.zenodo.org/api/deposit/depositions'
else:
url = 'https://zenodo.org/api/deposit/depositions'
r = requests.get(url,
params={'access_token': api_token})
try:
r.raise_for_status()
except requests.exceptions.HTTPError:
raise ValueError(f'Zenodo api token is invalid: {api_token}')


def set_api_token(sandbox: bool,
api_token: str,
zenodo_ini_filename: Union[str, pathlib.Path] = None):
"""Write the Zenodo API token to the config file."""
zenodo_ini_filename = _parse_ini_file(zenodo_ini_filename)
config = configparser.ConfigParser()
config.read(zenodo_ini_filename)
if sandbox:
section = 'zenodo:sandbox'
else:
section = 'zenodo'
if section not in config:
config[section] = {}
config[section]['api_token'] = api_token
with open(zenodo_ini_filename, 'w') as f:
config.write(f)
2 changes: 2 additions & 0 deletions tests/database/test_zenodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def delete_sandbox_deposits(self):
'https://sandbox.zenodo.org/api/deposit/depositions',
params={'access_token': get_api_token(sandbox=True)}
)
r.raise_for_status()
for deposit in r.json():
# if deposit['title'].startswith('[test]'):
if not deposit['submitted']:
Expand Down Expand Up @@ -71,6 +72,7 @@ def test_create_new_deposit(self):
f.write('This is a test file.')
zsr.add_file('testfile.txt')
zsr.create()
zsr.create() # call it again. does it crash?
pathlib.Path('testfile.txt').unlink()
self.assertTrue(zsr.exists())
zsr.delete()
Expand Down

0 comments on commit 810a0cd

Please sign in to comment.