|
| 1 | +''' |
| 2 | +Test LocalUsersConfigurationResetBase module |
| 3 | +''' |
| 4 | +import subprocess |
| 5 | +import pytest |
| 6 | +from mock import patch, mock_open |
| 7 | +from sonic_platform_base.local_users_passwords_reset_base import LocalUsersConfigurationResetBase |
| 8 | + |
| 9 | + |
| 10 | +DEFAULT_USERS_JSON_EXAMPLE_OUTPUT = ''' |
| 11 | +{ |
| 12 | + "admin": { |
| 13 | + "expire": "false", |
| 14 | + "password": "HASHED_PASSWORD_123" |
| 15 | + } |
| 16 | +} |
| 17 | +''' |
| 18 | + |
| 19 | + |
| 20 | +class TestLocalUsersConfigurationResetBase: |
| 21 | + ''' |
| 22 | + Collection of LocalUsersConfigurationResetBase test methods |
| 23 | + ''' |
| 24 | + @staticmethod |
| 25 | + def test_local_users_passwords_reset_base(): |
| 26 | + ''' |
| 27 | + Verify unimplemented methods |
| 28 | + ''' |
| 29 | + base = LocalUsersConfigurationResetBase() |
| 30 | + not_implemented_methods = [ |
| 31 | + (base.should_trigger,)] |
| 32 | + |
| 33 | + for method in not_implemented_methods: |
| 34 | + expected_exception = False |
| 35 | + try: |
| 36 | + func = method[0] |
| 37 | + args = method[1:] |
| 38 | + func(*args) |
| 39 | + except Exception as exc: |
| 40 | + expected_exception = isinstance(exc, NotImplementedError) |
| 41 | + assert expected_exception |
| 42 | + |
| 43 | + @patch('subprocess.call') |
| 44 | + def test_reset_passwords_method(self, mock_subproc_call): |
| 45 | + ''' |
| 46 | + Test the reset passwords static method |
| 47 | + ''' |
| 48 | + LocalUsersConfigurationResetBase.reset_password( |
| 49 | + user='admin', |
| 50 | + hashed_password='HASHED_PASSWORD_123', |
| 51 | + expire=True) |
| 52 | + mock_subproc_call.assert_any_call(["echo 'admin:HASHED_PASSWORD_123' | sudo chpasswd -e"], shell=True) |
| 53 | + mock_subproc_call.assert_any_call(['sudo', 'passwd', '-e', 'admin']) |
| 54 | + |
| 55 | + @patch('subprocess.call') |
| 56 | + @patch('sonic_platform.utils.read_int_from_file') |
| 57 | + @patch("builtins.open", new_callable=mock_open, read_data=DEFAULT_USERS_JSON_EXAMPLE_OUTPUT) |
| 58 | + def test_basic_flow_resetting_users_triggered(self, mock_open, mock_read_int, mock_subproc_call): |
| 59 | + ''' |
| 60 | + Test the basic flow of resetting local users when long button press is detected |
| 61 | + ''' |
| 62 | + # Mock long reset button press |
| 63 | + mock_read_int.return_value = int(1) |
| 64 | + LocalUsersConfigurationResetBase().start() |
| 65 | + mock_subproc_call.assert_any_call(["echo 'admin:HASHED_PASSWORD_123' | sudo chpasswd -e"], shell=True) |
| 66 | + mock_subproc_call.assert_any_call(['sudo', 'passwd', '-e', 'admin']) |
0 commit comments