Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ntk/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ def _wrapper(self, *func_args, **func_kwargs):
response = func(self, *func_args, **func_kwargs)
error_default = f'{func.__name__.capitalize().replace("_", " ")} of {self.store} failed.'
error_msg = ""
content_type = response.headers.get('content-type', '').lower()
if response.ok and not response_json:
return response
elif response.ok and response.headers.get('content-type') == 'application/json':
elif response.ok and content_type.startswith('application/json'):
return response
elif response.headers.get('content-type') == 'application/json':
elif content_type.startswith('application/json'):
result = response.json()
error_msg = " -> "
for key, value in result.items():
Expand Down
24 changes: 15 additions & 9 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def test_init_command_without_config_file_should_be_required_name_api_key_store_
@patch("ntk.command.Config.write_config", autospec=True)
def test_init_command_with_name_and_configs_should_be_call_create_theme_and_save_config(self, mock_write_config):
self.mock_gateway.return_value.create_theme.return_value.ok = True
self.mock_gateway.return_value.create_theme.return_value.headers = {'content-type': 'application/json'}
self.mock_gateway.return_value.create_theme.return_value.headers = {
'content-type': 'application/json; charset=utf-8'}
self.mock_gateway.return_value.create_theme.return_value.json.return_value = {
'id': 1234,
'name': 'Test Init Theme',
Expand Down Expand Up @@ -89,7 +90,8 @@ def test_list_command_without_config_file_should_be_required_api_key_store(self)
@patch("ntk.command.Config.write_config", autospec=True)
def test_list_command_with_configs_should_be_show_theme_id_and_theme_name(self, mock_write_config):
self.mock_gateway.return_value.get_themes.return_value.ok = True
self.mock_gateway.return_value.get_themes.return_value.headers = {'content-type': 'application/json'}
self.mock_gateway.return_value.get_themes.return_value.headers = {
'content-type': 'application/json; charset=utf-8'}
self.mock_gateway.return_value.get_themes.return_value.json.return_value = {
"count": 2,
"next": None,
Expand Down Expand Up @@ -131,7 +133,8 @@ def test_list_command_with_missing_theme_should_be_raise_message(
self, mock_write_config
):
self.mock_gateway.return_value.get_themes.return_value.ok = True
self.mock_gateway.return_value.get_themes.return_value.headers = {'content-type': 'application/json'}
self.mock_gateway.return_value.get_themes.return_value.headers = {
'content-type': 'application/json; charset=utf-8'}
self.mock_gateway.return_value.get_themes.return_value.json.return_value = {
"count": 0,
"next": None,
Expand Down Expand Up @@ -173,7 +176,8 @@ def test_checkout_command_with_theme_id_and_configs_should_be_download_file_corr
self, mock_write_config, mock_open_file
):
self.mock_gateway.return_value.get_templates.return_value.ok = True
self.mock_gateway.return_value.get_templates.return_value.headers = {'content-type': 'application/json'}
self.mock_gateway.return_value.get_templates.return_value.headers = {
'content-type': 'application/json; charset=utf-8'}
self.mock_gateway.return_value.get_templates.return_value.json.return_value = [
{
"theme": 1234,
Expand Down Expand Up @@ -278,7 +282,8 @@ def test_pull_command_with_configs_and_filenames_should_be_download_only_file_in
self, mock_write_config, mock_open_file
):
self.mock_gateway.return_value.get_template.return_value.ok = True
self.mock_gateway.return_value.get_template.return_value.headers = {'content-type': 'application/json'}
self.mock_gateway.return_value.get_template.return_value.headers = {
'content-type': 'application/json; charset=utf-8'}
self.mock_gateway.return_value.get_template.return_value.json.return_value = {
"theme": 1234,
"name": "assets/image.png",
Expand Down Expand Up @@ -319,10 +324,11 @@ def test_watch_command_should_call_gateway_with_correct_arguments_belong_to_file
self.mock_gateway.return_value.create_or_update_template.return_value.ok = True
self.mock_gateway.return_value.create_or_update_template.return_value.status_code = 200
self.mock_gateway.return_value.create_or_update_template.return_value.headers = {
'content-type': 'application/json'}
'content-type': 'application/json; charset=utf-8'}
self.mock_gateway.return_value.delete_template.return_value.ok = True
self.mock_gateway.return_value.delete_template.return_value.status_code = 204
self.mock_gateway.return_value.delete_template.return_value.headers = {'content-type': 'application/json'}
self.mock_gateway.return_value.delete_template.return_value.headers = {
'content-type': 'application/json; charset=utf-8'}
self.command.config.parser_config(self.parser)
changes = {
(Change.added, './assets/base.html'),
Expand Down Expand Up @@ -357,7 +363,7 @@ def test_watch_command_with_create_image_file_should_call_gateway_with_correct_a
self.command.config.parser_config(self.parser)
self.mock_gateway.return_value.create_or_update_template.return_value.ok = True
self.mock_gateway.return_value.create_or_update_template.return_value.headers = {
'content-type': 'application/json'}
'content-type': 'application/json; charset=utf-8'}
changes = [
(Change.added, './assets/image.jpg'),
]
Expand All @@ -381,7 +387,7 @@ def test_watch_command_with_sass_directory_should_call_compile_sass(
]
self.mock_gateway.return_value.create_or_update_template.return_value.ok = True
self.mock_gateway.return_value.create_or_update_template.return_value.headers = {
'content-type': 'application/json'}
'content-type': 'application/json; charset=utf-8'}

changes = {
(Change.modified, 'sass/theme.scss'),
Expand Down
21 changes: 13 additions & 8 deletions tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_get_themes(self, mock_request):
self.assertEqual(log.output, expected_logging)

# check if call request completed
mock_request.return_value.headers = {'content-type': 'application/json'}
mock_request.return_value.headers = {'content-type': 'application/json; charset=utf-8'}
self.gateway.get_themes()

expected_call = call('GET', 'http://simple.com/api/admin/themes/',
Expand All @@ -124,7 +124,7 @@ def test_create_theme(self, mock_request):

# check if call request completed
mock_request.return_value.ok = True
mock_request.return_value.headers = {'content-type': 'application/json'}
mock_request.return_value.headers = {'content-type': 'application/json; charset=utf-8'}
payload = {
"name": "Test Init Theme"
}
Expand Down Expand Up @@ -153,7 +153,7 @@ def test_get_templates(self, mock_request):

# check if call request completed
mock_request.return_value.ok = True
mock_request.return_value.headers = {'content-type': 'application/json'}
mock_request.return_value.headers = {'content-type': 'application/json; charset=utf-8'}

self.gateway.get_templates(theme_id=6)

Expand All @@ -170,16 +170,18 @@ def test_get_template(self, mock_request):
# check if call request failed
with self.assertLogs(level='INFO') as log:
mock_request.return_value.ok = False
mock_request.return_value.json.return_value = {
'detail': 'You do not have permission to perform this action.'}
self.gateway.get_template(theme_id=6, template_name=template_name)

expected_logging = [
'INFO:root:Downloading assets/custom.css file from theme id #6 failed.'
'INFO:root:Downloading assets/custom.css file from theme id #6 failed. -> You do not have permission to perform this action.' # noqa
]
self.assertEqual(log.output, expected_logging)

# check if call request completed
mock_request.return_value.ok = True
mock_request.return_value.headers = {'content-type': 'application/json'}
mock_request.return_value.headers = {'content-type': 'application/json; charset=utf-8'}

self.gateway.get_template(theme_id=6, template_name=template_name)

Expand All @@ -195,16 +197,19 @@ def test_create_or_update_template(self, mock_request):
# check if call request failed
with self.assertLogs(level='INFO') as log:
mock_request.return_value.ok = False
mock_request.return_value.json.return_value = {
'detail': 'You do not have permission to perform this action.'}
self.gateway.create_or_update_template(theme_id=6, template_name='asset/custom.css')

expected_logging = [
'INFO:root:Uploading asset/custom.css file to theme id #6 failed.'
'INFO:root:Uploading asset/custom.css file to theme id #6 failed. -> You do not have permission to perform this action.' # noqa
]
print(log.output)
self.assertEqual(log.output, expected_logging)

# check if call request completed
mock_request.return_value.ok = True
mock_request.return_value.headers = {'content-type': 'application/json'}
mock_request.return_value.headers = {'content-type': 'application/json; charset=utf-8'}
payload = {
'name': 'assets/base.html',
'content': '{% load i18n %}\n\n<div class="mt-2">My home page</div>'
Expand All @@ -223,7 +228,7 @@ def test_create_or_update_template(self, mock_request):
#####
@patch('ntk.gateway.requests.request', autospec=True)
def test_delete_template(self, mock_request):
mock_request.return_value.headers = {'content-type': 'application/json'}
mock_request.return_value.headers = {'content-type': 'application/json; charset=utf-8'}
# check if call request failed
with self.assertLogs(level='INFO') as log:
mock_request.return_value.ok = False
Expand Down