Skip to content

Commit d9151bc

Browse files
author
Renzo Frigato
committed
add test for batch download
1 parent 8dd8750 commit d9151bc

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

r.config

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# vim: filetype=sh
2+
3+
#SCITRAN_RUNTIME_HOST="127.0.0.1"
4+
#SCITRAN_RUNTIME_PORT="8080"
5+
#SCITRAN_RUNTIME_PATH="./runtime"
6+
#SCITRAN_RUNTIME_SSL_PEM="*"
7+
#SCITRAN_RUNTIME_BOOTSTRAP="bootstrap.json"
8+
9+
#SCITRAN_CORE_DEBUG=false # emit stack trace on error
10+
SCITRAN_CORE_INSECURE=true # accept user name as query param
11+
#SCITRAN_CORE_LOG_LEVEL=debug
12+
#SCITRAN_CORE_NEWRELIC=none
13+
SCITRAN_CORE_DRONE_SECRET="scitran_drone"
14+
15+
#SCITRAN_SITE_ID=""
16+
#SCITRAN_SITE_NAME=""
17+
#SCITRAN_SITE_URL=""
18+
#SCITRAN_SITE_CENTRAL_URL=""
19+
#SCITRAN_SITE_REGISTERED=""
20+
#SCITRAN_SITE_SSL_CERT=""
21+
22+
#SCITRAN_PERSISTENT_PATH="./persistent"
23+
#SCITRAN_PERSISTENT_DATA_PATH="./persistent/data" # for fine-grain control
24+
#SCITRAN_PERSISTENT_DB_PATH="./persistent/db" # for fine-grain control
25+
#SCITRAN_PERSISTENT_DB_PORT=9001
26+
#SCITRAN_PERSISTENT_DB_URI="mongodb://localhost:$SCITRAN_PERSISTENT_DB_PORT/scitran"
27+
28+
#SCITRAN_AUTH_AUTH_ENDPOINT=""
29+
#SCITRAN_AUTH_CLIENT_ID=""
30+
#SCITRAN_AUTH_ID_ENDPOINT=""
31+
#SCITRAN_AUTH_VERIFY_ENDPOINT=""
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import requests
2+
import json
3+
import time
4+
import logging
5+
from nose.tools import with_setup
6+
7+
log = logging.getLogger(__name__)
8+
sh = logging.StreamHandler()
9+
log.addHandler(sh)
10+
log.setLevel(logging.INFO)
11+
12+
13+
base_url = 'http://localhost:8080/api'
14+
test_data = type('',(object,),{})()
15+
16+
session = None
17+
18+
def setup_download():
19+
global session
20+
session = requests.Session()
21+
# all the requests will be performed as root
22+
session.params = {
23+
'user': 'test@user.com',
24+
'root': True
25+
}
26+
27+
# Create a group
28+
test_data.group_id = 'test_group_' + str(int(time.time()))
29+
payload = {
30+
'_id': test_data.group_id
31+
}
32+
payload = json.dumps(payload)
33+
r = session.post(base_url + '/groups', data=payload)
34+
assert r.ok
35+
36+
# Create a project
37+
payload = {
38+
'group': test_data.group_id,
39+
'label': 'scitran_testing',
40+
'public': False
41+
}
42+
payload = json.dumps(payload)
43+
r = session.post(base_url + '/projects', data=payload)
44+
test_data.pid = json.loads(r.content)['_id']
45+
assert r.ok
46+
log.debug('pid = \'{}\''.format(test_data.pid))
47+
48+
# Create a session
49+
payload = {
50+
'project': test_data.pid,
51+
'label': 'session_testing',
52+
'public': False
53+
}
54+
payload = json.dumps(payload)
55+
r = session.post(base_url + '/sessions', data=payload)
56+
assert r.ok
57+
test_data.sid = json.loads(r.content)['_id']
58+
log.debug('sid = \'{}\''.format(test_data.sid))
59+
60+
# Create an acquisition
61+
payload = {
62+
'session': test_data.sid,
63+
'label': 'acq_testing',
64+
'public': False
65+
}
66+
payload = json.dumps(payload)
67+
r = session.post(base_url + '/acquisitions', data=payload)
68+
assert r.ok
69+
test_data.aid = json.loads(r.content)['_id']
70+
log.debug('aid = \'{}\''.format(test_data.aid))
71+
72+
## multiform fields for the file upload
73+
files = {'file': ('test.csv', 'some,data,to,send\nanother,row,to,send\n'),
74+
'tags': ('', '["incomplete"]'),
75+
'metadata': ('',
76+
'{"group": {"_id": "scitran"}, ' \
77+
'"project": {"label": "Testdata"}, ' \
78+
'"session": {"uid": "1.2.840.113619.6.353.10437158128305161617400354036593525848"}, ' \
79+
'"file": {"type": "nifti"}, '
80+
'"acquisition": {"uid": "1.2.840.113619.2.353.4120.7575399.14591.1403393566.658_1", ' \
81+
'"timestamp": "1970-01-01T00:00:00", ' \
82+
'"label": "Screen Save", ' \
83+
'"instrument": "MRI", ' \
84+
'"measurement": "screensave", ' \
85+
'"timezone": "America/Los_Angeles"}, ' \
86+
'"subject": {"code": "ex7236"}}'
87+
)}
88+
89+
# upload the same file to each container created in the test
90+
session.post(base_url + '/acquisitions/' + test_data.aid +'/files', files=files)
91+
session.post(base_url + '/sessions/' + test_data.sid +'/files', files=files)
92+
session.post(base_url + '/projects/' + test_data.pid +'/files', files=files)
93+
94+
95+
def teardown_download():
96+
success = True
97+
# remove all the container created in the test
98+
r = session.delete(base_url + '/acquisitions/' + test_data.aid)
99+
success = success and r.ok
100+
r = session.delete(base_url + '/sessions/' + test_data.sid)
101+
success = success and r.ok
102+
r = session.delete(base_url + '/projects/' + test_data.pid)
103+
success = success and r.ok
104+
r = session.delete(base_url + '/groups/' + test_data.group_id)
105+
success = success and r.ok
106+
session.close()
107+
if not success:
108+
log.error('error in the teardown. These containers may have not been removed.')
109+
log.error(str(test_data.__dict__))
110+
111+
112+
@with_setup(setup_download, teardown_download)
113+
def test_download():
114+
# Retrieve a ticket for a batch download
115+
payload = {
116+
'optional': False,
117+
'nodes': [
118+
{
119+
'level': 'project',
120+
'_id': test_data.pid
121+
}
122+
]
123+
}
124+
payload = json.dumps(payload)
125+
r = session.post(base_url + '/download', data=payload)
126+
assert r.ok
127+
128+
# Perform the download
129+
ticket = json.loads(r.content)['ticket']
130+
r = session.get(base_url + '/download', params={'ticket': ticket})
131+
assert r.ok
132+
# Save the tar to a file if successful
133+
f = open('test_download.tar', 'w')
134+
f.write(r.content)
135+
f.close()
136+
137+

0 commit comments

Comments
 (0)