def test_subscribe(self):
        logging.basicConfig(level=logging.DEBUG)  # make sure it's init'ed
        logger = logging.getLogger("recurly.http.request")
        logger.setLevel(logging.DEBUG)

        plan = Plan(
            plan_code="basicplan", name="Basic Plan", setup_fee_in_cents=Money(0), unit_amount_in_cents=Money(1000)
        )
        with self.mock_request("subscription/plan-created.xml"):
            plan.save()

        try:
            account = Account(account_code="subscribe%s" % self.test_id)
            with self.mock_request("subscription/account-created.xml"):
                account.save()

            try:

                sub = Subscription(
                    plan_code="basicplan",
                    currency="USD",
                    unit_amount_in_cents=1000,
                    bulk=True,
                    terms_and_conditions="Some Terms and Conditions",
                    customer_notes="Some Customer Notes",
                )

                with self.mock_request("subscription/error-no-billing-info.xml"):
                    try:
                        account.subscribe(sub)
                    except BadRequestError as exc:
                        error = exc
                    else:
                        self.fail("Subscribing with no billing info did not raise a BadRequestError")
                self.assertEqual(error.symbol, "billing_info_required")

                binfo = BillingInfo(
                    first_name="Verena",
                    last_name="Example",
                    address1="123 Main St",
                    city=six.u("San Jos\xe9"),
                    state="CA",
                    zip="94105",
                    country="US",
                    type="credit_card",
                    number="4111 1111 1111 1111",
                    verification_value="7777",
                    year="2015",
                    month="12",
                )
                with self.mock_request("subscription/update-billing-info.xml"):
                    account.update_billing_info(binfo)

                with self.mock_request("subscription/subscribed.xml"):
                    account.subscribe(sub)
                self.assertTrue(sub._url)

                manualsub = Subscription(
                    plan_code="basicplan", currency="USD", net_terms=10, po_number="1000", collection_method="manual"
                )
                with self.mock_request("subscription/subscribed-manual.xml"):
                    account.subscribe(manualsub)
                self.assertTrue(manualsub._url)
                self.assertEqual(manualsub.net_terms, 10)
                self.assertEqual(manualsub.collection_method, "manual")
                self.assertEqual(manualsub.po_number, "1000")

                with self.mock_request("subscription/account-subscriptions.xml"):
                    subs = account.subscriptions()
                self.assertTrue(len(subs) > 0)
                self.assertEqual(subs[0].uuid, sub.uuid)

                with self.mock_request("subscription/all-subscriptions.xml"):
                    subs = Subscription.all()
                self.assertTrue(len(subs) > 0)
                self.assertEqual(subs[0].uuid, sub.uuid)

                with self.mock_request("subscription/cancelled.xml"):
                    sub.cancel()
                with self.mock_request("subscription/reactivated.xml"):
                    sub.reactivate()

                # Try modifying the subscription.
                sub.timeframe = "renewal"
                sub.unit_amount_in_cents = 800
                with self.mock_request("subscription/updated-at-renewal.xml"):
                    sub.save()
                pending_sub = sub.pending_subscription
                self.assertTrue(isinstance(pending_sub, Subscription))
                self.assertEqual(pending_sub.unit_amount_in_cents, 800)
                self.assertEqual(sub.unit_amount_in_cents, 1000)

                with self.mock_request("subscription/terminated.xml"):
                    sub.terminate(refund="none")

                log_content = StringIO()
                log_handler = logging.StreamHandler(log_content)
                logger.addHandler(log_handler)

                sub = Subscription(
                    plan_code="basicplan",
                    currency="USD",
                    account=Account(
                        account_code="subscribe%s" % self.test_id,
                        billing_info=BillingInfo(
                            first_name="Verena",
                            last_name="Example",
                            address1="123 Main St",
                            city=six.u("San Jos\xe9"),
                            state="CA",
                            zip="94105",
                            country="US",
                            type="credit_card",
                            number="4111 1111 1111 1111",
                            verification_value="7777",
                            year="2015",
                            month="12",
                        ),
                    ),
                )
                with self.mock_request("subscription/subscribed-billing-info.xml"):
                    account.subscribe(sub)

                logger.removeHandler(log_handler)
                log_content = log_content.getvalue()
                self.assertTrue("<subscription" in log_content)
                self.assertTrue("<billing_info" in log_content)
                # See if we redacted our sensitive fields properly.
                self.assertTrue("4111" not in log_content)
                self.assertTrue("7777" not in log_content)

            finally:
                with self.mock_request("subscription/account-deleted.xml"):
                    account.delete()

            account_code_2 = "subscribe-%s-2" % self.test_id
            sub = Subscription(
                plan_code="basicplan",
                currency="USD",
                account=Account(
                    account_code=account_code_2,
                    billing_info=BillingInfo(
                        first_name="Verena",
                        last_name="Example",
                        address1="123 Main St",
                        city=six.u("San Jos\xe9"),
                        state="CA",
                        zip="94105",
                        country="US",
                        type="credit_card",
                        number="4111 1111 1111 1111",
                        verification_value="7777",
                        year="2015",
                        month="12",
                    ),
                ),
            )
            with self.mock_request("subscription/subscribe-embedded-account.xml"):
                sub.save()

            with self.mock_request("subscription/embedded-account-exists.xml"):
                acc = Account.get(account_code_2)
            self.assertEqual(acc.account_code, account_code_2)

            with self.mock_request("subscription/embedded-account-deleted.xml"):
                acc.delete()

        finally:
            with self.mock_request("subscription/plan-deleted.xml"):
                plan.delete()

        with self.mock_request("subscription/show.xml"):
            sub = Subscription.get("123456789012345678901234567890ab")
            self.assertEqual(sub.tax_in_cents, 0)
            self.assertEqual(sub.tax_type, "usst")

            with self.mock_request("subscription/redemptions.xml"):
                self.assertEqual(type(sub.redemptions()), recurly.resource.Page)