Exemple #1
0
    def test_developer_offering_purchase_exception(self):
        self._user.userprofile.get_current_roles = MagicMock()
        self._user.userprofile.get_current_roles.return_value = ['customer']

        # Create the request
        data = {
            'offering': {
                'organization': 'test_organization',
                'name': 'test_offering',
                'version': '1.1'
            },
            'plan_label': 'developer',
            '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)
Exemple #2
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)
Exemple #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)
Exemple #4
0
    def test_developer_offering_purchase(self):
        # Create the request
        data = {
            'offering': {
                'organization': 'test_organization',
                'name': 'test_offering',
                'version': '1.1'
            },
            'plan_label': 'developer',
            '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/22222222.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/22222222.pdf')
        payment_info = {
            'tax_address': {
                'street': 'test street',
                'postal': '28000',
                'city': 'test city',
                'country': 'test country'
            },
            'payment_method': 'paypal',
            'plan': 'developer',
            'accepted': False
        }
        views.create_purchase.assert_called_once_with(
            self._user, offering, org_owned=False, payment_info=payment_info)
Exemple #5
0
     tagging_views.TagCollection(permitted_methods=('GET', 'PUT'))),
 url(
     r'^api/offering/offerings/(?P<organization>[\w -]+)/(?P<name>[\w -]+)/(?P<version>[\d.]+)/usdl/?$',
     usdl_proxy.USDLCollection(permitted_methods=('GET', ))),
 url(
     r'^api/offering/resources/(?P<provider>[\w -]+)/(?P<name>[\w -]+)/(?P<version>[\d.]+)/?$',
     offering_views.ResourceEntry(permitted_methods=('DELETE', 'POST',
                                                     'PUT'))),
 url(r'^api/offering/resources/?$',
     offering_views.ResourceCollection(permitted_methods=('GET', 'POST'))),
 url(r'^api/offering/resources/plugins?$',
     plugins_views.PluginCollection(permitted_methods=('GET', ))),
 url(r'^api/offering/applications/?$',
     offering_views.ApplicationCollection(permitted_methods=('GET', ))),
 url(r'^api/contracting/?$',
     contracting_views.PurchaseCollection(permitted_methods=('POST', ))),
 url(
     r'^api/contracting/form/?$',
     contracting_views.PurchaseFormCollection(permitted_methods=('POST',
                                                                 'GET'))),
 url(r'^api/contracting/(?P<reference>[\w]+)/?$',
     contracting_views.PurchaseEntry(permitted_methods=('GET', 'PUT'))),
 url(r'^api/contracting/(?P<reference>[\w]+)/accept/?$',
     charging_views.PayPalConfirmation(permitted_methods=('GET', ))),
 url(r'^api/contracting/(?P<reference>[\w]+)/cancel/?$',
     charging_views.PayPalCancelation(permitted_methods=('GET', ))),
 url(
     r'^api/contracting/(?P<reference>[\w]+)/accounting/?$',
     charging_views.ServiceRecordCollection(permitted_methods=('POST',
                                                               'GET'))),
 url(r'^api/search/keyword/(?P<text>[\w -]+)/?$',