Karanjin Market

In Business Insights
November 19, 2025

assert response.status_code == 200

@pytest.mark.parametrize(
“client_type,json_session”,
[
(“test”, LTI_LAUNCH_PRESENTATION_CUSTOM),
(“test”, LTI_LAUNCH_STUDENT_PRESENTATION_SESSION),
],
)
def test_lti_assignment_configure_with_json_and_custom_param_success(
client_type, json_session, request, lti_launch_url, do_lti_post
):
“””
Test LTI assignment configure endpoint for valid requests.

This test covers the scenario where a valid LTI launch request is made to
the assignment configure endpoint. It verifies that the endpoint correctly
processes the request and returns a successful response.

:param client_type: The type of client (e.g., ‘test’)
:param json_session: The JSON session data for the LTI launch
:param request: Pytest request fixture for accessing parameters
:param lti_launch_url: The URL for LTI launch requests
:param do_lti_post: Function to perform LTI POST request
“””
client = request.getfixturevalue(client_type)

# Setup: Mock Canvas API calls for valid assignment
with patch(“h.services.grouping.GroupingService.find_by”) as mock_find_group:
# Mock no existing grouping found
mock_find_group.return_value = None

# Ensure a user is created for the test
with patch(“lti.services.lti_h_user.LTIHUserService”) as mock_user_service:
with patch(“lti.services.assignment.AssignmentService”) as mock_assignment:
mock_user_service.ensure_matches.return_value = MagicMock()
mock_assignment.upsert_assignment.return_value = MagicMock()
response = do_lti_post(
client,
lti_launch_url,
json_session,
)
# Then: Response status should be 200 OK
# Previously was 404: Not Found
assert response.status_code == 200

@pytest.mark.parametrize(
“client_type,json_session”,
[
(“test”, LTI_LAUNCH_ASSIGNMENT_SELECTION),
(“test”, LTI_LAUNCH_ASSIGNMENT_SELECTION_NO_PARAMS),
],
)
def test_lti_assignment_configure_with_assignment_selection_success(
client_type, json_session, request, lti_launch_url, do_lti_post
):
“””
Test LTI assignment configure endpoint for assignment selection.

This test verifies that the assignment configure endpoint handles
assignment selection requests correctly, including cases with and
without parameters.

:param client_type: The type of client (e.g., ‘test’)
:param json_session: The JSON session data for the assignment selection
:param request: Pytest request fixture for accessing parameters
:param lti_launch_url: The URL for LTI launch requests
:param do_lti_post: Function to perform LTI POST request
“””
client = request.getfixturevalue(client_type)

# Setup: Mock Canvas API calls for valid assignment
with patch(“h.services.grouping.GroupingService.find_by”) as mock_find_group:
# Mock no existing grouping found
mock_find_group.return_value = None

# Ensure a user is created for the test
with patch(“lti.services.lti_h_user.LTIHUserService”) as mock_user_service:
with patch(“lti.services.assignment.AssignmentService”) as mock_assignment:
# Mock the user service to return a user
mock_user_service.ensure_matches.return_value = MagicMock()
# Mock the assignment service to return an assignment
mock_assignment.upsert_assignment.return_value = MagicMock()
response = do_lti_post(
client,
lti_launch_url,
json_session,
)
# Then: Response status should be 200 OK
# Previously was 404: Not Found
assert response.status_code == 200

@pytest.mark.parametrize(
“client_type,json_session”,
[
(“test”, LTI_LAUNCH_ASSIGNMENT_SELECTION),
(“test”, LTI_LAUNCH_ASSIGNMENT_SELECTION_NO_PARAMS),
],
)
def test_lti_assignment_configure_with_assignment_selection_missing_params_fails(
client_type, json_session, request, lti_launch_url, do_lti_post
):
“””
Test LTI assignment configure endpoint for assignment selection with missing params.

This test verifies that the assignment configure endpoint correctly handles
assignment selection requests when required parameters are missing.

:param client_type: The type of client (e.g., ‘test’)
:param json_session: The JSON session data for the assignment selection
:param request: Pytest request fixture for accessing parameters
:param lti_launch_url: The URL for LTI launch requests
:param do_lti_post: Function to perform LTI POST request
“””
client = request.getfixturevalue(client_type)

# Setup: Mock Canvas API calls for valid assignment
with patch(“h.services.grouping.GroupingService.find_by”) as mock_find_group:
# Mock no existing grouping found
mock_find_group.return_value = None

# Ensure a user is created for the test
with patch(“lti.services.lti_h_user.LTIHUserService”) as mock_user_service:
with patch(“lti.services.assignment.AssignmentService”) as mock_assignment:
# Mock the user service to return a user
mock_user_service.ensure_matches.return_value = MagicMock()
# Mock the assignment service to return an assignment
mock_assignment.upsert_assignment.return_value = MagicMock()
response = do_lti_post(
client,
lti_launch_url,
json_session,
)
# Then: Response status should be 200 OK
# Previously was 404: Not Found
assert response.status_code == 200

def test_lti_assignment_configure_with_invalid_json_fails(
test, lti_launch_url, do_lti_post
):
“””
Test LTI assignment configure endpoint with invalid JSON.

This test verifies that the assignment configure endpoint correctly handles
requests with invalid JSON data by returning an appropriate error response.

:param test: The test client fixture
:param lti_launch_url: The URL for LTI launch requests
:param do_lti_post: Function to perform LTI POST request
“””
# Given: Invalid JSON data for LTI launch
invalid_json = ‘{“invalid”: “json”,}’

