def test_create(self, voucher_type, max_uses, expected_max_uses): """Test the create method.""" title = 'Test coupon' stock_record = self.seat.stockrecords.first() self.coupon_data.update({ 'title': title, 'client_username': '******', 'stock_record_ids': [stock_record.id], '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_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 build_mock_request(url): """ Helper function to manually build a ``WSGIRequest`` object to be used to test the view. :param url: A string representing the URL for the request. :return: A prepared ``WSGIRequest`` object. """ # Build an interesting request. request = RequestFactory().get(url) request.COOKIES = { # Some silly cookies, just a PoC. 'company': 'MaykinMedia', 'worker': 'Jose', } request.META['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac ' \ 'OS X 10_10_5) AppleWebKit/537.36 ' \ '(KHTML, like Gecko) ' \ 'Chrome/49.0.2623.112 Safari/537.36' # Set up a known IP address to retrieve GeoIP data. This one is from # OpenDNS service, check https://www.opendns.com/ request.META['REMOTE_ADDR'] = '208.67.222.222' # Request is performed by a system user. request.user, _ = User.objects.get_or_create( username='******', first_name='Test', last_name='User', email='*****@*****.**') # Set up the 'django-user-agent' machinery in the request, as its own # middleware does. request.user_agent = SimpleLazyObject(lambda: get_user_agent(request)) return request
def test_body_handles_unparseable_dict(self, sqs_queue_post): """Handle when calls to format request.COOKIES or META fail.""" request = RequestFactory().get('/') request.COOKIES = {'test': ObjectThatCantBeRepresented()} request.META = {'test': ObjectThatCantBeRepresented()} self.logger.error('something', extra={'request': request}) args, kwargs = sqs_queue_post.call_args self.assertIn('<could not parse>', kwargs['message'])
def build_request(self): """ Build a request that contains the instance user, coupon data, site, and an empty set of cookies. """ request = RequestFactory() request.user = self.user request.data = self.coupon_data request.site = self.site request.COOKIES = {} return request
def test_body_handles_unparseable_dict(self, sqs_queue_post): """Handle when calls to format request.COOKIES or META fail.""" class ObjectThatCantBeRepresented(object): def __repr__(self): raise Exception request = RequestFactory().get('/') request.COOKIES = {'test': ObjectThatCantBeRepresented()} request.META = {'test': ObjectThatCantBeRepresented()} self.logger.error('something', extra={'request': request}) args, kwargs = sqs_queue_post.call_args self.assertIn('<could not parse>', kwargs['message'])
def build_mock_request(url): """ Helper function to manually build a ``WSGIRequest`` object to be used to test the view. :param url: A string representing the URL for the request. :return: A prepared ``WSGIRequest`` object. """ # Build an interesting request. request = RequestFactory().get(url) request.COOKIES = { # Some silly cookies, just a PoC. 'company': 'MaykinMedia', 'worker': 'Jose', } request.META['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac ' \ 'OS X 10_10_5) AppleWebKit/537.36 ' \ '(KHTML, like Gecko) ' \ 'Chrome/49.0.2623.112 Safari/537.36' # Set up a known IP address to retrieve GeoIP data. This one is from # OpenDNS service, check https://www.opendns.com/ request.META['REMOTE_ADDR'] = '208.67.222.222' # Request is performed by a system user. request.user, _ = User.objects.get_or_create( username='******', first_name='Test', last_name='User', email='*****@*****.**' ) # Set up the 'django-user-agent' machinery in the request, as its own # middleware does. request.user_agent = SimpleLazyObject(lambda: get_user_agent(request)) return request
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
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, max_uses=None, note=None, partner=None, price=100, quantity=5, title='Test coupon', voucher_type=Voucher.SINGLE_USE): """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 code(str): Custom coupon code course_seat_types(str): A string of comma-separated list of seat types email_domains(str): A comma seperated list of email domains partner(Partner): Partner used for creating a catalog price(int): Price of the coupon title(str): Title of the coupon 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_seat_types=course_seat_types, email_domains=email_domains, end_datetime=datetime.date(2020, 1, 1), max_uses=max_uses, note=note, partner=partner, price=price, quantity=quantity, start_datetime=datetime.date(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, catalog_query=None, course_seat_types=None, email_domains=None): """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 catalog_query(str): Course query string course_seat_types(str): A string of comma-separated list of seat types email_domains(str): A comma seperated list of email domains 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 data = { 'partner': partner, 'benefit_type': Benefit.PERCENTAGE, 'benefit_value': benefit_value, 'catalog': catalog, 'end_datetime': datetime.date(2020, 1, 1), 'code': code, 'quantity': quantity, 'start_datetime': datetime.date(2015, 1, 1), 'voucher_type': Voucher.SINGLE_USE, 'categories': [self.category], 'note': note, 'max_uses': max_uses, 'catalog_query': catalog_query, 'course_seat_types': course_seat_types, 'email_domains': email_domains, } 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, 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, catalog_query=None, course_seat_types=None): """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 catalog_query(str): Course query string course_seat_types(str): A string of comma-separated list of seat types 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 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, 'catalog_query': catalog_query, 'course_seat_types': course_seat_types, } 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