Ejemplo n.º 1
0
class CompletionTestCases(TestCase):

    def setUp(self):
        # set up the session
        reload(c_settings)
        session_key = '11111111111111111111111111111111'
        # make mock basket lines
        p1 = MockBasketLine('10.00', 2, 'test thing')
        p2 = MockBasketLine('2.00', 1, 'another')
        # make the checkout
        c = Checkout()
        c.session_id = session_key
        c.items = [p1, p2]
        c.item_total = Decimal('10.00')
        c.lines = 3
        c.save()
        self.checkout = c
        # make request
        self.rf = MockRequest()


    def test_complete_if_paid(self):
        """If the checkout instance from the session is complete then the
        checkout page renders correctly"""
        # checkout is not paid
        self.checkout.paid = True
        # 
        request = self.rf.post(reverse('cccheckout:complete'))
        request.session['cccheckout'] = self.checkout
        r = complete(request)
        # status is 200
        self.assertEqual(200, r.status_code)
        # put checkout back to false
        self.checkout.paid = False
        self.checkout.save()

    def test_complete_redirects_if_checkout_is_not_paid(self):
        """If the checkout instance from the session is not marked as paid then
        the view will redirect to the payment page"""
        # checkout is not paid
        self.assertEqual(False, self.checkout.paid)
        # 
        request = self.rf.post(reverse('cccheckout:complete'))
        request.session['cccheckout'] = self.checkout
        r = complete(request)
        # redirects to payment
        self.assertEqual(reverse('cccheckout:payment'), r['Location'])
