def test_create(self): """Test the create method.""" max_uses = 2 title = 'Test coupon' stock_record = self.seat.stockrecords.first() self.coupon_data.update({ 'title': title, 'client': 'Člient', 'stock_record_ids': [stock_record.id], 'voucher_type': Voucher.ONCE_PER_CUSTOMER, 'price': 100, 'category': {'name': self.category.name}, 'max_uses': max_uses, }) request = self.build_request() view = CouponViewSet() view.request = request response = view.create(request) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertDictEqual( response.data, {'payment_data': {'payment_processor_name': 'Invoice'}, 'id': 1, 'order': 1, 'coupon_id': 3} ) coupon = Product.objects.get(title=title) self.assertEqual( coupon.attr.coupon_vouchers.vouchers.first().offers.first().max_global_applications, max_uses )
def test_custom_code_integrity_error(self): """Test custom coupon code duplication.""" data = { 'partner': self.partner, 'benefit_type': Benefit.PERCENTAGE, 'benefit_value': 100, 'catalog': self.catalog, 'end_date': datetime.date(2020, 1, 1), 'code': 'CUSTOMCODE', 'quantity': 1, 'start_date': datetime.date(2015, 1, 1), 'voucher_type': Voucher.SINGLE_USE } CouponViewSet().create_coupon_product( title='Custom coupon', price=100, data=data ) with self.assertRaises(IntegrityError): CouponViewSet().create_coupon_product( title='Coupon with integrity issue', price=100, data=data )
def test_clean_voucher_request_data(self, voucher_type, max_uses): """ Test that the method "clean_voucher_request_data" returns expected cleaned data dict. """ title = 'Test coupon' stock_record = self.seat.stockrecords.first() self.coupon_data.update({ 'title': title, 'client': 'Člient', 'stock_record_ids': [stock_record.id], 'voucher_type': voucher_type, 'price': 100, 'category': { 'name': self.category.name }, 'max_uses': max_uses, }) request = self.build_request() view = CouponViewSet() view.request = request cleaned_voucher_data = view.clean_voucher_request_data(request) expected_cleaned_voucher_data_keys = [ 'benefit_type', 'benefit_value', 'coupon_catalog', 'catalog_query', 'category', 'code', 'course_catalog', 'course_seat_types', 'email_domains', 'end_datetime', 'enterprise_customer', 'max_uses', 'note', 'partner', 'price', 'quantity', 'start_datetime', 'title', 'voucher_type' ] self.assertEqual(sorted(expected_cleaned_voucher_data_keys), sorted(cleaned_voucher_data.keys()))
def test_create_bad_enterprise_data(self, voucher_type, max_uses): """Test the create method.""" title = 'Test coupon' stock_record = self.seat.stockrecords.first() self.coupon_data.update({ 'title': title, 'client': 'Člient', 'stock_record_ids': [stock_record.id], 'voucher_type': voucher_type, 'price': 100, 'category': { 'name': self.category.name }, 'max_uses': max_uses, 'enterprise_customer': { 'value': 'Truthy but wrong' } }) request = self.build_request() view = CouponViewSet() view.request = request response = view.create(request) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_delete_coupon(self): """Test the coupon deletion.""" coupon = self.create_coupon(partner=self.partner) self.assertEqual( Product.objects.filter(product_class=self.product_class).count(), 1) self.assertEqual(StockRecord.objects.filter(product=coupon).count(), 1) coupon_voucher_qs = CouponVouchers.objects.filter(coupon=coupon) self.assertEqual(coupon_voucher_qs.count(), 1) self.assertEqual(coupon_voucher_qs.first().vouchers.count(), 5) request = RequestFactory() request.site = self.setup_site_configuration() response = CouponViewSet().destroy(request, coupon.id) self.assertEqual( Product.objects.filter(product_class=self.product_class).count(), 0) self.assertEqual(StockRecord.objects.filter(product=coupon).count(), 0) coupon_voucher_qs = CouponVouchers.objects.filter(coupon=coupon) self.assertEqual(coupon_voucher_qs.count(), 0) self.assertEqual(Voucher.objects.count(), 0) self.assertEqual(response.status_code, 204) response = CouponViewSet().destroy(request, 100) self.assertEqual(response.status_code, 404)
def test_custom_code_integrity_error(self): """Test custom coupon code duplication.""" self.coupon_data.update({ 'code': 'CUSTOMCODE', 'quantity': 1, }) CouponViewSet().create_coupon_product(title='Custom coupon', price=100, data=self.coupon_data) with self.assertRaises(IntegrityError): CouponViewSet().create_coupon_product( title='Coupon with integrity issue', price=100, data=self.coupon_data)
def fulfill_product(self, order, lines): """ Fulfills the purchase of an Enrollment code product. For each line creates number of vouchers equal to that line's quantity. Creates a new OrderLineVouchers object to tie the order with the created voucher and adds the vouchers to the coupon's total vouchers. Args: order (Order): The Order associated with the lines to be fulfilled. lines (List of Lines): Order Lines, associated with purchased products in an Order. Returns: The original set of lines, with new statuses set based on the success or failure of fulfillment. """ msg = "Attempting to fulfill '{product_class}' product types for order [{order_number}]".format( product_class=ENROLLMENT_CODE_PRODUCT_CLASS_NAME, order_number=order.number ) logger.info(msg) for line in lines: name = 'Enrollment Code Range for {}'.format(line.product.attr.course_key) seat = Product.objects.filter( attributes__name='course_key', attribute_values__value_text=line.product.attr.course_key ).get( attributes__name='certificate_type', attribute_values__value_text=line.product.attr.seat_type ) _range, created = Range.objects.get_or_create(name=name) if created: _range.add_product(seat) stock_record = StockRecord.objects.get(product=seat, partner=seat.course.partner) coupon_catalog = CouponViewSet.get_coupon_catalog([stock_record.id], seat.course.partner) _range.catalog = coupon_catalog _range.save() vouchers = create_vouchers( name=unicode('Enrollment code voucher [{}]').format(line.product.title), benefit_type=Benefit.PERCENTAGE, benefit_value=100, catalog=coupon_catalog, coupon=seat, end_datetime=settings.ENROLLMENT_CODE_EXIPRATION_DATE, enterprise_customer=None, enterprise_customer_catalog=None, quantity=line.quantity, start_datetime=datetime.datetime.now(), voucher_type=Voucher.SINGLE_USE, _range=_range ) line_vouchers = OrderLineVouchers.objects.create(line=line) for voucher in vouchers: line_vouchers.vouchers.add(voucher) line.set_status(LINE.COMPLETE) self.send_email(order) logger.info("Finished fulfilling 'Enrollment code' product types for order [%s]", order.number) return order, lines
def test_append_to_existing_coupon(self): """Test adding additional vouchers to an existing coupon.""" create_coupon(partner=self.partner, catalog=self.catalog) data = { 'partner': self.partner, 'benefit_type': Benefit.PERCENTAGE, 'benefit_value': 100, 'catalog': self.catalog, 'end_date': datetime.date(2020, 1, 1), 'code': '', 'quantity': 2, 'start_date': datetime.date(2015, 1, 1), 'voucher_type': Voucher.MULTI_USE } coupon_append = CouponViewSet().create_coupon_product( title='Test coupon', price=100, data=data) self.assertEqual( Product.objects.filter(product_class=self.product_class).count(), 1) self.assertEqual( StockRecord.objects.filter(product=coupon_append).count(), 1) self.assertEqual(coupon_append.attr.coupon_vouchers.vouchers.count(), 7) self.assertEqual( coupon_append.attr.coupon_vouchers.vouchers.filter( usage=Voucher.MULTI_USE).count(), 2)
def test_retrieve_invoice_data(self): request_data = { 'invoice_discount_type': Invoice.PERCENTAGE, 'invoice_discount_value': 50, 'invoice_number': 'INV-00055', 'invoice_payment_date': datetime.datetime(2016, 1, 1, tzinfo=pytz.UTC).isoformat(), 'invoice_type': Invoice.PREPAID, 'tax_deducted_source': None } invoice_data = CouponViewSet().retrieve_invoice_data(request_data) self.assertDictEqual( invoice_data, { 'discount_type': request_data['invoice_discount_type'], 'discount_value': request_data['invoice_discount_value'], 'number': request_data['invoice_number'], 'payment_date': request_data['invoice_payment_date'], 'type': request_data['invoice_type'], 'tax_deducted_source': request_data['tax_deducted_source'] })
def test_create(self): """Test the create method.""" site_configuration = SiteConfigurationFactory(partner__name='TestX') site = SiteFactory() site.siteconfiguration = site_configuration data = { 'title': 'Test coupon', 'client_username': '******', 'stock_record_ids': [1], 'start_date': '2015-1-1', 'end_date': '2020-1-1', 'code': '', 'benefit_type': Benefit.PERCENTAGE, 'benefit_value': 100, 'voucher_type': Voucher.SINGLE_USE, 'quantity': 1, 'price': 100 } request = RequestFactory() request.data = data request.site = site response = CouponViewSet().create(request) self.assertEqual(response.status_code, 200) self.assertDictEqual( response.data, {'payment_data': {'payment_processor_name': 'Invoice'}, 'id': 1, 'order': 1, 'coupon_id': 3} )
def create_coupon(title='Test coupon', price=100, partner=None, catalog=None, code='', benefit_value=100): """Helper method for creating a coupon.""" if partner is None: partner = PartnerFactory(name='Tester') if catalog is None: catalog = Catalog.objects.create(partner=partner) quantity = 5 if code is not '': quantity = 1 data = { 'partner': partner, 'benefit_type': Benefit.PERCENTAGE, 'benefit_value': benefit_value, 'catalog': catalog, 'end_date': datetime.date(2020, 1, 1), 'code': code, 'quantity': quantity, 'start_date': datetime.date(2015, 1, 1), 'voucher_type': Voucher.SINGLE_USE } coupon = CouponViewSet().create_coupon_product( title=title, price=price, data=data ) return coupon
def test_create(self, voucher_type, max_uses, expected_max_uses): """Test the create method.""" title = 'Test coupon' self.coupon_data.update({ 'title': title, 'client_username': '******', 'stock_record_ids': [1], 'voucher_type': voucher_type, 'price': 100, 'category_ids': [self.category.id], 'max_uses': max_uses, }) request = RequestFactory() request.user = self.user request.data = self.coupon_data request.site = self.site request.COOKIES = {} response = CouponViewSet().create(request) self.assertEqual(response.status_code, 200) self.assertDictEqual( response.data, { 'payment_data': { 'payment_processor_name': 'Invoice' }, 'id': 1, 'order': 1, 'coupon_id': 3 }) coupon = Product.objects.get(title=title) self.assertEqual( coupon.attr.coupon_vouchers.vouchers.first().offers.first(). max_global_applications, expected_max_uses)
def test_update_invoice_data(self): invoice = Invoice.objects.get(order__lines__product=self.coupon) self.assertEqual(invoice.discount_type, Invoice.PERCENTAGE) CouponViewSet().update_invoice_data( coupon=self.coupon, data={'invoice_discount_type': Invoice.FIXED}) invoice = Invoice.objects.get(order__lines__product=self.coupon) self.assertEqual(invoice.discount_type, Invoice.FIXED)
def test_update_coupon_benefit_value(self): coupon = Product.objects.get(title='Test coupon') vouchers = coupon.attr.coupon_vouchers.vouchers.all() CouponViewSet().update_coupon_benefit_value(benefit_value=Decimal(54), vouchers=vouchers) for voucher in vouchers: self.assertEqual(voucher.offers.first().benefit.value, Decimal(54))
def test_update_coupon_client(self): coupon = Product.objects.get(title='Test coupon') baskets = Basket.objects.filter(lines__product_id=coupon.id) basket = baskets.first() CouponViewSet().update_coupon_client( baskets=baskets, client_username='******') invoice = Invoice.objects.get(order__basket=basket) self.assertEqual(invoice.business_client.name, 'Test Client Username') # To test the old coupon clients, we need to delete all basket orders Order.objects.filter(basket__in=baskets).delete() CouponViewSet().update_coupon_client( baskets=baskets, client_username='******') baskets = Basket.objects.filter(lines__product_id=coupon.id) self.assertEqual(baskets.first().owner.username, 'Test Client Username')
def test_update_coupon_category(self): category = factories.CategoryFactory() CouponViewSet().update_coupon_category(category_ids=[category.id], coupon=self.coupon) coupon_categories = ProductCategory.objects.filter( product=self.coupon).values_list('category', flat=True) self.assertIn(category.id, coupon_categories) self.assertEqual(len(coupon_categories), 1)
def test_coupon_note(self): """Test creating a coupon with a note.""" self.coupon_data.update({ 'note': '𝑵𝑶𝑻𝑬', }) note_coupon = CouponViewSet().create_coupon_product( title='Coupon', price=100, data=self.coupon_data) self.assertEqual(note_coupon.attr.note, '𝑵𝑶𝑻𝑬') self.assertEqual(note_coupon.title, 'Coupon')
def test_update_coupon_client(self): baskets = Basket.objects.filter(lines__product_id=self.coupon.id) basket = baskets.first() client_username = '******' CouponViewSet().update_coupon_client(baskets=baskets, client_username=client_username) invoice = Invoice.objects.get(order__basket=basket) self.assertEqual(invoice.business_client.name, client_username)
def test_clean_voucher_request_data(self, voucher_type, max_uses): """ Test that the method "clean_voucher_request_data" returns expected cleaned data dict. """ title = 'Test coupon' stock_record = self.seat.stockrecords.first() self.coupon_data.update({ 'title': title, 'client': 'Člient', 'stock_record_ids': [stock_record.id], 'voucher_type': voucher_type, 'price': 100, 'category': {'name': self.category.name}, 'max_uses': max_uses, }) request = self.build_request() view = CouponViewSet() view.request = request cleaned_voucher_data = view.clean_voucher_request_data(request) expected_cleaned_voucher_data_keys = [ 'benefit_type', 'benefit_value', 'coupon_catalog', 'catalog_query', 'category', 'code', 'course_catalog', 'course_seat_types', 'email_domains', 'end_datetime', 'enterprise_customer', 'max_uses', 'note', 'partner', 'price', 'quantity', 'start_datetime', 'title', 'voucher_type' ] self.assertEqual(sorted(expected_cleaned_voucher_data_keys), sorted(cleaned_voucher_data.keys()))
def test_create_order(self): """Test the order creation.""" coupon = create_coupon(partner=self.partner) coupon_client = Client.objects.create(username='******') basket = CouponViewSet().add_product_to_basket( product=coupon, client=coupon_client, site=self.site, partner=self.partner ) response_data = CouponViewSet().create_order_for_invoice(basket, coupon_id=coupon.id) self.assertEqual(response_data[AC.KEYS.BASKET_ID], 1) self.assertEqual(response_data[AC.KEYS.ORDER], 1) self.assertEqual(response_data[AC.KEYS.PAYMENT_DATA][AC.KEYS.PAYMENT_PROCESSOR_NAME], 'Invoice') self.assertEqual(Order.objects.count(), 1) self.assertEqual(Order.objects.first().status, 'Complete') self.assertEqual(Order.objects.first().total_incl_tax, 100) self.assertEqual(Basket.objects.first().status, 'Submitted')
def test_create(self): """Test the create method.""" max_uses = 2 title = 'Test coupon' stock_record = self.seat.stockrecords.first() self.coupon_data.update({ 'title': title, 'client': 'Člient', 'stock_record_ids': [stock_record.id], 'voucher_type': Voucher.ONCE_PER_CUSTOMER, 'price': 100, 'category': { 'name': self.category.name }, 'max_uses': max_uses, }) request = self.build_request() view = CouponViewSet() view.request = request response = view.create(request) self.assertEqual(response.status_code, status.HTTP_200_OK) coupon = Product.objects.get(title=title) basket = Basket.objects.last() order = Order.objects.last() self.assertDictEqual( response.data, { 'payment_data': { 'payment_processor_name': 'Invoice' }, 'id': basket.id, 'order': order.id, 'coupon_id': coupon.id }) self.assertEqual( coupon.attr.coupon_vouchers.vouchers.first().offers.first(). max_global_applications, max_uses)
def test_multi_use_coupon_creation(self): """Test that the endpoint supports the creation of multi-usage coupons.""" max_uses_number = 2 self.coupon_data.update({ 'max_uses': max_uses_number, }) coupon = CouponViewSet().create_coupon_product(title='Test coupon', price=100, data=self.coupon_data) voucher = coupon.attr.coupon_vouchers.vouchers.first() self.assertEqual(voucher.offers.first().max_global_applications, max_uses_number)
def test_create_bad_enterprise_data(self, voucher_type, max_uses): """Test the create method.""" title = 'Test coupon' stock_record = self.seat.stockrecords.first() self.coupon_data.update({ 'title': title, 'client': 'Člient', 'stock_record_ids': [stock_record.id], 'voucher_type': voucher_type, 'price': 100, 'category': {'name': self.category.name}, 'max_uses': max_uses, 'enterprise_customer': {'value': 'Truthy but wrong'} }) request = self.build_request() view = CouponViewSet() view.request = request response = view.create(request) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_update_coupon_benefit_value(self): vouchers = self.coupon.attr.coupon_vouchers.vouchers.all() max_uses = vouchers[0].offers.first().max_global_applications benefit_value = Decimal(54) CouponViewSet().update_coupon_benefit_value( benefit_value=benefit_value, vouchers=vouchers, coupon=self.coupon) for voucher in vouchers: self.assertEqual(voucher.offers.first().benefit.value, benefit_value) self.assertEqual(voucher.offers.first().max_global_applications, max_uses)
def test_add_product_to_basket(self): """Test adding a coupon product to a basket.""" coupon = create_coupon(partner=self.partner) coupon_client = Client.objects.create(username='******') basket = CouponViewSet().add_product_to_basket(product=coupon, client=coupon_client, site=self.site, partner=self.partner) self.assertIsInstance(basket, Basket) self.assertEqual(Basket.objects.count(), 1) self.assertEqual(basket.lines.count(), 1) self.assertEqual(basket.lines.first().price_excl_tax, 100)
def test_create_update_data_dict(self): """Test creating update data dictionary""" fields = ['title', 'start_datetime', 'end_datetime'] data = CouponViewSet().create_update_data_dict(data=self.coupon_data, fields=fields) self.assertDictEqual( data, { 'end_datetime': self.coupon_data['end_datetime'], 'start_datetime': self.coupon_data['start_datetime'], 'title': self.coupon_data['title'], })
def test_custom_code_string(self): """Test creating a coupon with custom voucher code.""" self.coupon_data.update({ 'voucher_type': Voucher.ONCE_PER_CUSTOMER, 'code': 'CUSTOMCODE', 'quantity': 1, }) custom_coupon = CouponViewSet().create_coupon_product( title='Custom coupon', price=100, data=self.coupon_data) self.assertEqual(custom_coupon.attr.coupon_vouchers.vouchers.count(), 1) self.assertEqual( custom_coupon.attr.coupon_vouchers.vouchers.first().code, 'CUSTOMCODE')
def test_append_to_existing_coupon(self): """Test adding additional vouchers to an existing coupon.""" self.create_coupon(partner=self.partner, catalog=self.catalog) coupon_append = CouponViewSet().create_coupon_product( title='Test coupon', price=100, data=self.coupon_data) self.assertEqual( Product.objects.filter(product_class=self.product_class).count(), 1) self.assertEqual( StockRecord.objects.filter(product=coupon_append).count(), 1) self.assertEqual(coupon_append.attr.coupon_vouchers.vouchers.count(), 7) self.assertEqual( coupon_append.attr.coupon_vouchers.vouchers.filter( usage=Voucher.ONCE_PER_CUSTOMER).count(), 2)
def test_create_update_data_dict(self): """Test the update data dictionary""" data = {} for field in AC.UPDATEABLE_VOUCHER_FIELDS: CouponViewSet().create_update_data_dict( request_data=self.coupon_data, request_data_key=field['request_data_key'], update_dict=data, update_dict_key=field['attribute']) self.assertDictEqual( data, { 'end_datetime': self.coupon_data['end_date'], 'start_datetime': self.coupon_data['start_date'], 'name': self.coupon_data['title'], })
def test_update_coupon_benefit_value(self): vouchers = self.coupon.attr.coupon_vouchers.vouchers.all() max_uses = vouchers[0].offers.first().max_global_applications benefit_value = Decimal(54) with mock.patch( "ecommerce.extensions.voucher.utils.get_enterprise_customer", mock.Mock(return_value={'name': 'Fake enterprise'}) ): CouponViewSet().update_coupon_offer( benefit_value=benefit_value, vouchers=vouchers, coupon=self.coupon ) for voucher in vouchers: self.assertEqual(voucher.offers.first().benefit.value, benefit_value) self.assertEqual(voucher.offers.first().max_global_applications, max_uses)
def test_custom_code_string(self): """Test creating a coupon with custom voucher code.""" data = { 'partner': self.partner, 'benefit_type': Benefit.PERCENTAGE, 'benefit_value': 100, 'catalog': self.catalog, 'end_date': datetime.date(2020, 1, 1), 'code': 'CUSTOMCODE', 'quantity': 1, 'start_date': datetime.date(2015, 1, 1), 'voucher_type': Voucher.ONCE_PER_CUSTOMER } custom_coupon = CouponViewSet().create_coupon_product( title='Custom coupon', price=100, data=data ) self.assertEqual(custom_coupon.attr.coupon_vouchers.vouchers.count(), 1) self.assertEqual(custom_coupon.attr.coupon_vouchers.vouchers.first().code, 'CUSTOMCODE')
def test_generate_sku_for_coupon(self): """Verify the method generates a SKU for a coupon.""" data = { 'partner': self.partner, 'benefit_type': Benefit.PERCENTAGE, 'benefit_value': 100, 'catalog': self.catalog, 'end_date': datetime.date(2020, 1, 1), 'code': '', 'quantity': 5, 'start_date': datetime.date(2015, 1, 1), 'voucher_type': Voucher.SINGLE_USE } coupon = CouponViewSet().create_coupon_product(title='Test coupon', price=100, data=data) _hash = ' '.join((unicode(coupon.id), unicode(self.catalog.id), str(self.partner.id))) _hash = md5(_hash.lower()).hexdigest()[-7:] expected = _hash.upper() actual = generate_sku(coupon, self.partner, catalog=self.catalog) self.assertEqual(actual, expected)
def create_coupon(self, benefit_type=Benefit.PERCENTAGE, benefit_value=100, catalog=None, catalog_query=None, client=None, code='', course_seat_types=None, email_domains=None, enterprise_customer=None, max_uses=None, note=None, partner=None, price=100, quantity=5, title='Test coupon', voucher_type=Voucher.SINGLE_USE, course_catalog=None): """Helper method for creating a coupon. Arguments: benefit_type(str): The voucher benefit type benefit_value(int): The voucher benefit value catalog(Catalog): Catalog of courses for which the coupon applies catalog_query(str): Course query string client (BusinessClient): Optional business client object code(str): Custom coupon code course_catalog (int): Course catalog id from Catalog Service course_seat_types(str): A string of comma-separated list of seat types enterprise_customer (str): Hex-encoded UUID for an Enterprise Customer object from the Enterprise app. email_domains(str): A comma seperated list of email domains max_uses (int): Number of Voucher max uses note (str): Coupon note. partner(Partner): Partner used for creating a catalog price(int): Price of the coupon quantity (int): Number of vouchers to be created and associated with the coupon title(str): Title of the coupon voucher_type (str): Voucher type Returns: coupon (Coupon) """ if partner is None: partner = PartnerFactory(name='Tester') if client is None: client, __ = BusinessClient.objects.get_or_create(name='Test Client') if catalog is None and not (catalog_query and course_seat_types): catalog = Catalog.objects.create(partner=partner) if code is not '': quantity = 1 coupon = create_coupon_product( benefit_type=benefit_type, benefit_value=benefit_value, catalog=catalog, catalog_query=catalog_query, category=self.category, code=code, course_catalog=course_catalog, course_seat_types=course_seat_types, email_domains=email_domains, end_datetime=datetime.datetime(2020, 1, 1), enterprise_customer=enterprise_customer, max_uses=max_uses, note=note, partner=partner, price=price, quantity=quantity, start_datetime=datetime.datetime(2015, 1, 1), title=title, voucher_type=voucher_type ) request = RequestFactory() request.site = self.site request.user = factories.UserFactory() request.COOKIES = {} self.basket = prepare_basket(request, coupon) view = CouponViewSet() view.request = request self.response_data = view.create_order_for_invoice(self.basket, coupon_id=coupon.id, client=client) coupon.client = client return coupon
def create_coupon( self, title='Test coupon', price=100, client=None, partner=None, catalog=None, code='', benefit_value=100, note=None, max_uses=None, quantity=5 ): """Helper method for creating a coupon. Arguments: title(str): Title of the coupon price(int): Price of the coupon partner(Partner): Partner used for creating a catalog catalog(Catalog): Catalog of courses for which the coupon applies code(str): Custom coupon code benefit_value(int): The voucher benefit value Returns: coupon (Coupon) """ if partner is None: partner = PartnerFactory(name='Tester') if client is None: client, __ = BusinessClient.objects.get_or_create(name='Test Client') if catalog is None: catalog = Catalog.objects.create(partner=partner) if code is not '': quantity = 1 data = { 'partner': partner, 'benefit_type': Benefit.PERCENTAGE, 'benefit_value': benefit_value, 'catalog': catalog, 'end_date': datetime.date(2020, 1, 1), 'code': code, 'quantity': quantity, 'start_date': datetime.date(2015, 1, 1), 'voucher_type': Voucher.SINGLE_USE, 'categories': [self.category], 'note': note, 'max_uses': max_uses, } coupon = CouponViewSet().create_coupon_product( title=title, price=price, data=data ) request = RequestFactory() request.site = self.site request.user = factories.UserFactory() request.COOKIES = {} self.basket = prepare_basket(request, coupon) self.response_data = CouponViewSet().create_order_for_invoice(self.basket, coupon_id=coupon.id, client=client) coupon.client = client return coupon
def create_coupon(self, benefit_type=Benefit.PERCENTAGE, benefit_value=100, catalog=None, catalog_query=None, client=None, code='', course_seat_types=None, email_domains=None, enterprise_customer=None, enterprise_customer_catalog=None, max_uses=None, note=None, partner=None, price=100, quantity=5, title='Test coupon', voucher_type=Voucher.SINGLE_USE, course_catalog=None, program_uuid=None, start_datetime=None, end_datetime=None): """Helper method for creating a coupon. Arguments: benefit_type(str): The voucher benefit type benefit_value(int): The voucher benefit value catalog(Catalog): Catalog of courses for which the coupon applies catalog_query(str): Course query string client (BusinessClient): Optional business client object code(str): Custom coupon code course_catalog (int): Course catalog id from Discovery Service course_seat_types(str): A string of comma-separated list of seat types enterprise_customer (str): Hex-encoded UUID for an Enterprise Customer object from the Enterprise app. enterprise_customer_catalog (str): UUID for an Enterprise Customer Catalog from the Enterprise app. email_domains(str): A comma seperated list of email domains max_uses (int): Number of Voucher max uses note (str): Coupon note. partner(Partner): Partner used for creating a catalog price(int): Price of the coupon quantity (int): Number of vouchers to be created and associated with the coupon title(str): Title of the coupon voucher_type (str): Voucher type program_uuid (str): Program UUID Returns: coupon (Coupon) """ if partner is None: partner = PartnerFactory(name='Tester') if client is None: client, __ = BusinessClient.objects.get_or_create(name='Test Client') if (catalog is None and not enterprise_customer_catalog and not ((catalog_query or course_catalog or program_uuid) and course_seat_types)): catalog = Catalog.objects.create(partner=partner) if code != '': quantity = 1 with mock.patch( "ecommerce.extensions.voucher.utils.get_enterprise_customer", mock.Mock(return_value={'name': 'Fake enterprise'})): coupon = create_coupon_product( benefit_type=benefit_type, benefit_value=benefit_value, catalog=catalog, catalog_query=catalog_query, category=self.category, code=code, course_catalog=course_catalog, course_seat_types=course_seat_types, email_domains=email_domains, end_datetime=end_datetime or (datetime.datetime.now() + datetime.timedelta(days=500)), enterprise_customer=enterprise_customer, enterprise_customer_catalog=enterprise_customer_catalog, max_uses=max_uses, note=note, partner=partner, price=price, quantity=quantity, start_datetime=start_datetime or datetime.datetime(2015, 1, 1), title=title, voucher_type=voucher_type, program_uuid=program_uuid, site=self.site ) request = RequestFactory() request.site = self.site request.user = UserFactory() request.COOKIES = {} request.GET = {} self.basket = prepare_basket(request, [coupon]) view = CouponViewSet() view.request = request self.response_data = view.create_order_for_invoice(self.basket, coupon_id=coupon.id, client=client) coupon.client = client return coupon