Пример #1
0
class RedirectEmptyBasketTests(TestCase):
    
    def setUp(self):
        # make request & add checkout
        self.rf = MockRequest()
        # reload the c_settings to make sure we're using the defaults
        reload(c_settings)

    def test_customer_redirects_empty(self):
        """If a basket is empty then the checkout redirects to the basket"""
        request = self.rf.get(reverse('cccheckout:customer'))
        response = customer(request)
        self.assertEqual(302, response.status_code)

    def test_postage_redirects_empty(self):
        """If a basket is empty then the checkout redirects to the basket"""
        request = self.rf.get(reverse('cccheckout:postage'))
        response = postage(request)
        self.assertEqual(302, response.status_code)

    def test_payment_redirects_empty(self):
        """If a basket is empty then the checkout redirects to the basket"""
        request = self.rf.get(reverse('cccheckout:payment'))
        response = payment(request)
        self.assertEqual(302, response.status_code)
 def test_nonexistant_adapter(self):
     """ a non existant adapter raises an exception when the
     prepare_checkout decorator processes it"""
     ORIGINAL_ADAPTER = getattr(settings, 'CCCHECKOUT_ADAPTER', None)
     # add a non existant adapter
     settings.CCCHECKOUT_ADAPTER = 'certainly.borking'
     # set up the checkout
     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()
     # request the customer page and we have an exception
     request = self.rf.get(reverse('cccheckout:customer'))
     request.session['cccheckout'] = self.checkout
     # this will raise an exception
     self.assertRaises(Exception, customer, request)
 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 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)
Пример #5
0
 def setUp(self):
     # override some of the customer forms 
     settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
     settings.CCCHECKOUT_PAYMENT_FORMS = (
             'cccheckout.payments.stripe.forms.StripeForm',)
     # 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()
     # now add a customer 
     customer = Customer()
     customer.email = '*****@*****.**'
     customer.save()
     c.customer = customer
     c.save()
     self.checkout = c
     # make request
     self.rf = MockRequest()
Пример #6
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'])
Пример #7
0
 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
Пример #8
0
 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()
Пример #9
0
 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()
Пример #10
0
 def setUp(self):
     # override some of the customer forms 
     settings.CCCHECKOUT_POSTAGE_MODELS = ()
     settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
     settings.CCCHECKOUT_PAYMENT_FORMS = ()
     # 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()
Пример #11
0
class RedirectNotEmptyBasketTests(TestCase):
    """Some of these tests will become redundant as the checkout9/**"""

    def setUp(self):
        # set up
        session_key = '11111111111111111111111111111111'
        settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
        settings.CCCHECKOUT_PAYMENT_FORMS = ()
        reload(c_settings)

        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_customer_200(self):
        """If a basket is not empty then the checkout reponds with 200"""
        request = self.rf.get(reverse('cccheckout:customer'))
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = customer(request)
        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_postage_200(self):
        """If a basket is not empty then the checkout reponds with 200"""
        request = self.rf.get(reverse('cccheckout:postage'))
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = postage(request)
        #
        request = self.rf.get(reverse('cccheckout:postage'))
        # add a rudimentray customer
        self.checkout.customer = Customer()
        self.checkout.customer.save()
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = postage(request)
        self.assertEqual(200, response.status_code)

    def test_payment_200(self):
        """If a basket is not empty then the checkout reponds with 200"""
        request = self.rf.get(reverse('cccheckout:payment'))
        request.session['cccheckout'] = self.checkout
        # add a customer
        self.checkout.customer = Customer()
        self.checkout.customer.save()
        # add postage
        postage = Line(name='test')
        postage.save()
        tier = LineTier(minimum=1,
                maximum=2,
                price=Decimal('2.00'),
                line=postage)
        tier.save()
        country = LineCountry(country='GB', line=postage)
        country.save()
        self.checkout.postage = postage
        self.checkout.postage_tier = tier
        request.session.save()
        response = payment(request)
        self.assertEqual(200, response.status_code)
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)
Пример #13
0
class NoCustomerOrPostageTestCases(TestCase):

    def setUp(self):
        # 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 = ()
        # 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):
        # 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

    def test_customer_redirects_to_postage(self):
        """Any attempt to acccess the customer view redirects to the 
        postage view"""
        request = self.rf.get(reverse('cccheckout:customer'))
        request.session['cccheckout'] = self.checkout
        r = customer(request)
        self.assertEqual(302, r.status_code)
        self.assertEqual(r['Location'], reverse('cccheckout:postage'))


    def test_postage_redirects_to_payment(self):
        """Any attempt to acccess the postage view redirect to the 
        payment view"""
        checkout = deepcopy(self.checkout)
        request = self.rf.get(reverse('cccheckout:postage'))
        request.session['cccheckout'] = checkout
        r = postage(request)
        self.assertEqual(302, r.status_code)
        self.assertEqual(r['Location'], reverse('cccheckout:payment'))
