Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

feat!: remove custom JWT decoding #3943

Merged
merged 6 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ jobs:
with:
python-version: "3.8"
architecture: x64
- name: Report coverage
if: matrix.testname == 'test-python'
uses: codecov/codecov-action@v3
with:
flags: unittests
fail_ci_if_error: false

docs:
runs-on: ubuntu-latest
Expand Down
120 changes: 0 additions & 120 deletions ecommerce/extensions/api/handlers.py

This file was deleted.

146 changes: 0 additions & 146 deletions ecommerce/extensions/api/tests/test_handlers.py

This file was deleted.

16 changes: 5 additions & 11 deletions ecommerce/extensions/api/v2/tests/views/test_courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import json

import jwt
import mock
from django.conf import settings
from django.contrib.auth import get_user_model
from django.urls import reverse
from oscar.core.loading import get_class, get_model
Expand All @@ -16,6 +14,7 @@
from ecommerce.courses.tests.factories import CourseFactory
from ecommerce.extensions.api.v2.tests.views import JSON_CONTENT_TYPE, ProductSerializerMixin
from ecommerce.extensions.catalogue.tests.mixins import DiscoveryTestMixin
from ecommerce.tests.mixins import JwtMixin
from ecommerce.tests.testcases import TestCase

Product = get_model('catalogue', 'Product')
Expand All @@ -24,7 +23,7 @@
User = get_user_model()


class CourseViewSetTests(ProductSerializerMixin, DiscoveryTestMixin, TestCase):
class CourseViewSetTests(JwtMixin, ProductSerializerMixin, DiscoveryTestMixin, TestCase):
list_path = reverse('api:v2:course-list')

def setUp(self):
Expand Down Expand Up @@ -75,14 +74,9 @@ def test_jwt_authentication(self):
""" Verify the endpoint supports JWT authentication and user creation. """
username = 'some-user'
email = '[email protected]'
payload = {
'administrator': True,
'username': username,
'email': email,
'iss': settings.JWT_AUTH['JWT_ISSUERS'][0]['ISSUER']
}
auth_header = "JWT {token}".format(
token=jwt.encode(payload, settings.JWT_AUTH['JWT_SECRET_KEY']).decode('utf-8'))
is_staff = True

auth_header = f'JWT {self.generate_new_user_token(username, email, is_staff)}'
self.assertFalse(User.objects.filter(username=username).exists())

response = self.client.get(
Expand Down
6 changes: 3 additions & 3 deletions ecommerce/extensions/api/v2/tests/views/test_refunds.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
from ecommerce.extensions.refund.tests.factories import RefundFactory, RefundLineFactory
from ecommerce.extensions.refund.tests.mixins import RefundTestMixin
from ecommerce.extensions.test.factories import create_order
from ecommerce.tests.mixins import JwtMixin, ThrottlingMixin
from ecommerce.tests.mixins import ThrottlingMixin
from ecommerce.tests.testcases import TestCase

Option = get_model('catalogue', 'Option')
Refund = get_model('refund', 'Refund')


class RefundCreateViewTests(RefundTestMixin, AccessTokenMixin, JwtMixin, TestCase):
class RefundCreateViewTests(RefundTestMixin, AccessTokenMixin, TestCase):
MODEL_LOGGER_NAME = 'ecommerce.core.models'
path = reverse('api:v2:refunds:create')

Expand Down Expand Up @@ -104,7 +104,7 @@ def test_jwt_authentication(self):
self.client.logout()

data = self._get_data(self.user.username, self.course_id)
auth_header = 'JWT ' + self.generate_token({'username': self.user.username})
auth_header = self.generate_jwt_token_header(self.user)

response = self.client.post(self.path, data, JSON_CONTENT_TYPE, HTTP_AUTHORIZATION=auth_header)
self.assert_ok_response(response)
Expand Down
2 changes: 1 addition & 1 deletion ecommerce/extensions/checkout/tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def test_handle_successful_order_segment_error(self, mock_track):
# ensure that analytics.track was called, but the exception was caught
self.assertTrue(mock_track.called)
# ensure we logged a warning.
self.assertTrue(mock_log_exc.called_with("Failed to emit tracking event upon order placement."))
mock_log_exc.assert_called_with("Failed to emit tracking event upon order completion.")

def test_handle_successful_async_order(self, __):
"""
Expand Down
4 changes: 2 additions & 2 deletions ecommerce/extensions/offer/dynamic_conditional_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import crum
import waffle
from edx_django_utils.monitoring import set_custom_attribute
from edx_rest_framework_extensions.auth.jwt.decoder import configured_jwt_decode_handler
from oscar.core.loading import get_class, get_model

from ecommerce.extensions.api.handlers import jwt_decode_handler
from ecommerce.extensions.offer.constants import DYNAMIC_DISCOUNT_FLAG
from ecommerce.extensions.offer.mixins import (
BenefitWithoutRangeMixin,
Expand Down Expand Up @@ -35,7 +35,7 @@ def get_decoded_jwt_discount_from_request():
return None

set_custom_attribute('ecom_discount_jwt', 'found')
return jwt_decode_handler(discount_jwt)
return configured_jwt_decode_handler(discount_jwt)


def get_percentage_from_request():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DynamicPercentageDiscountBenefitTests(BenefitTestMixin, TestCase):

@override_flag(DYNAMIC_DISCOUNT_FLAG, active=True)
@patch('crum.get_current_request')
@patch('ecommerce.extensions.offer.dynamic_conditional_offer.jwt_decode_handler',
@patch('ecommerce.extensions.offer.dynamic_conditional_offer.configured_jwt_decode_handler',
side_effect=_mock_jwt_decode_handler)
@patch('ecommerce.enterprise.utils.get_decoded_jwt',
side_effect=_mock_get_decoded_jwt)
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_name(self):

@override_flag(DYNAMIC_DISCOUNT_FLAG, active=True)
@patch('crum.get_current_request')
@patch('ecommerce.extensions.offer.dynamic_conditional_offer.jwt_decode_handler',
@patch('ecommerce.extensions.offer.dynamic_conditional_offer.configured_jwt_decode_handler',
side_effect=_mock_jwt_decode_handler)
@ddt.data(
{'discount_applicable': True, 'discount_percent': 15},
Expand Down
Loading