Files
edx-platform/openedx/features/discounts/tests/test_views.py
Kyle McCormick 151bd13666 Use full names for common.djangoapps imports; warn when using old style (#25477)
* Generate common/djangoapps import shims for LMS
* Generate common/djangoapps import shims for Studio
* Stop appending project root to sys.path
* Stop appending common/djangoapps to sys.path
* Import from common.djangoapps.course_action_state instead of course_action_state
* Import from common.djangoapps.course_modes instead of course_modes
* Import from common.djangoapps.database_fixups instead of database_fixups
* Import from common.djangoapps.edxmako instead of edxmako
* Import from common.djangoapps.entitlements instead of entitlements
* Import from common.djangoapps.pipline_mako instead of pipeline_mako
* Import from common.djangoapps.static_replace instead of static_replace
* Import from common.djangoapps.student instead of student
* Import from common.djangoapps.terrain instead of terrain
* Import from common.djangoapps.third_party_auth instead of third_party_auth
* Import from common.djangoapps.track instead of track
* Import from common.djangoapps.util instead of util
* Import from common.djangoapps.xblock_django instead of xblock_django
* Add empty common/djangoapps/__init__.py to fix pytest collection
* Fix pylint formatting violations
* Exclude import_shims/ directory tree from linting
2020-11-10 07:02:01 -05:00

61 lines
2.3 KiB
Python

"""Tests of openedx.features.discounts.views"""
# -*- coding: utf-8 -*-
import jwt
import six
from django.test.client import Client
from django.urls import reverse
from common.djangoapps.student.tests.factories import TEST_PASSWORD, UserFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
class TestCourseUserDiscount(ModuleStoreTestCase):
"""
CourseUserDiscount should return a jwt with the information if this combination of user and
course can receive a discount, and how much that discount should be.
"""
def setUp(self):
super(TestCourseUserDiscount, self).setUp()
self.user = UserFactory.create()
self.course = CourseFactory.create(run='test', display_name='test')
self.client = Client()
self.url = reverse(
'api_discounts:course_user_discount',
kwargs={'course_key_string': six.text_type(self.course.id)}
)
def test_url(self):
"""
Test that the url hasn't changed
"""
assert self.url == ('/api/discounts/course/' + six.text_type(self.course.id))
def test_course_user_discount(self):
"""
Test that the api returns a jwt with the discount information
"""
self.client.login(username=self.user.username, password=TEST_PASSWORD)
# the endpoint should return a 200 if all goes well
response = self.client.get(self.url)
assert response.status_code == 200
# for now, it should always return false
expected_payload = {'discount_applicable': False, 'discount_percent': 15}
assert expected_payload['discount_applicable'] == response.data['discount_applicable']
# make sure that the response matches the expected response
response_payload = jwt.decode(response.data['jwt'], verify=False)
assert all(item in list(response_payload.items()) for item in expected_payload.items())
def test_course_user_discount_no_user(self):
"""
Test that the endpoint returns a 401 if there is no user signed in
"""
# the endpoint should return a 401 because the user is not logged in
response = self.client.get(self.url)
assert response.status_code == 401