Skip to content

Commit

Permalink
Remove unused token from Configuration object
Browse files Browse the repository at this point in the history
It is not used anywhere, Cachet is called through Client.
  • Loading branch information
nijel committed Apr 29, 2020
1 parent 21063d6 commit e05932b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
9 changes: 1 addition & 8 deletions cachet_url_monitor/configuration.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ class Configuration(object):
endpoint_index: int
endpoint: str
client: CachetClient
token: str
current_fails: int
trigger_update: bool
headers: Dict[str, str]

endpoint_method: str
endpoint_url: str
Expand All @@ -45,12 +43,11 @@ class Configuration(object):
previous_status: ComponentStatus
message: str

def __init__(self, config, endpoint_index: int, client: CachetClient, token: str):
def __init__(self, config, endpoint_index: int, client: CachetClient):
self.endpoint_index = endpoint_index
self.data = config
self.endpoint = self.data['endpoints'][endpoint_index]
self.client = client
self.token = token

self.current_fails = 0
self.trigger_update = True
Expand All @@ -69,8 +66,6 @@ def __init__(self, config, endpoint_index: int, client: CachetClient, token: str

# We store the main information from the configuration file, so we don't keep reading from the data dictionary.

self.headers = {'X-Cachet-Token': self.token}

self.endpoint_method = self.endpoint['method']
self.endpoint_url = normalize_url(self.endpoint['url'])
self.endpoint_timeout = self.endpoint.get('timeout') or 1
Expand Down Expand Up @@ -174,8 +169,6 @@ def print_out(self):

def __repr__(self):
temporary_data = copy.deepcopy(self.data)
# Removing the token so we don't leak it in the logs.
del temporary_data['cachet']['token']
temporary_data['endpoints'] = temporary_data['endpoints'][self.endpoint_index]

return dump(temporary_data, default_flow_style=False)
Expand Down
2 changes: 1 addition & 1 deletion cachet_url_monitor/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ def fatal_error(message):
for endpoint_index in range(len(config_data['endpoints'])):
token = os.environ.get('CACHET_TOKEN') or config_data['cachet']['token']
api_url = os.environ.get('CACHET_API_URL') or config_data['cachet']['api_url']
configuration = Configuration(config_data, endpoint_index, CachetClient(api_url, token), token)
configuration = Configuration(config_data, endpoint_index, CachetClient(api_url, token))
NewThread(Scheduler(configuration,
build_agent(configuration, logging.getLogger('cachet_url_monitor.scheduler')))).start()
10 changes: 4 additions & 6 deletions tests/test_configuration.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,24 @@ def getLogger(name):

@pytest.fixture()
def configuration(config_file, mock_client, mock_logger):
yield Configuration(config_file, 0, mock_client, 'token2')
yield Configuration(config_file, 0, mock_client)


@pytest.fixture()
def multiple_urls_configuration(multiple_urls_config_file, mock_client, mock_logger):
yield [Configuration(multiple_urls_config_file, index, mock_client, 'token2') for index in
yield [Configuration(multiple_urls_config_file, index, mock_client) for index in
range(len(multiple_urls_config_file['endpoints']))]


def test_init(configuration):
assert len(configuration.data) == 2, 'Number of root elements in config.yml is incorrect'
assert len(configuration.expectations) == 3, 'Number of expectations read from file is incorrect'
assert configuration.headers == {'X-Cachet-Token': 'token2'}, 'Header was not set correctly'
assert configuration.endpoint_header == {'SOME-HEADER': 'SOME-VALUE'}, 'Header is incorrect'


def test_init_unknown_status(config_file, mock_client):
mock_client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.UNKNOWN
configuration = Configuration(config_file, 0, mock_client, 'token2')
configuration = Configuration(config_file, 0, mock_client)

assert configuration.previous_status == cachet_url_monitor.status.ComponentStatus.UNKNOWN

Expand Down Expand Up @@ -173,12 +172,11 @@ def test_init_multiple_urls(multiple_urls_configuration):
config = multiple_urls_configuration[index]
assert len(config.data) == 2, 'Number of root elements in config.yml is incorrect'
assert len(config.expectations) == 1, 'Number of expectations read from file is incorrect'
assert config.headers == {'X-Cachet-Token': 'token2'}, 'Header was not set correctly'

assert expected_method[index] == config.endpoint_method
assert expected_url[index] == config.endpoint_url


def test_init_invalid_configuration(invalid_config_file, mock_client):
with pytest.raises(cachet_url_monitor.configuration.ConfigurationValidationError):
Configuration(invalid_config_file, 0, mock_client, 'token2')
Configuration(invalid_config_file, 0, mock_client)

0 comments on commit e05932b

Please sign in to comment.