コード例 #1
0
    def test_create_customer_without_subscription(self):
        responses.add(responses.POST,
                      Customer.build_url('/customers/new', is_new=True),
                      status=200, body='<xml></xml>',
                      content_type='application/xml')

        customer = Customer()
        customer.code = 1
        customer.first_name = 'Test'
        customer.last_name = 'User'
        customer.email = 'Email'
        customer.update_metadata('address', '123 Test street')
        customer.save()

        request = responses.calls[0].request

        assert request.method == 'POST'

        body = urlparse.parse_qs(request.body)

        assert body['code'] == ['1']
        assert body['firstName'] == ['Test']
        assert body['lastName'] == ['User']
        assert body['email'] == ['Email']
        assert body['metaData[address]'] == ['123 Test street']
コード例 #2
0
    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']
コード例 #3
0
    def test_error_no_customer_parsing(self):
        responses.add(responses.POST,
                      Customer.build_url('/customers/get', code=-1),
                      status=404,
                      body=self.read_fixture('error_no_customer.xml'),
                      content_type='application/xml')

        with self.assertRaises(NotFound) as context:
            customer = Customer.get(-1)

        assert context.exception.error_id == '73542'
        assert context.exception.code == '404'
        assert context.exception.message == 'Customer not found'
        assert context.exception.aux_code == ''
コード例 #4
0
    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 == ''
コード例 #5
0
    def test_create_customer_with_subscription(self):
        responses.add(responses.POST,
                      Customer.build_url('/customers/new', is_new=True),
                      status=200, body='<xml></xml>',
                      content_type='application/xml')
        responses.add(responses.POST, Plan.build_url('/plans/get',
                      code='FREE_MONTHLY'),
                      body=self.read_fixture('plan_free_monthly.xml'),
                      content_type='application/xml')

        customer = Customer()
        customer.code = '1'
        customer.first_name = 'Test'
        customer.last_name = 'User'
        customer.email = 'Email'
        customer.subscription.plan_code = 'FREE_MONTHLY'
        customer.subscription.cc_number = '1234123412341234'
        customer.subscription.cc_first_name = 'First'
        customer.subscription.cc_last_name = 'Last'
        customer.subscription.cc_expiration = '20/2020'
        customer.subscription.cc_card_code = '123'
        customer.save()

        # There should be 2 calls, one to fetch the plan information then one
        # to create the customer with the subscription
        assert len(responses.calls) == 2
        assert responses.calls[0].request.method == 'POST'
        request = responses.calls[1].request
        assert request.method == 'POST'

        body = urlparse.parse_qs(request.body)

        assert body['code'] == ['1']
        assert body['firstName'] == ['Test']
        assert body['lastName'] == ['User']
        assert body['email'] == ['Email']
        assert body['subscription[planCode]'] == ['FREE_MONTHLY']
        assert body['subscription[ccNumber]'] == ['1234123412341234']
        assert body['subscription[ccFirstName]'] == ['First']
        assert body['subscription[ccLastName]'] == ['Last']
        assert body['subscription[ccExpiration]'] == ['20/2020']
        assert body['subscription[ccCardCode]'] == ['123']

        # This should not result in an additional call because nothing has been
        # changed since the last save
        customer.save()
        assert len(responses.calls) == 2
コード例 #6
0
    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']
コード例 #7
0
def get_items_by_customer_code():
    """Helper method that uses the /customer/list API of endpoint to get a
    dictionary of the items available to each customer keyed by code."""

    plans = {plan.code: plan for plan in Plan.all()}
    customers = Customer.list()

    items = {}
    for customer in customers:
        # Grab the items for each customer from the plan of the subscription
        items[int(customer.code)] = {item.name: item.quantity_included \
                for item in plans[customer.subscription.plan.code].items}

    return items
コード例 #8
0
    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 == []