Skip to content

Commit 1184026

Browse files
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 <[email protected]>
1 parent e7377d4 commit 1184026

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

openedx/core/djangoapps/content/search/handlers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,18 @@ def library_block_deleted(**kwargs) -> None:
192192
@only_if_meilisearch_enabled
193193
def content_library_created_handler(**kwargs) -> None:
194194
"""
195-
Create the index for the content library
195+
Create the index and SearchAccess for the content library
196196
"""
197197
content_library_data = kwargs.get("content_library", None)
198198
if not content_library_data or not isinstance(content_library_data, ContentLibraryData): # pragma: no cover
199199
log.error("Received null or incorrect data for event")
200200
return
201201
library_key = content_library_data.library_key
202202

203+
# Create SearchAccess record immediately so course creators can search this library
204+
# right after creation. Without this, the JWT token won't include the new library's
205+
# access_id until it's added by the document indexing process or the page is refreshed.
206+
SearchAccess.objects.get_or_create(context_key=library_key)
203207
update_content_library_index_docs.apply(args=[str(library_key), True])
204208

205209

openedx/core/djangoapps/content/search/tests/test_handlers.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ def test_create_delete_xblock(self, meilisearch_client):
138138
"block-v1orgatest_coursetest_runtypeverticalblocktest_vertical-011f143b"
139139
)
140140

141+
def test_library_creation_creates_search_access(self, meilisearch_client):
142+
"""
143+
Test that creating a library automatically creates a SearchAccess record.
144+
This is required for course creators to search library content immediately after creation.
145+
"""
146+
# Create a library
147+
library = library_api.create_library(
148+
org=self.orgA,
149+
slug="test_lib",
150+
title="Test Library",
151+
description="Test library for SearchAccess creation",
152+
)
153+
154+
assert SearchAccess.objects.filter(context_key=library.key).exists()
155+
search_access = SearchAccess.objects.get(context_key=library.key)
156+
assert search_access.context_key == library.key
157+
141158
def test_create_delete_library_block(self, meilisearch_client):
142159
# Create library
143160
library = library_api.create_library(
@@ -146,7 +163,7 @@ def test_create_delete_library_block(self, meilisearch_client):
146163
title="Library Org A",
147164
description="This is a library from Org A",
148165
)
149-
lib_access, _ = SearchAccess.objects.get_or_create(context_key=library.key)
166+
lib_access = SearchAccess.objects.get(context_key=library.key)
150167

151168
# Populate it with a problem, freezing the date so we can verify created date serializes correctly.
152169
created_date = datetime(2023, 4, 5, 6, 7, 8, tzinfo=timezone.utc)

0 commit comments

Comments
 (0)