def test_generate_secret_ko(): """Verify expected exception is raised when IOError""" with patch('builtins.open'): with patch('os.chmod'): with patch('api.authentication.chown', side_effect=PermissionError): assert authentication.generate_secret()
def test_generate_secret(mock_open, mock_chown, mock_chmod, mock_token): """Check if result's length and type are as expected and mocked function are called with correct params""" result = authentication.generate_secret() assert isinstance(result, str) assert result == 'test_token' calls = [call(authentication._secret_file_path, mode='x')] mock_open.has_calls(calls) mock_chown.assert_called_once_with(authentication._secret_file_path, 'ossec', 'ossec') mock_chmod.assert_called_once_with(authentication._secret_file_path, 0o640) with patch('os.path.exists', return_value=True): authentication.generate_secret() calls.append(call(authentication._secret_file_path, mode='r')) mock_open.has_calls(calls)
def test_generate_secret_ko(): """Verify expected exception is raised when IOError""" with patch('builtins.open'): with patch('api.authentication.chown', side_effect=PermissionError): with pytest.raises(APIException, match=".* 2002 .*"): authentication.generate_secret()