def get_data_values(self): # Data values list_value = ListValue() list_value.add_all([IntegerValue(val) for val in range(1,15)]) data_values = [ VoidValue(), IntegerValue(3), DoubleValue(decimal.Decimal('7.2')), StringValue('blah'), SecretValue(), BooleanValue(True), BlobValue(b"a blob"), BlobValue(b"0\x82\x04\x00"), list_value, OptionalValue(), OptionalValue(IntegerValue(7)), StructValue('name'), self.get_struct_value(), self.get_error_value(), ] struct_value = StructValue('struct') for idx, data_val in enumerate(data_values): struct_value.set_field(str(idx), data_val) data_values.append(struct_value) return data_values
def test_authn_op_without_sec_ctx(self): ctx = ExecutionContext(None, None) input_val = VoidValue() method_result = self.sec_ctx_filter.invoke('svc', 'op2', input_val, ctx) self.assertEqual(method_result.output, None) self.assertEqual(method_result.error, error_values[3])
def test_op(self): ctx = ExecutionContext(ApplicationContext(), None) input_val = VoidValue() method_result = self.sec_ctx_filter.invoke('svc', 'op1', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def test_void_value(self): input_val = VoidValue() actual_val = DataValueConverter.convert_to_json(input_val) expected_val = 'null' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_noauth_op_unknown_op(self): sec_ctx = None app_ctx = ApplicationContext() ctx = ExecutionContext(app_ctx, sec_ctx) input_val = VoidValue() method_result = self.authz_filter.invoke('com', 'op', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def invoke(self, ctx, method_id, input_value): name = method_id.get_name() if name == raise_python_exception: raise RuntimeError() elif name == return_invalid_output: return MethodResult(output=StructValue()) elif name == mock_definition: return MethodResult(output=VoidValue()) return MethodResult(error=not_found_error)
def test_void_value(self): input_status = 200 input_response = '' expected_result = MethodResult(output=VoidValue()) actual_result = RestSerializer.deserialize_response( status=input_status, response_str=input_response, is_vapi_rest=True) self.assertEqual(expected_result.output, actual_result.output) self.assertEqual(expected_result.error, actual_result.error)
def test_success_2(self): ctx = ExecutionContext() service_id = interface_id.get_name() operation_id = mock_definition input_value = StructValue(OPERATION_INPUT) input_value.set_field(param_name, IntegerValue(10)) method_result = self.local_provider.invoke(service_id, operation_id, input_value, ctx) self.assertTrue(method_result.success()) self.assertEqual(method_result.output, VoidValue())
def invoke(self, ctx, method_id, input_): name = method_id.get_name() self.counters[name] = self.counters.get(name, 0) + 1 if name == veto_method_name: method_input = input_.get_field('operation_input') raise_error = method_input.get_field('raise') if raise_error.value: msg = Message('mockup.message.id', 'mockup error message') return MethodResult( error=make_error_value_from_msgs(not_found_error_def, msg)) return MethodResult(output=VoidValue())
def test_json_loopback_negative(self): connector = self.get_loopback_connector(self.provider) remote = connector.get_api_provider() # Bogus interface / method ctx = connector.new_context() params = VoidValue() result = remote.invoke('bogus.interface', 'bogus', params, ctx) self.assertFalse(result.success()) self.assertTrue(result.error == self.provider.invoke('bogus.interface', 'bogus', params, ctx).error) logging.error("error: %s", str(result.error))
def test_success(self): ctx = ExecutionContext() service_id = interface_id.get_name() operation_id = mock_definition method_def = self.introspection.get_method(service_id, operation_id) input_value = method_def.get_input_definition().new_value() input_value.set_field(param_name, IntegerValue(10)) method_result = self.local_provider.invoke(service_id, operation_id, input_value, ctx) self.assertTrue(method_result.success()) self.assertEqual(method_result.output, VoidValue())
def test_authn_op_with_sec_ctx(self): sec_ctx = SecurityContext({ SCHEME_ID: USER_PASSWORD_SCHEME_ID, USER_KEY: 'testuser', PASSWORD_KEY: 'password' }) ctx = ExecutionContext(None, sec_ctx) input_val = VoidValue() method_result = self.sec_ctx_filter.invoke('svc', 'op2', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(20)) self.assertEqual(method_result.error, None)
def test_invalid_user_pwd_scheme(self): sec_ctx = SecurityContext({ SCHEME_ID: OAUTH_SCHEME_ID, ACCESS_TOKEN: 'token' }) app_ctx = ApplicationContext() ctx = ExecutionContext(app_ctx, sec_ctx) input_val = VoidValue() method_result = self.authn_filter.invoke('com.pkg.svc', 'op', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def test_invalid_user_pwd(self): sec_ctx = SecurityContext({ SCHEME_ID: USER_PASSWORD_SCHEME_ID, USER_KEY: 'testuser', PASSWORD_KEY: 'invalidpassword' }) app_ctx = ApplicationContext() ctx = ExecutionContext(app_ctx, sec_ctx) input_val = VoidValue() method_result = self.authn_filter.invoke('com.pkg.svc', 'op', input_val, ctx) self.assertEqual(method_result.error.name, 'com.vmware.vapi.std.errors.unauthenticated')
def test_op_with_sec_ctx_on_filter(self): sec_ctx = SecurityContext({ SCHEME_ID: USER_PASSWORD_SCHEME_ID, USER_KEY: 'testuser', PASSWORD_KEY: 'password' }) self.sec_ctx_filter.set_security_context(sec_ctx) ctx = ExecutionContext(ApplicationContext(), None) input_val = VoidValue() method_result = self.sec_ctx_filter.invoke('svc', 'op1', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def test_user_pwd_scheme(self): sec_ctx = SecurityContext({ SCHEME_ID: USER_PASSWORD_SCHEME_ID, USER_KEY: 'testuser', PASSWORD_KEY: 'password' }) app_ctx = ApplicationContext() ctx = ExecutionContext(app_ctx, sec_ctx) input_val = VoidValue() method_result = self.authn_filter.invoke('com.pkg.svc', 'op', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def test_noauth_op_with_valid_user(self): sec_ctx = SecurityContext({ SCHEME_ID: USER_PASSWORD_SCHEME_ID, USER_KEY: 'testuser', PASSWORD_KEY: 'password', AUTHN_IDENTITY: UserIdentity('testuser') }) app_ctx = ApplicationContext() ctx = ExecutionContext(app_ctx, sec_ctx) input_val = VoidValue() method_result = self.authz_filter.invoke('com.pkg.svc', 'op1', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def deserialize_response(status, response_str, is_vapi_rest): """ Deserialize the REST response :type status: :class:`int` :param status: HTTP response status code :type response_str: :class:`str` :param response_str: HTTP response body :type is_vapi_rest: :class:`bool` :param is_vapi_rest: Whether the Rest json message format is VAPI Rest or not :rtype :class:`vmware.vapi.core.MethodResult` :return: VAPI MethodResult """ output, error = None, None if response_str is not None and response_str != '': response_value = DataValueConverter.convert_to_data_value( response_str) if status in successful_status_codes: # Successful response, create output # Skyscraper uses 202 for returning tasks if is_vapi_rest: # VAPI REST output has a value wrapper output = response_value.get_field('value') else: output = response_value else: # Create error if is_vapi_rest: # VAPI REST error has specific format name = response_value.get_field('type').value values = dict( response_value.get_field('value').get_fields()) error = ErrorValue(name=name, values=values) else: # For other REST APIs create generic error for now error = ErrorValue(name=http_to_vapi_error_map[status], values={ 'messages': ListValue(), 'data': response_value, }) else: # No response body if status == 200: output = VoidValue() return MethodResult(output=output, error=error)