예제 #1
0
파일: tests.py 프로젝트: huygun/wstore
    def test_purchase_offering_update_exception(self):

        # Test view exceptions
        # Create the request, The user has not purchased a previous version
        # of the offering, so she is not allowed to purchase the offering
        data = {
            'offering': {
                'organization': 'test_organization',
                'name': 'test_offering',
                'version': '1.1'
            },
            'plan_label': 'update',
            'tax_address': {
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            'payment': {
                'method': 'paypal'
            }
        }
        request = self.factory.post(
            '/api/contracting/',
            json.dumps(data),
            HTTP_ACCEPT='application/json; charset=utf-8',
            content_type='application/json; charset=utf-8'
        )
        request.user = self._user
        purchase_collection = views.PurchaseCollection(permitted_methods=('POST',))

        response = purchase_collection.create(request)

        # Check response
        body_response = json.loads(response.content)
        self.assertEquals(body_response['result'], 'error')
        self.assertEquals(body_response['message'], 'Forbidden')
        self.assertEquals(response.status_code, 403)

        # Test Create contract exceptions
        # Load usdl info
        f = open('wstore/test/test_usdl2.ttl', 'rb')
        g = rdflib.Graph()
        g.parse(data=f.read(), format='n3')
        f.close()

        offering = Offering.objects.get(pk="71000aba8e05ac2115f022ff")
        offering.offering_description = json.loads(g.serialize(format='json-ld', auto_compact=True))
        offering.save()

        from datetime import datetime
        purchase = Purchase.objects.create(
            customer=self._user,
            date=datetime.now(),
            offering=offering,
            organization_owned=False,
            state='paid',
            tax_address={
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            bill=['/media/bills/11111111111.pdf']
        )
        from wstore.charging_engine.charging_engine import ChargingEngine

        # Check exceptions that can occur with multiple price plans when
        # creating the related purchase contract
        errors = {
            'The price plan label is required to identify the plan': None,
            'The specified plan does not exist': 'unexisting'
        }
        for err in errors:

            error = False
            msg = None
            try:
                charging = ChargingEngine(purchase, payment_method='paypal', plan=errors[err])
                charging._create_purchase_contract()
            except Exception, e:
                error = True
                msg = e.message

                self.assertTrue(error)
                self.assertEquals(msg, err)
예제 #2
0
파일: tests.py 프로젝트: huygun/wstore
    def test_purchase_offering_update_payment(self):

        current_org = Organization.objects.get(pk="91000aba8e06ac2199999999")
        current_org.offerings_purchased.append('61000aba8e05ac2115f022f9')
        self._user.userprofile.current_organization = current_org

        self._user.userprofile.get_current_roles = MagicMock()
        self._user.userprofile.get_current_roles.return_value = ['customer']
        self._user.userprofile.save()

        # Create the request
        data = {
            'offering': {
                'organization': 'test_organization',
                'name': 'test_offering',
                'version': '1.1'
            },
            'plan_label': 'update',
            'tax_address': {
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            'payment': {
                'method': 'paypal'
            }
        }
        request = self.factory.post(
            '/api/contracting/',
            json.dumps(data),
            HTTP_ACCEPT='application/json; charset=utf-8',
            content_type='application/json; charset=utf-8'
        )
        request.user = self._user
        
        # Test purchase view
        views.create_purchase = MagicMock(name='create_purchase')
        offering = Offering.objects.get(pk="71000aba8e05ac2115f022ff")

        from datetime import datetime
        purchase = Purchase.objects.create(
            customer=self._user,
            date=datetime.now(),
            offering=offering,
            organization_owned=True,
            state='paid',
            tax_address={
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            bill=['/media/bills/11111111111.pdf']
        )
        views.create_purchase.return_value = purchase

        views.get_current_site = MagicMock(name='get_current_site')
        views.get_current_site.return_value = Site.objects.get(name='antares')
        views.Context.objects.get = MagicMock(name='get')
        context = MagicMock()
        context.user_refs = []
        views.Context.objects.get.return_value = context

        purchase_collection = views.PurchaseCollection(permitted_methods=('POST',))

        response = purchase_collection.create(request)

        # Check response
        body_response = json.loads(response.content)
        self.assertEquals(len(body_response['bill']), 1)
        self.assertEquals(body_response['bill'][0], '/media/bills/11111111111.pdf')
        payment_info = {
            'tax_address': {
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            'payment_method': 'paypal',
            'plan': 'update'
        }
        views.create_purchase.assert_called_once_with(self._user, offering, org_owned=True, payment_info=payment_info)

        # Test Contract creation
        # Load usdl info
        f = open('wstore/test/test_usdl2.ttl', 'rb')
        g = rdflib.Graph()
        g.parse(data=f.read(), format='n3')
        f.close()

        offering.offering_description = json.loads(g.serialize(format='json-ld', auto_compact=True))
        offering.save()

        from wstore.charging_engine.charging_engine import ChargingEngine
        charging = ChargingEngine(purchase, payment_method='paypal', plan='update')
        charging._create_purchase_contract()

        # Refresh purchase
        purchase = Purchase.objects.get(pk=purchase.pk)
        contract = purchase.contract

        # Check contract pricing model
        self.assertTrue('single_payment' in contract.pricing_model)
        self.assertEquals(len(contract.pricing_model['single_payment']), 1)
        payment = contract.pricing_model['single_payment'][0]
        self.assertEquals(payment['title'], 'Price component update')
        self.assertEquals(payment['value'], '1.0')

        self.assertFalse('subscription' in contract.pricing_model)
        self.assertFalse('pay_per_use' in contract.pricing_model)
예제 #3
0
    def test_purchase_offering_update_exception(self):

        # Test view exceptions
        # Create the request, The user has not purchased a previous version
        # of the offering, so she is not allowed to purchase the offering
        data = {
            'offering': {
                'organization': 'test_organization',
                'name': 'test_offering',
                'version': '1.1'
            },
            'plan_label': 'update',
            'tax_address': {
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            'payment': {
                'method': 'paypal'
            }
        }
        request = self.factory.post(
            '/api/contracting/',
            json.dumps(data),
            HTTP_ACCEPT='application/json; charset=utf-8',
            content_type='application/json; charset=utf-8')
        request.user = self._user
        purchase_collection = views.PurchaseCollection(
            permitted_methods=('POST', ))

        response = purchase_collection.create(request)

        # Check response
        body_response = json.loads(response.content)
        self.assertEquals(body_response['result'], 'error')
        self.assertEquals(body_response['message'], 'Forbidden')
        self.assertEquals(response.status_code, 403)

        # Test Create contract exceptions

        offering = Offering.objects.get(pk="71000aba8e05ac2115f022ff")
        offering.offering_description = {
            'pricing': {
                'price_plans': [{
                    'title': 'Plan 1',
                    'label': 'update',
                    'price_components': []
                }, {
                    'title': 'Plan 1',
                    'label': 'regular',
                    'price_components': []
                }]
            }
        }

        offering.save()

        from datetime import datetime
        purchase = Purchase.objects.create(
            customer=self._user,
            date=datetime.now(),
            offering=offering,
            organization_owned=False,
            state='paid',
            tax_address={
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            bill=['/media/bills/11111111111.pdf'])
        from wstore.charging_engine.charging_engine import ChargingEngine

        # Check exceptions that can occur with multiple price plans when
        # creating the related purchase contract
        errors = {
            'The price plan label is required to identify the plan': None,
            'The specified plan does not exist': 'unexisting'
        }
        for err in errors:

            error = False
            msg = None
            try:
                charging = ChargingEngine(purchase,
                                          payment_method='paypal',
                                          plan=errors[err])
                charging._create_purchase_contract()
            except Exception, e:
                error = True
                msg = e.message

                self.assertTrue(error)
                self.assertEquals(msg, err)
예제 #4
0
    def test_purchase_offering_update_payment(self):

        current_org = Organization.objects.get(pk="91000aba8e06ac2199999999")
        current_org.offerings_purchased.append('61000aba8e05ac2115f022f9')
        self._user.userprofile.current_organization = current_org

        self._user.userprofile.get_current_roles = MagicMock()
        self._user.userprofile.get_current_roles.return_value = ['customer']
        self._user.userprofile.save()

        # Create the request
        data = {
            'offering': {
                'organization': 'test_organization',
                'name': 'test_offering',
                'version': '1.1'
            },
            'plan_label': 'update',
            'tax_address': {
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            'payment': {
                'method': 'paypal'
            }
        }
        request = self.factory.post(
            '/api/contracting/',
            json.dumps(data),
            HTTP_ACCEPT='application/json; charset=utf-8',
            content_type='application/json; charset=utf-8')
        request.user = self._user

        # Test purchase view
        views.create_purchase = MagicMock(name='create_purchase')
        offering = Offering.objects.get(pk="71000aba8e05ac2115f022ff")

        from datetime import datetime
        purchase = Purchase.objects.create(
            customer=self._user,
            date=datetime.now(),
            offering=offering,
            organization_owned=True,
            state='paid',
            tax_address={
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            bill=['/media/bills/11111111111.pdf'])
        views.create_purchase.return_value = purchase

        views.get_current_site = MagicMock(name='get_current_site')
        views.get_current_site.return_value = Site.objects.get(name='antares')
        views.Context.objects.get = MagicMock(name='get')
        context = MagicMock()
        context.user_refs = []
        views.Context.objects.get.return_value = context

        purchase_collection = views.PurchaseCollection(
            permitted_methods=('POST', ))

        response = purchase_collection.create(request)

        # Check response
        body_response = json.loads(response.content)
        self.assertEquals(len(body_response['bill']), 1)
        self.assertEquals(body_response['bill'][0],
                          '/media/bills/11111111111.pdf')
        payment_info = {
            'tax_address': {
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            'payment_method': 'paypal',
            'plan': 'update',
            'accepted': False
        }
        views.create_purchase.assert_called_once_with(
            self._user, offering, org_owned=True, payment_info=payment_info)

        # Test Contract creation
        offering.offering_description = {
            'pricing': {
                'price_plans': [{
                    'title':
                    'Price plan',
                    'currency':
                    'EUR',
                    'price_components': [{
                        'label': 'Price component update',
                        'unit': 'single payment',
                        'value': '1.0'
                    }]
                }]
            }
        }
        offering.save()

        from wstore.charging_engine.charging_engine import ChargingEngine
        charging = ChargingEngine(purchase,
                                  payment_method='paypal',
                                  plan='update')
        charging._create_purchase_contract()

        # Refresh purchase
        purchase = Purchase.objects.get(pk=purchase.pk)
        contract = purchase.contract

        # Check contract pricing model
        self.assertTrue('single_payment' in contract.pricing_model)
        self.assertEquals(len(contract.pricing_model['single_payment']), 1)
        payment = contract.pricing_model['single_payment'][0]
        self.assertEquals(payment['label'], 'Price component update')
        self.assertEquals(payment['value'], '1.0')

        self.assertFalse('subscription' in contract.pricing_model)
        self.assertFalse('pay_per_use' in contract.pricing_model)
예제 #5
0
파일: tests.py 프로젝트: Fiware/apps.Wstore
    def test_basic_purchase_offering_update(self):
        # Get context
        cnt = Context.objects.all()[0]
        cnt.allowed_currencies = {
            "default": "EUR",
            "allowed": [{
                "currency": "EUR",
                "in_use": True
            }]
        }
        cnt.save()
        self._user.userprofile.offerings_purchased.append('61000aba8e05ac2115f022f9')
        # Create the request
        data = {
            'offering': {
                'organization': 'test_organization',
                'name': 'test_offering',
                'version': '1.1'
            },
            'plan_label': 'update',
            'tax_address': {
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            'payment': {
                'method': 'paypal'
            }
        }
        request = self.factory.post(
            '/api/contracting/',
            json.dumps(data),
            HTTP_ACCEPT='application/json; charset=utf-8',
            content_type='application/json; charset=utf-8'
        )
        request.user = self._user

        # Test purchase view
        views.create_purchase = MagicMock(name='create_purchase')
        offering = Offering.objects.get(pk="71000aba8e05ac2115f022ff")

        from datetime import datetime
        purchase = Purchase.objects.create(
            customer=self._user,
            date=datetime.now(),
            offering=offering,
            organization_owned=False,
            state='paid',
            tax_address={
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            bill=['/media/bills/11111111111.pdf']
        )
        views.create_purchase.return_value = purchase

        views.get_current_site = MagicMock(name='get_current_site')
        views.get_current_site.return_value = Site.objects.get(name='antares')
        views.Context.objects.get = MagicMock(name='get')
        context = MagicMock()
        context.user_refs = []
        views.Context.objects.get.return_value = context

        purchase_collection = views.PurchaseCollection(permitted_methods=('POST',))

        response = purchase_collection.create(request)

        # Check response
        body_response = json.loads(response.content)
        self.assertEquals(len(body_response['bill']), 1)
        self.assertEquals(body_response['bill'][0], '/media/bills/11111111111.pdf')
        payment_info = {
            'tax_address': {
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            'payment_method': 'paypal',
            'plan': 'update',
            'accepted': False
        }
        views.create_purchase.assert_called_once_with(self._user, offering, org_owned=False, payment_info=payment_info)

        offering.offering_description = {
            'pricing': {
                'price_plans': []
            }
        }

        offering.save()

        from wstore.charging_engine.charging_engine import ChargingEngine
        charging = ChargingEngine(purchase, payment_method='paypal', plan='update')
        charging._create_purchase_contract()

        # Refresh purchase
        purchase = Purchase.objects.get(pk=purchase.pk)
        contract = purchase.contract

        self.assertFalse('single_payment' in contract.pricing_model)
        self.assertFalse('subscription' in contract.pricing_model)
        self.assertFalse('pay_per_use' in contract.pricing_model)