class StripeLiveViewTestCases(LiveServerTestCase):
    """Ensure that the stripe views are behaving as expected"""

    def setUp(self):
        # make request

        self.rf = MockRequest()
        self.session_key = '11111111111111111111111111111111'
        # make mock basket lines
        p1 = MockBasketLine('10.00', 2, 'test thing')
        p2 = MockBasketLine('2.00', 1, 'another')
        # make the checkout
        c = Checkout()
        c.session_id = self.session_key
        c.items = [p1, p2]
        c.item_total = Decimal('10.00')
        c.lines = 3
        c.paid = False
        c.save()
        self.checkout = c
        # lets be in dev mode
        settings.STRIPE_MODE = 'TEST'
        # store original so it can be set back later
        self.ORIGINAL_DEV_MODE = c_settings.STRIPE_MODE
        self.ORIGINAL_OUTCOME = c_settings.STRIPE_OUTCOME
        self.ORIGINAL_BASE_URL = c_settings.STRIPE_BASE_URL
        reload(c_settings)
        reload(stripe)

    def tearDown(self):
        # set back dev mode to whatever it was
        settings.STRIPE_MODE = self.ORIGINAL_DEV_MODE
        settings.STRIPE_BASE_URL = self.ORIGINAL_BASE_URL
        c_settings.STRIPE_OUTCOME = self.ORIGINAL_OUTCOME
    
    def test_successful_payment_redirects_custom_url(self):
        """Successul checkout can redirect to a url that throws a 
        NonReverseMatch exceprtion"""
        # store the original
        ORIGINAL_URL = c_settings.CCCHECKOUT_SUCCESS_URL
        # set the new one
        c_settings.CCCHECKOUT_SUCCESS_URL = '/epic-win.html'
        # checkout is not paid self.assertFalse(self.checkout.paid)
        data = {'stripeToken': 'testtoken'}
        request = self.rf.post(reverse('stripe:mock_api'), data)
        request.session['cccheckout'] = self.checkout
        r = card(request)
        # get the checkout
        c = Checkout.objects.get(pk=self.checkout.pk)
        # redirects to the new url
        self.assertEqual(r['Location'], '/epic-win.html')
        # put the original back
        c_settings.CCCHECKOUT_SUCCESS_URL = ORIGINAL_URL

    def test_apierror_500(self):
        """if an ApiError is raised then it is handled 
        gracefully"""
        settings.STRIPE_OUTCOME = 'APIERROR'
        reload(c_settings)
        # checkout is not paid self.assertFalse(self.checkout.paid)
        data = {'stripeToken': 'testtoken'}
        request = self.rf.post(reverse('stripe:mock_api'), data)
        request.session['cccheckout'] = self.checkout
        r = card(request)
        # get the checkout
        c = Checkout.objects.get(pk=self.checkout.pk)
        # checkout is not paid
        self.assertFalse(c.paid)
        # redirects to the payment
        self.assertEqual(200, r.status_code)

    def test_carderror_402(self):
        """if an CardError is raised then it is handled 
        gracefully"""
        settings.STRIPE_OUTCOME = 'CARDERROR'
        reload(c_settings)
        # checkout is not paid self.assertFalse(self.checkout.paid)
        data = {'stripeToken': 'testtoken'}
        request = self.rf.post(reverse('stripe:mock_api'), data)
        request.session['cccheckout'] = self.checkout
        r = card(request)
        # get the checkout
        c = Checkout.objects.get(pk=self.checkout.pk)
        # checkout is not paid
        self.assertFalse(c.paid)
        # redirects to the payment
        self.assertEqual(200, r.status_code)

    def test_authentication_error_401(self):
        """if an AuthenticationError is raised then it is handled 
        gracefully"""
        settings.STRIPE_OUTCOME = 'AUTHENTICATIONERROR'
        reload(c_settings)
        # checkout is not paid self.assertFalse(self.checkout.paid)
        data = {'stripeToken': 'testtoken'}
        request = self.rf.post(reverse('stripe:mock_api'), data)
        request.session['cccheckout'] = self.checkout
        r = card(request)
        # get the checkout
        c = Checkout.objects.get(pk=self.checkout.pk)
        # checkout is not paid
        self.assertFalse(c.paid)
        # redirects to the payment
        self.assertEqual(200, r.status_code)

    def test_invalid_request_error_404(self):
        """if an InvalidRequestError is raised then it is handled 
        gracefully"""
        settings.STRIPE_OUTCOME = 'INVALIDREQUEST_404'
        reload(c_settings)
        # checkout is not paid self.assertFalse(self.checkout.paid)
        data = {'stripeToken': 'testtoken'}
        request = self.rf.post(reverse('stripe:mock_api'), data)
        request.session['cccheckout'] = self.checkout
        r = card(request)
        # get the checkout
        c = Checkout.objects.get(pk=self.checkout.pk)
        # checkout is not paid
        self.assertFalse(c.paid)
        # redirects to the payment
        self.assertEqual(200, r.status_code)

    def test_invalid_request_error_400(self):
        """if an InvalidRequestError is raised then it is handled 
        gracefully"""
        settings.STRIPE_OUTCOME = 'INVALIDREQUEST_400'
        reload(c_settings)
        # checkout is not paid self.assertFalse(self.checkout.paid)
        data = {'stripeToken': 'testtoken'}
        request = self.rf.post(reverse('stripe:mock_api'), data)
        request.session['cccheckout'] = self.checkout
        r = card(request)
        # get the checkout
        c = Checkout.objects.get(pk=self.checkout.pk)
        # checkout is not paid
        self.assertFalse(c.paid)
        # redirects to the payment
        self.assertEqual(200, r.status_code)

    def test_unsuccessful_payment_redirects_to_payment(self):
        """if the response returns not paid then the checkout
        is not marked as paid"""
        settings.STRIPE_OUTCOME = 'UNPAID'
        reload(c_settings)
        # checkout is not paid self.assertFalse(self.checkout.paid)
        data = {'stripeToken': 'testtoken'}
        request = self.rf.post(reverse('stripe:mock_api'), data)
        request.session['cccheckout'] = self.checkout
        r = card(request)
        # get the checkout
        c = Checkout.objects.get(pk=self.checkout.pk)
        # checkout is not paid
        self.assertFalse(c.paid)
        # redirects to the payment
        self.assertEqual(200, r.status_code)

    def test_successful_payment_redirects(self):
        """if the response returns a paid amount then the checkout
        is marked as paid"""
        settings.STRIPE_OUTCOME = 'PASS'
        reload(c_settings)
        # checkout is not paid self.assertFalse(self.checkout.paid)
        data = {'stripeToken': 'testtoken'}
        request = self.rf.post(reverse('stripe:mock_api'), data)
        request.session['cccheckout'] = self.checkout
        r = card(request)
        # get the checkout
        c = Checkout.objects.get(pk=self.checkout.pk)
        # checkout is now paid
        self.assertTrue(c.paid)
        # redirects to the checkout:comlete
        self.assertEqual(r['Location'],
                reverse(c_settings.CCCHECKOUT_SUCCESS_URL))

    def test_requestresponse_saving_in_charge_create(self):
        """Request respone objects are saving when invoking charge_create"""
        self.assertEqual(0, RequestResponse.objects.count())
        settings.STRIPE_OUTCOME = 'PASS'
        reload(c_settings)
        # make an API
        api = StripeAPI(
                    checkout=self.checkout,
                    api_key='testkey')
        # our params
        params = {
            'amount': 1000,
            'currency': 'usd',
            'card': 'stripeToken',
            'description': 'test'}
        # make the call
        api.charge_create(**params)
        # now we have a request response object
        self.assertEqual(1, RequestResponse.objects.count())
