# Path: authorizenet/models.py
# class CustomerProfile(models.Model):
# 
#     """Authorize.NET customer profile"""
# 
#     customer = models.OneToOneField(settings.CUSTOMER_MODEL,
#                                     related_name='customer_profile')
#     profile_id = models.CharField(max_length=50)
# 
#     def save(self, *args, **kwargs):
#         """If creating new instance, create profile on Authorize.NET also"""
#         data = kwargs.pop('data', {})
#         sync = kwargs.pop('sync', True)
#         if not self.id and sync:
#             self.push_to_server(data)
#         super(CustomerProfile, self).save(*args, **kwargs)
# 
#     def delete(self):
#         """Delete the customer profile remotely and locally"""
#         response = delete_profile(self.profile_id)
#         response.raise_if_error()
#         super(CustomerProfile, self).delete()
# 
#     def push_to_server(self, data):
#         """Create customer profile for given ``customer`` on Authorize.NET"""
#         output = add_profile(self.customer.pk, data, data)
#         output['response'].raise_if_error()
#         self.profile_id = output['profile_id']
#         self.payment_profile_ids = output['payment_profile_ids']
# 
#     def sync(self):
#         """Overwrite local customer profile data with remote data"""
#         output = get_profile(self.profile_id)
#         output['response'].raise_if_error()
#         for payment_profile in output['payment_profiles']:
#             instance, created = CustomerPaymentProfile.objects.get_or_create(
#                 customer_profile=self,
#                 payment_profile_id=payment_profile['payment_profile_id']
#             )
#             instance.sync(payment_profile)
# 
#     objects = CustomerProfileManager()
# 
#     def __unicode__(self):
#         return self.profile_id
# 
# Path: tests/tests/utils.py
# def create_user(id=None, username='', password=''):
#     user = User(username=username)
#     user.id = id
#     user.set_password(password)
#     user.save()
#     return user
# 
# def xml_to_dict(node):
#     """Recursively convert minidom XML node to dictionary"""
#     node_data = {}
#     if node.nodeType == node.TEXT_NODE:
#         node_data = node.data
#     elif node.nodeType not in (node.DOCUMENT_NODE, node.DOCUMENT_TYPE_NODE):
#         node_data.update(node.attributes.items())
#     if node.nodeType not in (node.TEXT_NODE, node.DOCUMENT_TYPE_NODE):
#         for child in node.childNodes:
#             child_name, child_data = xml_to_dict(child)
#             if not child_data:
#                 child_data = ''
#             if child_name not in node_data:
#                 node_data[child_name] = child_data
#             else:
#                 if not isinstance(node_data[child_name], list):
#                     node_data[child_name] = [node_data[child_name]]
#                 node_data[child_name].append(child_data)
#         if node_data.keys() == ['#text']:
#             node_data = node_data['#text']
#     if node.nodeType == node.DOCUMENT_NODE:
#         return node_data
#     else:
#         return node.nodeName, node_data
# 
# Path: tests/tests/mocks.py

# 
# Path: tests/tests/test_data.py



from httmock import HTTMock, with_httmock
from xml.dom.minidom import parseString
from django.test import TestCase
from authorizenet.models import CustomerProfile
from .utils import create_user, xml_to_dict
from .mocks import cim_url_match, customer_profile_success, delete_success
from .test_data import create_empty_profile_success, delete_profile_success



class RequestError(Exception):
    pass


def error_on_request(url, request):
    raise RequestError("CIM Request")


class CustomerProfileModelTests(TestCase):

    """Tests for CustomerProfile model"""

    def setUp(self):

# Path: authorizenet/models.py
# class CustomerProfile(models.Model):
# 
#     """Authorize.NET customer profile"""
# 
#     customer = models.OneToOneField(settings.CUSTOMER_MODEL,
#                                     related_name='customer_profile')
#     profile_id = models.CharField(max_length=50)
# 
#     def save(self, *args, **kwargs):
#         """If creating new instance, create profile on Authorize.NET also"""
#         data = kwargs.pop('data', {})
#         sync = kwargs.pop('sync', True)
#         if not self.id and sync:
#             self.push_to_server(data)
#         super(CustomerProfile, self).save(*args, **kwargs)
# 
#     def delete(self):
#         """Delete the customer profile remotely and locally"""
#         response = delete_profile(self.profile_id)
#         response.raise_if_error()
#         super(CustomerProfile, self).delete()
# 
#     def push_to_server(self, data):
#         """Create customer profile for given ``customer`` on Authorize.NET"""
#         output = add_profile(self.customer.pk, data, data)
#         output['response'].raise_if_error()
#         self.profile_id = output['profile_id']
#         self.payment_profile_ids = output['payment_profile_ids']
# 
#     def sync(self):
#         """Overwrite local customer profile data with remote data"""
#         output = get_profile(self.profile_id)
#         output['response'].raise_if_error()
#         for payment_profile in output['payment_profiles']:
#             instance, created = CustomerPaymentProfile.objects.get_or_create(
#                 customer_profile=self,
#                 payment_profile_id=payment_profile['payment_profile_id']
#             )
#             instance.sync(payment_profile)
# 
#     objects = CustomerProfileManager()
# 
#     def __unicode__(self):
#         return self.profile_id
# 
# Path: tests/tests/utils.py
# def create_user(id=None, username='', password=''):
#     user = User(username=username)
#     user.id = id
#     user.set_password(password)
#     user.save()
#     return user
# 
# def xml_to_dict(node):
#     """Recursively convert minidom XML node to dictionary"""
#     node_data = {}
#     if node.nodeType == node.TEXT_NODE:
#         node_data = node.data
#     elif node.nodeType not in (node.DOCUMENT_NODE, node.DOCUMENT_TYPE_NODE):
#         node_data.update(node.attributes.items())
#     if node.nodeType not in (node.TEXT_NODE, node.DOCUMENT_TYPE_NODE):
#         for child in node.childNodes:
#             child_name, child_data = xml_to_dict(child)
#             if not child_data:
#                 child_data = ''
#             if child_name not in node_data:
#                 node_data[child_name] = child_data
#             else:
#                 if not isinstance(node_data[child_name], list):
#                     node_data[child_name] = [node_data[child_name]]
#                 node_data[child_name].append(child_data)
#         if node_data.keys() == ['#text']:
#             node_data = node_data['#text']
#     if node.nodeType == node.DOCUMENT_NODE:
#         return node_data
#     else:
#         return node.nodeName, node_data
# 
# Path: tests/tests/mocks.py

# 
# Path: tests/tests/test_data.py



# Path: tests/tests/models.py
from httmock import HTTMock, with_httmock
from xml.dom.minidom import parseString
from django.test import TestCase
from authorizenet.models import CustomerProfile
from .utils import create_user, xml_to_dict
from .mocks import cim_url_match, customer_profile_success, delete_success
from .test_data import create_empty_profile_success, delete_profile_success




class RequestError(Exception):
    pass


def error_on_request(url, request):
    raise RequestError("CIM Request")


class CustomerProfileModelTests(TestCase):

    """Tests for CustomerProfile model"""

    def setUp(self):

        self.user = create_user(id=42, username='billy', password='password')