Пример #14
0
class NoPostageTestCases(TestCase):

    def setUp(self):
        # override some of the customer forms 
        settings.CCCHECKOUT_POSTAGE_MODELS = ()
        settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
        settings.CCCHECKOUT_PAYMENT_FORMS = ()
        # 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):
        # 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

    def test_customer_is_200(self):
        """customer responds with a 200 ok"""
        request = self.rf.get(reverse('cccheckout:customer'))
        request.session['cccheckout'] = self.checkout
        r = customer(request)
        self.assertEqual(200, r.status_code)

    def test_customer_redirects_to_payment(self):
        """Because there is no postage required, any attempt to access
        postage redirects to the payment page"""
        # no customer the postage redirects and so does the payment
        request = self.rf.get(reverse('cccheckout:postage'))
        request.session['cccheckout'] = self.checkout
        r = postage(request)
        self.assertEqual(302, r.status_code)
        # payment redirects
        request = self.rf.get(reverse('cccheckout:payment'))
        request.session['cccheckout'] = self.checkout
        r = payment(request)
        self.assertEqual(302, r.status_code)
        # add a customer and it is now only postage redirects
        self.checkout.customer = Customer()
        self.checkout.customer.save()
        self.checkout.save()
        # get the views again and postage is still 302
        request = self.rf.get(reverse('cccheckout:postage'))
        request.session['cccheckout'] = self.checkout
        r = postage(request)
        self.assertEqual(302, r.status_code)
        # payment redirects
        request = self.rf.get(reverse('cccheckout:payment'))
        request.session['cccheckout'] = self.checkout
        r = payment(request)
        self.assertEqual(200, r.status_code)
Пример #15
0
class CustomerAndPostageTestCases(TestCase):

    def setUp(self):
        # override some of the customer forms 
        settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
        settings.CCCHECKOUT_PAYMENT_FORMS = (
                'cccheckout.payments.stripe.forms.StripeForm',)
        # 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()
        # now add a customer 
        customer = Customer()
        customer.email = '*****@*****.**'
        customer.save()
        c.customer = customer
        c.save()
        self.checkout = c
        # make request
        self.rf = MockRequest()

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


    def test_if_postage_required_and_has_none_then_payment_redirects(self):
        """if a checkout has no postage and postage is required then payment
        redirects to the postage page"""
        # no customer the postage redirects and so does the payment
        request = self.rf.get(reverse('cccheckout:payment'))
        request.session['cccheckout'] = self.checkout
        r = payment(request)
        self.assertEqual(302, r.status_code)
        self.assertEqual(r['Location'], reverse('cccheckout:postage'))


    def test_payment_responds_with_postage(self):
        """If there is postage then payment responds 200"""
        p = Line()
        p.name = 'test'
        p.save()
        p_tier = LineTier()
        p_tier.line = p
        p_tier.minimum = Decimal('0.00')
        p_tier.maximum = Decimal('0.00')
        p_tier.price = Decimal('2.00')
        p_tier.save()
        p_country = LineCountry()
        p_country.line = p
        p_country.country = 'GB'
        p_country.save()
        checkout = deepcopy(self.checkout)
        checkout.postage = p
        checkout.postage_tier = p_tier
        checkout.save()
        # process and all is fine
        request = self.rf.get(reverse('cccheckout:payment'))
        request.session['cccheckout'] = checkout
        r = payment(request)
        self.assertEqual(200, r.status_code)
