FAQ | This is a LIVE service | Changelog

Commit 94c5183e authored by Robin Goodall's avatar Robin Goodall 💬
Browse files

Initial connection tests

parent c4e7ed4c
from contextlib import contextmanager
from tempfile import NamedTemporaryFile
import os
@contextmanager
def temp_password_file(pwd: str):
"""
Context manager for temporary file containing given string
"""
try:
file = NamedTemporaryFile(delete=False, mode='w')
file_name = file.name
file.write(pwd)
file.close()
yield file_name
finally:
if file_name is not None:
os.unlink(file_name)
from unittest.mock import MagicMock, patch
from . import temp_password_file
from lookupsync.api_gateway import create_api_gateway_session, API_GATEWAY_TOKEN_URL
@patch('oauthlib.oauth2.BackendApplicationClient')
@patch('requests_oauthlib.OAuth2Session')
def test_create_api_gateway_session_var(oauthlib_mock: MagicMock, backend_client_mock: MagicMock):
"""
create_api_gateway_session will request a token from the API Gateway Token URL with the
given client_id and client_secret
"""
session = create_api_gateway_session({
'--gateway-client-id': 'TEST_ID',
'--gateway-client-secret': 'TEST_SECRET',
})
# Backend client created with client id
backend_client_mock.assert_called_once_with(client_id='TEST_ID')
backend_instance = backend_client_mock.return_value
# OAuth2Session created with created backend client
oauthlib_mock.assert_called_once_with(client=backend_instance)
oauth2session = oauthlib_mock.return_value
# fetch_token called with client id and secret
oauth2session.fetch_token.assert_called_with(
token_url=API_GATEWAY_TOKEN_URL, client_id='TEST_ID', client_secret='TEST_SECRET'
)
assert session == oauth2session
@patch('oauthlib.oauth2.BackendApplicationClient')
@patch('requests_oauthlib.OAuth2Session')
def test_create_api_gateway_session_file(oauthlib_mock: MagicMock, backend_client_mock: MagicMock):
"""
create_api_gateway_session will request a token from the API Gateway Token URL with the
given client_id, and client_secret fetched from given file
"""
with temp_password_file('SECRET_IN_FILE') as secret_file:
session = create_api_gateway_session({
'--gateway-client-id': 'TEST_ID',
'--gateway-client-secret-from': secret_file,
})
# Backend client created with client id
backend_client_mock.assert_called_once_with(client_id='TEST_ID')
backend_instance = backend_client_mock.return_value
# OAuth2Session created with created backend client
oauthlib_mock.assert_called_once_with(client=backend_instance)
oauth2session = oauthlib_mock.return_value
# fetch_token called with client id and secret
oauth2session.fetch_token.assert_called_with(
token_url=API_GATEWAY_TOKEN_URL, client_id='TEST_ID', client_secret='SECRET_IN_FILE'
)
assert session == oauth2session
def test_example():
"""
Example test. TODO: replace.
"""
from unittest.mock import MagicMock, patch
from . import temp_password_file
from lookupsync.lookup import create_lookup_connection
@patch('ibisclient.createConnection')
def test_create_lookup_connection_var(ibis_connect_mock: MagicMock):
"""
create_lookup_connection will create an ibisclient.IbisClientConnection with the
given username and password
"""
ibis_conn = create_lookup_connection({
'--lookup-username': 'TEST_USER',
'--lookup-password': 'TEST_PASSWORD',
'--lookup-test': False,
})
# createConnection called once
ibis_connect_mock.assert_called_once()
ibis_connect_instance = ibis_connect_mock.return_value
# Connection has username and password set
ibis_connect_instance.set_username.assert_called_once_with('TEST_USER')
ibis_connect_instance.set_password.assert_called_once_with('TEST_PASSWORD')
assert ibis_conn == ibis_connect_instance
@patch('ibisclient.createConnection')
def test_create_lookup_connection_file(ibis_connect_mock: MagicMock):
"""
create_lookup_connection will create an ibisclient.IbisClientConnection with the
given username, and password fetched from given file
"""
with temp_password_file('PASSWORD_IN_FILE') as password_file:
ibis_conn = create_lookup_connection({
'--lookup-username': 'TEST_USER',
'--lookup-password-from': password_file,
'--lookup-test': False,
})
# createConnection called once
ibis_connect_mock.assert_called_once()
ibis_connect_instance = ibis_connect_mock.return_value
# Connection has username and password set
ibis_connect_instance.set_username.assert_called_once_with('TEST_USER')
ibis_connect_instance.set_password.assert_called_once_with('PASSWORD_IN_FILE')
assert ibis_conn == ibis_connect_instance
@patch('ibisclient.createTestConnection')
@patch('ibisclient.createConnection')
def test_create_lookup_test_connection(
ibis_connect_mock: MagicMock, ibis_test_connect_mock: MagicMock):
"""
create_lookup_connection will create an ibisclient.IbisClientTestConnection with the
given username and password, if '--lookup-test' also set in `opts`
"""
ibis_conn = create_lookup_connection({
'--lookup-username': 'TEST_USER',
'--lookup-password': 'TEST_PASSWORD',
'--lookup-test': True,
})
# createConnection is not called
ibis_connect_mock.assert_not_called()
# but createTestConnection is instead
ibis_test_connect_mock.assert_called_once()
ibis_connect_instance = ibis_test_connect_mock.return_value
# Connection has username and password set
ibis_connect_instance.set_username.assert_called_once_with('TEST_USER')
ibis_connect_instance.set_password.assert_called_once_with('TEST_PASSWORD')
assert ibis_conn == ibis_connect_instance
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment