|
7 | 7 | from supabase_auth.errors import AuthInvalidJwtError, AuthSessionMissingError |
8 | 8 | from supabase_auth.helpers import decode_jwt |
9 | 9 |
|
10 | | -from .clients import GOTRUE_JWT_SECRET, auth_client, auth_client_with_asymmetric_session |
| 10 | +from .clients import ( |
| 11 | + GOTRUE_JWT_SECRET, |
| 12 | + auth_client, |
| 13 | + auth_client_with_asymmetric_session, |
| 14 | + auth_client_with_session, |
| 15 | +) |
11 | 16 | from .utils import mock_user_credentials |
12 | 17 |
|
13 | 18 |
|
@@ -189,3 +194,97 @@ async def test_set_session_with_invalid_token(): |
189 | 194 | # Try to set the session with invalid tokens |
190 | 195 | with pytest.raises(AuthInvalidJwtError): |
191 | 196 | await client.set_session("invalid.token.here", "invalid_refresh_token") |
| 197 | + |
| 198 | + |
| 199 | +async def test_mfa_enroll(): |
| 200 | + client = auth_client_with_session() |
| 201 | + |
| 202 | + credentials = mock_user_credentials() |
| 203 | + |
| 204 | + # First sign up to get a valid session |
| 205 | + await client.sign_up( |
| 206 | + { |
| 207 | + "email": credentials.get("email"), |
| 208 | + "password": credentials.get("password"), |
| 209 | + } |
| 210 | + ) |
| 211 | + |
| 212 | + # Test MFA enrollment |
| 213 | + enroll_response = await client.mfa.enroll( |
| 214 | + {"issuer": "test-issuer", "factor_type": "totp", "friendly_name": "test-factor"} |
| 215 | + ) |
| 216 | + |
| 217 | + assert enroll_response.id is not None |
| 218 | + assert enroll_response.type == "totp" |
| 219 | + assert enroll_response.friendly_name == "test-factor" |
| 220 | + assert enroll_response.totp.qr_code is not None |
| 221 | + |
| 222 | + |
| 223 | +async def test_mfa_challenge(): |
| 224 | + client = auth_client() |
| 225 | + credentials = mock_user_credentials() |
| 226 | + |
| 227 | + # First sign up to get a valid session |
| 228 | + signup_response = await client.sign_up( |
| 229 | + { |
| 230 | + "email": credentials.get("email"), |
| 231 | + "password": credentials.get("password"), |
| 232 | + } |
| 233 | + ) |
| 234 | + assert signup_response.session is not None |
| 235 | + |
| 236 | + # Enroll a factor first |
| 237 | + enroll_response = await client.mfa.enroll( |
| 238 | + {"factor_type": "totp", "issuer": "test-issuer", "friendly_name": "test-factor"} |
| 239 | + ) |
| 240 | + |
| 241 | + # Test MFA challenge |
| 242 | + challenge_response = await client.mfa.challenge({"factor_id": enroll_response.id}) |
| 243 | + assert challenge_response.id is not None |
| 244 | + assert challenge_response.expires_at is not None |
| 245 | + |
| 246 | + |
| 247 | +async def test_mfa_unenroll(): |
| 248 | + client = auth_client() |
| 249 | + credentials = mock_user_credentials() |
| 250 | + |
| 251 | + # First sign up to get a valid session |
| 252 | + signup_response = await client.sign_up( |
| 253 | + { |
| 254 | + "email": credentials.get("email"), |
| 255 | + "password": credentials.get("password"), |
| 256 | + } |
| 257 | + ) |
| 258 | + assert signup_response.session is not None |
| 259 | + |
| 260 | + # Enroll a factor first |
| 261 | + enroll_response = await client.mfa.enroll( |
| 262 | + {"factor_type": "totp", "issuer": "test-issuer", "friendly_name": "test-factor"} |
| 263 | + ) |
| 264 | + |
| 265 | + # Test MFA unenroll |
| 266 | + unenroll_response = await client.mfa.unenroll({"factor_id": enroll_response.id}) |
| 267 | + assert unenroll_response.id == enroll_response.id |
| 268 | + |
| 269 | + |
| 270 | +async def test_mfa_list_factors(): |
| 271 | + client = auth_client() |
| 272 | + credentials = mock_user_credentials() |
| 273 | + |
| 274 | + # First sign up to get a valid session |
| 275 | + signup_response = await client.sign_up( |
| 276 | + { |
| 277 | + "email": credentials.get("email"), |
| 278 | + "password": credentials.get("password"), |
| 279 | + } |
| 280 | + ) |
| 281 | + assert signup_response.session is not None |
| 282 | + |
| 283 | + # Enroll a factor first |
| 284 | + await client.mfa.enroll( |
| 285 | + {"factor_type": "totp", "issuer": "test-issuer", "friendly_name": "test-factor"} |
| 286 | + ) |
| 287 | + |
| 288 | + # Test MFA list factors |
| 289 | + list_response = await client.mfa.list_factors() |
| 290 | + assert len(list_response.all) == 1 |
0 commit comments