Skip to content

Commit

Permalink
feat: Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
kuipumu committed May 15, 2024
1 parent c00f7ce commit 4bad036
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 105 deletions.
39 changes: 0 additions & 39 deletions openedx_lti_tool_plugin/deep_linking/tests/test_validators.py

This file was deleted.

107 changes: 70 additions & 37 deletions openedx_lti_tool_plugin/deep_linking/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,48 @@
from openedx_lti_tool_plugin.apps import OpenEdxLtiToolPluginConfig as app_config
from openedx_lti_tool_plugin.deep_linking.exceptions import DeepLinkingException
from openedx_lti_tool_plugin.deep_linking.tests import MODULE_PATH
from openedx_lti_tool_plugin.deep_linking.views import DeepLinkingFormView, DeepLinkingView
from openedx_lti_tool_plugin.deep_linking.views import (
DeepLinkingFormView,
DeepLinkingView,
validate_deep_linking_message,
)

MODULE_PATH = f'{MODULE_PATH}.views'


class TestValidateDeepLinkingMessage(TestCase):
"""Test validate_deep_linking_message function."""

def test_with_lti_deep_linking_request_message(self: MagicMock):
"""Test with LtiDeepLinkingRequest message."""
message = MagicMock()
message.is_deep_link_launch.return_value = True

validate_deep_linking_message(message)

message.is_deep_link_launch.assert_called_once_with()

@patch(f'{MODULE_PATH}._', return_value='')
def test_without_deep_linking_request_message(
self: MagicMock,
gettext_mock: MagicMock,
):
"""Test without LtiDeepLinkingRequest message."""
message = MagicMock()
message.is_deep_link_launch.return_value = False

with self.assertRaises(DeepLinkingException) as ctxm:
validate_deep_linking_message(message)

message.is_deep_link_launch.assert_called_once_with()
gettext_mock.assert_called_once_with('Message type is not LtiDeepLinkingRequest.')
self.assertEqual(gettext_mock(), str(ctxm.exception))


