def test_purchase_non_published_off(self): user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering2') payment_info = { 'payment_method': 'credit_card', 'credit_card': { 'number': '1234123412341234', 'type': 'Visa', 'expire_year': '2018', 'expire_month': '3', 'cvv2': '111' }, 'tax_address': { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } } error = False msg = None try: purchases_management.create_purchase(user, offering, payment_info=payment_info) except Exception, e: error = True msg = e.message
def test_purchase_already_purchased(self): user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering3') user_profile = UserProfile.objects.get(user=user) user_profile.offerings_purchased = ['81000aba8e05ac2115f022f0'] org = Organization.objects.get(name='test_organization') user_profile.organization = org user_profile.save() payment_info = { 'payment_method': 'credit_card', 'credit_card': { 'number': '1234123412341234', 'type': 'Visa', 'expire_year': '2018', 'expire_month': '3', 'cvv2': '111' }, 'tax_address': { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } } error = False msg = None try: purchases_management.create_purchase(user, offering, payment_info=payment_info) except Exception, e: error = True msg = e.message
def test_purchase_creation_organization_payment(self): user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering') user_profile = UserProfile.objects.get(user=user) org = Organization.objects.get(name='test_organization') org.payment_info = { 'number': '1234123412341234', 'type': 'Visa', 'expire_year': '2018', 'expire_month': '3', 'cvv2': '111' } org.save() user_profile.current_organization = org user_profile.organizations = [{ 'organization': org.pk, 'roles': ['customer'] }] user_profile.save() payment_info = { 'payment_method': 'credit_card', 'tax_address': { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } } purchases_management.ChargingEngine.resolve_charging.return_value = 'http://paypal.com/redirect' redirect_url = purchases_management.create_purchase(user, offering, True, payment_info=payment_info) purchases_management.ChargingEngine.resolve_charging.assert_called_once_with(new_purchase=True) self.assertEquals(redirect_url, 'http://paypal.com/redirect')
def test_purchase_creation_paypal(self): user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering') payment_info = { 'payment_method': 'paypal', 'tax_address': { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } } purchase = purchases_management.create_purchase(user, offering, payment_info=payment_info) self.assertEqual(purchase.customer.username, 'test_user') self.assertEqual(purchase.organization_owned, False) self.assertEqual(purchase.offering_id, offering.pk) self.assertEqual(purchase.tax_address['street'], 'test street') self.assertEqual(purchase.tax_address['postal'], '28000') self.assertEqual(purchase.tax_address['city'], 'test city') self.assertEqual(purchase.tax_address['country'], 'test country') purchase = Purchase.objects.get(customer=user, offering=offering) self.assertEqual(purchase.customer.username, 'test_user') self.assertEqual(purchase.organization_owned, False) self.assertEqual(purchase.offering_id, offering.pk) self.assertEqual(purchase.tax_address['street'], 'test street') self.assertEqual(purchase.tax_address['postal'], '28000') self.assertEqual(purchase.tax_address['city'], 'test city') self.assertEqual(purchase.tax_address['country'], 'test country') user_profile = UserProfile.objects.get(user=user) self.assertEqual(len(user_profile.offerings_purchased), 1) self.assertEqual(user_profile.offerings_purchased[0], offering.pk)
def test_purchase_creation(self, name, side_effect, org_owned=False, payment_info=None, err_type=None, err_msg=None): if side_effect: side_effect(self) user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering') error = None try: purchase = purchases_management.create_purchase(user, offering, org_owned, payment_info) except Exception as e: error = e if not err_type: # Check that no error occurs self.assertEquals(error, None) # Check purchase information self.assertEqual(purchase.customer.username, 'test_user') self.assertEqual(purchase.owner_organization.name, 'test_user') self.assertEqual(purchase.organization_owned, False) self.assertEqual(purchase.offering_id, offering.pk) self.assertEqual(purchase.tax_address['street'], 'test street') self.assertEqual(purchase.tax_address['postal'], '28000') self.assertEqual(purchase.tax_address['city'], 'test city') self.assertEqual(purchase.tax_address['country'], 'test country') user_profile = UserProfile.objects.get(user=user) self.assertEqual(len(user_profile.offerings_purchased), 1) self.assertEqual(user_profile.offerings_purchased[0], offering.pk) else: self.assertTrue(isinstance(error, err_type)) self.assertEquals(unicode(e), err_msg)
def test_basic_purchase_creation(self): user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering') tax_address = { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } user_profile = UserProfile.objects.get(user=user) user_profile.tax_address = tax_address org = Organization.objects.get(name='test_organization') user_profile.organization = org user_profile.save() payment_info = { 'payment_method': 'credit_card', 'credit_card': { 'number': '1234123412341234', 'type': 'Visa', 'expire_year': '2018', 'expire_month': '3', 'cvv2': '111' } } purchase = purchases_management.create_purchase( user, offering, payment_info=payment_info) self.assertEqual(purchase.customer.username, 'test_user') self.assertEqual(purchase.organization_owned, False) self.assertEqual(purchase.offering_id, offering.pk) self.assertEqual(purchase.tax_address['street'], 'test street') self.assertEqual(purchase.tax_address['postal'], '28000') self.assertEqual(purchase.tax_address['city'], 'test city') self.assertEqual(purchase.tax_address['country'], 'test country') purchase = Purchase.objects.get(customer=user, offering=offering) self.assertEqual(purchase.customer.username, 'test_user') self.assertEqual(purchase.organization_owned, False) self.assertEqual(purchase.offering_id, offering.pk) self.assertEqual(purchase.tax_address['street'], 'test street') self.assertEqual(purchase.tax_address['postal'], '28000') self.assertEqual(purchase.tax_address['city'], 'test city') self.assertEqual(purchase.tax_address['country'], 'test country') user_profile = UserProfile.objects.get(user=user) self.assertEqual(len(user_profile.offerings_purchased), 1) self.assertEqual(user_profile.offerings_purchased[0], offering.pk)
def test_basic_purchase_creation(self): user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering') tax_address = { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } user_profile = UserProfile.objects.get(user=user) user_profile.tax_address = tax_address org = Organization.objects.get(name='test_organization') user_profile.organization = org user_profile.save() payment_info = { 'payment_method': 'credit_card', 'credit_card': { 'number': '1234123412341234', 'type': 'Visa', 'expire_year': '2018', 'expire_month': '3', 'cvv2': '111' } } purchase = purchases_management.create_purchase(user, offering, payment_info=payment_info) self.assertEqual(purchase.customer.username, 'test_user') self.assertEqual(purchase.organization_owned, False) self.assertEqual(purchase.offering_id, offering.pk) self.assertEqual(purchase.tax_address['street'], 'test street') self.assertEqual(purchase.tax_address['postal'], '28000') self.assertEqual(purchase.tax_address['city'], 'test city') self.assertEqual(purchase.tax_address['country'], 'test country') purchase = Purchase.objects.get(customer=user, offering=offering) self.assertEqual(purchase.customer.username, 'test_user') self.assertEqual(purchase.organization_owned, False) self.assertEqual(purchase.offering_id, offering.pk) self.assertEqual(purchase.tax_address['street'], 'test street') self.assertEqual(purchase.tax_address['postal'], '28000') self.assertEqual(purchase.tax_address['city'], 'test city') self.assertEqual(purchase.tax_address['country'], 'test country') user_profile = UserProfile.objects.get(user=user) self.assertEqual(len(user_profile.offerings_purchased), 1) self.assertEqual(user_profile.offerings_purchased[0], offering.pk)
def test_purchase_creation(self, name, side_effect, org_owned=False, payment_info=None, err_type=None, err_msg=None): if side_effect: side_effect(self) user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering') error = None try: purchase = purchases_management.create_purchase( user, offering, org_owned, payment_info) except Exception as e: error = e if not err_type: # Check that no error occurs self.assertEquals(error, None) # Check purchase information self.assertEqual(purchase.customer.username, 'test_user') self.assertEqual(purchase.owner_organization.name, 'test_user') self.assertEqual(purchase.organization_owned, False) self.assertEqual(purchase.offering_id, offering.pk) self.assertEqual(purchase.tax_address['street'], 'test street') self.assertEqual(purchase.tax_address['postal'], '28000') self.assertEqual(purchase.tax_address['city'], 'test city') self.assertEqual(purchase.tax_address['country'], 'test country') user_profile = UserProfile.objects.get(user=user) self.assertEqual(len(user_profile.offerings_purchased), 1) self.assertEqual(user_profile.offerings_purchased[0], offering.pk) else: self.assertTrue(isinstance(error, err_type)) self.assertEquals(unicode(e), err_msg)
def test_purchase_creation_organization_payment(self): user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering') user_profile = UserProfile.objects.get(user=user) org = Organization.objects.get(name='test_organization') org.payment_info = { 'number': '1234123412341234', 'type': 'Visa', 'expire_year': '2018', 'expire_month': '3', 'cvv2': '111' } org.save() user_profile.current_organization = org user_profile.organizations = [{ 'organization': org.pk, 'roles': ['customer'] }] user_profile.save() payment_info = { 'payment_method': 'credit_card', 'tax_address': { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } } purchases_management.ChargingEngine.resolve_charging.return_value = 'http://paypal.com/redirect' redirect_url = purchases_management.create_purchase( user, offering, True, payment_info=payment_info) purchases_management.ChargingEngine.resolve_charging.assert_called_once_with( new_purchase=True) self.assertEquals(redirect_url, 'http://paypal.com/redirect')
def test_purchase_creation_paypal(self): user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering') payment_info = { 'payment_method': 'paypal', 'tax_address': { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } } purchase = purchases_management.create_purchase( user, offering, payment_info=payment_info) self.assertEqual(purchase.customer.username, 'test_user') self.assertEqual(purchase.organization_owned, False) self.assertEqual(purchase.offering_id, offering.pk) self.assertEqual(purchase.tax_address['street'], 'test street') self.assertEqual(purchase.tax_address['postal'], '28000') self.assertEqual(purchase.tax_address['city'], 'test city') self.assertEqual(purchase.tax_address['country'], 'test country') purchase = Purchase.objects.get(customer=user, offering=offering) self.assertEqual(purchase.customer.username, 'test_user') self.assertEqual(purchase.organization_owned, False) self.assertEqual(purchase.offering_id, offering.pk) self.assertEqual(purchase.tax_address['street'], 'test street') self.assertEqual(purchase.tax_address['postal'], '28000') self.assertEqual(purchase.tax_address['city'], 'test city') self.assertEqual(purchase.tax_address['country'], 'test country') user_profile = UserProfile.objects.get(user=user) self.assertEqual(len(user_profile.offerings_purchased), 1) self.assertEqual(user_profile.offerings_purchased[0], offering.pk)
def create(self, request): user = request.user content_type = get_content_type(request)[0] try: data = json.loads(request.raw_post_data) payment_info = {} if isinstance(data['offering'], dict): id_ = data['offering'] org = Organization.objects.get(name=id_['organization']) offering = Offering.objects.get(owner_organization=org, name=id_['name'], version=id_['version']) else: offering = Offering.objects.get(description_url=data['offering']) if 'tax_address' in data: payment_info['tax_address'] = data['tax_address'] payment_info['payment_method'] = data['payment']['method'] if 'credit_card' in data['payment']: payment_info['credit_card'] = data['payment']['credit_card'] # Get terms and conditions flag payment_info['accepted'] = data.get('conditions_accepted', False) # Check the selected price plan update_plan = False developer_plan = False if 'plan_label' in data: payment_info['plan'] = data['plan_label'] # Classify the plan # Check if the plan is an update if data['plan_label'].lower() == 'update': update_plan = True # Check if the plan is a developer purchase elif data['plan_label'].lower() == 'developer': developer_plan = True # Check if the user is purchasing for an organization org_owned = True if user.userprofile.is_user_org(): org_owned = False # Check if the user has the customer role for the organization roles = user.userprofile.get_current_roles() if org_owned: if not 'customer' in roles and not developer_plan: return build_response(request, 403, 'Forbidden') # Load the purchased offerings if it is an update in # order to check versions later if update_plan: purchased_offerings = user.userprofile.current_organization.offerings_purchased elif update_plan: purchased_offerings = user.userprofile.offerings_purchased # Check if the plan is an update if update_plan: # Check if the user has purchased a previous version found = False offerings = Offering.objects.filter(owner_organization=offering.owner_organization, name=offering.name) for off in offerings: if off.pk in purchased_offerings and is_lower_version(off.version, offering.version): found = True break if not found: return build_response(request, 403, 'Forbidden') if developer_plan and not 'developer' in roles: return build_response(request, 403, 'Forbidden') response_info = create_purchase(user, offering, org_owned=org_owned, payment_info=payment_info) except Exception as e: # Check if the offering has been paid before the exception has been raised paid = False if org_owned: if offering.pk in request.user.userprofile.current_organization.offerings_purchased: paid = True response_info = Purchase.objects.get(owner_organization=request.user.userprofile.current_organization, offering=offering) else: if offering.pk in request.user.userprofile.offerings_purchased: paid = True response_info = Purchase.objects.get(customer=request.user, offering=offering, organization_owned=False) if not paid: return build_response(request, 400, unicode(e)) response = {} # If the value returned by the create_purchase method is a string means that # the purchase is not ended and need user confirmation. response_info contains # the URL where redirect the user if isinstance(response_info, str) or isinstance(response_info, unicode): response['redirection_link'] = response_info status = 200 else: # The purchase is finished so the download links are returned # Load download resources URL response['resources'] = [] for res in offering.resources: r = store_resource.objects.get(pk=res) # Check if the resource has been uploaded to the store or if is # in an external applications server if r.resource_path != '': response['resources'].append(r.resource_path) elif r.download_link != '': response['resources'].append(r.download_link) # Load bill URL response['bill'] = response_info.bill status = 201 # Check if it is needed to redirect the user token = offering.pk + user.pk site = get_current_site(request) context = Context.objects.get(site=site) if token in context.user_refs: redirect_uri = context.user_refs[token]['redirect_uri'] del(context.user_refs[token]) context.save() response['client_redirection_uri'] = redirect_uri return HttpResponse(json.dumps(response), status=status, mimetype=content_type)
def test_purchase_exeptions(self): # Load test info user = User.objects.get(username='******') offering = Offering.objects.get(name='test_offering') error_messages = [ 'The customer does not have a tax address', 'The tax address is not valid', 'Invalid credit card info', 'Invalid payment method', 'The customer does not have payment info' ] payment_info = [{ 'payment_method': 'credit_card', 'credit_card': { 'number': '1234123412341234', 'type': 'Visa', 'expire_year': '2018', 'expire_month': '3', 'cvv2': '111' } }, { 'payment_method': 'credit_card', 'credit_card': { 'number': '1234123412341234', 'type': 'Visa', 'expire_year': '2018', 'expire_month': '3', 'cvv2': '111' }, 'tax_address': { 'postal': '28000', 'city': 'test city', 'country': 'test country' } }, { 'payment_method': 'credit_card', 'credit_card': { 'type': 'Visa', 'expire_year': '2018', 'expire_month': '3', 'cvv2': '111' }, 'tax_address': { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } }, { 'payment_method': 'credit', 'credit_card': { 'number': '1234123412341234', 'type': 'Visa', 'expire_year': '2018', 'expire_month': '3', 'cvv2': '111' }, 'tax_address': { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } }, { 'payment_method': 'credit_card', 'tax_address': { 'street': 'test street', 'postal': '28000', 'city': 'test city', 'country': 'test country' } } ] # Test exceptions for i in range(0, 3): error = False msg = None try: purchases_management.create_purchase(user, offering, payment_info=payment_info[i]) except Exception, e: error = True msg = e.message self.assertTrue(error) self.assertEqual(msg, error_messages[i])
def create(self, request): user = request.user content_type = get_content_type(request)[0] if content_type == 'application/json': try: data = json.loads(request.raw_post_data) payment_info = {} if isinstance(data['offering'], dict): id_ = data['offering'] org = Organization.objects.get(name=id_['organization']) offering = Offering.objects.get(owner_organization=org, name=id_['name'], version=id_['version']) else: offering = Offering.objects.get(description_url=data['offering']) if 'tax_address' in data: payment_info['tax_address'] = data['tax_address'] payment_info['payment_method'] = data['payment']['method'] if 'credit_card' in data['payment']: payment_info['credit_card'] = data['payment']['credit_card'] # Check the selected price plan update_plan = False developer_plan = False if 'plan_label' in data: payment_info['plan'] = data['plan_label'] # Classify the plan # Check if the plan is an update if data['plan_label'].lower() == 'update': update_plan = True # Check if the plan is a developer purchase elif data['plan_label'].lower() == 'developer': developer_plan = True # Check if the user is purchasing for an organization org_owned = True if user.userprofile.is_user_org(): org_owned = False # Check if the user has the customer role for the organization roles = user.userprofile.get_current_roles() if org_owned: if not 'customer' in roles and not developer_plan: return build_response(request, 403, 'Forbidden') # Load the purchased offerings if it is an update in # order to check versions later if update_plan: purchased_offerings = user.userprofile.current_organization.offerings_purchased elif update_plan: purchased_offerings = user.userprofile.offerings_purchased # Check if the plan is an update if update_plan: # Check if the user has purchased a previous version found = False offerings = Offering.objects.filter(owner_organization=offering.owner_organization, name=offering.name) for off in offerings: if off.pk in purchased_offerings and is_lower_version(off.version, offering.version): found = True break if not found: return build_response(request, 403, 'Forbidden') if developer_plan and not 'developer' in roles: return build_response(request, 403, 'Forbidden') response_info = create_purchase(user, offering, org_owned=org_owned, payment_info=payment_info) except Exception, e: # Check if the offering has been paid before the exception has been raised paid = False if org_owned: if offering.pk in request.user.userprofile.current_organization.offerings_purchased: paid = True response_info = Purchase.objects.get(owner_organization=request.user.userprofile.current_organization, offering=offering) else: if offering.pk in request.user.userprofile.offerings_purchased: paid = True response_info = Purchase.objects.get(customer=request.user, offering=offering, organization_owned=False) if not paid: return build_response(request, 400, e.message)