Skip to content

Commit

Permalink
Merge pull request #24 from edx/saleem-latif/ENT-1783-cleanup
Browse files Browse the repository at this point in the history
Removed get_request_or_stub and get_decoded_jwt_from_request from utils.py
  • Loading branch information
saleem-latif authored May 22, 2019
2 parents 0af7b25 + 48ecde6 commit 698a0e0
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 112 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Change Log

.. There should always be an "Unreleased" section for changes pending release.
[1.0.0] - 2019-05-20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* Removed `get_request_or_stub` and `get_decoded_jwt_from_request` from utils.py

[0.2.1] - 2019-05-08
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion edx_rbac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

from __future__ import absolute_import, unicode_literals

__version__ = '0.2.1'
__version__ = '1.0.0'

default_app_config = 'edx_rbac.apps.EdxRbacConfig' # pylint: disable=invalid-name
49 changes: 0 additions & 49 deletions edx_rbac/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,12 @@

import importlib

import crum
from django.apps import apps
from django.conf import settings
from django.test.client import RequestFactory
from edx_rest_framework_extensions.auth.jwt.cookies import jwt_cookie_name
from edx_rest_framework_extensions.auth.jwt.decoder import jwt_decode_handler
from six.moves.urllib.parse import urlparse # pylint: disable=import-error

ALL_ACCESS_CONTEXT = '*'


# Taken from edx-platform
def get_request_or_stub():
"""
Return the current request or a stub request.
If called outside the context of a request, construct a fake
request that can be used to build an absolute URI.
This is useful in cases where we need to pass in a request object
but don't have an active request (for example, in tests, celery tasks, and XBlocks).
"""
request = crum.get_current_request()

if request is None:

# The settings SITE_NAME may contain a port number, so we need to
# parse the full URL.
full_url = "http://{site_name}".format(site_name=settings.SITE_NAME)
parsed_url = urlparse(full_url)

# Construct the fake request. This can be used to construct absolute
# URIs to other paths.
return RequestFactory(
SERVER_NAME=parsed_url.hostname,
SERVER_PORT=parsed_url.port or 80,
).get("/")

else:
return request


def get_decoded_jwt_from_request(request):
"""
Grab jwt from request if possible.
Returns a decoded jwt dict if it finds it.
Returns a None if it does not.
"""
jwt_cookie = request.COOKIES.get(jwt_cookie_name(), None) or getattr(request, 'auth', None)

if not jwt_cookie:
return None
return jwt_decode_handler(jwt_cookie)


def request_user_has_implicit_access_via_jwt(decoded_jwt, role_name, context=None):
"""
Check the request's user access by mapping user's roles found in jwt to local feature roles.
Expand Down
62 changes: 0 additions & 62 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
"""
from __future__ import absolute_import, unicode_literals

from django.conf import settings
from django.contrib.auth.models import User
from django.test import RequestFactory, TestCase
from edx_rest_framework_extensions.auth.jwt.cookies import jwt_cookie_name
# edx_rest_framework_extensions test utils should change when the package
# does. Given edx_rbac is tightly coupled to edx_rest_framework_extensions,
# using those utils seems reasonable in the way of not repeating ourselves
from edx_rest_framework_extensions.auth.jwt.tests.utils import generate_jwt_token, generate_unversioned_payload
from mock import patch

from edx_rbac.utils import (
ALL_ACCESS_CONTEXT,
create_role_auth_claim_for_user,
get_decoded_jwt_from_request,
get_request_or_stub,
request_user_has_implicit_access_via_jwt,
user_has_access_via_database
)
Expand All @@ -36,60 +28,6 @@ def setUp(self):
self.user = User.objects.create(username='test_user', password='pw')
self.request.user = self.user

def test_get_request_or_stub(self):
"""
Outside the context of the request, we should still get a request
that allows us to build an absolute URI.
"""
stub = get_request_or_stub()
expected_url = "http://{site_name}/foobar".format(site_name=settings.SITE_NAME)
self.assertEqual(stub.build_absolute_uri("foobar"), expected_url)

@patch('edx_rbac.utils.jwt_decode_handler')
def test_get_decoded_jwt_from_request(self, mock_decoder):
"""
A decoded jwt should be returned from request if it exists
"""
payload = generate_unversioned_payload(self.request.user)
payload.update({
"roles": [
"some_new_role_name:some_context"
]
})
jwt_token = generate_jwt_token(payload)

self.request.COOKIES[jwt_cookie_name()] = jwt_token
get_decoded_jwt_from_request(self.request)

mock_decoder.assert_called_once()

@patch('edx_rbac.utils.jwt_decode_handler')
def test_get_decoded_jwt_from_request_from_auth_attr(self, mock_decoder):
"""
A dcoded jwt should be returned from the request auth if it is not set on the cookie.
"""
payload = generate_unversioned_payload(self.request.user)
payload.update({
"roles": [
"some_new_role_name:some_context"
]
})
jwt_token = generate_jwt_token(payload)
self.request.auth = jwt_token
get_decoded_jwt_from_request(self.request)

mock_decoder.assert_called_once()

@patch('edx_rbac.utils.jwt_decode_handler')
def test_get_decoded_jwt_from_request_no_jwt_in_request(self, mock_decoder):
"""
None should be returned if the request has no jwt
"""
result = get_decoded_jwt_from_request(self.request)

assert result is None
mock_decoder.assert_not_called()

# Check out test_settings for the variable declaration
def test_request_user_has_implicit_access_via_jwt(self):
"""
Expand Down

0 comments on commit 698a0e0

Please sign in to comment.