Auth create_environment

This commit is contained in:
richard-dds 2019-09-04 11:25:09 -04:00
parent f757e86eb3
commit 25bedb816d
2 changed files with 14 additions and 4 deletions

View File

@ -7,6 +7,10 @@ from atst.models.environment import Environment
from atst.models.environment_role import EnvironmentRole from atst.models.environment_role import EnvironmentRole
class GeneralCSPException(Exception):
pass
class CloudProviderInterface: class CloudProviderInterface:
def create_environment( def create_environment(
self, auth_credentials: Dict, user: User, environment: Environment self, auth_credentials: Dict, user: User, environment: Environment
@ -117,10 +121,11 @@ class CloudProviderInterface:
class MockCloudProvider(CloudProviderInterface): class MockCloudProvider(CloudProviderInterface):
# TODO: All of these constants # TODO: All of these constants
AUTH_EXCEPTION = ValueError("Could not authenticate.") AUTH_EXCEPTION = GeneralCSPException("Authentication failure.")
NETWORK_EXCEPTION = ValueError("Network failure.") NETWORK_EXCEPTION = GeneralCSPException("Network failure.")
NETWORK_FAILURE_PCT = 12 NETWORK_FAILURE_PCT = 12
ENV_CREATE_FAILURE_PCT = 12
def __init__(self, with_delay=True, with_failure=True): def __init__(self, with_delay=True, with_failure=True):
from time import sleep from time import sleep
@ -132,8 +137,12 @@ class MockCloudProvider(CloudProviderInterface):
self._random = random self._random = random
def create_environment(self, auth_credentials, user, environment): def create_environment(self, auth_credentials, user, environment):
self._delay(1, 5)
self._authorize(auth_credentials)
self._delay(1, 5) self._delay(1, 5)
self._maybe_throw(self.NETWORK_FAILURE_PCT, self.NETWORK_EXCEPTION) self._maybe_throw(self.NETWORK_FAILURE_PCT, self.NETWORK_EXCEPTION)
self._maybe_throw(self.ENV_CREATE_FAILURE_PCT, GeneralCSPException("Could not create environment."))
return self._id() return self._id()
@ -181,5 +190,5 @@ class MockCloudProvider(CloudProviderInterface):
return {"username": "mock-cloud", "pass": "shh"} return {"username": "mock-cloud", "pass": "shh"}
def _authorize(self, credentials): def _authorize(self, credentials):
if credentials != _auth_credentials(): if credentials != self._auth_credentials():
raise self.AUTH_EXCEPTION raise self.AUTH_EXCEPTION

View File

@ -9,5 +9,6 @@ def mock_csp():
def test_create_environment(mock_csp: MockCloudProvider): def test_create_environment(mock_csp: MockCloudProvider):
environment_id = mock_csp.create_environment({}, {}, {}) credentials = mock_csp._auth_credentials()
environment_id = mock_csp.create_environment(credentials, {}, {})
assert isinstance(environment_id, str) assert isinstance(environment_id, str)