class StripeViewTestCases(TestCase):

    def setUp(self):
        # store original so it can be set back later
        self.ORIGINAL_DEV_MODE = c_settings.STRIPE_MODE
        reload(stripe)
        # override some of the customer forms 
        settings.CCCHECKOUT_POSTAGE_MODELS = ()
        settings.CCCHECKOUT_CUSTOMER_FORM = None 
        settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
        settings.CCCHECKOUT_PAYMENT_FORMS = ()
        settings.STRIPE_MODE = 'TEST'
        # set up the session
        reload(c_settings)
        session_key = '11111111111111111111111111111111'
        # make mock basket lines
        p1 = MockBasketLine('10.00', 2, 'test thing')
        p2 = MockBasketLine('2.00', 1, 'another')
        # make the checkout
        c = Checkout()
        c.session_id = session_key
        c.items = [p1, p2]
        c.item_total = Decimal('10.00')
        c.lines = 3
        c.save()
        self.checkout = c
        # make request
        self.rf = MockRequest()

    def tearDown(self):
        # set back dev mode to whatever it was
        settings.STRIPE_MODE = self.ORIGINAL_DEV_MODE
        # remove the test adapter
        try:
            del(settings.CCCHECKOUT_ADAPTER)
        except AttributeError:
            pass
        try:
            del(settings.CCCHECKOUT_POSTAGE_MODELS)
        except AttributeError:
            pass
        try:
            del(settings.CCCHECKOUT_PAYMENT_FORMS)
        except AttributeError:
            pass
        try:
            del(settings.CCCHECKOUT_CUSTOMER_FORM)
        except AttributeError:
            pass
        # reload the settings
        reload(c_settings)

    def test_post_with_no_token_redirects(self):
        """if a request is posted to the card view and the post data
        is missiung ['stripeToken'] then a HttpResponseRedirect is returned"""
        request = self.rf.post(reverse('stripe:card'))
        request.session['cccheckout'] = self.checkout
        r = card(request)
        # was a redirect
        self.assertEqual(reverse('stripe:card'), r['Location'])

    def test_mock_api_only_when_test(self):
        """mock_api view is only available when STRIPE_MODE == TEST"""
        # set mode to test and we get a 200 respone
        settings.STRIPE_MODE = 'TEST'
        request = self.rf.post(reverse('stripe:mock_api'))
        r = mock_api(request)
        self.assertEqual(200, r.status_code)
        # set mode to live and now we get a 404
        settings.STRIPE_MODE = 'LIVE'
        reload(c_settings)
        self.assertRaises(Http404, mock_api, request)
