Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cert_manager/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def __init__(self, **kwargs):
auth_url: The full URL to the Sectigo OAuth2 token endpoint; the default is "https://auth.sso.sectigo.com/auth/realms/apiclients/protocol/openid-connect/token"
client_id: The Client ID to use for OAuth2 authentication
client_secret: The Client Secret to use for OAuth2 authentication
session: A requests.Session object to use instead of creating a new one; the default is None,
which will create a new session
"""
# Initialize class variables
self._base_url = None
Expand All @@ -51,7 +53,8 @@ def __init__(self, **kwargs):
self._user_key_file = None
self._username = None

self._session = requests.Session()
self._session = kwargs.get("session", requests.Session())

# Set the default HTTP headers
self._headers = {
"Accept": "application/json",
Expand Down
19 changes: 19 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from unittest import mock

import pytest
import requests
import responses
from requests.exceptions import HTTPError
from testtools import TestCase
Expand Down Expand Up @@ -138,6 +139,24 @@ def test_versioning(self):
self.assertEqual(client.headers["User-Agent"], user_agent)
self.assertEqual(client._session.headers["User-Agent"], user_agent)

def test_session_passed(self):
"""Test that a passed session is used."""
session = requests.Session()
client = Client(
base_url=self.cfixt.base_url,
login_uri=self.cfixt.login_uri,
username=self.cfixt.username,
password=self.cfixt.password,
session=session,
)
self.assertIs(client._session, session)

def test_session_created(self):
"""Test that a session is created if not passed."""
# The setUp method already creates a client without a session passed
self.assertIsInstance(self.client._session, requests.Session)
self.assertIsNotNone(self.client._session)

def test_need_crt(self):
"""Raise an exception without a cert file if cert_auth=True."""
self.assertRaises(KeyError, Client, base_url=self.cfixt.base_url, login_uri=self.cfixt.login_uri,
Expand Down
Loading