예제 #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)
class NonExistantAdapter(TestCase):

    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)
예제 #3
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'))
예제 #4
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)
예제 #5
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)
예제 #6
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
예제 #7
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)
예제 #8
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 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'])
예제 #10
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)