def test_handle_get_return_object(self): resource = 'lbvserver' args = copy.deepcopy(module_arguments) args.update( dict( nitro_user='******', nitro_pass='******', resource=resource, )) resource_data = { 'property1': 'value1', 'property2': 'value2', } module_mock = Mock(params=args, from_json=json.loads) with patch( 'ansible_collections.notstdlib.moveitallout.plugins.modules.netscaler_nitro_request.AnsibleModule', Mock(return_value=module_mock)): instance = netscaler_nitro_request.NitroAPICaller() data = {resource: resource_data} result = { 'nitro_errorcode': 0, 'http_response_body': json.dumps(data), } expected_result = {'nitro_object': resource_data} expected_result.update(result) instance.handle_get_return_object(result) self.assertDictEqual(result, expected_result)
def test_edit_response_data_actual_body_data_irrelevant(self): args = copy.deepcopy(module_arguments) args.update( dict( nitro_user='******', nitro_pass='******', nitro_auth_token='##DDASKLFDJ', )) module_mock = Mock(params=args, from_json=json.loads) with patch( 'ansible_collections.notstdlib.moveitallout.plugins.modules.netscaler_nitro_request.AnsibleModule', Mock(return_value=module_mock)): with tempfile.TemporaryFile() as r: actual_body = {} r.write(codecs.encode(json.dumps(actual_body), 'utf-8')) r.seek(0) instance = netscaler_nitro_request.NitroAPICaller() info = { 'status': 200, } result = {} success_status = 200 expected_result = { 'http_response_body': json.dumps(actual_body), 'http_response_data': info, 'nitro_errorcode': 0, 'nitro_message': 'Success', 'nitro_severity': 'NONE', } instance.edit_response_data(r, info, result, success_status) self.assertDictEqual(result, expected_result)
def test_nitro_user_pass_credentials(self): args = copy.deepcopy(module_arguments) args.update(dict( nitro_user='******', nitro_pass='******', )) mock_module_instance = Mock(params=args) expected_headers = { 'Content-Type': 'application/json', 'X-NITRO-USER': '******', 'X-NITRO-PASS': '******', } module_mock = Mock(return_value=mock_module_instance) with patch( 'ansible_collections.notstdlib.moveitallout.plugins.modules.netscaler_nitro_request.AnsibleModule', module_mock): instance = netscaler_nitro_request.NitroAPICaller() self.assertDictEqual(instance._headers, expected_headers)
def test_mas_proxy_call_headers_instance_name(self): args = copy.deepcopy(module_arguments) args.update( dict( nitro_auth_token='##ABDB', operation='add', instance_name='myname', )) mock_module_instance = Mock(params=args) expected_headers = { 'Content-Type': 'application/json', '_MPS_API_PROXY_MANAGED_INSTANCE_NAME': args['instance_name'], 'Cookie': 'NITRO_AUTH_TOKEN=%s' % args['nitro_auth_token'], } module_mock = Mock(return_value=mock_module_instance) with patch( 'ansible_collections.notstdlib.moveitallout.plugins.modules.netscaler_nitro_request.AnsibleModule', module_mock): instance = netscaler_nitro_request.NitroAPICaller() self.assertDictEqual(instance._headers, expected_headers)
def test_edit_response_data_no_body_fail_status(self): with patch( 'ansible_collections.notstdlib.moveitallout.plugins.modules.netscaler_nitro_request.AnsibleModule' ): instance = netscaler_nitro_request.NitroAPICaller() r = None info = { 'status': 201, } result = {} success_status = 200 expected_result = { 'nitro_errorcode': -1, 'nitro_message': 'HTTP status %s' % info['status'], 'nitro_severity': 'ERROR', 'http_response_body': '', 'http_response_data': info, } instance.edit_response_data(r, info, result, success_status) self.assertDictEqual(result, expected_result)
def test_fail_on_conflicting_authentication_methods(self): args = copy.deepcopy(module_arguments) args.update( dict( nitro_user='******', nitro_pass='******', nitro_auth_token='##DDASKLFDJ', )) mock_module_instance = Mock(params=args) expected_calls = [ call.fail_json( changed=False, failed=True, msg= 'Cannot define both authentication token and username/password' ) ] module_mock = Mock(return_value=mock_module_instance) with patch( 'ansible_collections.notstdlib.moveitallout.plugins.modules.netscaler_nitro_request.AnsibleModule', module_mock): netscaler_nitro_request.NitroAPICaller() mock_module_instance.assert_has_calls(expected_calls)
def test_edit_response_data_body_in_info(self): args = copy.deepcopy(module_arguments) args.update(dict( nitro_user='******', nitro_pass='******', )) module_mock = Mock(params=args, from_json=json.loads) with patch( 'ansible_collections.notstdlib.moveitallout.plugins.modules.netscaler_nitro_request.AnsibleModule', Mock(return_value=module_mock)): body = { 'errorcode': 258, 'message': 'Numerical error 258', 'severity': 'ERROR' } instance = netscaler_nitro_request.NitroAPICaller() r = None info = { 'status': 200, 'body': codecs.encode(json.dumps(body), 'utf-8'), } result = {} success_status = 200 expected_result = { 'http_response_body': json.dumps(body), 'http_response_data': info, } nitro_data = {} for key, value in body.items(): nitro_data['nitro_%s' % key] = value expected_result.update(nitro_data) instance.edit_response_data(r, info, result, success_status) self.assertDictEqual(result, expected_result)