def test_execute_command_success(self): """Ensure execution of thread based on s3user operation request from client. """ request_mock = mock.MagicMock() command_args = { 'command': 'user', 'action': 'remove', 'name': 'test', 'access_key': 'TgVCZxb7TY2wG-ySXVIR1w', 'secret_key': 'Ihcg/nOEMAVuTB8MQZuUdwDaoAkGdkMhl+AFcd+B' } request_mock.selection_args = command_args s3user_provider = S3UsersProvider("users", "handling users") s3user_provider._handle_success = mock.MagicMock() with mock.patch("sspl_hl.providers.s3_users.provider.deferToThread", return_value=defer.succeed( SsplHlProviderS3Users_list.user_list_response)): with mock.patch('{}.{}.{}'.format("sspl_hl.utils.s3admin", "user_handler", "UsersUtility.remove")) \ as user_remove_handler_mock: s3user_provider.execute_command(request_mock) action = request_mock.selection_args.get('action', None) self.assertEqual(action, 'remove') s3user_provider._handle_success.assert_called_once_with( SsplHlProviderS3Users_list.user_list_response, request_mock)
def test_execute_command_failure(self): """Handle failure while execution of thread based on s3user operation request from client. """ request_mock = mock.MagicMock() command_args = { 'command': 'user', 'action': 'create', 'name': 'test', 'access_key': 'TgVCZxb7TY2wG-ySXVIR1w', 'secret_key': 'Ihcg/nOEMAVuTB8MQZuUdwDaoAkGdkMhl+AFcd+B' } request_mock.selection_args = command_args s3user_provider = S3UsersProvider("users", "handling users") s3user_provider._handle_failure = mock.MagicMock() with mock.patch("sspl_hl.providers.s3_users.provider.deferToThread", return_value=defer.fail(FakeError('error'))): with mock.patch('{}.{}.{}'.format("sspl_hl.utils.s3admin", "user_handler", "UsersUtility.create")) \ as user_create_handler_mock: s3user_provider.execute_command(request_mock) action = request_mock.selection_args.get('action', None) self.assertEqual(action, 'create') self.assertEqual(s3user_provider._handle_failure.call_count, 1)
def handle_users_command(self, request): """ Handles all user related operations. """ self._child_provider = S3UsersProvider("users", "handling users") self._child_provider.handleRequest(request)
def test_handleRequest(self): """ Ensure request from s3admin subcommand is received and processed further. """ request_mock = mock.MagicMock() s3user_provider = S3UsersProvider("users", "handling users") s3user_provider.render_query = mock.MagicMock() s3user_provider.handleRequest(request_mock) s3user_provider.render_query.assert_called_once_with(request_mock)
def test_render_query(self): """ Ensure request from handleRequest function is received and processed further. """ request_mock = mock.MagicMock() s3user_provider = S3UsersProvider("users", "handling users") s3user_provider.process_s3admin_request = mock.MagicMock() with mock.patch('{}.{}.{}'.format("sspl_hl.utils", "base_castor_provider", "BaseCastorProvider.render_query"), return_value=True): s3user_provider.render_query(request_mock) s3user_provider.process_s3admin_request.assert_called_once_with( request_mock)
def test_handle_list_success(self): """ When user list is successful, ensure that further function is called to process the recived response from s3 server. """ request_mock = mock.MagicMock() s3user_provider = S3UsersProvider("users", "handling users") with mock.patch('{}.{}.{}'.format( "sspl_hl.utils", "message_utils", "S3CommandResponse.get_response_message")) \ as response_mock: s3user_provider._handle_success(self.user_list_response, request_mock) response_mock.assert_called_once_with(self.user_list_response) self.assertEqual(request_mock.reply.call_count, 1)
def test_process_s3admin_request_failure(self, get_client_mock): """ Ensure process the request bundle request based on the command """ get_client_mock.return_value = None, "Fake_Response" request_mock = mock.MagicMock() command_args = { 'command': 'user', 'action': 'create', 'name': 'test', 'access_key': 'TgVCZxb7TY2wG-ySXVIR1w', 'secret_key': 'Ihcg/nOEMAVuTB8MQZuUdwDaoAkGdkMhl+AFcd+B' } request_mock.selection_args = command_args s3user_provider = S3UsersProvider("users", "handling users") s3user_provider.execute_command = mock.MagicMock() with mock.patch('{}.{}.{}'.format( "sspl_hl.utils", "message_utils", "S3CommandResponse.get_response_message")) \ as response_mock: s3user_provider.process_s3admin_request(request_mock) self.assertEqual(response_mock.call_count, 1) self.assertEqual(request_mock.reply.call_count, 1) self.assertEqual(s3user_provider.execute_command.call_count, 0)