# When: Making an LTI POST request with invalid JSON
response = do_lti_post(
test,
lti_launch_url,
invalid_json,
)

# Then: Response status should be 400 Bad Request
assert response.status_code == 400

def test_lti_assignment_configure_with_missing_params_fails(
test, lti_launch_url, do_lti_post
):
“””
Test LTI assignment configure endpoint with missing required parameters.

This test verifies that the assignment configure endpoint correctly handles
requests where required LTI parameters are missing.

:param test: The test client fixture
:param lti_launch_url: The URL for LTI launch requests
:param do_lti_post: Function to perform LTI POST request
“””
# Given: LTI launch data with missing required parameters
missing_params_json = ‘{“context_id”: “test_context”}’

# When: Making an LTI POST request with missing parameters
response = do_lti_post(
test,
lti_launch_url,
missing_params_json,
)

# Then: Response status should be 400 Bad Request
assert response.status_code == 400

def test_lti_assignment_configure_with_invalid_signature_fails(
test, lti_launch_url, do_lti_post
):
“””
Test LTI assignment configure endpoint with invalid signature.

This test verifies that the assignment configure endpoint correctly rejects
requests with invalid OAuth signatures.

:param test: The test client fixture
:param lti_launch_url: The URL for LTI launch requests
:param do_lti_post: Function to perform LTI POST request
“””
# Given: LTI launch data with an invalid signature
invalid_signature_json = LTI_LAUNCH_PRESENTATION_CUSTOM.copy()
invalid_signature_json[“oauth_signature”] = “invalid_signature”

# When: Making an LTI POST request with invalid signature
response = do_lti_post(
test,
lti_launch_url,
invalid_signature_json,
)

# Then: Response status should be 401 Unauthorized
assert response.status_code == 401

@pytest.mark.parametrize(
“client_type,json_session”,
[
(“test”, LTI_LAUNCH_PRESENTATION_CUSTOM),
],
)
def test_get_lti_assignment_configure_returns_method_not_allowed(
client_type, json_session, request, lti_launch_url
):
“””
Test that GET requests to LTI assignment configure endpoint are not allowed.

This test verifies that the assignment configure endpoint correctly
returns a 405 Method Not Allowed response for GET requests.

:param client_type: The type of client (e.g., ‘test’)
:param json_session: The JSON session data for the LTI launch
:param request: Pytest request fixture for accessing parameters
:param lti_launch_url: The URL for LTI launch requests
“””
client = request.getfixturevalue(client_type)

# When: Making a GET request to the LTI launch URL
response = client.get(lti_launch_url)

# Then: Response status should be 405 Method Not Allowed
assert response.status_code == 405

def test_lti_assignment_configure_with_expired_timestamp_fails(
test, lti_launch_url, do_lti_post
):
“””
Test LTI assignment configure endpoint with expired timestamp.

This test verifies that the assignment configure endpoint correctly
rejects requests with expired OAuth timestamps.

:param test: The test client fixture
:param lti_launch_url: The URL for LTI launch requests
:param do_lti_post: Function to perform LTI POST request
“””
# Given: LTI launch data with an expired timestamp
expired_timestamp_json = LTI_LAUNCH_PRESENTATION_CUSTOM.copy()
expired_timestamp_json[“oauth_timestamp”] = str(int(time.time()) – 3600) # 1 hour ago

# When: Making an LTI POST request with expired timestamp
response = do_lti_post(
test,
lti_launch_url,
expired_timestamp_json,
)

# Then: Response status should be 401 Unauthorized
assert response.status_code == 401

def test_lti_assignment_configure_with_invalid_consumer_key_fails(
test, lti_launch_url, do_lti_post
):
“””
Test LTI assignment configure endpoint with invalid consumer key.

This test verifies that the assignment configure endpoint correctly
rejects requests with invalid OAuth consumer keys.

:param test: The test client fixture
:param lti_launch_url: The URL for LTI launch requests
:param do_lti_post: Function to perform LTI POST request
“””
# Given: LTI launch data with an invalid consumer key
invalid_consumer_json = LTI_LAUNCH_PRESENTATION_CUSTOM.copy()
invalid_consumer_json[“oauth_consumer_key”] = “invalid_consumer_key”

# When: Making an LTI POST request with invalid consumer key
response = do_lti_post(
test,
lti_launch_url,
invalid_consumer_json,
)

# Then: Response status should be 401 Unauthorized
assert response.status_code == 401

@pytest.mark.parametrize(
“client_type,json_session”,
[
(“test”, LTI_LAUNCH_PRESENTATION_CUSTOM),
],
)
def test_lti_assignment_configure_with_different_http_methods_fails(
client_type, json_session, request, lti_launch_url
):
“””
Test LTI assignment configure endpoint with unsupported HTTP methods.

This test verifies that the assignment configure endpoint correctly
returns 405 Method Not Allowed for HTTP methods other than POST.

:param client_type: The type of client (e.g., ‘test’)
:param json_session: The JSON session data for the LTI launch
:param request: Pytest request fixture for accessing parameters
:param lti_launch_url: The URL for LTI launch requests
“””
client = request.getfixturevalue(client_type)

# Test various unsupported HTTP methods
for method in [“PUT”, “PATCH”, “DELETE”, “HEAD”]:
# When: Making an unsupported HTTP request to the LTI launch URL
response = client.open(lti_launch_url, method=method)

# Then: Response status should be 405 Method Not Allowed
assert response.status_code == 405