class SuccessfulPaymentTestCases(LiveServerTestCase):

    def setUp(self):
        # 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.PAYPAL_MODE = 'DEV'
        # 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):
        # 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_setcheckout_with_discount(self):
        """test the set checkout with discounts"""
        # add a discount
        d = DiscountCode()
        d.type = DiscountCode.PERCENTAGE
        d.discount = Decimal('50.00')
        d.code = 'TEST'
        d.save()
        self.checkout.discount = d
        self.checkout.save()
        # get the api
        api = PaypalExpressCheckout(checkout=self.checkout)
        # make the setExpressCheckout call
        api.setExpressCheckout()
        # we now have a token
        self.assertTrue(api.token)
        # remove the discount
        self.checkout.discount = None
        self.checkout.save()

    def test_paymentrequestresponse_saving(self):
        """ensure that every request generates a RequestResponse"""
        # we have none
        self.assertEqual(0, RequestResponse.objects.count())
        # get the api
        api = PaypalExpressCheckout(checkout=self.checkout)
        # make the setExpressCheckout call
        api.setExpressCheckout()
        # we have one
        self.assertEqual(1, RequestResponse.objects.count())
        # make the expresscheckout payment
        api.doExpressCheckout()
        # we have two
        self.assertEqual(2, RequestResponse.objects.count())

    def test_doexpresscheckout_raises_without_params(self):
        """if token and payerid are not present when calling doexpresscheckout
        then an exception is raised"""
        api = PaypalExpressCheckout(checkout=self.checkout)
        self.assertRaises(Exception, api.doExpressCheckout)

    def test_doexpresscheckout_token_borked(self):
        """ensure setExpressCheckout works as expected when everything goes
        according to plan"""
        # save the setting for later
        ORIGINAL_OUTCOME = settings.PAYPAL_OUTCOME
        settings.PAYPAL_OUTCOME = 'BORK'
        reload(c_settings)
        # make the API again, this time with details
        api = PaypalExpressCheckout(
                checkout=self.checkout,
                token='asasdsadas',
                payerid='asdasdsad')
        # make the setExpressCheckout call and the return is False 
        self.assertFalse(api.doExpressCheckout())
        # set it back
        settings.PAYPAL_OUTCOME = ORIGINAL_OUTCOME
        reload(c_settings)


    def test_do_expresscheckout_redirects_if_no_token_or_payerid(self):
        """if GET['token'] or GET['PayerID'] are missing as querystring
        parameters then do_express_checkout_redirects to payment"""

        # send the view nothing and it redirects

        request = self.rf.get( reverse('paypalexpresscheckout:return'),{
            'METHOD': 'DoExpressCheckoutPayment'},)
        request.session['cccheckout'] = self.checkout
        r = do_express_checkout(request)
        self.assertEqual(reverse('cccheckout:payment'), r['Location'])
        # doesn't matter if we send one
        request = self.rf.get( reverse('paypalexpresscheckout:return'),{
            'METHOD': 'DoExpressCheckoutPayment',
            'PayerID': 'testpayerid'})
        request.session['cccheckout'] = self.checkout
        r = do_express_checkout(request)
        self.assertEqual(reverse('cccheckout:payment'), r['Location'])
        # or the other
        request = self.rf.get( reverse('paypalexpresscheckout:return'),{
            'METHOD': 'DoExpressCheckoutPayment',
            'token': 'testtoken'})
        request.session['cccheckout'] = self.checkout
        r = do_express_checkout(request)
        self.assertEqual(reverse('cccheckout:payment'), r['Location'])
        # we need both...
        request = self.rf.get( reverse('paypalexpresscheckout:return'),{
            'METHOD': 'DoExpressCheckoutPayment',
            'token': 'testtoken',
            'PayerID': 'testpayerid'})
        request.session['cccheckout'] = self.checkout
        r = do_express_checkout(request)
        self.assertEqual(reverse('cccheckout:complete'), r['Location'])

    def test_doexpresscheckout_fail(self):
        """ensure setExpressCheckout fails as expected when everything goes
        according to plan and a payment is not succesful"""
        # save the setting for later
        ORIGINAL_OUTCOME = settings.PAYPAL_OUTCOME
        settings.PAYPAL_OUTCOME = 'FAIL'
        reload(c_settings)
        # make the request for the view
        request = self.rf.get( reverse('paypalexpresscheckout:return'),{
            'METHOD': 'DoExpressCheckoutPayment',
            'token': 'testtoken',
            'PayerID': 'testpayerid'})
        request.session['cccheckout'] = self.checkout
        r = do_express_checkout(request)
        # redirected back to payment
        self.assertEqual(reverse('cccheckout:payment'), r['Location'])
        # checkout is not paid
        self.assertFalse(self.checkout.paid)
        # set it back
        settings.PAYPAL_OUTCOME = ORIGINAL_OUTCOME
        reload(c_settings)

    def test_doexpresscheckout_token_working(self):
        """ensure setExpressCheckout works as expected when everything goes
        according to plan"""
        settings.PAYPAL_OUTCOME = 'PASS'
        reload(c_settings)
        # get the api
        api = PaypalExpressCheckout(
                checkout=self.checkout,
                token='asasdsadas',
                payerid='asdasdsad')
        # make the setExpressCheckout call and the return is True
        self.assertTrue(api.doExpressCheckout())


    def test_setexpresscheckout_token_working(self):
        """ensure setExpressCheckout works as expected when everything goes
        according to plan"""
        settings.PAYPAL_OUTCOME = 'PASS'
        reload(c_settings)
        # get the api
        api = PaypalExpressCheckout(checkout=self.checkout)
        # make the setExpressCheckout call
        api.setExpressCheckout()
        # we now have a token
        self.assertTrue(api.token)

    def test_setexpresscheckout_token_borked(self):
        """ensure setExpressCheckout works as expected when everything goes
        according to plan"""
        # set bork mode & reload settings
        ORIGINAL_OUTCOME = settings.PAYPAL_OUTCOME
        settings.PAYPAL_OUTCOME = 'BORK'
        reload(c_settings)
        # get the api
        api = PaypalExpressCheckout(checkout=self.checkout)
        # make the setExpressCheckout call
        api.setExpressCheckout()
        # we now have no token
        self.assertFalse(api.token)
        # set it back
        settings.PAYPAL_OUTCOME = ORIGINAL_OUTCOME
        reload(c_settings)

    def test_checkout_paid_custom_complete(self):
        """Ensure that the checkout is marked as paid"""
        # specify a custome location and reload settings
        settings.CCCHECKOUT_SUCCESS_URL = '/'
        reload(c_settings)
        # make a request and call the do_express_checkout
        request = self.rf.get( reverse('paypalexpresscheckout:return'),{
            'METHOD': 'DoExpressCheckoutPayment',
            'token': 'testtoken',
            'PayerID': 'testpayerid'})
        request.session['cccheckout'] = self.checkout
        r = do_express_checkout(request)
        # we were redirected to complete
        self.assertEqual(r['Location'], '/')
        # checkout is complete
        del(settings.CCCHECKOUT_SUCCESS_URL)

    def test_checkout_paid(self):
        """Ensure that the checkout is marked as paid"""
        # checkout is not paid
        self.assertFalse(self.checkout.paid)
        # make a request and call the do_express_checkout
        request = self.rf.get(reverse('paypalexpresscheckout:return'), {
            'METHOD': 'DoExpressCheckoutPayment',
            'token': 'testtoken',
            'PayerID': 'testpayerid'})
        request.session['cccheckout'] = self.checkout
        r = do_express_checkout(request)
        # we were redirected to complete
        self.assertEqual(r['Location'], reverse('cccheckout:complete'))
        # checkout is complete
        self.assertTrue(self.checkout.paid)

    def test_checkout_paid_signal_sent(self):
        """Ensure that the checkout is marked as paid"""
        # define a listener
        def test_listener(sender, request, **kwargs):
            request.session['mynameis'] = 'slimshady'
        # connect it
        checkout_paid.connect(test_listener)
        # checkout is not paid
        self.assertFalse(self.checkout.paid)
        # make a request and call the do_express_checkout
        request = self.rf.get( reverse('paypalexpresscheckout:return'),{
            'METHOD': 'DoExpressCheckoutPayment',
            'token': 'testtoken',
            'PayerID': 'testpayerid'})
        request.session['cccheckout'] = self.checkout
        request.session['mynameis'] = 'eminem'
        # his name is eminem
        self.assertEqual('eminem', request.session['mynameis'])
        r = do_express_checkout(request)
        # his name is eminem
        self.assertEqual('slimshady', request.session['mynameis'])
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())
Пример #18
0
 def setUp(self):
     # make request & add checkout
     self.rf = MockRequest()
     # reload the c_settings to make sure we're using the defaults
     reload(c_settings)
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)
Пример #20
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)
Пример #21
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
Пример #22
0
class AllowGuestTestCases(TestCase):
    """Ensure the CCCHECKOUT_ALLOW_GUEST config flag works as expected"""
    
    def setUp(self):
        # set up
        session_key = '11111111111111111111111111111111'
        settings.CCCHECKOUT_ADAPTER = 'cccheckout.tests.mock.testcase_adapter'
        engine = import_module(django_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.lines = 3
        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):
        # reset the setting
        settings.CCCHECKOUT_ALLOW_GUEST = True
        settings.CCCHECKOUT_ADAPTER = 'cccheckout.adapters.ccbasket_adapter'

    def test_is_true_start(self):
        """  
        if CCCHECKOUT_ALLOW_GUEST == `True`: 
            checkout flow proceeds without a redirect to settings.LOGIN_URL
        """
        request = self.rf.get(reverse('cccheckout:customer'))
        request.session['cccheckout'] = self.checkout
        request.session.save()
        # start
        response = customer(request)
        self.assertEqual(200, response.status_code)

    def test_is_false(self):
        """  
        if CCCHECKOUT_ALLOW_GUEST == `True`: 
            checkout flow proceeds without a redirect to settings.LOGIN_URL
        """
        # set to False
        settings.CCCHECKOUT_ALLOW_GUEST = False
        # customer
        request = self.rf.get(reverse('cccheckout:customer'))
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = customer(request)
        self.assertEqual(302, response.status_code)
        # postage
        request = self.rf.get(reverse('cccheckout:postage'))
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = postage(request)
        self.assertEqual(302, response.status_code)
        # payment
        request = self.rf.get(reverse('cccheckout:payment'))
        request.session['cccheckout'] = self.checkout
        request.session.save()
        response = payment(request)
        self.assertEqual(302, response.status_code)