-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add |openneuro-py login" command to enable authentication (#74)
To download restricted datasets, we need to authenticate ourselves using an API token. This PR adds a new command `openneuro login` to write the API token to the `~/.openneuro` config file. The `openneuro-py download` command has been modified to send the token along as a cookie. fixes #60 Co-authored-by: Richard Höchenberger <[email protected]>
- Loading branch information
1 parent
6afb471
commit a82c677
Showing
9 changed files
with
149 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from pathlib import Path | ||
from unittest import mock | ||
|
||
import openneuro | ||
from openneuro.config import init_config, load_config, get_token, Config | ||
|
||
|
||
def test_config(tmp_path: Path): | ||
"""Test creating and reading the config file.""" | ||
with mock.patch.object(openneuro.config, 'CONFIG_PATH', tmp_path / '.openneuro'): | ||
assert not openneuro.config.CONFIG_PATH.exists() | ||
|
||
with mock.patch('getpass.getpass', lambda _: 'test'): | ||
init_config() | ||
assert openneuro.config.CONFIG_PATH.exists() | ||
|
||
expected_config = Config(endpoint='https://openneuro.org/', apikey='test') | ||
assert load_config() == expected_config | ||
assert get_token() == 'test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
from pathlib import Path | ||
|
||
import pytest | ||
from unittest import mock | ||
import openneuro | ||
from openneuro import download | ||
|
||
|
||
|
@@ -130,3 +132,19 @@ def test_doi_handling(tmp_path: Path): | |
include=['participants.tsv'], | ||
target_dir=tmp_path | ||
) | ||
|
||
|
||
def test_restricted_dataset(tmp_path: Path): | ||
"""Test downloading a restricted dataset.""" | ||
# API token for dummy user [email protected] | ||
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxOGNhNjE2ZS00OWQxLTRmOTUtODI1OS0xNzYwYzVhYjZjMDciLCJlbWFpbCI6ImFsaWpmbHNkdmJqaWVsc2Rsa2pmZWlsanN2akBnbWFpbC5jb20iLCJwcm92aWRlciI6Imdvb2dsZSIsIm5hbWUiOiJzZGZrbGVpamZsa3NkamYgc2xmZGRsa2phYWlmbCIsImFkbWluIjpmYWxzZSwiaWF0IjoxNjY1NDY4MjM4LCJleHAiOjE2OTcwMDQyMzh9.7YVL_Cagli84nTmumdcmrV1bW5hZMq3VJlMUDmTEpGU' # noqa | ||
|
||
with mock.patch.object(openneuro.config, 'CONFIG_PATH', tmp_path / '.openneuro'): | ||
with mock.patch('getpass.getpass', lambda _: token): | ||
openneuro.config.init_config() | ||
|
||
# This is a restricted dataset that is only available if the API token | ||
# was used correctly. | ||
download(dataset='ds004287', target_dir=tmp_path) | ||
|
||
assert (tmp_path / 'README').exists() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters