def test_scan_worldwritable_directories_with_no_sticky_bit_set(self, mock_os): # Prepare data and mocks test_subject = WorldWritable(None) mock_os.walk.return_value = [ ('/world/writable/not/sticky', (), ()), ('/not/world/writable/not/sticky', (), ()), ('/world/writable/sticky', (), ()), ] test_subject.is_world_writable = MagicMock(side_effect=[True, False, True]) test_subject.is_sticky_bit_set = MagicMock(side_effect=[False, True]) # Run test scenario result = test_subject.scan_worldwritable_directories_with_no_sticky_bit_set() # Assertions self.assertEqual(result, ['/world/writable/not/sticky']) mock_os.walk.assert_called_once_with('/') test_subject.is_world_writable.assert_has_calls( [ call('/world/writable/not/sticky'), call('/not/world/writable/not/sticky'), call('/world/writable/sticky'), ] ) test_subject.is_sticky_bit_set.assert_has_calls( [ call('/world/writable/not/sticky'), call('/world/writable/sticky'), ] )
def test_scan_worldwritable_directories_with_no_sticky_bit_set( self, mock_os): # Prepare data and mocks test_subject = WorldWritable(None) mock_os.walk.return_value = [ ('/world/writable/not/sticky', (), ()), ('/not/world/writable/not/sticky', (), ()), ('/world/writable/sticky', (), ()), ] test_subject.is_world_writable = MagicMock( side_effect=[True, False, True]) test_subject.is_sticky_bit_set = MagicMock(side_effect=[False, True]) # Run test scenario result = test_subject.scan_worldwritable_directories_with_no_sticky_bit_set( ) # Assertions self.assertEqual(result, ['/world/writable/not/sticky']) mock_os.walk.assert_called_once_with('/') test_subject.is_world_writable.assert_has_calls([ call('/world/writable/not/sticky'), call('/not/world/writable/not/sticky'), call('/world/writable/sticky'), ]) test_subject.is_sticky_bit_set.assert_has_calls([ call('/world/writable/not/sticky'), call('/world/writable/sticky'), ])
def test_scan_when_two_failures(self): # Prepare data and mocks test_subject = WorldWritable(None) test_subject.scan_worldwritable_files_starting_with_dot = MagicMock( return_value=[]) test_subject.scan_worldwritable_directories_with_no_sticky_bit_set = MagicMock( return_value=['/some/failure']) test_subject.scan_worldwritable_files_owned_by_root = MagicMock( return_value=['/other/failure']) test_subject.get_scan_text = MagicMock( side_effect=['Test2 Failed', 'Test3 Failed']) # Run test scenario result = test_subject.scan() # Assertions test_subject.get_scan_text.assert_has_calls([ call('World writable directories with no sticky bit set', ['/some/failure']), call('World writable files owned by root', ['/other/failure']), ]) test_subject.scan_worldwritable_files_starting_with_dot.assert_called_once_with( ) test_subject.scan_worldwritable_directories_with_no_sticky_bit_set.assert_called_once_with( ) test_subject.scan_worldwritable_files_owned_by_root.assert_called_once_with( ) self.assertEqual(result[0], ScanStatus.fail) self.assertEqual(result[1], 'Test2 Failed\nTest3 Failed')
def test_scan_when_success(self): # Prepare data and mocks test_subject = WorldWritable(None) test_subject.scan_worldwritable_files_starting_with_dot = MagicMock(return_value=[]) test_subject.scan_worldwritable_directories_with_no_sticky_bit_set = MagicMock(return_value=[]) test_subject.scan_worldwritable_files_owned_by_root = MagicMock(return_value=[]) # Run test scenario result = test_subject.scan() # Assertions test_subject.scan_worldwritable_files_starting_with_dot.assert_called_once_with() test_subject.scan_worldwritable_directories_with_no_sticky_bit_set.assert_called_once_with() test_subject.scan_worldwritable_files_owned_by_root.assert_called_once_with() self.assertEqual(result[0], ScanStatus.success) self.assertEqual(result[1], '')
def test_scan_when_success(self): # Prepare data and mocks test_subject = WorldWritable(None) test_subject.scan_worldwritable_files_starting_with_dot = MagicMock( return_value=[]) test_subject.scan_worldwritable_directories_with_no_sticky_bit_set = MagicMock( return_value=[]) test_subject.scan_worldwritable_files_owned_by_root = MagicMock( return_value=[]) # Run test scenario result = test_subject.scan() # Assertions test_subject.scan_worldwritable_files_starting_with_dot.assert_called_once_with( ) test_subject.scan_worldwritable_directories_with_no_sticky_bit_set.assert_called_once_with( ) test_subject.scan_worldwritable_files_owned_by_root.assert_called_once_with( ) self.assertEqual(result[0], ScanStatus.success) self.assertEqual(result[1], '')
def test_scan_when_two_failures(self): # Prepare data and mocks test_subject = WorldWritable(None) test_subject.scan_worldwritable_files_starting_with_dot = MagicMock(return_value=[]) test_subject.scan_worldwritable_directories_with_no_sticky_bit_set = MagicMock(return_value=['/some/failure']) test_subject.scan_worldwritable_files_owned_by_root = MagicMock(return_value=['/other/failure']) test_subject.get_scan_text = MagicMock(side_effect=['Test2 Failed', 'Test3 Failed']) # Run test scenario result = test_subject.scan() # Assertions test_subject.get_scan_text.assert_has_calls( [ call('World writable directories with no sticky bit set', ['/some/failure']), call('World writable files owned by root', ['/other/failure']), ] ) test_subject.scan_worldwritable_files_starting_with_dot.assert_called_once_with() test_subject.scan_worldwritable_directories_with_no_sticky_bit_set.assert_called_once_with() test_subject.scan_worldwritable_files_owned_by_root.assert_called_once_with() self.assertEqual(result[0], ScanStatus.fail) self.assertEqual(result[1], 'Test2 Failed\nTest3 Failed')