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

Commit

Permalink
fixup! fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robrap committed Apr 13, 2023
1 parent 54327ee commit 2cb9b63
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
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
23 changes: 22 additions & 1 deletion ecommerce/tests/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
import json
from decimal import Decimal
from unittest.mock import Mock

import responses
from crum import set_current_request
Expand Down Expand Up @@ -102,7 +103,7 @@ def setUp(self):

class JwtMixin:
""" Mixin with JWT-related helper functions. """
JWT_SECRET_KEY = settings.JWT_AUTH['JWT_SECRET_KEY']
JWT_SECRET_KEY = settings.JWT_AUTH['JWT_ISSUERS'][0]['SECRET_KEY']
issuer = settings.JWT_AUTH['JWT_ISSUERS'][0]['ISSUER']

def generate_token(self, payload, secret=None):
Expand All @@ -126,6 +127,26 @@ def set_jwt_cookie(self, system_wide_role=SYSTEM_ENTERPRISE_ADMIN_ROLE, context=

self.client.cookies[jwt_cookie_name()] = jwt_token

def generate_new_user_token(self, username, email, is_staff):
"""
Generates a JWT token for a user with provided attributes.
Note: Doesn't actually create the user object, so it can be created
during JWT authentication.
"""
# create a mock user, and not the actual user, because we want to confirm that
# the user is created during JWT authentication
user = Mock()
user.username = username
user.email = email
user.is_staff = is_staff

payload = generate_unversioned_payload(user)
# At this time, generate_unversioned_payload isn't setting 'administrator', but
# it should.
payload['administrator'] = is_staff
return self.generate_token(payload)


class BasketCreationMixin(UserMixin, JwtMixin):
"""Provides utility methods for creating baskets in test cases."""
Expand Down

0 comments on commit 2cb9b63

Please sign in to comment.