From 78469624a5bdada2f844669626c10ea2a334e70e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 13 Dec 2023 18:21:08 +0000 Subject: [PATCH] Commit changes made by code formatters --- README.md | 6 +++++- src/juniper.py | 15 ++++++++++----- src/main.py | 1 + test/test_juniper.py | 28 +++++++++++++++------------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 033218a..7aaa3bf 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ This repo has been developed by the DevOps Lan&Wi-Fi to automate site creation o ### Required package prerequisites #### Docker + [Docker docs mac install](https://docs.docker.com/desktop/install/mac-install/) ### Run script: @@ -24,12 +25,15 @@ curl -o .env https://raw.githubusercontent.com/ministryofjustice/juniper-mist-in ``` 3. Configure .env file: - Review the .env file by running: + Review the .env file by running: + ``` nano ~/example.env ``` + To fully take advantage of the .env files, you can duplicate them and have different .envs per environment. If you choose to do this make sure you edit the docker run command in step 6 to pass in the correctly named file. + ``` .env-dev .env-prod diff --git a/src/juniper.py b/src/juniper.py index fc49caf..029317c 100644 --- a/src/juniper.py +++ b/src/juniper.py @@ -11,7 +11,7 @@ class Admin: def login_via_username_and_password(self, username): # If no username defined ask user if username is None: - username= input("Enter Mist Username:") + username = input("Enter Mist Username:") else: print('Username provided: {usr}'.format(usr=username)) password = getpass.getpass(prompt='Enter Mist password:') @@ -55,7 +55,8 @@ def __init__(self, username=None, mist_login_method=None): elif mist_login_method == 'credentials': self.login_via_username_and_password(username) else: - raise ValueError('Invalid mist_login_method: {method}'.format(method=mist_login_method)) + raise ValueError('Invalid mist_login_method: {method}'.format( + method=mist_login_method)) def post(self, url, payload, timeout=60): url = 'https://api.eu.mist.com{}'.format(url) @@ -102,10 +103,12 @@ def check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups(gov_wifi, moj_wi result.append(site_group_ids['gov_wifi']) return result + def warn_if_using_org_id_production(org_id): - production_org_id='3e824dd6-6b37-4cc7-90bb-97d744e91175' + production_org_id = '3e824dd6-6b37-4cc7-90bb-97d744e91175' if org_id == production_org_id: - production_warning_answer = input("Warning you are using production ORG_ID, would you like to proceed? Y/N: ").upper() + production_warning_answer = input( + "Warning you are using production ORG_ID, would you like to proceed? Y/N: ").upper() if production_warning_answer == "Y": print("Continuing with run") return 'Continuing_with_run' @@ -115,6 +118,8 @@ def warn_if_using_org_id_production(org_id): raise ValueError('Invalid input') # Main function + + def juniper_script( data, org_id=None, @@ -139,7 +144,7 @@ def juniper_script( raise ValueError('Must define network_template_id') if mist_login_method is None: print("mist_login_method not defined. Defaulting to credentials") - mist_login_method='credentials' + mist_login_method = 'credentials' # Prompt user if we are using production org_id warn_if_using_org_id_production(org_id) diff --git a/src/main.py b/src/main.py index 530a4b4..8569bb2 100644 --- a/src/main.py +++ b/src/main.py @@ -40,6 +40,7 @@ def add_geocoding_to_json(data): d['time_zone'] = time_zone return data + if __name__ == '__main__': csv_file_path = os.getcwd() + '/../data_src/sites_with_clients.csv' diff --git a/test/test_juniper.py b/test/test_juniper.py index 4db7bb8..3325369 100644 --- a/test/test_juniper.py +++ b/test/test_juniper.py @@ -3,6 +3,7 @@ from src.juniper import juniper_script, Admin, check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups, warn_if_using_org_id_production from io import StringIO + class TestJuniperScript(unittest.TestCase): @patch('getpass.getpass', return_value='token') @@ -121,19 +122,19 @@ def test_juniper_script_missing_rf_template_id(self): @patch('builtins.input', return_value='y') def test_given_production_org_id_when_user_prompted_for_input_and_user_inputs_y_then_continue_to_run(self, user_input): - production_org_id='3e824dd6-6b37-4cc7-90bb-97d744e91175' - result=warn_if_using_org_id_production(production_org_id) + production_org_id = '3e824dd6-6b37-4cc7-90bb-97d744e91175' + result = warn_if_using_org_id_production(production_org_id) self.assertEqual(result, 'Continuing_with_run') @patch('builtins.input', return_value='n') def test_given_production_org_id_when_user_prompted_for_input_and_user_inputs_n_then_sys_exit(self, user_input): - production_org_id='3e824dd6-6b37-4cc7-90bb-97d744e91175' + production_org_id = '3e824dd6-6b37-4cc7-90bb-97d744e91175' with self.assertRaises(SystemExit): warn_if_using_org_id_production(production_org_id) @patch('builtins.input', return_value='invalid') def test_given_production_org_id_when_user_prompted_for_input_and_user_inputs_invalid_then_raise_error(self, user_input): - production_org_id='3e824dd6-6b37-4cc7-90bb-97d744e91175' + production_org_id = '3e824dd6-6b37-4cc7-90bb-97d744e91175' with self.assertRaises(ValueError) as cm: warn_if_using_org_id_production(production_org_id) @@ -153,7 +154,6 @@ def test_juniper_script_missing_network_template_id(self): self.assertEqual(str(cm.exception), 'Must define network_template_id') - @patch('src.juniper.Admin') def test_given_mist_login_method_not_defined_then_default_to_credentials(self, mock_admin): output_buffer = StringIO() @@ -169,22 +169,23 @@ def test_given_mist_login_method_not_defined_then_default_to_credentials(self, m ) actual_output = mock_stdout.getvalue() expected_message = "mist_login_method not defined. Defaulting to credentials" - self.assertIn(expected_message, actual_output, f"Output should contain: '{expected_message}'") - + self.assertIn(expected_message, actual_output, + f"Output should contain: '{expected_message}'") def test_juniper_script_missing_org_id(self): - # Test when org_id is missing - with self.assertRaises(ValueError) as cm: - juniper_script([], org_id=None) + # Test when org_id is missing + with self.assertRaises(ValueError) as cm: + juniper_script([], org_id=None) - self.assertEqual(str(cm.exception), 'Please provide Mist org_id') + self.assertEqual(str(cm.exception), 'Please provide Mist org_id') # Mocking the input function to provide a static MFA code @patch('getpass.getpass', return_value='password') @patch('builtins.input', return_value='123456') @patch('src.juniper.requests.Session.post', return_value=MagicMock(status_code=200)) def test_login_successfully_via_username_and_password(self, mock_post, input_mfa, input_password): - admin = Admin(username='test@example.com', mist_login_method='credentials') + admin = Admin(username='test@example.com', + mist_login_method='credentials') self.assertIsNotNone(admin) mock_post.assert_called_with( @@ -196,7 +197,8 @@ def test_login_successfully_via_username_and_password(self, mock_post, input_mfa @patch('src.juniper.requests.Session.post', return_value=MagicMock(status_code=400)) def test_given_valid_username_and_password_when_post_to_api_and_non_200_status_code_received_then_raise_error_to_user(self, mock_post, mfa_input, password_input): with self.assertRaises(ValueError) as context: - admin = Admin(username='test@example.com', mist_login_method='credentials') + admin = Admin(username='test@example.com', + mist_login_method='credentials') # Check the expected part of the exception message expected_error_message = "Login was not successful:"