Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__
.idea
tests/api_key
9 changes: 5 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ The tests are run regularly on travis-ci.com.

Running the tests
-----------------
You need to have Python 3 installed. Either define the environment variable
You need to have Python 3 installed. You may define the environment variable
`DCOR_API_KEY` or create the file `api_key` containing your API key in the
`tests` directory. Then run:
`tests` directory. Otherwise, a test user is created automatically on DCOR-dev
and its API key is stored in `tests/api_key`. Then run:

::

pip install -r requirements
py.test tests

Note that datasets, collections, and circles created during testing are
only purged for registered testing users (e.g. "remote_tester").
Note that all users, datasets, collections, and circles on DCOR-dev are
purged regularly.


.. |Tests Status| image:: https://img.shields.io/github/actions/workflow/status/DCOR-dev/dcor-remote-tests/check.yml
Expand Down
35 changes: 28 additions & 7 deletions tests/helper.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import functools
import inspect
import os
import pathlib
import time

from dcoraid.api import CKANAPI
from dcoraid.api import CKANAPI, create_dcor_dev_user, is_valid_api_key


SERVER = "dcor-dev.mpl.mpg.de"
Expand All @@ -14,24 +15,43 @@ def get_api():
return api


@functools.lru_cache
def get_api_key():
"""Return the API key of the test user

The API key is taken from the `DCOR_API_KEY` environment variable.
If that is not set, the key stored in the file `tests/api_key` is
used. If that file does not exist or the key is invalid (users on
DCOR-dev are purged regularly), a new user is created on DCOR-dev
and its API key is written to `tests/api_key`.
"""
key = os.environ.get("DCOR_API_KEY")
if not key:
# local file
kp = pathlib.Path(__file__).parent / "api_key"
if not kp.exists():
raise ValueError("No DCOR_API_KEY variable or api_key file!")
key = kp.read_text().strip()
if kp.exists():
key = kp.read_text().strip()
if not is_valid_api_key(server=SERVER, api_key=key):
key = create_dcor_dev_user(server=SERVER,
fullname="DCOR remote tester")
kp.write_text(key)
return key


@functools.lru_cache
def get_user_name():
"""Return the name of the test user"""
return get_api().get_user_dict()["name"]


def make_dataset_dict(circle=None, hint=None):
"""Generate a test dataset in a test circle

Parameters
----------
circle: str or None
circle name; if None, the calling functions module is used
circle name; if None, the calling functions module is used,
prefixed with the name of the test user (circle names are
unique on DCOR and test users are created on the fly)
hint: str
descriptive dataset title string; if None, the calling function
is used; the time since the epoch is added to the dataset title
Expand All @@ -41,7 +61,8 @@ def make_dataset_dict(circle=None, hint=None):
hint = "test " + inspect.currentframe().f_back.f_code.co_name
if circle is None:
filename = inspect.currentframe().f_back.f_code.co_filename
circle = filename.split("/")[-1].rsplit(".", 1)[0]
module = filename.split("/")[-1].rsplit(".", 1)[0]
circle = f"{get_user_name()}-{module}".replace("_", "-")
dataset_dict = {
"title": "{} {}".format(hint, time.time()),
"private": True,
Expand Down
7 changes: 5 additions & 2 deletions tests/test_dcserv.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_access_public_data():
assert res is True


def test_access_private_data():
def test_access_private_data(monkeypatch):
"""Upload a private dataset and try to access it"""
# create some metadata
dataset_dict = make_dataset_dict()
Expand Down Expand Up @@ -92,7 +92,10 @@ def test_access_private_data():
0.011666297,
atol=0, rtol=1e-5)

# Try to access the private dataset as a public user
# Try to access the private dataset as a public user. dclab remembers
# all API keys used in this process, so we have to forget them first.
monkeypatch.setattr(dclab.rtdc_dataset.fmt_dcor.api.APIHandler,
"api_keys", [])
with pytest.raises(dclab.rtdc_dataset.fmt_dcor.api.DCORAccessError):
dclab.new_dataset(res_id, host=api.server, dcserv_api_version=2)

Expand Down
Loading