Skip to content

Commit 4e311bb

Browse files
committed
added additonal checks for org and schema names
1 parent 6e96fba commit 4e311bb

File tree

2 files changed

+82
-101
lines changed

2 files changed

+82
-101
lines changed

synapseclient/models/schema_organization.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,22 +1385,32 @@ def list_json_schema_organizations(
13851385
return all_orgs
13861386

13871387

1388-
def _check_name(name) -> None:
1388+
def _check_name(name: str) -> None:
13891389
"""
13901390
Checks that the input name is a valid Synapse Organization or JSONSchema name
1391-
- Must start with a letter
1392-
- Must contains only letters, numbers and periods.
1391+
- Length requirement of 6 ≤ x ≤ 250
1392+
- Names do not contain the string sagebionetworks (case insensitive)
1393+
- May contain periods (each part is separated by periods)
1394+
- Each part must start with a letter
1395+
- Each part contains only letters and numbers
13931396
13941397
Arguments:
13951398
name: The name of the organization to be checked
13961399
13971400
Raises:
13981401
ValueError: When the name isn't valid
13991402
"""
1400-
if not re.match("^([A-Za-z])([A-Za-z]|\d|\.)*$", name):
1401-
raise ValueError(
1402-
(
1403-
"Name must start with a letter and contain "
1404-
f"only letters numbers and periods: {name}"
1403+
if not 6 <= len(name) <= 250:
1404+
raise ValueError(f"The name must be of length 6 to 250 characters: {name}")
1405+
if re.search("sagebionetworks", name.lower()):
1406+
raise ValueError(f"The name must not contain 'sagebionetworks' : {name}")
1407+
parts = name.split(".")
1408+
for part in parts:
1409+
if not re.match("^([A-Za-z])([A-Za-z]|\d|)*$", part):
1410+
raise ValueError(
1411+
(
1412+
"Name may be separated by periods, "
1413+
"but each part must start with a letter and contain "
1414+
f"only letters and numbers: {name}"
1415+
)
14051416
)
1406-
)
Lines changed: 63 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,13 @@
11
"""Unit tests for SchemaOrganization and JSONSchema classes"""
2-
from typing import Any
3-
42
import pytest
53

64
from synapseclient.models import JSONSchema, SchemaOrganization
7-
from synapseclient.models.schema_organization import CreateSchemaRequest
5+
from synapseclient.models.schema_organization import CreateSchemaRequest, _check_name
86

97

108
class TestSchemaOrganization:
119
"""Synchronous unit tests for SchemaOrganization."""
1210

13-
@pytest.mark.parametrize(
14-
"name",
15-
["AAAAAAA", "A12345", "A....."],
16-
ids=["Just letters", "Numbers", "Periods"],
17-
)
18-
def test_init(self, name: str) -> None:
19-
"Tests that legal names don't raise a ValueError on init"
20-
assert SchemaOrganization(name)
21-
22-
@pytest.mark.parametrize(
23-
"name",
24-
["1AAAAAA", ".AAAAAA", "AAAAAA!"],
25-
ids=["Starts with a number", "Starts with a period", "Special character"],
26-
)
27-
def test_init_name_exceptions(self, name: str) -> None:
28-
"Tests that illegal names raise a ValueError on init"
29-
with pytest.raises(
30-
ValueError,
31-
match="Name must start with a letter and contain only letters numbers and periods",
32-
):
33-
SchemaOrganization(name)
34-
3511
def test_fill_from_dict(self) -> None:
3612
"Tests that fill_from_dict fills in all fields"
3713
organization = SchemaOrganization()
@@ -56,28 +32,6 @@ def test_fill_from_dict(self) -> None:
5632
class TestJSONSchema:
5733
"""Synchronous unit tests for JSONSchema."""
5834

59-
@pytest.mark.parametrize(
60-
"name",
61-
["AAAAAAA", "A12345", "A....."],
62-
ids=["Just letters", "Numbers", "Periods"],
63-
)
64-
def test_init(self, name: str) -> None:
65-
"Tests that legal names don't raise a ValueError on init"
66-
assert JSONSchema(name, "org.name")
67-
68-
@pytest.mark.parametrize(
69-
"name",
70-
["1AAAAAA", ".AAAAAA", "AAAAAA!"],
71-
ids=["Starts with a number", "Starts with a period", "Special character"],
72-
)
73-
def test_init_name_exceptions(self, name: str) -> None:
74-
"Tests that illegal names raise a ValueError on init"
75-
with pytest.raises(
76-
ValueError,
77-
match="Name must start with a letter and contain only letters numbers and periods",
78-
):
79-
JSONSchema(name, "org.name")
80-
8135
@pytest.mark.parametrize(
8236
"uri",
8337
["ORG.NAME-SCHEMA.NAME", "ORG.NAME-SCHEMA.NAME-0.0.1"],
@@ -147,74 +101,91 @@ def test_check_semantic_version_with_exceptions(self) -> None:
147101

148102
class TestCreateSchemaRequest:
149103
@pytest.mark.parametrize(
150-
"name",
151-
["AAAAAAA", "A12345", "A....."],
152-
ids=["Just letters", "Numbers", "Periods"],
104+
"version",
105+
["0.0.1", "1.0.0"],
153106
)
154-
def test_init_name(self, name: str) -> None:
155-
"Tests that legal names don't raise a ValueError on init"
156-
assert CreateSchemaRequest(schema={}, name=name, organization_name="org.name")
107+
def test_init_version(self, version: str) -> None:
108+
"Tests that legal versions don't raise a ValueError on init"
109+
assert CreateSchemaRequest(
110+
schema={}, name="schema.name", organization_name="org.name", version=version
111+
)
157112

158113
@pytest.mark.parametrize(
159-
"name",
160-
["1AAAAAA", ".AAAAAA", "AAAAAA!"],
161-
ids=["Starts with a number", "Starts with a period", "Special character"],
114+
"version",
115+
["1", "1.0", "0.0.0.1", "0.0.0"],
162116
)
163-
def test_init_name_exceptions(self, name: str) -> None:
164-
"Tests that illegal names raise a ValueError on init"
117+
def test_init_version_exceptions(self, version: str) -> None:
118+
"Tests that illegal versions raise a ValueError on init"
165119
with pytest.raises(
166120
ValueError,
167-
match="Name must start with a letter and contain only letters numbers and periods",
121+
match="Schema version must be a semantic version starting at 0.0.1",
168122
):
169-
CreateSchemaRequest(schema={}, name=name, organization_name="org.name")
123+
CreateSchemaRequest(
124+
schema={},
125+
name="schema.name",
126+
organization_name="org.name",
127+
version=version,
128+
)
129+
130+
131+
class TestCheckName:
132+
"""Tests for check name helper function"""
170133

171134
@pytest.mark.parametrize(
172135
"name",
173-
["AAAAAAA", "A12345", "A....."],
174-
ids=["Just letters", "Numbers", "Periods"],
136+
["aaaaaaa", "aaaaaa1", "aa.aa.aa", "a1.a1.a1"],
175137
)
176-
def test_init_org_name(self, name: str) -> None:
177-
"Tests that legal org names don't raise a ValueError on init"
178-
assert CreateSchemaRequest(
179-
schema={}, name="schema.name", organization_name=name
180-
)
138+
def test_check_name(self, name: str):
139+
"""Checks that legal names don't raise an exception"""
140+
_check_name(name)
181141

182142
@pytest.mark.parametrize(
183143
"name",
184-
["1AAAAAA", ".AAAAAA", "AAAAAA!"],
185-
ids=["Starts with a number", "Starts with a period", "Special character"],
144+
[
145+
"a",
146+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
147+
],
186148
)
187-
def test_init_org_name_exceptions(self, name: str) -> None:
188-
"Tests that illegal org names raise a ValueError on init"
149+
def test_check_length_exception(self, name: str):
150+
"""Checks that names that are too short or long raise an exception"""
189151
with pytest.raises(
190-
ValueError,
191-
match="Name must start with a letter and contain only letters numbers and periods",
152+
ValueError, match="The name must be of length 6 to 250 characters"
192153
):
193-
CreateSchemaRequest(schema={}, name="schema.name", organization_name=name)
154+
_check_name(name)
194155

195156
@pytest.mark.parametrize(
196-
"version",
197-
["0.0.1", "1.0.0"],
157+
"name",
158+
[
159+
"sagebionetworks",
160+
"asagebionetworks",
161+
"sagebionetworksa",
162+
"aaa.sagebionetworks.aaa",
163+
"SAGEBIONETWORKS",
164+
"SageBionetworks",
165+
],
198166
)
199-
def test_init_version(self, version: str) -> None:
200-
"Tests that legal versions don't raise a ValueError on init"
201-
assert CreateSchemaRequest(
202-
schema={}, name="schema.name", organization_name="org.name", version=version
203-
)
167+
def test_check_sage_exception(self, name: str):
168+
"""Checks that names that contain 'sagebionetworks' raise an exception"""
169+
with pytest.raises(
170+
ValueError, match="The name must not contain 'sagebionetworks'"
171+
):
172+
_check_name(name)
204173

205174
@pytest.mark.parametrize(
206-
"version",
207-
["1", "1.0", "0.0.0.1", "0.0.0"],
175+
"name",
176+
["1AAAAA", "AAA.1AAA", "AAA.AAA.1AAA", ".AAAAAAA", "AAAAAAAA!"],
177+
ids=[
178+
"Starts with number",
179+
"Part2 starts with number",
180+
"Part3 starts with number",
181+
"Starts with period",
182+
"Contains special characters",
183+
],
208184
)
209-
def test_init_version_exceptions(self, version: str) -> None:
210-
"Tests that illegal versions raise a ValueError on init"
185+
def test_check_content_exception(self, name: str):
186+
"""Checks that names that contain special characters(besides periods) or have parts that start with numbers raise an exception"""
211187
with pytest.raises(
212188
ValueError,
213-
match="Schema version must be a semantic version starting at 0.0.1",
189+
match="Name may be separated by periods, but each part must start with a letter and contain only letters and numbers",
214190
):
215-
CreateSchemaRequest(
216-
schema={},
217-
name="schema.name",
218-
organization_name="org.name",
219-
version=version,
220-
)
191+
_check_name(name)

0 commit comments

Comments
 (0)