Commit 2a30f64d by Clinton Blackburn

Voucher test cleanup

- Added custom factories to avoid unique constraint issues
- Added basic test of offers URL

ECOM-7096
parent ac3c52f1
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime import datetime
import json
import mock
import ddt import ddt
import httpretty import httpretty
import mock
import pytz import pytz
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.http import Http404 from django.http import Http404
from django.utils.timezone import now from django.utils.timezone import now
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from oscar.core.loading import get_model from oscar.core.loading import get_model
from oscar.test.factories import ( from oscar.test.factories import BenefitFactory, OrderLineFactory, OrderFactory, RangeFactory
BenefitFactory, ConditionalOfferFactory, OrderLineFactory, OrderFactory, RangeFactory, VoucherFactory
)
from requests.exceptions import ConnectionError, Timeout from requests.exceptions import ConnectionError, Timeout
from rest_framework.test import APIRequestFactory from rest_framework.test import APIRequestFactory
from slumber.exceptions import SlumberBaseException from slumber.exceptions import SlumberBaseException
...@@ -26,16 +23,13 @@ from ecommerce.extensions.api import serializers ...@@ -26,16 +23,13 @@ from ecommerce.extensions.api import serializers
from ecommerce.extensions.api.v2.views.vouchers import VoucherViewSet from ecommerce.extensions.api.v2.views.vouchers import VoucherViewSet
from ecommerce.extensions.catalogue.tests.mixins import CourseCatalogTestMixin from ecommerce.extensions.catalogue.tests.mixins import CourseCatalogTestMixin
from ecommerce.extensions.partner.strategy import DefaultStrategy from ecommerce.extensions.partner.strategy import DefaultStrategy
from ecommerce.extensions.test.factories import prepare_voucher from ecommerce.extensions.test.factories import prepare_voucher, VoucherFactory, ConditionalOfferFactory
from ecommerce.tests.mixins import Catalog, LmsApiMockMixin from ecommerce.tests.mixins import Catalog, LmsApiMockMixin
from ecommerce.tests.testcases import TestCase from ecommerce.tests.testcases import TestCase
COUPON_CODE = 'COUPONCODE' Product = get_model('catalogue', 'Product')
Range = get_model('offer', 'Range') Range = get_model('offer', 'Range')
StockRecord = get_model('partner', 'StockRecord') StockRecord = get_model('partner', 'StockRecord')
Product = get_model('catalogue', 'Product')
@ddt.ddt @ddt.ddt
...@@ -47,27 +41,32 @@ class VoucherViewSetTests(CourseCatalogMockMixin, CourseCatalogTestMixin, LmsApi ...@@ -47,27 +41,32 @@ class VoucherViewSetTests(CourseCatalogMockMixin, CourseCatalogTestMixin, LmsApi
super(VoucherViewSetTests, self).setUp() super(VoucherViewSetTests, self).setUp()
self.user = self.create_user(is_staff=True) self.user = self.create_user(is_staff=True)
self.client.login(username=self.user.username, password=self.password) self.client.login(username=self.user.username, password=self.password)
voucher1 = VoucherFactory()
voucher1.offers.add(ConditionalOfferFactory())
self.voucher = VoucherFactory(code=COUPON_CODE)
self.voucher.offers.add(ConditionalOfferFactory(name='test2'))
def test_voucher_listing(self): def test_list(self):
""" Verify the endpoint lists out all vouchers. """ """ Verify the endpoint lists all vouchers. """
vouchers = VoucherFactory.create_batch(3)
for voucher in vouchers:
voucher.offers.add(ConditionalOfferFactory())
response = self.client.get(self.path) response = self.client.get(self.path)
response_data = json.loads(response.content)
self.assertEqual(response_data['count'], 2) self.assertEqual(response.data['count'], len(vouchers))
self.assertEqual(response_data['results'][1]['code'], COUPON_CODE)
def test_voucher_filtering(self): actual_codes = [datum['code'] for datum in response.data['results']]
""" Verify the endpoint filters by code. """ expected_codes = [voucher.code for voucher in vouchers]
filter_path = '{}?code={}'.format(self.path, COUPON_CODE) self.assertEqual(actual_codes, expected_codes)
response = self.client.get(filter_path)
response_data = json.loads(response.content)
self.assertEqual(response_data['count'], 1) def test_list_with_code_filter(self):
self.assertEqual(response_data['results'][0]['code'], COUPON_CODE) """ Verify the endpoint list all vouchers, filtered by the specified code. """
voucher = VoucherFactory()
voucher.offers.add(ConditionalOfferFactory())
url = '{path}?code={code}'.format(path=self.path, code=voucher.code)
response = self.client.get(url)
self.assertEqual(response.data['count'], 1)
self.assertEqual(response.data['results'][0]['code'], voucher.code)
def prepare_get_offers_response(self, quantity=1, seat_type='verified', seats=None): def prepare_get_offers_response(self, quantity=1, seat_type='verified', seats=None):
"""Helper method for creating response the voucher offers endpoint. """Helper method for creating response the voucher offers endpoint.
...@@ -123,17 +122,23 @@ class VoucherViewSetTests(CourseCatalogMockMixin, CourseCatalogTestMixin, LmsApi ...@@ -123,17 +122,23 @@ class VoucherViewSetTests(CourseCatalogMockMixin, CourseCatalogTestMixin, LmsApi
return products, request, voucher return products, request, voucher
def build_offers_url(self, voucher):
return '{path}?code={code}'.format(path=reverse('api:v2:vouchers-offers-list'), code=voucher.code)
@httpretty.activate @httpretty.activate
@mock_course_catalog_api_client @mock_course_catalog_api_client
def test_omitting_unavailable_seats(self): def test_omitting_unavailable_seats(self):
""" Verify an unavailable seat is omitted from offer page results. """ """ Verify an unavailable seat is omitted from offer page results. """
products, request, voucher = self.prepare_get_offers_response(quantity=2) products, request, voucher = self.prepare_get_offers_response(quantity=2)
url = self.build_offers_url(voucher)
offers = VoucherViewSet().get_offers(request=request, voucher=voucher)['results'] response = self.client.get(url)
self.assertEqual(len(offers), 2) self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 2)
products[0].expires = pytz.utc.localize(datetime.datetime.min) product = products[0]
products[0].save() product.expires = pytz.utc.localize(datetime.datetime.min)
product.save()
offers = VoucherViewSet().get_offers(request=request, voucher=voucher)['results'] offers = VoucherViewSet().get_offers(request=request, voucher=voucher)['results']
self.assertEqual(len(offers), 1) self.assertEqual(len(offers), 1)
...@@ -208,13 +213,8 @@ class VoucherViewSetTests(CourseCatalogMockMixin, CourseCatalogTestMixin, LmsApi ...@@ -208,13 +213,8 @@ class VoucherViewSetTests(CourseCatalogMockMixin, CourseCatalogTestMixin, LmsApi
@ddt.ddt @ddt.ddt
@httpretty.activate @httpretty.activate
class VoucherViewOffersEndpointTests( class VoucherViewOffersEndpointTests(CourseCatalogMockMixin, CouponMixin, CourseCatalogTestMixin, LmsApiMockMixin,
CourseCatalogMockMixin, TestCase):
CouponMixin,
CourseCatalogTestMixin,
LmsApiMockMixin,
TestCase
):
""" Tests for the VoucherViewSet offers endpoint. """ """ Tests for the VoucherViewSet offers endpoint. """
def setUp(self): def setUp(self):
......
from datetime import datetime from datetime import datetime
import factory
from django.utils.timezone import now from django.utils.timezone import now
from oscar.test.factories import * # pylint:disable=wildcard-import,unused-wildcard-import from oscar.test.factories import * # pylint:disable=wildcard-import,unused-wildcard-import
from oscar.test.factories import (
ConditionalOfferFactory as BaseConditionalOfferFactory,
VoucherFactory as BaseVoucherFactory,
)
Benefit = get_model('offer', 'Benefit') Benefit = get_model('offer', 'Benefit')
Catalog = get_model('catalogue', 'Catalog') Catalog = get_model('catalogue', 'Catalog')
...@@ -87,3 +92,12 @@ def prepare_voucher(code='COUPONTEST', _range=None, start_datetime=None, end_dat ...@@ -87,3 +92,12 @@ def prepare_voucher(code='COUPONTEST', _range=None, start_datetime=None, end_dat
offer = ConditionalOfferFactory(name='PrepareVoucherOffer', benefit=benefit, condition=condition) offer = ConditionalOfferFactory(name='PrepareVoucherOffer', benefit=benefit, condition=condition)
voucher.offers.add(offer) voucher.offers.add(offer)
return voucher, product return voucher, product
class VoucherFactory(BaseVoucherFactory): # pylint: disable=function-redefined
name = factory.Faker('word')
code = factory.Faker('word')
class ConditionalOfferFactory(BaseConditionalOfferFactory): # pylint: disable=function-redefined
name = factory.Faker('word')
...@@ -5,6 +5,7 @@ bok-choy==0.4.7 ...@@ -5,6 +5,7 @@ bok-choy==0.4.7
coverage==4.2 coverage==4.2
ddt==1.0.0 ddt==1.0.0
django-nose==1.4.2 django-nose==1.4.2
factory-boy==2.8.1
freezegun==0.3.7 freezegun==0.3.7
httpretty==0.8.14 httpretty==0.8.14
mock==1.3.0 mock==1.3.0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment