From c0c28dbe0ba235e8a3e05c12db8baaeb2ffdf1e0 Mon Sep 17 00:00:00 2001 From: Fabio Testa Date: Thu, 14 Mar 2024 11:06:55 +0100 Subject: [PATCH 1/2] add test resources to gitignore add test class to implement test cases add test case to cover filestation login add test case to cover surveillance station login --- .gitignore | 5 ++- tests/__init__.py | 0 tests/resources/config-test-sample.json | 10 +++++ tests/test_syno_api.py | 53 +++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/resources/config-test-sample.json create mode 100644 tests/test_syno_api.py diff --git a/.gitignore b/.gitignore index 5df3bcd..dc74be3 100644 --- a/.gitignore +++ b/.gitignore @@ -398,4 +398,7 @@ FodyWeavers.xsd # JetBrains Rider .idea/ -*.sln.iml \ No newline at end of file +*.sln.iml + +# Test resources +tests/resources/config-test.json \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/resources/config-test-sample.json b/tests/resources/config-test-sample.json new file mode 100644 index 0000000..a23f733 --- /dev/null +++ b/tests/resources/config-test-sample.json @@ -0,0 +1,10 @@ +#### COPY THIS FILE AND NAME IT config-test.json AND REPLACE WITH YOUR VALUES +{ + "synology_ip": "your_dsm_ip", + "synology_port": your_dsm_port_as_int, + "synology_user": "your_dsm_user", + "synology_password": "your_dsm_password", + "synology_secure": true_if_your_dsm_http_endpoint_is_https, + "dsm_version": your_dsm_version_as_int, + "otp_code": "your_dsm_otp_code"_or_null +} \ No newline at end of file diff --git a/tests/test_syno_api.py b/tests/test_syno_api.py new file mode 100644 index 0000000..276699f --- /dev/null +++ b/tests/test_syno_api.py @@ -0,0 +1,53 @@ +import datetime +import json +from unittest import TestCase + +from synology_api.filestation import FileStation +from synology_api.surveillancestation import SurveillanceStation + + +def parse_config(config_path) -> dict[str, str]: + with open(config_path, 'r') as config_file: + config_data = json.load(config_file) + return config_data + + +class TestSynoApi(TestCase): + config: dict[str, str] + + def setUp(self): + self.config = parse_config('./resources/config-test.json') + + def test_syno_filestation_login(self): + fs = FileStation(ip_address=self.config["synology_ip"], port=self.config["synology_port"], + username=self.config["synology_user"], + password=self.config["synology_password"], + secure=bool(self.config["synology_secure"]), cert_verify=False, + dsm_version=int(self.config["dsm_version"]), debug=True, + otp_code=self.config["otp_code"]) + + self.assertIsNotNone(fs) + self.assertIsNotNone(fs.session) + self.assertIsNotNone(fs.session.sid) + self.assertIsNot(fs.session.sid, '') + shares_list = fs.get_list_share() + self.assertIsNotNone(shares_list) + self.assertEqual(shares_list.__len__(), 2) + + def test_syno_surveillancestation_login(self): + ss = SurveillanceStation(ip_address=self.config["synology_ip"], port=self.config["synology_port"], + username=self.config["synology_user"], + password=self.config["synology_password"], + secure=bool(self.config["synology_secure"]), cert_verify=False, + dsm_version=int(self.config["dsm_version"]), debug=True, + otp_code=self.config["otp_code"]) + + self.assertIsNotNone(ss) + self.assertIsNotNone(ss.session) + self.assertIsNotNone(ss.session.sid) + self.assertIsNot(ss.session.sid, '') + ss_info = ss.surveillance_station_info() + self.assertIsNotNone(ss_info) + ss_info_data = ss_info['data'] + self.assertIsNotNone(ss_info_data) + self.assertEqual(ss_info_data['path'], '/webman/3rdparty/SurveillanceStation/') From 2dfc4420a8ecdacc770a1efbb8905ace10f18d0d Mon Sep 17 00:00:00 2001 From: Fabio Testa Date: Thu, 14 Mar 2024 11:26:47 +0100 Subject: [PATCH 2/2] reformat file add main to run test suite from commandline --- tests/test_syno_api.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/test_syno_api.py b/tests/test_syno_api.py index 276699f..914ed91 100644 --- a/tests/test_syno_api.py +++ b/tests/test_syno_api.py @@ -1,7 +1,7 @@ import datetime import json from unittest import TestCase - +import unittest from synology_api.filestation import FileStation from synology_api.surveillancestation import SurveillanceStation @@ -20,11 +20,11 @@ def setUp(self): def test_syno_filestation_login(self): fs = FileStation(ip_address=self.config["synology_ip"], port=self.config["synology_port"], - username=self.config["synology_user"], - password=self.config["synology_password"], - secure=bool(self.config["synology_secure"]), cert_verify=False, - dsm_version=int(self.config["dsm_version"]), debug=True, - otp_code=self.config["otp_code"]) + username=self.config["synology_user"], + password=self.config["synology_password"], + secure=bool(self.config["synology_secure"]), cert_verify=False, + dsm_version=int(self.config["dsm_version"]), debug=True, + otp_code=self.config["otp_code"]) self.assertIsNotNone(fs) self.assertIsNotNone(fs.session) @@ -36,11 +36,11 @@ def test_syno_filestation_login(self): def test_syno_surveillancestation_login(self): ss = SurveillanceStation(ip_address=self.config["synology_ip"], port=self.config["synology_port"], - username=self.config["synology_user"], - password=self.config["synology_password"], - secure=bool(self.config["synology_secure"]), cert_verify=False, - dsm_version=int(self.config["dsm_version"]), debug=True, - otp_code=self.config["otp_code"]) + username=self.config["synology_user"], + password=self.config["synology_password"], + secure=bool(self.config["synology_secure"]), cert_verify=False, + dsm_version=int(self.config["dsm_version"]), debug=True, + otp_code=self.config["otp_code"]) self.assertIsNotNone(ss) self.assertIsNotNone(ss.session) @@ -51,3 +51,7 @@ def test_syno_surveillancestation_login(self): ss_info_data = ss_info['data'] self.assertIsNotNone(ss_info_data) self.assertEqual(ss_info_data['path'], '/webman/3rdparty/SurveillanceStation/') + + +if __name__ == '__main__': + unittest.main()