Skip to content

Commit

Permalink
Commit changes made by code formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Dec 13, 2023
1 parent c4d8792 commit 7846962
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
15 changes: 10 additions & 5 deletions src/juniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'
Expand All @@ -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,
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
28 changes: 15 additions & 13 deletions test/test_juniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)

Expand All @@ -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()
Expand All @@ -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='[email protected]', mist_login_method='credentials')
admin = Admin(username='[email protected]',
mist_login_method='credentials')
self.assertIsNotNone(admin)

mock_post.assert_called_with(
Expand All @@ -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='[email protected]', mist_login_method='credentials')
admin = Admin(username='[email protected]',
mist_login_method='credentials')

# Check the expected part of the exception message
expected_error_message = "Login was not successful:"
Expand Down

0 comments on commit 7846962

Please sign in to comment.