Skip to content

Commit

Permalink
moved juniper_payload tests into module
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgreen-moj committed Mar 20, 2024
1 parent b61251e commit 22d78dd
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 66 deletions.
85 changes: 84 additions & 1 deletion test/test_build_juniper_payload.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from src.build_juniper_payload import BuildPayload
import unittest
from unittest.mock import patch
import json


class TestBuildPayLoad(unittest.TestCase):
maxDiff = None

def setUp(self):
self.sample_data = [
Expand Down Expand Up @@ -168,3 +169,85 @@ def test_given_invalid_data_when_get_site_payload_called_then_raise_error(self):

self.assertEqual(str(cm.exception),
"Missing the following keys ['Site Name', 'Site Address', 'gps', 'country_code', 'time_zone', 'Enable GovWifi', 'Enable MoJWifi']")

class TestCheckIfNeedToAppend(unittest.TestCase):

def setUp(self):
# Define sample site group IDs for testing
self.site_group_ids = {
'moj_wifi': '0b33c61d-8f51-4757-a14d-29263421a904',
'gov_wifi': '70f3e8af-85c3-484d-8d90-93e28b911efb'
}
self.sample_data = [
{
'Site Name': 'MOJ-0137-Probation-Gloucestershire-TwyverHouse',
'Site Address': 'Twyver House, Bruton Way, Gloucester, GL1 1PB',
'gps': [51.86467215, -2.2396225106929535],
'country_code': 'GB',
'time_zone': 'Europe/London',
'Enable GovWifi': 'True',
'Enable MoJWifi': 'True',
'GovWifi Radius Key': 'govwifi_secret',
'Wired NACS Radius Key': 'wired_nacs_secret'
},
{
'Site Name': 'Test location 2',
'Site Address': '102 Petty France, London SW1H 9AJ',
'gps': [51.499929300000005, -0.13477761285315926],
'country_code': 'GB',
'time_zone': 'Europe/London',
'Enable GovWifi': 'False',
'Enable MoJWifi': 'False'
},
{
'Site Name': 'Test location 3',
'Site Address': 'Met Office, FitzRoy Road, Exeter, Devon, EX1 3PB',
'gps': [50.727350349999995, -3.4744726127760086],
'country_code': 'GB',
'time_zone': 'Europe/London',
'Enable GovWifi': 'False',
'Enable MoJWifi': 'True',
'Wired NACS Radius Key': 'wired_nacs_secret'
}
]
self.rf_template_id = "rf_template_123"
self.network_template_id = "network_template_123"
self.site_group_ids = {"moj_wifi": "moj_wifi_id", "gov_wifi": "gov_wifi_id"}
self.ap_versions = {"ap_version_1": "1.0", "ap_version_2": "2.0"}
self.payload_processor = BuildPayload(
data=self.sample_data,
rf_template_id=self.rf_template_id,
network_template_id=self.network_template_id,
site_group_ids='{"moj_wifi": "moj_wifi_id", "gov_wifi": "gov_wifi_id"}',
ap_versions=self.ap_versions
)

def test_append_gov_wifi(self):
gov_wifi = 'TRUE'
moj_wifi = 'FALSE'
result = self.payload_processor._check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups(
gov_wifi, moj_wifi,self.site_group_ids)
self.assertEqual(result, [self.site_group_ids['gov_wifi']])

def test_append_moj_wifi(self):
gov_wifi = 'FALSE'
moj_wifi = 'TRUE'
result = self.payload_processor._check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups(
gov_wifi, moj_wifi, self.site_group_ids)
self.assertEqual(result, [self.site_group_ids['moj_wifi']])

def test_append_both_wifi(self):
gov_wifi = 'TRUE'
moj_wifi = 'TRUE'
result = self.payload_processor._check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups(
gov_wifi, moj_wifi, self.site_group_ids)
expected_result = [self.site_group_ids['moj_wifi'],
self.site_group_ids['gov_wifi']]
self.assertEqual(result, expected_result)

def test_append_neither_wifi(self):
gov_wifi = 'FALSE'
moj_wifi = 'FALSE'
result = self.payload_processor._check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups(
gov_wifi, moj_wifi, self.site_group_ids)
self.assertEqual(result, [])
98 changes: 33 additions & 65 deletions test/test_juniper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from unittest.mock import patch, MagicMock
from src.juniper import juniper_script, check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups, \
from src.juniper import juniper_script, \
warn_if_using_org_id_production, plan_of_action
from io import StringIO
from datetime import datetime
Expand All @@ -13,8 +13,8 @@ class TestJuniperScript(unittest.TestCase):
@patch('src.juniper.plan_of_action')
@patch('getpass.getpass', return_value='token')
@patch('src.juniper.requests.Session.get', return_value=MagicMock(status_code=200))
@patch('src.juniper.Admin.post')
@patch('src.juniper.Admin.put')
@patch('src.juniper.JuniperClient.post')
@patch('src.juniper.JuniperClient.put')
def test_juniper_script_with_govwifi(self, mock_put, mock_post, mock_successful_login, api_token, mock_plan_of_action):
# Mock Mist API responses
mock_post.return_value = {'id': '123', 'name': 'TestSite'}
Expand Down Expand Up @@ -157,7 +157,7 @@ def test_juniper_script_missing_network_template_id(self):
self.assertEqual(str(cm.exception), 'Must define network_template_id')

@patch('src.juniper.plan_of_action')
@patch('src.juniper.Admin')
@patch('src.juniper.JuniperClient')
def test_given_mist_login_method_not_defined_then_default_to_credentials(self, mock_admin, mock_plan_of_action):
output_buffer = StringIO()
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
Expand All @@ -168,7 +168,8 @@ def test_given_mist_login_method_not_defined_then_default_to_credentials(self, m
'gov_wifi': '70f3e8af-85c3-484d-8d90-93e28b911efb'},
rf_template_id='46b87163-abd2-4b08-a67f-1ccecfcfd061',
network_template_id='46b87163-abd2-4b08-a67f-1ccecfcfd061',
mist_login_method=None
mist_login_method=None,
ap_versions={"AP45": "0.12.27139", "AP32": "0.12.27139"}
)
actual_output = mock_stdout.getvalue()
expected_message = "mist_login_method not defined. Defaulting to credentials"
Expand Down Expand Up @@ -201,78 +202,45 @@ def test_juniper_script_missing_org_id(self):
# Admin.get_ap_versions()
# self.assertEqual(str(cm.exception), 'Invalid AP_VERSIONS')


