99 PlatformMembership ,
1010 PlatformMembershipHistory ,
1111)
12+ from routers .sbp_register import validate_sbp_email_domain
1213from schemas .biocommons import BiocommonsRegisterData
1314from tests .datagen import (
1415 Auth0UserDataFactory ,
@@ -23,7 +24,7 @@ def valid_registration_data():
2324 username = "testuser" ,
2425 first_name = "Test" ,
2526 last_name = "User" ,
26- email = "test@example.com " ,
27+ email = "test@unsw.edu.au " ,
2728 reason = "Need access to SBP resources" ,
2829 password = "SecurePass123!" ,
2930 ).model_dump ()
@@ -37,6 +38,34 @@ def test_to_biocommons_register_data():
3738 assert register_data .app_metadata .registration_from == "sbp"
3839
3940
41+ def test_validate_sbp_email_domain_function ():
42+ from unittest .mock import Mock
43+ mock_settings = Mock ()
44+ mock_settings .sbp_allowed_email_domains = [
45+ "unsw.edu.au" , "ad.unsw.edu.au" , "student.unsw.edu.au" ,
46+ "biocommons.org.au" ,
47+ "sydney.edu.au" , "uni.sydney.edu.au" ,
48+ "wehi.edu.au" ,
49+ "monash.edu" , "student.monash.edu" ,
50+ "griffith.edu.au" , "griffithuni.edu.au" ,
51+ "unimelb.edu.au" , "student.unimelb.edu.au"
52+ ]
53+
54+ # Test approved domains
55+ assert validate_sbp_email_domain ("user@unsw.edu.au" , mock_settings )
56+ assert validate_sbp_email_domain ("user@biocommons.org.au" , mock_settings )
57+ assert validate_sbp_email_domain ("user@sydney.edu.au" , mock_settings )
58+ assert validate_sbp_email_domain ("USER@UNSW.EDU.AU" , mock_settings )
59+
60+ # Test rejected domains
61+ assert not validate_sbp_email_domain ("user@gmail.com" , mock_settings )
62+ assert not validate_sbp_email_domain ("user@unsw.com" , mock_settings )
63+ assert not validate_sbp_email_domain ("user@biocommons.org" , mock_settings )
64+ assert not validate_sbp_email_domain ("user@evilunsw.edu.au" , mock_settings )
65+ assert not validate_sbp_email_domain ("user@malicious.biocommons.org.au" , mock_settings )
66+ assert not validate_sbp_email_domain ("user@fakeunimelb.edu.au" , mock_settings )
67+
68+
4069def test_successful_registration (
4170 test_client , valid_registration_data , mock_auth0_client , test_db_session
4271):
@@ -136,3 +165,14 @@ def test_registration_email_format(test_client, valid_registration_data):
136165 details = response .json ()
137166 errors = details ["field_errors" ]
138167 assert "email" in [error ["field" ] for error in errors ]
168+
169+
170+ def test_registration_rejected_email_domains (test_client , valid_registration_data , mock_auth0_client ):
171+ data = valid_registration_data .copy ()
172+ data ["email" ] = "user@gmail.com"
173+
174+ response = test_client .post ("/sbp/register" , json = data )
175+
176+ assert response .status_code == 400
177+ assert "Email domain not approved for SBP registration" in response .json ()["message" ]
178+ assert not mock_auth0_client .create_user .called
0 commit comments