Skip to content

Commit

Permalink
Functional test for the DeepLinking fields API
Browse files Browse the repository at this point in the history
Just covering the LTI1.1 version on this commit.
  • Loading branch information
marcospri committed Dec 16, 2024
1 parent 819a8f4 commit b2a92df
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lms/validation/authentication/_bearer_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class BearerTokenSchema(PyramidRequestSchema):

def __init__(self, request):
super().__init__(request)
self._jwt_service = request.find_service(iface=JWTService)
self._jwt_service: JWTService = request.find_service(iface=JWTService)
self._lti_user_service = request.find_service(iface=LTIUserService)
self._secret = request.registry.settings["jwt_secret"]

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"jwt_secret": "test_secret",
"google_client_id": "fake_client_id",
"google_developer_key": "fake_developer_key",
"lms_secret": "TEST_LMS_SECRET",
"lms_secret": "test_secret",
"aes_secret": b"TSeQ7E3dzbHgu5ydX2xCrKJiXTmfJbOe",
"jinja2.filters": {
"static_path": "pyramid_jinja2.filters:static_path_filter",
Expand Down
8 changes: 7 additions & 1 deletion tests/factories/lti_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
from tests.factories.application_instance import ApplicationInstance
from tests.factories.attributes import TOOL_CONSUMER_INSTANCE_GUID, USER_ID

_LTI = make_factory(
LTI,
course_id=Faker("hexify", text="^" * 40),
product_family="UNKNOWN",
)

LTIUser = make_factory(
LTIUser,
user_id=USER_ID,
Expand All @@ -13,6 +19,6 @@
effective_lti_roles=[],
tool_consumer_instance_guid=TOOL_CONSUMER_INSTANCE_GUID,
display_name=Faker("name"),
lti=LTI(course_id=Faker("hexify", text="^" * 40), product_family="UNKNOWN"),
lti=SubFactory(_LTI),
application_instance=SubFactory(ApplicationInstance),
)
69 changes: 69 additions & 0 deletions tests/functional/views/lti/deep_linking_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import json
import time
from datetime import timedelta

import oauthlib.common
import oauthlib.oauth1
import pytest
from h_matchers import Any

from lms.resources._js_config import JSConfig
from lms.services.jwt import JWTService
from lms.services.lti_user import LTIUserService
from tests import factories
from tests.conftest import TEST_SETTINGS


class TestDeepLinkingLaunch:
Expand Down Expand Up @@ -68,6 +74,69 @@ def test_basic_lti_launch_canvas_deep_linking_url(
}


class TestDeepLinkingFieldsViews:
def test_file_picker_to_form_fields_v11(
self, app, authorization_param, application_instance
):
response = app.post_json(
"/lti/1.1/deep_linking/form_fields",
params={
"content_item_return_url": "https://apps.imsglobal.org/lti/cert/tp/tp_return.php/basic-lti-launch-request",
"content": {"type": "url", "url": "https://example.com"},
},
headers={"Authorization": f"Bearer {authorization_param}"},
status=200,
)

response_json = response.json
content_items = response_json.pop(
"content_items"
) # We'll assert this separately
assert response_json == {
"oauth_version": "1.0",
"oauth_nonce": Any.string(),
"oauth_timestamp": Any.string(),
"oauth_consumer_key": application_instance.consumer_key,
"oauth_signature_method": "HMAC-SHA1",
"lti_message_type": "ContentItemSelection",
"lti_version": "LTI-1p0",
"oauth_signature": Any.string(),
}
assert json.loads(content_items) == {
"@context": "http://purl.imsglobal.org/ctx/lti/v1/ContentItem",
"@graph": [
{
"@type": "LtiLinkItem",
"mediaType": "application/vnd.ims.lti.v1.ltilink",
"url": "http://localhost/lti_launches",
"custom": {
"deep_linking_uuid": Any.string(),
"url": "https://example.com",
},
}
],
}

@pytest.fixture
def lti_user(self, application_instance, lti_params):
return factories.LTIUser(
application_instance_id=application_instance.id,
application_instance=application_instance,
user_id=lti_params["user_id"],
roles=lti_params["roles"],
)

@pytest.fixture
def authorization_param(self, lti_user):
return JWTService.encode_with_secret(
LTIUserService(
lti_role_service=None, application_instance_service=None
).serialize(lti_user),
secret=TEST_SETTINGS["lms_secret"],
lifetime=timedelta(days=1),
)


@pytest.fixture
def lti_params(application_instance, sign_lti_params):
params = {
Expand Down

0 comments on commit b2a92df

Please sign in to comment.