class TestCheckIfNeedToAppend(unittest.TestCase):

def setUp(self):
# Define sample site group IDs for testing
self.site_group_ids = {
'moj_wifi': '0b33c61d-8f51-4757-a14d-29263421a904',
'gov_wifi': '70f3e8af-85c3-484d-8d90-93e28b911efb'
}

def test_append_gov_wifi(self):
gov_wifi = 'TRUE'
moj_wifi = 'FALSE'
result = check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups(
gov_wifi, moj_wifi, self.site_group_ids)
self.assertEqual(result, [self.site_group_ids['gov_wifi']])

def test_append_moj_wifi(self):
gov_wifi = 'FALSE'
moj_wifi = 'TRUE'
result = check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups(
gov_wifi, moj_wifi, self.site_group_ids)
self.assertEqual(result, [self.site_group_ids['moj_wifi']])

def test_append_both_wifi(self):
gov_wifi = 'TRUE'
moj_wifi = 'TRUE'
result = check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups(
gov_wifi, moj_wifi, self.site_group_ids)
expected_result = [self.site_group_ids['moj_wifi'],
self.site_group_ids['gov_wifi']]
self.assertEqual(result, expected_result)

def test_append_neither_wifi(self):
gov_wifi = 'FALSE'
moj_wifi = 'FALSE'
result = check_if_we_need_to_append_gov_wifi_or_moj_wifi_site_groups(
gov_wifi, moj_wifi, self.site_group_ids)
self.assertEqual(result, [])



class TestPlanOfActionFunction(unittest.TestCase):

def setUp(self):
self.data = {'Site Name': 'TestSite', 'Site Address': '123 Main St',
'gps': [1.23, 4.56], 'country_code': 'US', 'time_zone': 'UTC',
'Enable GovWifi': 'true', 'Enable MoJWifi': 'false',
'Wired NACS Radius Key': 'key1', 'GovWifi Radius Key': 'key2'},
self.rf_template_id = "rf_template_id",
self.network_template_id = "network_template_id",
self.site_group_ids = '{"moj_wifi": "foo","gov_wifi": "bar"}'
@patch("src.juniper.BuildPayload")
def test_plan_of_action_creates_file(self, mock_payload_processor):

# Mocking the payload processor
mock_processor_instance = mock_payload_processor.return_value
mock_processor_instance.get_site_payload.return_value = {"hello": "test"}

def test_plan_of_action_creates_file(self):
with patch('builtins.input', return_value='Y'), patch('sys.exit') as mock_exit:
plan_of_action(self.data, self.rf_template_id,
self.network_template_id, self.site_group_ids)
plan_of_action(mock_processor_instance)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
expected_file_name = f"../data_src/mist_plan_{timestamp}.json"
self.assertTrue(os.path.exists(expected_file_name))
os.remove(expected_file_name)

@patch("src.juniper.BuildPayload")
@patch("builtins.open")
def test_plan_of_action_exit_on_no(self, mock_open_file):
def test_plan_of_action_exit_on_no(self, mock_open_file, mock_payload_processor):

# Mocking the payload processor
mock_processor_instance = mock_payload_processor.return_value
mock_processor_instance.get_site_payload.return_value = {"hello": "test"}

with patch('builtins.input', return_value='N'), self.assertRaises(SystemExit) as cm:
plan_of_action(self.data, self.rf_template_id,
self.network_template_id, self.site_group_ids)
plan_of_action(mock_processor_instance)
self.assertEqual(cm.exception.code, 0)

@patch("src.juniper.BuildPayload")
@patch("builtins.open")
def test_plan_of_action_invalid_input(self, mock_open_file):
with patch('builtins.input', return_value='invalid_input'), self.assertRaises(ValueError) as cm:
plan_of_action(self.data, self.rf_template_id,
self.network_template_id, self.site_group_ids)
def test_plan_of_action_invalid_input(self, mock_open_file, mock_payload_processor):

# Mocking the payload processor
mock_processor_instance = mock_payload_processor.return_value
mock_processor_instance.get_site_payload.return_value = {"hello": "test"}

# Mocking the built-in input
with patch('builtins.input', return_value='invalid_input'):
with self.assertRaises(ValueError) as cm:
plan_of_action(mock_processor_instance)

self.assertEqual(str(cm.exception), 'Invalid input')

0 comments on commit 22d78dd

Please sign in to comment.