forked from uc-cdis/peregrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·136 lines (106 loc) · 3.5 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
from authutils import ROLES as all_roles
from collections import defaultdict
from mock import patch, PropertyMock
import os
from peregrine.api import run_for_development
from psqlgraph import PolyNode as Node
import requests
requests.packages.urllib3.disable_warnings()
all_role_values = list(all_roles.values())
roles = defaultdict(lambda: all_role_values)
class FakeBotoKey(object):
def __init__(self, name):
self.name = name
def close(self):
pass
def open_read(self, *args, **kwargs):
pass
@property
def size(self):
return len("fake data for {}".format(self.name))
def __iter__(self):
for string in ["fake ", "data ", "for ", self.name]:
yield string
def fake_get_nodes(dids):
nodes = []
for did in dids:
try:
file_name = files.get(did, {})["data"]["file_name"]
except ValueError:
file_name = did
nodes.append(
Node(
node_id=did,
label="file",
acl=["open"],
properties={
"file_name": file_name,
"file_size": len("fake data for {}".format(did)),
"md5sum": "fake_md5sum",
"state": "live",
},
)
)
return nodes
def fake_urls_from_index_client(did):
return ["s3://fake-host/fake_bucket/{}".format(did)]
def fake_key_for(parsed):
return FakeBotoKey(parsed.netloc.split("/")[-1])
def fake_key_for_node(node):
return FakeBotoKey(node.node_id)
class FakeUser(object):
username = "test"
roles = roles
def set_user(*args, **kwargs):
from flask import g
g.user = FakeUser()
def run_with_fake_auth():
with patch(
"peregrine.auth.CurrentUser.roles",
new_callable=PropertyMock,
return_value=roles,
), patch(
"peregrine.auth.CurrentUser.logged_in",
new_callable=PropertyMock,
return_value=lambda: True,
), patch(
"peregrine.auth.verify_hmac", new=set_user
):
run_for_development(debug=debug, threaded=True)
def run_with_fake_authz():
"""
By mocking `get_read_access_projects`, we avoid checking the
Authorization header and access token, and avoid making arborist
calls to fetch a list of authorized resources.
"""
# `user_projects` contains a list of `project_id`s (in format
# "<program.name>-<project.code>") the user has access to.
# Update it to mock specific access:
user_projects = []
with patch(
"peregrine.resources.submission.get_read_access_projects",
return_value=user_projects,
):
run_for_development(debug=debug, threaded=True)
def run_with_fake_download():
with patch("peregrine.download.get_nodes", fake_get_nodes):
with patch.multiple(
"peregrine.download",
key_for=fake_key_for,
key_for_node=fake_key_for_node,
urls_from_index_client=fake_urls_from_index_client,
):
if os.environ.get("GDC_FAKE_AUTH"):
run_with_fake_auth()
else:
run_for_development(debug=debug, threaded=True)
if __name__ == "__main__":
debug = bool(os.environ.get("PEREGRINE_DEBUG", True))
if os.environ.get("GDC_FAKE_DOWNLOAD") == "True":
run_with_fake_download()
else:
if os.environ.get("GDC_FAKE_AUTH") == "True":
run_with_fake_auth()
else:
run_with_fake_authz()