Fix: Create SearchAccess on library creation for course creator search access (#38091)
* fix: create SearchAccess on library creation for course creator search access When a course creator creates a new library, the SearchAccess record must exist immediately so their JWT token can include the library's access_id. Without this, course creators cannot see newly added components in search results until the page is refreshed. This issue doesn't affect superusers who bypass access_id filtering. * test: verify SearchAccess is created automatically on library creation --------- Co-authored-by: Usama Sadiq <usama7274@gmail.com>
This commit is contained in:
@@ -192,7 +192,7 @@ def library_block_deleted(**kwargs) -> None:
|
||||
@only_if_meilisearch_enabled
|
||||
def content_library_created_handler(**kwargs) -> None:
|
||||
"""
|
||||
Create the index for the content library
|
||||
Create the index and SearchAccess for the content library
|
||||
"""
|
||||
content_library_data = kwargs.get("content_library", None)
|
||||
if not content_library_data or not isinstance(content_library_data, ContentLibraryData): # pragma: no cover
|
||||
@@ -200,6 +200,10 @@ def content_library_created_handler(**kwargs) -> None:
|
||||
return
|
||||
library_key = content_library_data.library_key
|
||||
|
||||
# Create SearchAccess record immediately so course creators can search this library
|
||||
# right after creation. Without this, the JWT token won't include the new library's
|
||||
# access_id until it's added by the document indexing process or the page is refreshed.
|
||||
SearchAccess.objects.get_or_create(context_key=library_key)
|
||||
update_content_library_index_docs.apply(args=[str(library_key), True])
|
||||
|
||||
|
||||
|
||||
@@ -138,6 +138,23 @@ class TestUpdateIndexHandlers(ImmediateOnCommitMixin, ModuleStoreTestCase, LiveS
|
||||
"block-v1orgatest_coursetest_runtypeverticalblocktest_vertical-011f143b"
|
||||
)
|
||||
|
||||
def test_library_creation_creates_search_access(self, meilisearch_client):
|
||||
"""
|
||||
Test that creating a library automatically creates a SearchAccess record.
|
||||
This is required for course creators to search library content immediately after creation.
|
||||
"""
|
||||
# Create a library
|
||||
library = library_api.create_library(
|
||||
org=self.orgA,
|
||||
slug="test_lib",
|
||||
title="Test Library",
|
||||
description="Test library for SearchAccess creation",
|
||||
)
|
||||
|
||||
assert SearchAccess.objects.filter(context_key=library.key).exists()
|
||||
search_access = SearchAccess.objects.get(context_key=library.key)
|
||||
assert search_access.context_key == library.key
|
||||
|
||||
def test_create_delete_library_block(self, meilisearch_client):
|
||||
# Create library
|
||||
library = library_api.create_library(
|
||||
@@ -146,7 +163,7 @@ class TestUpdateIndexHandlers(ImmediateOnCommitMixin, ModuleStoreTestCase, LiveS
|
||||
title="Library Org A",
|
||||
description="This is a library from Org A",
|
||||
)
|
||||
lib_access, _ = SearchAccess.objects.get_or_create(context_key=library.key)
|
||||
lib_access = SearchAccess.objects.get(context_key=library.key)
|
||||
|
||||
# Populate it with a problem, freezing the date so we can verify created date serializes correctly.
|
||||
created_date = datetime(2023, 4, 5, 6, 7, 8, tzinfo=timezone.utc)
|
||||
|
||||
Reference in New Issue
Block a user