Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic functional test for DeepLinking launches #6918

Merged
merged 2 commits into from
Dec 16, 2024
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
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
4 changes: 3 additions & 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 All @@ -26,6 +26,7 @@
"h_api_url_private": "https://h.example.com/private/api/",
"rpc_allowed_origins": ["http://localhost:5000"],
"oauth2_state_secret": "test_oauth2_state_secret",
"onedrive_client_id": "test_one_drive_client_id",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add missing setting on the tests fixture.

"session_cookie_secret": "notasecret",
"via_secret": "not_a_secret",
"blackboard_api_client_id": "test_blackboard_api_client_id",
Expand All @@ -34,6 +35,7 @@
"disable_key_rotation": False,
"admin_users": [],
"email_preferences_secret": "test_email_preferences_secret",
"youtube_api_key": "test_youtube_api_key",
}


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),
)
74 changes: 57 additions & 17 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import contextlib
import functools
import json
import os
import re
from os import environ
from urllib.parse import urlencode

import httpretty
import oauthlib.oauth1
import pytest
from _pytest.monkeypatch import MonkeyPatch
from h_matchers import Any
Expand All @@ -13,9 +16,10 @@

from lms import db
from lms.app import create_app
from tests import factories
from tests.conftest import TEST_SETTINGS

TEST_SETTINGS["database_url"] = environ["DATABASE_URL"]
TEST_SETTINGS["database_url"] = os.environ["DATABASE_URL"]

TEST_ENVIRONMENT = {
key.upper(): value for key, value in TEST_SETTINGS.items() if isinstance(value, str)
Expand All @@ -25,6 +29,14 @@
)


@pytest.fixture
def environ():
environ = dict(os.environ)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixture to make easier to access the os.environ version that contains all TEST_ENVIROMENT values.

environ.update(TEST_ENVIRONMENT)

return environ


@pytest.fixture(autouse=True)
def clean_database(db_engine):
"""Delete any data added by the previous test."""
Expand Down Expand Up @@ -71,24 +83,52 @@ def db_session(db_engine, db_sessionfactory):
connection.close()


def _lti_v11_launch(app, url, post_params, get_params=None, **kwargs):
if get_params:
url += f"?{urlencode(get_params)}"

return app.post(
url,
params=post_params,
headers={
"Accept": "text/html",
"Content-Type": "application/x-www-form-urlencoded",
},
**kwargs,
)


@pytest.fixture
def do_lti_launch(app):
def do_lti_launch(post_params, get_params=None, **kwargs):
url = "/lti_launches"
if get_params:
url += f"?{urlencode(get_params)}"

return app.post(
url,
params=post_params,
headers={
"Accept": "text/html",
"Content-Type": "application/x-www-form-urlencoded",
},
**kwargs,
)
return functools.partial(_lti_v11_launch, app, "/lti_launches")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor this fixture a bit to be able to reuse it for deep linking launches.



@pytest.fixture
def do_deep_link_launch(app):
return functools.partial(_lti_v11_launch, app, "/content_item_selection")


@pytest.fixture
def get_client_config():
def _get_client_config(response):
return json.loads(response.html.find("script", {"class": "js-config"}).string)

return _get_client_config

return do_lti_launch

@pytest.fixture
def application_instance(db_session): # noqa: ARG001
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Common fixtures between basic launch and deep linking launch moved here.

return factories.ApplicationInstance(
tool_consumer_instance_guid="IMS Testing",
organization=factories.Organization(),
)


@pytest.fixture
def oauth_client(application_instance):
return oauthlib.oauth1.Client(
application_instance.consumer_key, application_instance.shared_secret
)


@pytest.fixture(autouse=True)
Expand Down
36 changes: 10 additions & 26 deletions tests/functional/views/lti/basic_lti_launch_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import time
from urllib.parse import urlencode

Expand All @@ -9,7 +8,6 @@

from lms.models import Assignment
from lms.resources._js_config import JSConfig
from tests import factories


class TestBasicLTILaunch:
Expand All @@ -23,33 +21,35 @@ def test_requests_with_no_oauth_signature_are_forbidden(
assert response.headers["Content-Type"] == Any.string.matching("^text/html")
assert response.html

def test_unconfigured_basic_lti_launch(self, lti_params, do_lti_launch):
def test_unconfigured_basic_lti_launch(
self, lti_params, do_lti_launch, get_client_config
):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few changes to accommodate get_client_config being now a fixture instead of a local function.

response = do_lti_launch(
post_params=lti_params,
status=200,
)

assert self.get_client_config(response)["mode"] == JSConfig.Mode.FILE_PICKER
assert get_client_config(response)["mode"] == JSConfig.Mode.FILE_PICKER

def test_db_configured_basic_lti_launch(
self, lti_params, assignment, do_lti_launch
self, lti_params, assignment, do_lti_launch, get_client_config
):
response = do_lti_launch(post_params=lti_params, status=200)

js_config = self.get_client_config(response)
js_config = get_client_config(response)
assert js_config["mode"] == JSConfig.Mode.BASIC_LTI_LAUNCH
assert urlencode({"url": assignment.document_url}) in js_config["viaUrl"]

def test_basic_lti_launch_canvas_deep_linking_url(
self, do_lti_launch, url_launch_params, db_session
self, do_lti_launch, url_launch_params, db_session, get_client_config
):
get_params, post_params = url_launch_params

response = do_lti_launch(
get_params=get_params, post_params=post_params, status=200
)

js_config = self.get_client_config(response)
js_config = get_client_config(response)
assert js_config["mode"] == JSConfig.Mode.BASIC_LTI_LAUNCH
assert (
urlencode({"url": "https://url-configured.com/document.pdf"})
Expand All @@ -63,15 +63,15 @@ def test_basic_lti_launch_canvas_deep_linking_url(
)

def test_basic_lti_launch_canvas_deep_linking_canvas_file(
self, do_lti_launch, db_session, canvas_file_launch_params
self, do_lti_launch, db_session, canvas_file_launch_params, get_client_config
):
get_params, post_params = canvas_file_launch_params

response = do_lti_launch(
get_params=get_params, post_params=post_params, status=200
)

js_config = self.get_client_config(response)
js_config = get_client_config(response)
assert js_config["mode"] == JSConfig.Mode.BASIC_LTI_LAUNCH
assert (
js_config["api"]["viaUrl"]["path"]
Expand All @@ -84,13 +84,6 @@ def test_basic_lti_launch_canvas_deep_linking_canvas_file(
== 1
)

@pytest.fixture(autouse=True)
def application_instance(self, db_session): # noqa: ARG002
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to conftest.py

return factories.ApplicationInstance(
tool_consumer_instance_guid="IMS Testing",
organization=factories.Organization(),
)

@pytest.fixture
def assignment(self, db_session, application_instance, lti_params):
assignment = Assignment(
Expand All @@ -104,12 +97,6 @@ def assignment(self, db_session, application_instance, lti_params):

return assignment

@pytest.fixture
def oauth_client(self, application_instance):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, moved to conftest.py

return oauthlib.oauth1.Client(
application_instance.consumer_key, application_instance.shared_secret
)

@pytest.fixture
def lti_params(self, application_instance, sign_lti_params):
params = {
Expand Down Expand Up @@ -184,6 +171,3 @@ def _sign(params):
return params

return _sign

def get_client_config(self, response):
return json.loads(response.html.find("script", {"class": "js-config"}).string)
Loading
Loading