Skip to content

Commit

Permalink
chore: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
leangseu-edx committed Sep 5, 2023
1 parent 5dd4b33 commit da8d840
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 52 deletions.
5 changes: 2 additions & 3 deletions openassessment/xblock/load_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import json
import logging
from urllib.parse import urlparse

from pkg_resources import resource_string
from django.conf import settings
Expand Down Expand Up @@ -53,8 +52,8 @@ def get_url(key):
get url from key
"""
LoadStatic.reload_manifest()
url = LoadStatic._manifest[key] if key in LoadStatic._manifest else key
if LoadStatic._is_dev_server:
url = LoadStatic._manifest.get(key, key)
if LoadStatic.get_is_dev_server():
return url
return urljoin(settings.STATIC_URL, 'dist', url)

Expand Down
64 changes: 15 additions & 49 deletions openassessment/xblock/test/test_load_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

class TestLoadStatic(TestCase):
"""Test load static class"""
default_base_url = '/static/dist/'

def setUp(self):
LoadStatic._manifest = {} # pylint: disable=protected-access
LoadStatic._is_loaded = False # pylint: disable=protected-access
LoadStatic._base_url = '' # pylint: disable=protected-access
LoadStatic._is_dev_server = False # pylint: disable=protected-access
return super().setUp()

def test_urljoin(self):
Expand Down Expand Up @@ -45,60 +44,27 @@ def test_urljoin(self):

def test_get_url_default(self):
key_url = 'some_url.js'
self.assertEqual(LoadStatic.get_url(key_url),
urljoin(self.default_base_url, key_url))
self.assertEqual(LoadStatic.get_url(key_url), urljoin('/static', 'dist', key_url))

def test_get_url_absolute_base_url(self):
# Given my base_url is an absolute URL and is overridden (as in hot-reload
# local dev setup)
@override_settings(STATIC_URL='/cms')
def test_get_url_with_custom_static_url(self):
key_url = 'some_url.js'
absolute_base_url = 'foo://bar'
loaded_key_url = '/some_url.hashchunk.js'
self.assertEqual(LoadStatic.get_url(key_url), urljoin('/cms', 'dist', key_url))

# When I get a static URL
with patch('json.loads') as jsondata:
jsondata.return_value = {
'base_url': absolute_base_url,
'some_url.js': loaded_key_url,
}
# Then I use the base URL instead of default LMS URL
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
absolute_base_url, loaded_key_url))

@patch('pkg_resources.resource_string')
def test_get_url_file_not_found(self, resource_string):
key_url = 'some_url.js'
resource_string.side_effect = IOError()
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
self.default_base_url, 'some_url.js'))

@override_settings(LMS_ROOT_URL='localhost/')
@patch('pkg_resources.resource_string')
def test_get_url_file_not_found_with_root_url(self, resource_string):
key_url = 'some_url.js'
resource_string.side_effect = IOError()
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
'localhost/', self.default_base_url, 'some_url.js'))

@patch('pkg_resources.resource_string')
def test_get_url_with_manifest(self, resource_string):
def test_is_dev_server_url(self, resource_string):
resource_string.return_value = None
key_url = 'some_url.js'
with patch('json.loads') as jsondata:
jsondata.return_value = {
'some_url.js': 'some_url.hashchunk.js'
}
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
self.default_base_url, 'some_url.hashchunk.js'))
jsondata.return_value = dict({
'some_url.js': 'some_url.hash.js',
'is_dev_server': True
})
self.assertEqual(LoadStatic.get_is_dev_server(), True)
self.assertEqual(LoadStatic.get_url(key_url), 'some_url.hash.js')

@override_settings(LMS_ROOT_URL='localhost/')
@patch('pkg_resources.resource_string')
def test_get_url_with_manifest_and_root_url(self, resource_string):
resource_string.return_value = None
def test_get_url_file_not_found(self, resource_string):
key_url = 'some_url.js'
with patch('json.loads') as jsondata:
jsondata.return_value = {
'some_url.js': 'some_url.hashchunk.js'
}
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
'localhost/', self.default_base_url, 'some_url.hashchunk.js'))
resource_string.side_effect = IOError()
self.assertEqual(LoadStatic.get_url(key_url), urljoin('/static', 'dist', key_url))

0 comments on commit da8d840

Please sign in to comment.