| 
					
				 | 
			
			
				@@ -16,6 +16,7 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 # You should have received a copy of the GNU General Public License 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 # along with this program.  If not, see <https://www.gnu.org/licenses/>. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import json 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import unittest.mock 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import pytest 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -98,11 +99,12 @@ def test__main( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         mqtt_username=expected_username, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         mqtt_password=expected_password, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         retry_count=expected_retry_count, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        device_passwords={}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 @pytest.mark.parametrize( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    ("password_file_content", "expected_password"), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    ("mqtt_password_file_content", "expected_password"), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         ("secret", "secret"), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         ("secret space", "secret space"), 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -115,10 +117,12 @@ def test__main( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         ("你好\n", "你好"), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     ], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-def test__main_password_file(tmpdir, password_file_content, expected_password): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+def test__main_mqtt_password_file( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    tmpdir, mqtt_password_file_content, expected_password 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     mqtt_password_path = tmpdir.join("mqtt-password") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     with mqtt_password_path.open("w") as mqtt_password_file: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        mqtt_password_file.write(password_file_content) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mqtt_password_file.write(mqtt_password_file_content) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         "sys.argv", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         [ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -139,10 +143,11 @@ def test__main_password_file(tmpdir, password_file_content, expected_password): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         mqtt_username="me", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         mqtt_password=expected_password, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         retry_count=3, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        device_passwords={}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-def test__main_password_file_collision(capsys): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+def test__main_mqtt_password_file_collision(capsys): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     with unittest.mock.patch( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         "sys.argv", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         [ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -166,3 +171,35 @@ def test__main_password_file_collision(capsys): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         "argument --mqtt-password-file: not allowed with argument --mqtt-password\n" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         in err 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@pytest.mark.parametrize( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    "device_passwords", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        {}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        {"11:22:33:44:55:66": "password", "aa:bb:cc:dd:ee:ff": "secret"}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    ], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+def test__main_device_password_file(tmpdir, device_passwords): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    device_passwords_path = tmpdir.join("passwords.json") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    device_passwords_path.write_text(json.dumps(device_passwords), encoding="utf8") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        "sys.argv", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "--mqtt-host", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "localhost", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "--device-password-file", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            str(device_passwords_path), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    ): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        # pylint: disable=protected-access 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        switchbot_mqtt._main() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    run_mock.assert_called_once_with( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mqtt_host="localhost", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mqtt_port=1883, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mqtt_username=None, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mqtt_password=None, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        retry_count=3, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        device_passwords=device_passwords, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    ) 
			 |