Skip to content
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
6 changes: 5 additions & 1 deletion openedx/core/djangoapps/content/search/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,18 @@ 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
log.error("Received null or incorrect data for event")
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])


Expand Down
19 changes: 18 additions & 1 deletion openedx/core/djangoapps/content/search/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ def test_create_delete_xblock(self, meilisearch_client):
"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(
Expand All @@ -146,7 +163,7 @@ def test_create_delete_library_block(self, meilisearch_client):
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)
Expand Down
Loading