Skip to content

Commit 07b9410

Browse files
committed
fix: reducing mocking
1 parent fd93781 commit 07b9410

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

test/unit/oauth/test_oauth1a_u.py

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -678,34 +678,53 @@ def test_req_live_session_token_integration(mock_file, mock_time_func, mock_choi
678678
assert 'OAuth realm=' in auth_header
679679
assert 'oauth_signature=' in auth_header
680680

681-
@patch('ibind.oauth.oauth1a.prepare_oauth')
682-
@patch('ibind.oauth.oauth1a.generate_oauth_headers')
683-
def test_req_live_session_token_api_failure(mock_gen_headers, mock_prepare, oauth_config, mock_client):
684-
# Arrange
685-
mock_prepare.return_value = ('prepend_value', {'diffie_hellman_challenge': 'challenge'}, 'dh_random_value')
686-
mock_gen_headers.return_value = {'Authorization': 'OAuth realm="limited_poa"'}
681+
@patch('secrets.randbits', return_value=0x123, autospec=True)
682+
@patch('secrets.choice', side_effect=lambda x: 'a', autospec=True)
683+
@patch('time.time', return_value=1234567890, autospec=True)
684+
@patch('builtins.open', new_callable=mock_open, read_data='dummy_key_content')
685+
def test_req_live_session_token_api_failure(mock_file, mock_time_func, mock_choice_func, mock_randbits_func, oauth_config, mock_client, real_test_keys):
686+
# Arrange - Use real crypto behavior, only mock API failure
687+
oauth_config.encryption_key_fp = '/tmp/encryption_key.pem' # noqa: S108
688+
oauth_config.signature_key_fp = '/tmp/signature_key.pem' # noqa: S108
689+
690+
# Create real encrypted access token secret for testing
691+
test_secret = b'test_decrypted_secret'
692+
cipher = PKCS1_v1_5_Cipher.new(real_test_keys['public_key'])
693+
encrypted_secret = cipher.encrypt(test_secret)
694+
oauth_config.access_token_secret = base64.b64encode(encrypted_secret).decode('utf-8')
687695

688-
# Mock API failure
689-
mock_client.post.side_effect = Exception('API request failed')
696+
with patch('ibind.oauth.oauth1a.RSA.importKey', autospec=True) as mock_rsa_import:
697+
mock_rsa_import.return_value = real_test_keys['private_key']
690698

691-
# Act & Assert
692-
with pytest.raises(Exception, match='API request failed'):
693-
req_live_session_token(mock_client, oauth_config)
699+
mock_client.post.side_effect = Exception('API request failed')
694700

701+
# Act & Assert - Test real OAuth flow behavior with API failure
702+
with pytest.raises(Exception, match='API request failed'):
703+
req_live_session_token(mock_client, oauth_config)
695704

696-
@patch('ibind.oauth.oauth1a.prepare_oauth')
697-
@patch('ibind.oauth.oauth1a.generate_oauth_headers')
698-
@patch('ibind.oauth.oauth1a.calculate_live_session_token')
699-
def test_req_live_session_token_missing_response_data(mock_calculate_lst, mock_gen_headers, mock_prepare, oauth_config, mock_client):
700-
# Arrange
701-
mock_prepare.return_value = ('prepend_value', {'diffie_hellman_challenge': 'challenge'}, 'dh_random_value')
702-
mock_gen_headers.return_value = {'Authorization': 'OAuth realm="limited_poa"'}
703705

704-
# Mock response with missing data
705-
mock_response = MagicMock()
706-
mock_response.data = {} # Missing required fields
707-
mock_client.post.return_value = mock_response
706+
@patch('secrets.randbits', return_value=0x123, autospec=True)
707+
@patch('secrets.choice', side_effect=lambda x: 'a', autospec=True)
708+
@patch('time.time', return_value=1234567890, autospec=True)
709+
@patch('builtins.open', new_callable=mock_open, read_data='dummy_key_content')
710+
def test_req_live_session_token_missing_response_data(mock_file, mock_time_func, mock_choice_func, mock_randbits_func, oauth_config, mock_client, real_test_keys):
711+
# Arrange - Use real crypto behavior, only mock API response
712+
oauth_config.encryption_key_fp = '/tmp/encryption_key.pem' # noqa: S108
713+
oauth_config.signature_key_fp = '/tmp/signature_key.pem' # noqa: S108
714+
715+
# Create real encrypted access token secret for testing
716+
test_secret = b'test_decrypted_secret'
717+
cipher = PKCS1_v1_5_Cipher.new(real_test_keys['public_key'])
718+
encrypted_secret = cipher.encrypt(test_secret)
719+
oauth_config.access_token_secret = base64.b64encode(encrypted_secret).decode('utf-8')
720+
721+
with patch('ibind.oauth.oauth1a.RSA.importKey', autospec=True) as mock_rsa_import:
722+
mock_rsa_import.return_value = real_test_keys['private_key']
723+
724+
mock_response = MagicMock()
725+
mock_response.data = {} # Missing required fields
726+
mock_client.post.return_value = mock_response
708727

709-
# Act & Assert
710-
with pytest.raises(KeyError):
711-
req_live_session_token(mock_client, oauth_config)
728+
# Act & Assert - Test real OAuth flow behavior with missing response data
729+
with pytest.raises(KeyError):
730+
req_live_session_token(mock_client, oauth_config)

0 commit comments

Comments
 (0)