Ejemplo n.º 4
0
class PostageTestCases(TestCase):

    def setUp(self):
        """set up the environment for the tests"""
        reload(c_settings)
        session_key = '11111111111111111111111111111111'
        settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
        engine = import_module(settings.SESSION_ENGINE)
        # make mock basket lines
        p1 = MockBasketLine('10.00', 2, 'test thing')
        p2 = MockBasketLine('2.00', 1, 'anotjer')
        # make a customer
        customer = Customer(delivery_country='GB')
        customer.save()
        # make a checkout on the session
        c = Checkout()
        c.session_id = session_key
        c.customer = customer
        c.items = [p1, p2]
        c.item_total = Decimal('10.00')
        c.lines = 3
        c.save()
        self.checkout = c
        # make request & add checkout
        self.rf = MockRequest()
        # add the postages
        line1 = Line(name='Test case line based postage')
        line1.save()
        country1  = LineCountry(
                        country='GB',
                        line = line1)
        country1.save()
        # add the tiers
        tier1 = LineTier(
                    minimum=1,
                    maximum=22,
                    price=Decimal('1.00'),
                    line=line1)
        tier1.save()
        # tier 2
        tier2 = LineTier(
                    minimum=23,
                    maximum=50,
                    price=Decimal('2.00'),
                    line=line1)
        tier2.save()
        # now the content types
        line_ct = ContentType.objects.get_for_model(line1)
        tier_ct = ContentType.objects.get_for_model(tier1)
        # set up the selfs
        self.line1 = line1
        self.country1 = country1
        self.tier1 = tier1
        self.tier2 = tier2
        self.line_ct = line_ct
        self.tier_ct = tier_ct

    def tearDown(self):
        # remove the test adapter
        try:
            del(settings.CCCHECKOUT_ADAPTER)
        except AttributeError:
            pass

        try:
            # repair the postage model setting
            del(settings.CCCHECKOUT_POSTAGE_MODELS)
        except AttributeError:
            pass

    def test_invalid_postage_is_200(self):
        """Invalid postage data will result the user not being forwarded
        to the next step and they're returned to the postage page"""

        value = '20:20|30:30' 
        data = {'available_methods': value}
        request = self.rf.post(reverse('cccheckout:postage'), data)
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = postage(request)
        # postage and postage_tier have not been saved onto the checkout
        checkout = request.session['cccheckout']
        self.assertEqual(checkout.postage, None)
        self.assertEqual(checkout.postage_tier, None)
        # and the status was 200
        self.assertEqual(200, response.status_code)

    def test_postage_decorator_not_required(self):
        """If there is no postage models in the settings then the postage
        view redirects"""
        settings.CCCHECKOUT_POSTAGE_MODELS = [] 
        reload(c_settings)
        request = self.rf.get(reverse('cccheckout:postage'))
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = postage(request)
        self.assertEqual(302, response.status_code)
        self.assertEqual(response['Location'], reverse('cccheckout:payment'))

    def test_decorator_200_if_postage_view(self):
        """If the postage view is being called then there are no validations"""
        request = self.rf.get(reverse('cccheckout:postage'))
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = postage(request)
        self.assertEqual(200, response.status_code)

    def test_valid_postage_saved(self):
        """if valid postage information is sent to the view the resulting
        postage and tier is saved onto the checkout"""
        value = '%s:%s|%s:%s' % (
                            self.line_ct.pk,
                            self.line1.pk,
                            self.tier_ct.pk,
                            self.tier1.pk)
        data = {'available_methods': value}
        request = self.rf.post(reverse('cccheckout:postage'), data)
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = postage(request)
        # postage and postage_tier have been saved onto the checkout
        checkout = request.session['cccheckout']
        self.assertEqual(checkout.postage, self.line1)
        self.assertEqual(checkout.postage_tier, self.tier1)
        self.assertEqual(checkout.postage_form_value, value)

    def test_postage_cannot_be_gamed(self):
        """Postage is evaluated on each call so that if the basket changes
Ejemplo n.º 5
0
class CustomerTestCases(TestCase):

    def setUp(self):
        # set up
        session_key = '11111111111111111111111111111111'
        settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
        engine = import_module(settings.SESSION_ENGINE)
        # make mock basket lines
        p1 = MockBasketLine('10.00', 2, 'test thing')
        p2 = MockBasketLine('2.00', 1, 'anotjer')
        # make a checkout on the session
        c = Checkout()
        c.session_id = session_key
        #c.items = MockQuerySet(items=[p1, p2])
        c.items = [p1, p2]
        c.item_total = Decimal('10.00')
        c.save()
        self.checkout = c
        # make request & add checkout
        self.rf = MockRequest()

    def tearDown(self):
        # remove the test adapter
        try:
            del(settings.CCCHECKOUT_ADAPTER)
        except AttributeError:
            pass

        try:
            # repair the customer form setting
            del(settings.CCCHECKOUT_CUSTOMER_FORM)
        except AttributeError:
            pass

    def test_no_form_bypasses_customer(self):
        """if `CCCHECKOUT_CUSTOMER_FORM` is None then the customer is not
        required"""
        settings.CCCHECKOUT_CUSTOMER_FORM = None
        reload(c_settings)
        # hit the customer view and it should be a 302
        request = self.rf.get('/')
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = customer(request)
        self.assertEqual(302, response.status_code)

    def test_customer_get_200(self):
        """when customer is called via get we have a 200"""
        request = self.rf.get(reverse('cccheckout:customer'))
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = customer(request)
        self.assertEqual(200, response.status_code)

    def test_customer_post_200(self):
        """post incorrect data and we get a 200"""
        request = self.rf.post(reverse('cccheckout:customer'), {})
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = customer(request)
        self.assertEqual(200, response.status_code)

    def test_customer_post_302(self):
        """send valid post information and we get a 302"""
        data = {
            'name': 'Testy Tester',
            'email': '*****@*****.**',
            'phone': '+0044-123123',
            'delivery_address_1': 'Somewhere',
            'delivery_city': 'Some City',
            'delivery_postcode': 'NE12 8UJ',
            'delivery_country': 'GB',
            'billing_address_1': 'Somewhere',
            'billing_city': 'Some City',
            'billing_postcode': 'NE12 8UJ',
            'billing_country': 'GB',
        }
        request = self.rf.post(reverse('cccheckout:customer'), data)
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = customer(request)
        self.assertEqual(302, response.status_code)

    def test_customer_model_saved_to_checkout(self):
        data = {
            'name': 'Testy Tester',
            'email': '*****@*****.**',
            'phone': '+0044-123123',
            'delivery_address_1': 'Somewhere',
            'delivery_city': 'Some City',
            'delivery_postcode': 'NE12 8UJ',
            'delivery_country': 'GB',
            'billing_address_1': 'Somewhere',
            'billing_city': 'Some City',
            'billing_postcode': 'NE12 8UJ',
            'billing_country': 'GB',
        }
        # no customer
        self.assertFalse(self.checkout.customer)
        request = self.rf.post(reverse('cccheckout:customer'), data)
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = customer(request)
        self.assertEqual(302, response.status_code)
        self.assertTrue(self.checkout.customer)
class DiscountCodeTestCases(TestCase):

    def setUp(self):
        # set up
        session_key = '11111111111111111111111111111111'
        settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
        engine = import_module(settings.SESSION_ENGINE)
        # make mock basket lines
        p1 = MockBasketLine('10.00', 2, 'test thing')
        p2 = MockBasketLine('2.00', 1, 'anotjer')
        # make a checkout on the session
        c = Checkout()
        c.session_id = session_key
        #c.items = MockQuerySet(items=[p1, p2])
        c.items = [p1, p2]
        c.item_total = Decimal('10.00')
        c.save()
        self.checkout = c
        # make request & add checkout
        self.rf = MockRequest()

    def tearDown(self):
        # remove the test adapter
        del(settings.CCCHECKOUT_ADAPTER)

    def test_percentage_based_discount(self):
        """percentage based discounts behave correctly"""
        d = DiscountCode()
        d.title = 'test discount code'
        d.code = 'TEST'
        d.type = DiscountCode.PERCENTAGE
        d.discount = 10
        d.save()
        # make the request

        post_request = self.rf.post('/yeah', {'code': 'TEST'})
        self.request = post_request
        self.request.session['cccheckout'] = self.checkout
        self.request.session.save()
        # get start page abd checkout total is now 9.00
        response = customer(self.request)
        self.assertEqual(self.request.session['cccheckout'].total_price(),
                        Decimal('9.00'))

    def test_monetary_based_discount(self):
        """monetary based discounts behave correctly"""
        d = DiscountCode()
        d.title = 'test discount code'
        d.code = 'TEST'
        d.type = DiscountCode.MONETARY
        d.discount = Decimal('2.00')
        d.save()
        # make the request
        #settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
        post_request = self.rf.post('/yeah', {'code': 'TEST'})
        self.request = post_request
        self.request.session['cccheckout'] = self.checkout
        self.request.session.save()
        # get start page ad checkout total is now 9.00
        response = customer(self.request)
        self.assertEqual(self.request.session['cccheckout'].total_price(),
                        Decimal('8.00'))

    def test_discount_code_form(self):
        """ensure the discoint form works as expected"""
        # make a discount code
        d = DiscountCode()
        d.type = DiscountCode.PERCENTAGE
        d.discount = Decimal('50.00')
        d.title = 'test discount'
        d.code = 'test'
        d.save()
        # make the form and get the discount
        form = DiscountCodeForm({'code': 'test'})
        form.is_valid()
        discount = form.save()
        self.assertEqual(discount.pk, d.pk)