Skip to content

Commit 52d5a61

Browse files
Henry Chanhenrylamchan
authored andcommitted
Refactor mocking response
1 parent 3ccd20f commit 52d5a61

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

tests/conftest.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import pytest
2+
import requests
23

34
import workos
45

6+
class MockResponse(object):
7+
def __init__(self, response_dict, status_code):
8+
self.response_dict = response_dict
9+
self.status_code = status_code
10+
11+
def json(self):
12+
return self.response_dict
13+
514
@pytest.fixture
615
def set_api_key(monkeypatch):
716
monkeypatch.setattr(workos, 'api_key', 'sk_abdsomecharactersm284')
@@ -12,4 +21,14 @@ def set_project_id(monkeypatch):
1221

1322
@pytest.fixture
1423
def set_api_key_and_project_id(set_api_key, set_project_id):
15-
pass
24+
pass
25+
26+
@pytest.fixture
27+
def mock_request_method(monkeypatch):
28+
def _mock_request_method(method, response_dict, status_code):
29+
def mock_response(*args, **kwargs):
30+
return MockResponse(response_dict, status_code)
31+
32+
monkeypatch.setattr(requests, method, mock_response)
33+
34+
return _mock_request_method

tests/test_sso.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@
22
from urllib.parse import parse_qsl, urlparse
33

44
import pytest
5-
import requests
65

76
import workos
87
from workos.sso import SSO
98
from workos.utils.requests import RESPONSE_TYPE_CODE
109

11-
class MockResponse(object):
12-
def __init__(self, response_dict, status_code):
13-
self.response_dict = response_dict
14-
self.status_code = status_code
15-
16-
def json(self):
17-
return self.response_dict
18-
1910
class TestSSO(object):
2011
@pytest.fixture(autouse=True)
2112
def setup(self, set_api_key_and_project_id):
@@ -36,24 +27,6 @@ def mock_profile(self):
3627
'idp_id': '00u1klkowm8EGah2H357'
3728
}
3829

39-
@pytest.fixture
40-
def mock_request_post_profile(self, monkeypatch, mock_profile):
41-
def mock_post(*args, **kwargs):
42-
return MockResponse({
43-
'profile': {
44-
'object': 'profile',
45-
'id': mock_profile['id'],
46-
'email': mock_profile['email'],
47-
'first_name': mock_profile['first_name'],
48-
'connection_type': mock_profile['connection_type'],
49-
'last_name': mock_profile['last_name'],
50-
'idp_id': mock_profile['idp_id'],
51-
},
52-
'access_token': '01DY34ACQTM3B1CSX1YSZ8Z00D',
53-
}, 200)
54-
55-
monkeypatch.setattr(requests, 'post', mock_post)
56-
5730
def test_authorization_url_has_expected_query_params(self):
5831
authorization_url = self.sso.get_authorization_url(
5932
self.customer_domain,
@@ -73,8 +46,23 @@ def test_authorization_url_has_expected_query_params(self):
7346

7447

7548
def test_get_profile_returns_expected_workosprofile_object(
76-
self, mock_request_post_profile, mock_profile
49+
self, mock_profile, mock_request_method
7750
):
51+
response_dict = {
52+
'profile': {
53+
'object': 'profile',
54+
'id': mock_profile['id'],
55+
'email': mock_profile['email'],
56+
'first_name': mock_profile['first_name'],
57+
'connection_type': mock_profile['connection_type'],
58+
'last_name': mock_profile['last_name'],
59+
'idp_id': mock_profile['idp_id'],
60+
},
61+
'access_token': '01DY34ACQTM3B1CSX1YSZ8Z00D',
62+
}
63+
64+
mock_request_method('post', response_dict, 200)
65+
7866
profile = self.sso.get_profile(123)
7967

8068
assert profile.to_dict() == mock_profile

0 commit comments

Comments
 (0)