Skip to content

Commit

Permalink
✨(lti_toolbox) add LTIMessageType enum
Browse files Browse the repository at this point in the history
Introduced a new enumeration encapsulating all LTI message types,
replacing hardcoded constants for improved code organization
and maintainability.
  • Loading branch information
lebaudantoine committed Aug 30, 2023
1 parent 2a43930 commit b1953dd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).
### Added

- Accept Content-Item selection request used in a deep linking context
- Add an LTIMessageType enum

### Fixed

Expand Down
11 changes: 10 additions & 1 deletion src/lti_toolbox/launch_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@
This is based on lti library (https://github.com/pylti/lti).
"""
from collections.abc import MutableMapping
from enum import Enum
from typing import List, Set, Union
from urllib.parse import urlencode

from .exceptions import InvalidParamException, MissingParamException

DEFAULT_LTI_VERSION = "LTI-1.0"
SELECTION_PARAMS_MESSAGE_TYPE = "ContentItemSelectionRequest"


class LTIMessageType(Enum):
"""Enum describing all type of message received through LTI requests."""

LAUNCH_REQUEST = "basic-lti-launch-request"
SELECTION_REQUEST = "ContentItemSelectionRequest"
SELECTION_RESPONSE = "ContentItemSelection"


LAUNCH_PARAMS_REQUIRED = {"lti_message_type", "lti_version", "resource_link_id"}

Expand Down
11 changes: 3 additions & 8 deletions src/lti_toolbox/lti.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
from oauthlib.oauth1 import SignatureOnlyEndpoint

from .exceptions import LTIException, LTIRequestNotVerifiedException, ParamException
from .launch_params import (
SELECTION_PARAMS_MESSAGE_TYPE,
LaunchParams,
ParamsMixins,
SelectionParams,
)
from .launch_params import LaunchParams, LTIMessageType, ParamsMixins, SelectionParams
from .models import LTIConsumer, LTIPassport
from .validator import LTIRequestValidator

Expand All @@ -37,8 +32,8 @@ def __init__(self, request):
def _process_params(self) -> ParamsMixins:
"""Process LTI parameters based on request type."""
is_selection_request = (
self.request.POST.get("lti_message_type")
== SELECTION_PARAMS_MESSAGE_TYPE
self.request.POST.get("lti_message_type")
== LTIMessageType.SELECTION_REQUEST.value
)
try:
params = (
Expand Down
3 changes: 1 addition & 2 deletions tests/lti_toolbox/test_launch_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ def test_invalid_parameter(self):
)



class SelectionParamTestCase(TestCase):
"""Test the SelectionParam class"""

Expand All @@ -144,7 +143,7 @@ def _selection_params(lti_parameters):
url = "http://testserver/lti/launch"
signed_parameters = sign_parameters(passport, lti_parameters, url)
return SelectionParams(signed_parameters)

def test_only_required_parameters(self):
"""Test validation of a minimalistic LTI Content-Item
Selection request with only required parameters.
Expand Down

0 comments on commit b1953dd

Please sign in to comment.