@patch.object(DeepLinkingView, 'tool_config', new_callable=PropertyMock)
@patch.object(DeepLinkingView, 'tool_storage', new_callable=PropertyMock)
@patch(f'{MODULE_PATH}.DjangoMessageLaunch')
@patch(f'{MODULE_PATH}.validate_message')
@patch(f'{MODULE_PATH}.validate_deep_linking_message')
@patch(f'{MODULE_PATH}.redirect')
class TestDeepLinkingViewPost(TestCase):
"""Test ResourceLinkLaunchView post method."""
Expand All @@ -33,7 +66,7 @@ def setUp(self):
def test_with_deep_linking_request(
self,
redirect_mock: MagicMock,
validate_message_mock: MagicMock,
validate_deep_linking_message_mock: MagicMock,
message_launch_mock: MagicMock,
tool_storage_mock: MagicMock,
tool_conf_mock: MagicMock,
Expand All @@ -48,7 +81,7 @@ def test_with_deep_linking_request(
tool_conf_mock(),
launch_data_storage=tool_storage_mock(),
)
validate_message_mock.assert_called_once_with(message_launch_mock())
validate_deep_linking_message_mock.assert_called_once_with(message_launch_mock())
message_launch_mock().get_launch_id.assert_called_once_with()
message_launch_mock().get_launch_id().replace.assert_called_once_with('lti1p3-launch-', '')
redirect_mock.assert_called_once_with(
Expand All @@ -57,16 +90,16 @@ def test_with_deep_linking_request(
)

@patch.object(DeepLinkingView, 'http_response_error')
def test_with_lti_exception(
def test_raises_lti_exception(
self,
http_response_error_mock: MagicMock,
redirect_mock: MagicMock,
validate_message_mock: MagicMock,
validate_deep_linking_message_mock: MagicMock,
message_launch_mock: MagicMock,
tool_storage_mock: MagicMock,
tool_conf_mock: MagicMock,
):
"""Test with LtiException."""
"""Test raises LtiException."""
exception = LtiException('Error message')
message_launch_mock.side_effect = exception

Expand All @@ -79,23 +112,23 @@ def test_with_lti_exception(
tool_conf_mock(),
launch_data_storage=tool_storage_mock(),
)
validate_message_mock.assert_not_called()
validate_deep_linking_message_mock.assert_not_called()
redirect_mock.assert_not_called()
http_response_error_mock.assert_called_once_with(exception)

@patch.object(DeepLinkingView, 'http_response_error')
def test_with_deep_linking_exception(
def test_raises_deep_linking_exception(
self,
http_response_error_mock: MagicMock,
redirect_mock: MagicMock,
validate_message_mock: MagicMock,
validate_deep_linking_message_mock: MagicMock,
message_launch_mock: MagicMock,
tool_storage_mock: MagicMock,
tool_conf_mock: MagicMock,
):
"""Test with DeepLinkingException."""
"""Test raises DeepLinkingException."""
exception = DeepLinkingException('Error message')
validate_message_mock.side_effect = exception
validate_deep_linking_message_mock.side_effect = exception

self.assertEqual(
self.view_class.as_view()(self.request),
Expand All @@ -106,13 +139,13 @@ def test_with_deep_linking_exception(
tool_conf_mock(),
launch_data_storage=tool_storage_mock(),
)
validate_message_mock.assert_called_once_with(message_launch_mock())
validate_deep_linking_message_mock.assert_called_once_with(message_launch_mock())
redirect_mock.assert_not_called()
http_response_error_mock.assert_called_once_with(exception)


@patch.object(DeepLinkingFormView, 'get_message_from_cache')
@patch(f'{MODULE_PATH}.validate_message')
@patch(f'{MODULE_PATH}.validate_deep_linking_message')
class TestDeepLinkingFormViewGet(TestCase):
"""Test DeepLinkingFormView get method."""

Expand All @@ -129,7 +162,7 @@ def setUp(self):
def test_with_cached_launch_id(
self,
http_response_mock: MagicMock,
validate_message_mock: MagicMock,
validate_deep_linking_message_mock: MagicMock,
get_message_from_cache_mock: MagicMock,
):
"""Test with the launch_id of a cached DjangoMessageLaunch object."""
Expand All @@ -138,17 +171,17 @@ def test_with_cached_launch_id(
http_response_mock.return_value,
)
get_message_from_cache_mock.assert_called_once_with(self.request, self.launch_id)
validate_message_mock.assert_called_once_with(get_message_from_cache_mock())
validate_deep_linking_message_mock.assert_called_once_with(get_message_from_cache_mock())
http_response_mock.assert_called_once_with()

@patch.object(DeepLinkingFormView, 'http_response_error')
def test_with_lti_exception(
def test_raises_lti_exception(
self,
http_response_error_mock: MagicMock,
validate_message_mock: MagicMock,
validate_deep_linking_message_mock: MagicMock,
get_message_from_cache_mock: MagicMock,
):
"""Test with LtiException."""
"""Test raises LtiException."""
exception = LtiException('Error message')
get_message_from_cache_mock.side_effect = exception

Expand All @@ -157,31 +190,31 @@ def test_with_lti_exception(
http_response_error_mock.return_value,
)
get_message_from_cache_mock.assert_called_once_with(self.request, self.launch_id)
validate_message_mock.assert_not_called()
validate_deep_linking_message_mock.assert_not_called()
http_response_error_mock.assert_called_once_with(exception)

@patch.object(DeepLinkingFormView, 'http_response_error')
def test_with_deep_linking_exception(
def test_raises_deep_linking_exception(
self,
http_response_error_mock: MagicMock,
validate_message_mock: MagicMock,
validate_deep_linking_message_mock: MagicMock,
get_message_from_cache_mock: MagicMock,
):
"""Test with DeepLinkingException."""
"""Test raises DeepLinkingException."""
exception = DeepLinkingException('Error message')
validate_message_mock.side_effect = exception
validate_deep_linking_message_mock.side_effect = exception

self.assertEqual(
self.view_class.as_view()(self.request, self.launch_id),
http_response_error_mock.return_value,
)
get_message_from_cache_mock.assert_called_once_with(self.request, self.launch_id)
validate_message_mock.assert_called_once_with(get_message_from_cache_mock())
validate_deep_linking_message_mock.assert_called_once_with(get_message_from_cache_mock())
http_response_error_mock.assert_called_once_with(exception)


@patch.object(DeepLinkingFormView, 'get_message_from_cache')
@patch(f'{MODULE_PATH}.validate_message')
@patch(f'{MODULE_PATH}.validate_deep_linking_message')
class TestDeepLinkingFormViewPost(TestCase):
"""Test DeepLinkingFormView post method."""

Expand All @@ -198,7 +231,7 @@ def setUp(self):
def test_with_cached_launch_id(
self,
http_response_mock: MagicMock,
validate_message_mock: MagicMock,
validate_deep_linking_message_mock: MagicMock,
get_message_from_cache_mock: MagicMock,
):
"""Test with the launch_id of a cached DjangoMessageLaunch object."""
Expand All @@ -207,17 +240,17 @@ def test_with_cached_launch_id(
http_response_mock.return_value,
)
get_message_from_cache_mock.assert_called_once_with(self.request, self.launch_id)
validate_message_mock.assert_called_once_with(get_message_from_cache_mock())
validate_deep_linking_message_mock.assert_called_once_with(get_message_from_cache_mock())
http_response_mock.assert_called_once_with()

@patch.object(DeepLinkingFormView, 'http_response_error')
def test_with_lti_exception(
def test_raises_lti_exception(
self,
http_response_error_mock: MagicMock,
validate_message_mock: MagicMock,
validate_deep_linking_message_mock: MagicMock,
get_message_from_cache_mock: MagicMock,
):
"""Test with LtiException."""
"""Test raises LtiException."""
exception = LtiException('Error message')
get_message_from_cache_mock.side_effect = exception

Expand All @@ -226,26 +259,26 @@ def test_with_lti_exception(
http_response_error_mock.return_value,
)
get_message_from_cache_mock.assert_called_once_with(self.request, self.launch_id)
validate_message_mock.assert_not_called()
validate_deep_linking_message_mock.assert_not_called()
http_response_error_mock.assert_called_once_with(exception)

@patch.object(DeepLinkingFormView, 'http_response_error')
def test_with_deep_linking_exception(
def test_raises_deep_linking_exception(
self,
http_response_error_mock: MagicMock,
validate_message_mock: MagicMock,
validate_deep_linking_message_mock: MagicMock,
get_message_from_cache_mock: MagicMock,
):
"""Test with DeepLinkingException."""
"""Test raises DeepLinkingException."""
exception = DeepLinkingException('Error message')
validate_message_mock.side_effect = exception
validate_deep_linking_message_mock.side_effect = exception

self.assertEqual(
self.view_class.as_view()(self.request, self.launch_id),
http_response_error_mock.return_value,
)
get_message_from_cache_mock.assert_called_once_with(self.request, self.launch_id)
validate_message_mock.assert_called_once_with(get_message_from_cache_mock())
validate_deep_linking_message_mock.assert_called_once_with(get_message_from_cache_mock())
http_response_error_mock.assert_called_once_with(exception)


Expand Down
25 changes: 0 additions & 25 deletions openedx_lti_tool_plugin/deep_linking/validators.py

This file was deleted.

Loading

0 comments on commit 4bad036

Please sign in to comment.