Skip to content

Commit 621149e

Browse files
guyp-descopeomercnetdorsha
authored
Set localhost as default base_url (instead of prod link) (#88)
* set localhost as default base_url (instead of prod link) + remove unnecessary ut * add magiclink test for full flow of fetching (mock) public-key by Get request Co-authored-by: Omer Cohen <[email protected]> Co-authored-by: Doron Sharon <[email protected]>
1 parent b5f0184 commit 621149e

File tree

5 files changed

+46
-57
lines changed

5 files changed

+46
-57
lines changed

descope/common.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import sys
12
from enum import Enum
23

34
from descope.exceptions import ERROR_TYPE_INVALID_ARGUMENT, AuthException
45

5-
DEFAULT_BASE_URL = "https://api.descope.com"
6+
if "unittest" in sys.modules:
7+
DEFAULT_BASE_URL = "http://127.0.0.1"
8+
else:
9+
DEFAULT_BASE_URL = "https://api.descope.com" # pragma: no cover
610

711
PHONE_REGEX = """^(?:(?:\\(?(?:00|\\+)([1-4]\\d\\d|[1-9]\\d?)\\)?)?[\\-\\.\\ \\\\/]?)?((?:\\(?\\d{1,}\\)?[\\-\\.\\ \\\\/]?){0,})(?:[\\-\\.\\ \\\\/]?(?:#|ext\\.?|extension|x)[\\-\\.\\ \\\\/]?(\\d+))?$"""
812

tests/test_magiclink.py

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import unittest
23
from unittest import mock
34
from unittest.mock import patch
@@ -94,20 +95,6 @@ def test_sign_in(self):
9495
magiclink = MagicLink(Auth(self.dummy_project_id, self.public_key_dict))
9596

9697
# Test failed flows
97-
self.assertRaises(
98-
AuthException,
99-
magiclink.sign_in,
100-
DeliveryMethod.EMAIL,
101-
"dummy@dummy",
102-
"http://test.me",
103-
)
104-
self.assertRaises(
105-
AuthException,
106-
magiclink.sign_in,
107-
DeliveryMethod.EMAIL,
108-
"",
109-
"http://test.me",
110-
)
11198
self.assertRaises(
11299
AuthException,
113100
magiclink.sign_in,
@@ -155,22 +142,6 @@ def test_sign_up(self):
155142
magiclink = MagicLink(Auth(self.dummy_project_id, self.public_key_dict))
156143

157144
# Test failed flows
158-
self.assertRaises(
159-
AuthException,
160-
magiclink.sign_up,
161-
DeliveryMethod.EMAIL,
162-
"dummy@dummy",
163-
"http://test.me",
164-
signup_user_details,
165-
)
166-
self.assertRaises(
167-
AuthException,
168-
magiclink.sign_up,
169-
DeliveryMethod.EMAIL,
170-
"",
171-
"http://test.me",
172-
signup_user_details,
173-
)
174145
self.assertRaises(
175146
AuthException,
176147
magiclink.sign_up,
@@ -226,13 +197,6 @@ def test_sign_up_or_in(self):
226197
magiclink = MagicLink(Auth(self.dummy_project_id, self.public_key_dict))
227198

228199
# Test failed flows
229-
self.assertRaises(
230-
AuthException,
231-
magiclink.sign_up_or_in,
232-
DeliveryMethod.EMAIL,
233-
"dummy@dummy",
234-
"http://test.me",
235-
)
236200

237201
with patch("requests.post") as mock_post:
238202
mock_post.return_value.ok = False
@@ -279,6 +243,29 @@ def test_verify(self):
279243
}
280244
self.assertIsNotNone(magiclink.verify(token))
281245

246+
def test_verify_with_get_keys_mock(self):
247+
token = "1234"
248+
magiclink = MagicLink(
249+
Auth(self.dummy_project_id, None)
250+
) # public key will be "fetched" by Get mock
251+
252+
# Test success flow
253+
valid_jwt_token = "eyJhbGciOiJFUzM4NCIsImtpZCI6IlAyQ3R6VWhkcXBJRjJ5czlnZzdtczA2VXZ0QzQiLCJ0eXAiOiJKV1QifQ.eyJkcm4iOiJEU1IiLCJleHAiOjIyNjQ0Mzc1OTYsImlhdCI6MTY1OTYzNzU5NiwiaXNzIjoiUDJDdHpVaGRxcElGMnlzOWdnN21zMDZVdnRDNCIsInN1YiI6IlUyQ3UwajBXUHczWU9pUElTSmI1Mkwwd1VWTWcifQ.WLnlHugvzZtrV9OzBB7SjpCLNRvKF3ImFpVyIN5orkrjO2iyAKg_Rb4XHk9sXGC1aW8puYzLbhE1Jv3kk2hDcKggfE8OaRNRm8byhGFZHnvPJwcP_Ya-aRmfAvCLcKOL"
254+
with patch("requests.get") as mock_get:
255+
mock_get.return_value.text = json.dumps([self.public_key_dict])
256+
mock_get.return_value.ok = True
257+
258+
with patch("requests.post") as mock_post:
259+
my_mock_response = mock.Mock()
260+
my_mock_response.ok = True
261+
my_mock_response.json.return_value = {}
262+
mock_post.return_value = my_mock_response
263+
mock_post.return_value.cookies = {
264+
SESSION_COOKIE_NAME: "dummy session token",
265+
REFRESH_SESSION_COOKIE_NAME: valid_jwt_token,
266+
}
267+
self.assertIsNotNone(magiclink.verify(token))
268+
282269
def test_update_user_email(self):
283270
magiclink = MagicLink(Auth(self.dummy_project_id, self.public_key_dict))
284271

tests/test_oauth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ def test_exchange_token(self):
124124
self.assertRaises(AuthException, oauth.exchange_token, "")
125125
self.assertRaises(AuthException, oauth.exchange_token, None)
126126

127-
with patch("requests.get") as mock_get:
128-
mock_get.return_value.ok = False
127+
with patch("requests.post") as mock_post:
128+
mock_post.return_value.ok = False
129129
self.assertRaises(AuthException, oauth.exchange_token, "c1")
130130

131131
# Test success flow

tests/test_otp.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ def test_compose_update_user_email_body(self):
9090
)
9191

9292
def test_sign_up(self):
93+
invalid_signup_user_details = {
94+
"username": "jhon",
95+
"name": "john",
96+
"phone": "972525555555",
97+
"email": "dummy@dummy",
98+
}
9399
signup_user_details = {
94100
"username": "jhon",
95101
"name": "john",
@@ -105,21 +111,23 @@ def test_sign_up(self):
105111
client.otp.sign_up,
106112
DeliveryMethod.EMAIL,
107113
"dummy@dummy",
108-
signup_user_details,
114+
invalid_signup_user_details,
109115
)
116+
invalid_signup_user_details["email"] = "[email protected]" # set valid mail
117+
invalid_signup_user_details["phone"] = "aaaaaaaa" # set invalid phone
110118
self.assertRaises(
111119
AuthException,
112120
client.otp.sign_up,
113121
DeliveryMethod.EMAIL,
114122
"",
115-
signup_user_details,
123+
invalid_signup_user_details,
116124
)
117125
self.assertRaises(
118126
AuthException,
119127
client.otp.sign_up,
120-
DeliveryMethod.EMAIL,
121-
None,
122-
signup_user_details,
128+
DeliveryMethod.PHONE,
129+
130+
invalid_signup_user_details,
123131
)
124132

125133
with patch("requests.post") as mock_post:
@@ -167,9 +175,6 @@ def test_sign_in(self):
167175
client = DescopeClient(self.dummy_project_id, self.public_key_dict)
168176

169177
# Test failed flows
170-
self.assertRaises(
171-
AuthException, client.otp.sign_in, DeliveryMethod.EMAIL, "dummy@dummy"
172-
)
173178
self.assertRaises(AuthException, client.otp.sign_in, DeliveryMethod.EMAIL, "")
174179
self.assertRaises(AuthException, client.otp.sign_in, DeliveryMethod.EMAIL, None)
175180

@@ -225,13 +230,6 @@ def test_verify_code(self):
225230

226231
client = DescopeClient(self.dummy_project_id, self.public_key_dict)
227232

228-
self.assertRaises(
229-
AuthException,
230-
client.otp.verify_code,
231-
DeliveryMethod.EMAIL,
232-
"dummy@dummy",
233-
code,
234-
)
235233
self.assertRaises(
236234
AuthException, client.otp.verify_code, DeliveryMethod.EMAIL, "", code
237235
)

tests/test_saml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ def test_exchange_token(self):
118118
self.assertRaises(AuthException, saml.exchange_token, "")
119119
self.assertRaises(AuthException, saml.exchange_token, None)
120120

121-
with patch("requests.get") as mock_get:
122-
mock_get.return_value.ok = False
121+
with patch("requests.post") as mock_post:
122+
mock_post.return_value.ok = False
123123
self.assertRaises(AuthException, saml.exchange_token, "c1")
124124

125125
# Test success flow

0 commit comments

Comments
 (0)