def summarize_stats(controller: Controller, do_json=False, do_list=False): """Collects and summarizes statistics""" if not controller.logged_in: return stats = controller.get_minutely_stats() summarize_minutes(stats, DATETIME_FORMAT, do_json=do_json, do_list=do_list)
def test_cont_02(self, mock_post: MagicMock): """Tests A Connection is Refused""" mock_post.side_effect = ConnectionError() host = 'localhost' port = 8443 user = '******' password = '******' with self.assertRaises(ConnectionError): #pylint: disable=unused-variable controller = Controller(host, port, user, password)
def test_cont_03(self, mock_post: MagicMock): """Tests Good Connection""" host = 'localhost' port = 8443 user = '******' password = '******' mock_post.return_value = MockResponse( 200, f"https://{host}:{port}/api/login", '{"meta":{"rc":"ok"},"data":[]}') controller = Controller(host, port, user, password) res = controller.logged_in self.assertTrue(res)
def test_cont_01(self, mock_post: MagicMock): """Test Bad Credentials""" host = 'localhost' port = 8443 mock_post.return_value = MockResponse( 400, f"https://{host}:{port}/api/login", '{"meta":{"rc":"error","msg":"api.err.Invalid"},"data":[]}') user = '******' password = '******' controller = Controller(host, port, user, password) res = controller.logged_in self.assertFalse(res)
def main(host, port, user, password, site, do_json, do_list): """Gather minutely data usage stats from a Unfi Controller.""" controller = Controller(host, port, user, password, site=site, ssl_verify=False) if not controller.logged_in: return summarize_stats(controller, do_json=do_json, do_list=do_list)
def test_cont_04(self, mock_post: MagicMock): """Test stat methods return None when not logged in""" host = 'localhost' port = 8443 mock_post.return_value = MockResponse( 400, f"https://{host}:{port}/api/login", '{"meta":{"rc":"error","msg":"api.err.Invalid"},"data":[]}') user = '******' password = '******' controller = Controller(host, port, user, password) res = controller.logged_in self.assertFalse(res) test_methods = [ controller.get_daily_stats, controller.get_hourly_stats, controller.get_minutely_stats, controller.site_info_detailed, controller.site_info_simplified ] for test_method in test_methods: with self.subTest(test_method=test_method): res = test_method() self.assertIsNone(res)