Skip to content

Commit e2b4635

Browse files
authored
[SYNPY-1553] Removes Blank Auth Header (#1185)
* strips auth from headers if blank * adds message about authenticating * updates comment * updates client and tests * pre-commit * adds syn.credentials = None handling and test * updates warning mock for missing ones
1 parent 3b5bc03 commit e2b4635

File tree

4 files changed

+188
-122
lines changed

4 files changed

+188
-122
lines changed

Diff for: synapseclient/api/entity_factory.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,19 @@ def _check_entity_restrictions(
412412
if restriction_information and restriction_information.get(
413413
"hasUnmetAccessRequirement", None
414414
):
415-
warning_message = (
416-
"\nThis entity has access restrictions. Please visit the web page for this entity "
417-
f'(syn.onweb("{synapse_id}")). Look for the "Access" label and the lock icon underneath '
418-
'the file name. Click "Request Access", and then review and fulfill the file '
419-
"download requirement(s).\n"
420-
)
415+
if not syn.credentials or not syn.credentials._token:
416+
warning_message = (
417+
"You have not provided valid credentials for authentication with Synapse."
418+
" Please provide an authentication token and use `synapseclient.login()` before your next attempt."
419+
" See https://python-docs.synapse.org/tutorials/authentication/ for more information."
420+
)
421+
else:
422+
warning_message = (
423+
"\nThis entity has access restrictions. Please visit the web page for this entity "
424+
f'(syn.onweb("{synapse_id}")). Look for the "Access" label and the lock icon underneath '
425+
'the file name. Click "Request Access", and then review and fulfill the file '
426+
"download requirement(s).\n"
427+
)
421428
if download_file and bundle.get("entityType") not in ("project", "folder"):
422429
raise SynapseUnmetAccessRestrictions(warning_message)
423-
syn.logger.warn(warning_message)
430+
syn.logger.warning(warning_message)

Diff for: synapseclient/client.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -1445,17 +1445,26 @@ def _check_entity_restrictions(
14451445
Raises:
14461446
SynapseUnmetAccessRestrictions: Warning for unmet access requirements.
14471447
"""
1448-
restrictionInformation = bundle["restrictionInformation"]
1449-
if restrictionInformation["hasUnmetAccessRequirement"]:
1450-
warning_message = (
1451-
"\nThis entity has access restrictions. Please visit the web page for this entity "
1452-
f'(syn.onweb("{id_of(entity)}")). Look for the "Access" label and the lock icon underneath '
1453-
'the file name. Click "Request Access", and then review and fulfill the file '
1454-
"download requirement(s).\n"
1455-
)
1448+
restriction_information = bundle.get("restrictionInformation", None)
1449+
if restriction_information and restriction_information.get(
1450+
"hasUnmetAccessRequirement", None
1451+
):
1452+
if not self.credentials or not self.credentials._token:
1453+
warning_message = (
1454+
"You have not provided valid credentials for authentication with Synapse."
1455+
" Please provide an authentication token and use `synapseclient.login()` before your next attempt."
1456+
" See https://python-docs.synapse.org/tutorials/authentication/ for more information."
1457+
)
1458+
else:
1459+
warning_message = (
1460+
"\nThis entity has access restrictions. Please visit the web page for this entity "
1461+
f'(syn.onweb("{id_of(entity)}")). Look for the "Access" label and the lock icon underneath '
1462+
'the file name. Click "Request Access", and then review and fulfill the file '
1463+
"download requirement(s).\n"
1464+
)
14561465
if downloadFile and bundle.get("entityType") not in ("project", "folder"):
14571466
raise SynapseUnmetAccessRestrictions(warning_message)
1458-
warnings.warn(warning_message)
1467+
self.logger.warning(warning_message)
14591468

14601469
def _getFromFile(
14611470
self, filepath: str, limitSearch: str = None, md5: str = None

Diff for: synapseclient/core/credentials/cred_data.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def secret(self) -> str:
109109
return self._token
110110

111111
def __call__(self, r):
112-
r.headers.update({"Authorization": f"Bearer {self.secret}"})
112+
if self.secret:
113+
r.headers.update({"Authorization": f"Bearer {self.secret}"})
113114
return r
114115

115116
def __repr__(self):

Diff for: tests/unit/synapseclient/unit_test_client.py

+154-105
Original file line numberDiff line numberDiff line change
@@ -1157,121 +1157,169 @@ def expected_request_json(token) -> str:
11571157
)
11581158

11591159

1160-
def test_check_entity_restrictions_no_unmet_restriction(syn: Synapse) -> None:
1161-
with patch("warnings.warn") as mocked_warn:
1162-
bundle = {
1163-
"entity": {
1164-
"id": "syn123",
1165-
"name": "anonymous",
1166-
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1167-
"parentId": "syn12345",
1168-
},
1169-
"restrictionInformation": {"hasUnmetAccessRequirement": False},
1170-
}
1171-
entity = "syn123"
1172-
syn._check_entity_restrictions(bundle, entity, True)
1173-
mocked_warn.assert_not_called()
1174-
1160+
class TestCheckEntityRestrictions:
1161+
@pytest.fixture(autouse=True, scope="function")
1162+
def init_syn(self, syn: Synapse) -> None:
1163+
self.syn = syn
1164+
self.syn.credentials = SynapseAuthTokenCredentials(token="abc", username="def")
11751165

1176-
def test_check_entity_restrictions_unmet_restriction_entity_file_with_download_file_is_true(
1177-
syn: Synapse,
1178-
) -> None:
1179-
with patch("warnings.warn") as mocked_warn:
1180-
bundle = {
1181-
"entity": {
1182-
"id": "syn123",
1183-
"name": "anonymous",
1184-
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1185-
"parentId": "syn12345",
1186-
},
1187-
"entityType": "file",
1188-
"restrictionInformation": {"hasUnmetAccessRequirement": True},
1189-
}
1190-
entity = "syn123"
1191-
pytest.raises(
1192-
SynapseUnmetAccessRestrictions,
1193-
syn._check_entity_restrictions,
1194-
bundle,
1195-
entity,
1196-
True,
1197-
)
1198-
mocked_warn.assert_not_called()
1166+
def test_check_entity_restrictions_no_unmet_restriction(self) -> None:
1167+
with patch("logging.Logger.warning") as mocked_warn:
1168+
bundle = {
1169+
"entity": {
1170+
"id": "syn123",
1171+
"name": "anonymous",
1172+
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1173+
"parentId": "syn12345",
1174+
},
1175+
"restrictionInformation": {"hasUnmetAccessRequirement": False},
1176+
}
1177+
entity = "syn123"
1178+
self.syn._check_entity_restrictions(bundle, entity, True)
1179+
mocked_warn.assert_not_called()
11991180

1181+
def test_check_entity_restrictions_unmet_restriction_entity_file_with_download_file_is_true(
1182+
self,
1183+
) -> None:
1184+
with patch("logging.Logger.warning") as mocked_warn:
1185+
bundle = {
1186+
"entity": {
1187+
"id": "syn123",
1188+
"name": "anonymous",
1189+
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1190+
"parentId": "syn12345",
1191+
},
1192+
"entityType": "file",
1193+
"restrictionInformation": {"hasUnmetAccessRequirement": True},
1194+
}
1195+
entity = "syn123"
1196+
pytest.raises(
1197+
SynapseUnmetAccessRestrictions,
1198+
self.syn._check_entity_restrictions,
1199+
bundle,
1200+
entity,
1201+
True,
1202+
)
1203+
mocked_warn.assert_not_called()
12001204

1201-
def test_check_entity_restrictions_unmet_restriction_entity_project_with_download_file_is_true(
1202-
syn: Synapse,
1203-
) -> None:
1204-
with patch("warnings.warn") as mocked_warn:
1205-
bundle = {
1206-
"entity": {
1207-
"id": "syn123",
1208-
"name": "anonymous",
1209-
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1210-
"parentId": "syn12345",
1211-
},
1212-
"entityType": "project",
1213-
"restrictionInformation": {"hasUnmetAccessRequirement": True},
1214-
}
1215-
entity = "syn123"
1216-
syn._check_entity_restrictions(bundle, entity, True)
1217-
mocked_warn.assert_called_with(
1218-
"\nThis entity has access restrictions. Please visit the web page for this entity "
1219-
'(syn.onweb("syn123")). Look for the "Access" label and the lock icon underneath '
1220-
'the file name. Click "Request Access", and then review and fulfill the file '
1221-
"download requirement(s).\n"
1222-
)
1205+
def test_check_entity_restrictions_unmet_restriction_entity_project_with_download_file_is_true(
1206+
self,
1207+
) -> None:
1208+
with patch("logging.Logger.warning") as mocked_warn:
1209+
bundle = {
1210+
"entity": {
1211+
"id": "syn123",
1212+
"name": "anonymous",
1213+
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1214+
"parentId": "syn12345",
1215+
},
1216+
"entityType": "project",
1217+
"restrictionInformation": {"hasUnmetAccessRequirement": True},
1218+
}
1219+
entity = "syn123"
1220+
self.syn._check_entity_restrictions(bundle, entity, True)
1221+
mocked_warn.assert_called_with(
1222+
"\nThis entity has access restrictions. Please visit the web page for this entity "
1223+
'(syn.onweb("syn123")). Look for the "Access" label and the lock icon underneath '
1224+
'the file name. Click "Request Access", and then review and fulfill the file '
1225+
"download requirement(s).\n"
1226+
)
12231227

1228+
def test_check_entity_restrictions_unmet_restriction_entity_folder_with_download_file_is_true_and_no_token(
1229+
self,
1230+
) -> None:
1231+
with patch("logging.Logger.warning") as mocked_warn:
1232+
bundle = {
1233+
"entity": {
1234+
"id": "syn123",
1235+
"name": "anonymous",
1236+
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1237+
"parentId": "syn12345",
1238+
},
1239+
"entityType": "folder",
1240+
"restrictionInformation": {"hasUnmetAccessRequirement": True},
1241+
}
1242+
entity = "syn123"
1243+
self.syn.credentials = SynapseAuthTokenCredentials(token="")
1244+
self.syn._check_entity_restrictions(bundle, entity, True)
1245+
mocked_warn.assert_called_with(
1246+
"You have not provided valid credentials for authentication with Synapse."
1247+
" Please provide an authentication token and use `synapseclient.login()` before your next attempt."
1248+
" See https://python-docs.synapse.org/tutorials/authentication/ for more information."
1249+
)
12241250

1225-
def test_check_entity_restrictions_unmet_restriction_entity_folder_with_download_file_is_true(
1226-
syn: Synapse,
1227-
) -> None:
1228-
with patch("warnings.warn") as mocked_warn:
1229-
bundle = {
1230-
"entity": {
1231-
"id": "syn123",
1232-
"name": "anonymous",
1233-
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1234-
"parentId": "syn12345",
1235-
},
1236-
"entityType": "folder",
1237-
"restrictionInformation": {"hasUnmetAccessRequirement": True},
1238-
}
1239-
entity = "syn123"
1240-
syn._check_entity_restrictions(bundle, entity, True)
1241-
mocked_warn.assert_called_with(
1242-
"\nThis entity has access restrictions. Please visit the web page for this entity "
1243-
'(syn.onweb("syn123")). Look for the "Access" label and the lock icon underneath '
1244-
'the file name. Click "Request Access", and then review and fulfill the file '
1245-
"download requirement(s).\n"
1246-
)
1251+
def test_check_entity_restrictions_unmet_restriction_entity_folder_with_download_file_is_true_and_no_credentials(
1252+
self,
1253+
) -> None:
1254+
with patch("logging.Logger.warning") as mocked_warn:
1255+
bundle = {
1256+
"entity": {
1257+
"id": "syn123",
1258+
"name": "anonymous",
1259+
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1260+
"parentId": "syn12345",
1261+
},
1262+
"entityType": "folder",
1263+
"restrictionInformation": {"hasUnmetAccessRequirement": True},
1264+
}
1265+
entity = "syn123"
1266+
self.syn.credentials = None
1267+
self.syn._check_entity_restrictions(bundle, entity, True)
1268+
mocked_warn.assert_called_with(
1269+
"You have not provided valid credentials for authentication with Synapse."
1270+
" Please provide an authentication token and use `synapseclient.login()` before your next attempt."
1271+
" See https://python-docs.synapse.org/tutorials/authentication/ for more information."
1272+
)
12471273

1274+
def test_check_entity_restrictions_unmet_restriction_entity_folder_with_download_file_is_true(
1275+
self,
1276+
) -> None:
1277+
with patch("logging.Logger.warning") as mocked_warn:
1278+
bundle = {
1279+
"entity": {
1280+
"id": "syn123",
1281+
"name": "anonymous",
1282+
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1283+
"parentId": "syn12345",
1284+
},
1285+
"entityType": "folder",
1286+
"restrictionInformation": {"hasUnmetAccessRequirement": True},
1287+
}
1288+
entity = "syn123"
1289+
self.syn._check_entity_restrictions(bundle, entity, True)
1290+
mocked_warn.assert_called_with(
1291+
"\nThis entity has access restrictions. Please visit the web page for this entity "
1292+
'(syn.onweb("syn123")). Look for the "Access" label and the lock icon underneath '
1293+
'the file name. Click "Request Access", and then review and fulfill the file '
1294+
"download requirement(s).\n"
1295+
)
12481296

1249-
def test_check_entity_restrictions__unmet_restriction_downloadFile_is_False(
1250-
syn: Synapse,
1251-
) -> None:
1252-
with patch("warnings.warn") as mocked_warn:
1253-
bundle = {
1254-
"entity": {
1255-
"id": "syn123",
1256-
"name": "anonymous",
1257-
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1258-
"parentId": "syn12345",
1259-
},
1260-
"entityType": "file",
1261-
"restrictionInformation": {"hasUnmetAccessRequirement": True},
1262-
}
1263-
entity = "syn123"
1297+
def test_check_entity_restrictions__unmet_restriction_downloadFile_is_False(
1298+
self,
1299+
) -> None:
1300+
with patch("logging.Logger.warning") as mocked_warn:
1301+
bundle = {
1302+
"entity": {
1303+
"id": "syn123",
1304+
"name": "anonymous",
1305+
"concreteType": "org.sagebionetworks.repo.model.FileEntity",
1306+
"parentId": "syn12345",
1307+
},
1308+
"entityType": "file",
1309+
"restrictionInformation": {"hasUnmetAccessRequirement": True},
1310+
}
1311+
entity = "syn123"
12641312

1265-
syn._check_entity_restrictions(bundle, entity, False)
1266-
mocked_warn.assert_called_once()
1313+
self.syn._check_entity_restrictions(bundle, entity, False)
1314+
mocked_warn.assert_called_once()
12671315

1268-
bundle["entityType"] = "project"
1269-
syn._check_entity_restrictions(bundle, entity, False)
1270-
assert mocked_warn.call_count == 2
1316+
bundle["entityType"] = "project"
1317+
self.syn._check_entity_restrictions(bundle, entity, False)
1318+
assert mocked_warn.call_count == 2
12711319

1272-
bundle["entityType"] = "folder"
1273-
syn._check_entity_restrictions(bundle, entity, False)
1274-
assert mocked_warn.call_count == 3
1320+
bundle["entityType"] = "folder"
1321+
self.syn._check_entity_restrictions(bundle, entity, False)
1322+
assert mocked_warn.call_count == 3
12751323

12761324

12771325
class TestGetColumns(object):
@@ -1299,6 +1347,7 @@ class TestPrivateGetEntityBundle:
12991347
@pytest.fixture(autouse=True, scope="function")
13001348
def init_syn(self, syn: Synapse) -> None:
13011349
self.syn = syn
1350+
self.syn.credentials = SynapseAuthTokenCredentials(token="abc", username="def")
13021351

13031352
@pytest.fixture(scope="function", autouse=True)
13041353
def setup_method(self) -> None:

0 commit comments

Comments
 (0)