Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set localhost as default base_url (instead of prod link) #88

Merged
merged 10 commits into from
Nov 28, 2022
6 changes: 5 additions & 1 deletion descope/common.py
Original file line number Diff line number Diff line change
@@ -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+))?$"""

Expand Down
61 changes: 24 additions & 37 deletions tests/test_magiclink.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import unittest
from unittest import mock
from unittest.mock import patch
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand 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
Expand Down Expand Up @@ -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))

Expand Down
4 changes: 2 additions & 2 deletions tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
guyp-descope marked this conversation as resolved.
Show resolved Hide resolved
mock_post.return_value.ok = False
self.assertRaises(AuthException, oauth.exchange_token, "c1")

# Test success flow
Expand Down
28 changes: 13 additions & 15 deletions tests/test_otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"] = "[email protected]" # 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,
"[email protected]",
invalid_signup_user_details,
)

with patch("requests.post") as mock_post:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_saml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down