Skip to content

Commit e83eace

Browse files
committed
revert REGEX, add tests, improve validator
1 parent 4c6cf27 commit e83eace

3 files changed

Lines changed: 129 additions & 3 deletions

File tree

osf/external/spam/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
logging.basicConfig(level=logging.INFO)
1616

1717

18-
DOMAIN_REGEX = re.compile(r'(?:'r'(?P<protocol>\w+://)'r'|'r'(?P<www>www\.)'r')'r'(?P<domain>([\w-]+\.)*[\w-]+)'r'(?P<path>[/\-\.\w]*)?', re.IGNORECASE)
18+
DOMAIN_REGEX = re.compile(r'\W*(?P<protocol>\w+://)?(?P<www>www\.)?(?P<domain>([\w-]+\.)+[a-zA-Z]+)(?P<path>[/\-\.\w]*)?\W*')
1919
REDIRECT_CODES = {301, 302, 303, 307, 308}
2020

2121

osf/models/validators.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,18 @@ def validate_email(value):
114114

115115

116116
def has_domain_in_user_fields_for_names(user):
117-
name_content = ' '.join([getattr(user, field) or '' for field in user.DOMAIN_VALIDATION_FIELDS])
117+
name_content = ' '.join(getattr(user, field) or '' for field in user.DOMAIN_VALIDATION_FIELDS).strip()
118+
119+
if not name_content:
120+
return False
121+
122+
text = name_content.lower()
123+
if any(suffix in text for suffix in ['m.sc.', 'msc.', 'phd.', 'ph.d.', 'msc.pt', 'pt.', 'prof.', 'dr.','md.', 'jd.', 'esq.']):
124+
return False
125+
126+
if re.search(r'\b[a-z]\.[a-z0-9-]+\b', text):
127+
return False
128+
118129
return True if DOMAIN_REGEX.search(name_content) else False
119130

120131

osf_tests/test_validators.py

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
from osf.exceptions import ValidationValueError
33

44
from osf.models import validators
5-
from osf_tests.factories import SubjectFactory
5+
from osf.models.validators import has_domain_in_user_fields_for_names
6+
from osf_tests.factories import SubjectFactory, AuthUserFactory
7+
68

79
# Ported from tests/framework/test_mongo.py
810

11+
@pytest.fixture()
12+
def user(user_data):
13+
return AuthUserFactory(**user_data)
14+
15+
916
def test_string_required_passes_with_string():
1017
assert validators.string_required('Hi!') is True
1118

@@ -50,3 +57,111 @@ def test_validate_expand_subject_hierarchy():
5057
subject_list = [fruit._id, '12345_bad_id']
5158
with pytest.raises(ValidationValueError):
5259
validators.expand_subject_hierarchy(subject_list)
60+
61+
62+
@pytest.mark.parametrize(
63+
'user_data',
64+
[
65+
{
66+
'fullname': 'Judith Sarah Preuss, M.Sc.',
67+
'given_name': 'Judith',
68+
'family_name': 'Preuss',
69+
'middle_names': 'Sarah',
70+
'suffix': 'M.Sc.',
71+
},
72+
{
73+
'fullname': 'J.H. van Hateren',
74+
'given_name': 'J.H.',
75+
'family_name': 'van Hateren',
76+
'middle_names': '',
77+
},
78+
{
79+
'fullname': 'Sami-Egil Ahonen',
80+
'given_name': 'Sami-Egil',
81+
'family_name': 'Ahonen',
82+
'middle_names': '',
83+
},
84+
{
85+
'fullname': 'Giovanni Luca Ciampaglia',
86+
'given_name': 'Giovanni Luca',
87+
'family_name': 'Ciampaglia',
88+
'middle_names': '',
89+
},
90+
{
91+
'fullname': 'Joseph P.R.O. Orgel',
92+
'given_name': 'Joseph',
93+
'family_name': 'Orgel',
94+
'middle_names': 'P.R.O.',
95+
},
96+
{
97+
'fullname': 'Andrew Daoust',
98+
'given_name': 'Andrew',
99+
'family_name': 'Daoust',
100+
'middle_names': '',
101+
},
102+
{
103+
'fullname': 'Aidan G.C. Wright',
104+
'given_name': 'Aidan',
105+
'family_name': 'Wright',
106+
'middle_names': 'G.C.',
107+
},
108+
{
109+
'fullname': 'Guillermo Perez Algorta',
110+
'given_name': 'Guillermo',
111+
'family_name': 'Perez Algorta',
112+
'middle_names': '',
113+
},
114+
{
115+
'fullname': 'Sarah Wojkowski, MSc.PT, PhD.',
116+
'given_name': 'Sarah',
117+
'family_name': 'Wojkowski',
118+
'middle_names': 'MSc.PT',
119+
},
120+
{
121+
'fullname': 'Brockmann, L.C. (Leon)',
122+
'given_name': 'Leon',
123+
'family_name': 'Brockmann',
124+
'middle_names': 'L.C.',
125+
},
126+
{
127+
'fullname': 'Gragnolati, G.M. (Gaia Mariavittoria)',
128+
'given_name': 'Gaia',
129+
'family_name': 'Gragnolati',
130+
'middle_names': 'G.M.',
131+
},
132+
{
133+
'fullname': 'F.H. Leeuwis',
134+
'given_name': 'F.H.',
135+
'family_name': 'Leeuwis',
136+
'middle_names': '',
137+
},
138+
{
139+
'fullname': 'Grauss, S.E. (Sophie)',
140+
'given_name': 'Sophie',
141+
'family_name': 'Grauss',
142+
'middle_names': 'S.E.',
143+
},
144+
{
145+
'fullname': 'Sandhya N.Sathesh',
146+
'given_name': 'Sandhya',
147+
'family_name': 'N.Sathesh',
148+
'middle_names': '',
149+
},
150+
{
151+
'fullname': 'John Doe',
152+
'given_name': 'John',
153+
'family_name': 'Doe',
154+
'middle_names': '',
155+
}
156+
]
157+
)
158+
def test_has_domain_in_user_fields_for_names_returns_false(user, user_data):
159+
user.DOMAIN_VALIDATION_FIELDS = [
160+
'fullname',
161+
'given_name',
162+
'middle_names',
163+
'family_name',
164+
'suffix',
165+
]
166+
167+
assert has_domain_in_user_fields_for_names(user) is False

0 commit comments

Comments
 (0)