Skip to content

Commit

Permalink
test: blockstore_api backport
Browse files Browse the repository at this point in the history
Two files backport from openedx#31903
  • Loading branch information
andrey-canon committed May 3, 2024
1 parent 40d7cb5 commit c70b65d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 42 additions & 0 deletions openedx/core/lib/blockstore_api/tests/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Common code for tests that work with Blockstore
"""
from unittest import mock, skipUnless
from urllib.parse import urlparse

from django.conf import settings
from django.test.client import RequestFactory

# Decorators for tests that require the blockstore service/app
requires_blockstore = skipUnless(settings.RUN_BLOCKSTORE_TESTS, "Requires a running Blockstore server")

requires_blockstore_app = skipUnless(settings.BLOCKSTORE_USE_BLOCKSTORE_APP_API, "Requires blockstore app")


class BlockstoreAppTestMixin:
"""
Sets up the environment for tests to be run using the installed Blockstore app.
"""
def setUp(self):
"""
Ensure there's an active request, so that bundle file URLs can be made absolute.
"""
super().setUp()

# Patch the blockstore get_current_request to use our live_server_url
mock.patch('blockstore.apps.api.methods.get_current_request',
mock.Mock(return_value=self._get_current_request())).start()
self.addCleanup(mock.patch.stopall)

def _get_current_request(self):
"""
Returns a request object using the live_server_url, if available.
"""
request_args = {}
if hasattr(self, 'live_server_url'):
live_server_url = urlparse(self.live_server_url)
name, port = live_server_url.netloc.split(':')
request_args['SERVER_NAME'] = name
request_args['SERVER_PORT'] = port or '80'
request_args['wsgi.url_scheme'] = live_server_url.scheme
return RequestFactory().request(**request_args)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.test import TestCase

from openedx.core.lib import blockstore_api as api
from openedx.core.djangoapps.content_libraries.tests.base import (
from openedx.core.lib.blockstore_api.tests.base import (
BlockstoreAppTestMixin,
requires_blockstore,
requires_blockstore_app,
Expand Down

0 comments on commit c70b65d

Please sign in to comment.