def test_update_customer_subscription(self):
        # Response for customer query
        responses.add(responses.POST, Customer.build_url('/customers/get'),
                      body=self.read_fixture('customers_without_items.xml'),
                      content_type='application/xml')
        # Response for query to new plan when plan code for subscription is
        # changed
        responses.add(responses.POST,
                      Plan.build_url('/plans/get', code='PAID_MONTHLY'),
                      body=self.read_fixture('plan_paid_monthly.xml'),
                      content_type='application/xml')

        customers = Customer.all()
        assert len(customers) == 1
        customer = customers[0]

        # Response for edit request
        responses.add(responses.POST,
                      Customer.build_url('/customers/edit', code=customer.code),
                      status=200, body='<xml></xml>',
                      content_type='application/xml')

        customer.subscription.plan_code = 'PAID_MONTHLY'
        customer.save()

        assert len(responses.calls) == 3

        edit_request = responses.calls[2].request
        body = urlparse.parse_qs(edit_request.body)

        assert body['subscription[planCode]'] == ['PAID_MONTHLY']
    def test_error_no_product_parsing(self):
        responses.add(responses.POST,
                      Customer.build_url('/customers/get'),
                      status=400,
                      body=self.read_fixture('error_no_product.xml'),
                      content_type='application/xml')

        with self.assertRaises(BadRequest) as context:
            customers = Customer.all()

        assert context.exception.error_id == '149947'
        assert context.exception.code == '400'
        assert context.exception.message == 'No product selected. Need a ' \
                'productId or productCode.'
        assert context.exception.aux_code == ''
    def test_update_customer(self):
        # Response for customer query
        responses.add(responses.POST, Customer.build_url('/customers/get'),
                      body=self.read_fixture('customers_without_items.xml'),
                      content_type='application/xml')

        customers = Customer.all()
        assert len(customers) == 1
        customer = customers[0]

        # Response for edit request
        responses.add(responses.POST,
                      Customer.build_url('/customers/edit', code=customer.code),
                      status=200, body='<xml></xml>',
                      content_type='application/xml')

       # Nothing to update yet
        customer.save()
        assert len(responses.calls) == 1

        # Still nothing to save
        customer.first_name = getattr(customer, 'first_name')
        customer.last_name = getattr(customer, 'last_name')
        customer.save()
        assert len(responses.calls) == 1

        # Really saving something this time
        customer.first_name = 'New first name'
        customer.last_name = 'New last name'
        customer.update_metadata('key_2', 'value_9')
        customer.update_metadata('key_3', 'value_3')
        customer.save()
        request = responses.calls[1].request
        assert request.method == 'POST'
        body = urlparse.parse_qs(request.body)
        assert body['firstName'] == ['New first name']
        assert body['lastName'] == ['New last name']
        assert body['metaData[key_2]'] == ['value_9']
        assert body['metaData[key_3]'] == ['value_3']
    def test_customer_without_items_parsing(self):
        #: Mock plans response
        responses.add(responses.POST, Customer.build_url('/customers/get'),
                      body=self.read_fixture('customers_without_items.xml'),
                      content_type='application/xml')

        customers = Customer.all()

        assert len(customers) == 1

        customer = customers[0]
        assert isinstance(customer, Customer)

        assert customer._to_persist == {}
        assert customer._product_code == 'Test'

        #: Customer data
        assert customer.id == '10681b62-6dcd-102e-b098-40402145ee8b'
        assert customer.first_name == 'Test'
        assert customer.last_name == 'User'
        assert customer.email == '*****@*****.**'
        assert customer.vat_number == None
        assert customer.is_vat_exempt == 0
        assert customer.company == None
        assert customer.gateway_token == None
        assert customer.modified_datetime == \
                arrow.get('2011-01-10T05:45:51+00:00').datetime
        assert customer.created_datetime == \
                arrow.get('2011-01-10T05:45:51+00:00').datetime
        assert customer.referer == None
        assert customer.referer_host == None
        assert customer.notes == None
        #: TODO fix metadata implementation
        #: assert customer.metadata == []

        #: Campaign data
        assert customer.campaign_name == None
        assert customer.campaign_medium == None
        assert customer.campaign_source == None
        assert customer.campaign_content == None
        assert customer.campaign_term == None
        assert customer.first_contact_datetime == None

        #: Verify subscription
        assert len(customer.subscriptions) == 1
        #: Check that subscription property correctly points to the current
        #: subscription
        assert customer.subscription == customer.subscriptions[0]
        subscription = customer.subscription
        #: Check that parent customer is set correctly
        assert subscription.customer == customer
        assert subscription._to_persist == {}
        assert subscription._product_code == 'Test'

        assert subscription.id == '106953e3-6dcd-102e-b098-40402145ee8b'
        assert subscription.cc_last_four == None
        assert subscription.cc_company == None
        assert subscription.cc_state == None
        assert subscription.created_datetime == \
                arrow.get('2011-01-10T05:45:51+00:00').datetime
        assert subscription.canceled_datetime == None
        assert subscription.cc_type == None
        assert subscription.cc_city == None
        assert subscription.cc_zip == None
        assert subscription.cc_address == None
        assert subscription.cc_country == None
        assert subscription.cc_first_name == None
        assert subscription.cc_last_name == None
        assert subscription.cc_expiration_date == None
        assert subscription.gateway_token == None

        assert isinstance(subscription.gateway_account, GatewayAccount)
        #: TODO fix handling of id
        #: assert subscription.gateway_account.id == \
        #:         'f3fb7029-11f4-475f-ab8d-80f8771e26d0'
        assert subscription.gateway_account.type == 'paypal'
        assert subscription.gateway_account.gateway == 'PayPal'
        assert subscription.gateway_account.subscription == subscription

        #: Verify subscription plan
        assert len(subscription.plans) == 1
        assert subscription.plan == subscription.plans[0]
        plan = subscription.plan
        #: Check parent subscription is set correctly
        assert plan.subscription == subscription

        assert plan._to_persist == {}
        assert plan._product_code == 'Test'

        assert plan.code == 'FREE_MONTHLY'
        assert plan.id == '6b0d13f4-6bef-102e-b098-40402145ee8b'
        assert plan.recurring_charge_amount == 0.0
        assert plan.name == 'Free Monthly'
        assert plan.billing_frequency == 'monthly'
        assert plan.setup_charge_code == None
        assert plan.is_active == 1
        assert plan.is_free == 1
        assert plan.billing_frequency_unit == 'months'
        assert plan.initial_bill_count_unit == 'months'
        assert plan.setup_charge_amount == 0.0
        assert plan.billing_frequency_per == 'month'
        assert plan.trial_days == 0
        assert plan.created_datetime == \
                arrow.get('2011-01-07T20:46:43+00:00').datetime
        assert plan.recurring_charge_code == 'FREE_MONTHLY_RECURRING'
        assert plan.initial_bill_count == 1
        assert plan.description == 'A free monthly plan'

        #: Verify subscription invoices
        assert len(subscription.invoices) == 1
        invoice = subscription.invoices[0]

        #: Check parent subscription is set correctly
        assert invoice.subscription == subscription

        assert invoice._to_persist == {}
        assert invoice._product_code == 'Test'

        assert invoice.id == '106ed222-6dcd-102e-b098-40402145ee8b'
        assert invoice.code == None
        assert invoice.paid_transaction_id == None
        assert invoice.created_datetime == \
                arrow.get('2011-01-10T05:45:51+00:00').datetime
        assert invoice.number == 1
        assert invoice.billing_datetime == \
                arrow.get('2011-02-10T05:45:51+00:00').datetime
        assert invoice.vat_rate == None
        assert invoice.type == 'subscription'
        assert invoice.charges == []