diff --git a/descope/common.py b/descope/common.py index f60992c3..fc2e057a 100644 --- a/descope/common.py +++ b/descope/common.py @@ -1,8 +1,12 @@ +import sys from enum import Enum from descope.exceptions import ERROR_TYPE_INVALID_ARGUMENT, AuthException -DEFAULT_BASE_URL = "https://api.descope.com" +if "unittest" in sys.modules: + DEFAULT_BASE_URL = "http://127.0.0.1" +else: + DEFAULT_BASE_URL = "https://api.descope.com" # pragma: no cover PHONE_REGEX = """^(?:(?:\\(?(?:00|\\+)([1-4]\\d\\d|[1-9]\\d?)\\)?)?[\\-\\.\\ \\\\/]?)?((?:\\(?\\d{1,}\\)?[\\-\\.\\ \\\\/]?){0,})(?:[\\-\\.\\ \\\\/]?(?:#|ext\\.?|extension|x)[\\-\\.\\ \\\\/]?(\\d+))?$""" diff --git a/tests/test_magiclink.py b/tests/test_magiclink.py index 7699e005..6e6e52f9 100644 --- a/tests/test_magiclink.py +++ b/tests/test_magiclink.py @@ -1,3 +1,4 @@ +import json import unittest from unittest import mock from unittest.mock import patch @@ -94,20 +95,6 @@ def test_sign_in(self): magiclink = MagicLink(Auth(self.dummy_project_id, self.public_key_dict)) # Test failed flows - self.assertRaises( - AuthException, - magiclink.sign_in, - DeliveryMethod.EMAIL, - "dummy@dummy", - "http://test.me", - ) - self.assertRaises( - AuthException, - magiclink.sign_in, - DeliveryMethod.EMAIL, - "", - "http://test.me", - ) self.assertRaises( AuthException, magiclink.sign_in, @@ -155,22 +142,6 @@ def test_sign_up(self): magiclink = MagicLink(Auth(self.dummy_project_id, self.public_key_dict)) # Test failed flows - self.assertRaises( - AuthException, - magiclink.sign_up, - DeliveryMethod.EMAIL, - "dummy@dummy", - "http://test.me", - signup_user_details, - ) - self.assertRaises( - AuthException, - magiclink.sign_up, - DeliveryMethod.EMAIL, - "", - "http://test.me", - signup_user_details, - ) self.assertRaises( AuthException, magiclink.sign_up, @@ -226,13 +197,6 @@ def test_sign_up_or_in(self): magiclink = MagicLink(Auth(self.dummy_project_id, self.public_key_dict)) # Test failed flows - self.assertRaises( - AuthException, - magiclink.sign_up_or_in, - DeliveryMethod.EMAIL, - "dummy@dummy", - "http://test.me", - ) with patch("requests.post") as mock_post: mock_post.return_value.ok = False @@ -279,6 +243,29 @@ def test_verify(self): } self.assertIsNotNone(magiclink.verify(token)) + def test_verify_with_get_keys_mock(self): + token = "1234" + magiclink = MagicLink( + Auth(self.dummy_project_id, None) + ) # public key will be "fetched" by Get mock + + # Test success flow + valid_jwt_token = "eyJhbGciOiJFUzM4NCIsImtpZCI6IlAyQ3R6VWhkcXBJRjJ5czlnZzdtczA2VXZ0QzQiLCJ0eXAiOiJKV1QifQ.eyJkcm4iOiJEU1IiLCJleHAiOjIyNjQ0Mzc1OTYsImlhdCI6MTY1OTYzNzU5NiwiaXNzIjoiUDJDdHpVaGRxcElGMnlzOWdnN21zMDZVdnRDNCIsInN1YiI6IlUyQ3UwajBXUHczWU9pUElTSmI1Mkwwd1VWTWcifQ.WLnlHugvzZtrV9OzBB7SjpCLNRvKF3ImFpVyIN5orkrjO2iyAKg_Rb4XHk9sXGC1aW8puYzLbhE1Jv3kk2hDcKggfE8OaRNRm8byhGFZHnvPJwcP_Ya-aRmfAvCLcKOL" + with patch("requests.get") as mock_get: + mock_get.return_value.text = json.dumps([self.public_key_dict]) + mock_get.return_value.ok = True + + with patch("requests.post") as mock_post: + my_mock_response = mock.Mock() + my_mock_response.ok = True + my_mock_response.json.return_value = {} + mock_post.return_value = my_mock_response + mock_post.return_value.cookies = { + SESSION_COOKIE_NAME: "dummy session token", + REFRESH_SESSION_COOKIE_NAME: valid_jwt_token, + } + self.assertIsNotNone(magiclink.verify(token)) + def test_update_user_email(self): magiclink = MagicLink(Auth(self.dummy_project_id, self.public_key_dict)) diff --git a/tests/test_oauth.py b/tests/test_oauth.py index e6ce9a4e..e21b6d5c 100644 --- a/tests/test_oauth.py +++ b/tests/test_oauth.py @@ -124,8 +124,8 @@ def test_exchange_token(self): self.assertRaises(AuthException, oauth.exchange_token, "") self.assertRaises(AuthException, oauth.exchange_token, None) - with patch("requests.get") as mock_get: - mock_get.return_value.ok = False + with patch("requests.post") as mock_post: + mock_post.return_value.ok = False self.assertRaises(AuthException, oauth.exchange_token, "c1") # Test success flow diff --git a/tests/test_otp.py b/tests/test_otp.py index be0e5ef1..dcd63eaa 100644 --- a/tests/test_otp.py +++ b/tests/test_otp.py @@ -90,6 +90,12 @@ def test_compose_update_user_email_body(self): ) def test_sign_up(self): + invalid_signup_user_details = { + "username": "jhon", + "name": "john", + "phone": "972525555555", + "email": "dummy@dummy", + } signup_user_details = { "username": "jhon", "name": "john", @@ -105,21 +111,23 @@ def test_sign_up(self): client.otp.sign_up, DeliveryMethod.EMAIL, "dummy@dummy", - signup_user_details, + invalid_signup_user_details, ) + invalid_signup_user_details["email"] = "dummy@dummy.com" # set valid mail + invalid_signup_user_details["phone"] = "aaaaaaaa" # set invalid phone self.assertRaises( AuthException, client.otp.sign_up, DeliveryMethod.EMAIL, "", - signup_user_details, + invalid_signup_user_details, ) self.assertRaises( AuthException, client.otp.sign_up, - DeliveryMethod.EMAIL, - None, - signup_user_details, + DeliveryMethod.PHONE, + "dummy@dummy.com", + invalid_signup_user_details, ) with patch("requests.post") as mock_post: @@ -167,9 +175,6 @@ def test_sign_in(self): client = DescopeClient(self.dummy_project_id, self.public_key_dict) # Test failed flows - self.assertRaises( - AuthException, client.otp.sign_in, DeliveryMethod.EMAIL, "dummy@dummy" - ) self.assertRaises(AuthException, client.otp.sign_in, DeliveryMethod.EMAIL, "") self.assertRaises(AuthException, client.otp.sign_in, DeliveryMethod.EMAIL, None) @@ -225,13 +230,6 @@ def test_verify_code(self): client = DescopeClient(self.dummy_project_id, self.public_key_dict) - self.assertRaises( - AuthException, - client.otp.verify_code, - DeliveryMethod.EMAIL, - "dummy@dummy", - code, - ) self.assertRaises( AuthException, client.otp.verify_code, DeliveryMethod.EMAIL, "", code ) diff --git a/tests/test_saml.py b/tests/test_saml.py index 725054bb..c126afa8 100644 --- a/tests/test_saml.py +++ b/tests/test_saml.py @@ -118,8 +118,8 @@ def test_exchange_token(self): self.assertRaises(AuthException, saml.exchange_token, "") self.assertRaises(AuthException, saml.exchange_token, None) - with patch("requests.get") as mock_get: - mock_get.return_value.ok = False + with patch("requests.post") as mock_post: + mock_post.return_value.ok = False self.assertRaises(AuthException, saml.exchange_token, "c1") # Test success flow