@@ -168,6 +168,7 @@ def _linked_oauth_providers() -> AuthListOAuthProvidersResponse200:
168168
169169class StateTransport :
170170 on_signin : Callable [[], None ] | None = None
171+ on_signup : Callable [[], None ] | None = None
171172
172173 def __init__ (self ) -> None :
173174 self .next_access_token = "access-1"
@@ -343,6 +344,8 @@ def auth_signin(self, **kwargs: Any) -> Response:
343344 def auth_signup (self , ** kwargs : Any ) -> Response :
344345 self .authorizations .append (("signup" , kwargs ["authorization" ]))
345346 self .signup_calls .append (kwargs )
347+ if self .on_signup is not None :
348+ self .on_signup ()
346349 return self .signup_response
347350
348351 def auth_signup_anonymous (self , ** kwargs : Any ) -> Response :
@@ -1181,6 +1184,75 @@ def test_sign_up_uses_empty_metadata_without_creating_a_session() -> None:
11811184 assert transport .signup_calls [0 ]["metadata" ] == {}
11821185
11831186
1187+ @pytest .mark .parametrize ("confirmation_required" , [True , False ])
1188+ @pytest .mark .parametrize ("sign_in_when_allowed" , [True , False ])
1189+ def test_sign_up_only_signs_in_when_opted_in_and_allowed (
1190+ * , confirmation_required : bool , sign_in_when_allowed : bool
1191+ ) -> None :
1192+ transport = StateTransport ()
1193+ transport .signup_response = Response (
1194+ 201 , {"confirmation_required" : confirmation_required , "message" : "Accepted" }
1195+ )
1196+ client = VolcanoClient (anon_key = "anon" , _transport = transport )
1197+ result = client .auth .sign_up (
1198+ email = "new@example.com" ,
1199+ password = "secret" ,
1200+ sign_in_when_allowed = sign_in_when_allowed ,
1201+ )
1202+
1203+ signed_in = sign_in_when_allowed and not confirmation_required
1204+ assert (result .session is not None ) is signed_in
1205+ assert result .session is client .auth .get_session ()
1206+ assert result .confirmation_required is confirmation_required
1207+ assert result .message == "Accepted"
1208+ assert transport .authorizations == [("signup" , "anon" )] + (
1209+ [("auth" , "anon" )] if signed_in else []
1210+ )
1211+
1212+
1213+ @pytest .mark .parametrize ("replace_during" , ["signup" , "signin" ])
1214+ def test_signup_followup_does_not_replace_a_newer_session (replace_during : str ) -> None :
1215+ transport = StateTransport ()
1216+ transport .signup_response = Response (
1217+ 201 , {"confirmation_required" : False , "message" : "Accepted" }
1218+ )
1219+ client = VolcanoClient (anon_key = "anon" , _transport = transport )
1220+ replacement = Session ("replacement" , "refresh" , "other" )
1221+
1222+ def replace_session () -> None :
1223+ client .auth .set_session (replacement )
1224+
1225+ if replace_during == "signup" :
1226+ transport .on_signup = replace_session
1227+ else :
1228+ transport .on_signin = replace_session
1229+ with pytest .raises (SessionChangedError ):
1230+ client .auth .sign_up (
1231+ email = "new@example.com" , password = "secret" , sign_in_when_allowed = True
1232+ )
1233+ assert client .auth .get_session () == replacement
1234+
1235+
1236+ def test_sign_up_surfaces_followup_signin_failure_without_changing_session () -> None :
1237+ transport = StateTransport ()
1238+ client = VolcanoClient (anon_key = "anon" , _transport = transport )
1239+ established = client .auth .sign_in (email = "user@example.com" , password = "secret" )
1240+ transport .signup_response = Response (
1241+ 201 , {"confirmation_required" : False , "message" : "Accepted" }
1242+ )
1243+
1244+ def reject_signin () -> None :
1245+ message = "Sign-in rejected"
1246+ raise AuthenticationError (message )
1247+
1248+ transport .on_signin = reject_signin
1249+ with pytest .raises (AuthenticationError , match = "Sign-in rejected" ):
1250+ client .auth .sign_up (
1251+ email = "new@example.com" , password = "secret" , sign_in_when_allowed = True
1252+ )
1253+ assert client .auth .get_session () is established
1254+
1255+
11841256def test_sign_up_raises_typed_errors_without_changing_session () -> None :
11851257 transport = StateTransport ()
11861258 client = VolcanoClient (anon_key = "anon" , _transport = transport )
0 commit comments