forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from nelc/and/backport_blockstore
test: blockstore_api backport
- Loading branch information
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters