@@ -149,6 +149,84 @@ def test_markdown_view_trailing_slash(tp):
149149 tp .response_200 (res )
150150
151151
152+ def test_doc_libs_get_content_with_db_cache (request_factory ):
153+ """Test DocLibsTemplateView.get_content with database caching enabled."""
154+ from core .views import DocLibsTemplateView
155+
156+ # Mock S3 returning content
157+ mock_s3_result = {
158+ "content" : b"<html>Test content</html>" ,
159+ "content_type" : "text/html" ,
160+ }
161+
162+ with patch ("core.views.ENABLE_DB_CACHE" , True ), patch (
163+ "core.views.DocLibsTemplateView.get_from_database" , return_value = None
164+ ) as mock_get_from_db , patch (
165+ "core.views.DocLibsTemplateView.get_from_s3" , return_value = mock_s3_result
166+ ) as mock_get_from_s3 , patch (
167+ "core.views.DocLibsTemplateView.save_to_database"
168+ ) as mock_save_to_db :
169+
170+ view = DocLibsTemplateView ()
171+ view .request = request_factory .get ("/doc/libs/test/" )
172+ result = view .get_content ("test/path" )
173+
174+ # verify database was checked first
175+ mock_get_from_db .assert_called_once_with ("static_content_test/path" )
176+ # verify S3 was called after cache miss
177+ mock_get_from_s3 .assert_called_once_with ("test/path" )
178+ # verify content was saved to database
179+ mock_save_to_db .assert_called_once_with (
180+ "static_content_test/path" , mock_s3_result
181+ )
182+
183+ assert result ["content" ] == mock_s3_result ["content" ]
184+ assert result ["content_type" ] == mock_s3_result ["content_type" ]
185+ assert "redirect" in result
186+
187+
188+ def test_doc_libs_get_content_without_db_cache (request_factory ):
189+ """Test DocLibsTemplateView.get_content with database caching disabled."""
190+ from core .views import DocLibsTemplateView
191+
192+ # Mock S3 returning content
193+ mock_s3_result = {
194+ "content" : b"<html>Test content</html>" ,
195+ "content_type" : "text/html" ,
196+ }
197+
198+ with patch ("core.views.ENABLE_DB_CACHE" , False ), patch (
199+ "core.views.DocLibsTemplateView.get_from_s3" , return_value = mock_s3_result
200+ ) as mock_get_from_s3 :
201+
202+ view = DocLibsTemplateView ()
203+ view .request = request_factory .get ("/doc/libs/test/" )
204+ result = view .get_content ("test/path" )
205+
206+ # verify S3 was called directly
207+ mock_get_from_s3 .assert_called_once_with ("test/path" )
208+
209+ assert result ["content" ] == mock_s3_result ["content" ]
210+ assert result ["content_type" ] == mock_s3_result ["content_type" ]
211+ assert "redirect" in result
212+
213+
214+ @patch ("core.views.DocLibsTemplateView.get_from_s3" )
215+ def test_doc_libs_get_content_not_found (mock_get_from_s3 , request_factory ):
216+ """Test DocLibsTemplateView.get_content when content is not found."""
217+ from core .views import DocLibsTemplateView , ContentNotFoundException
218+
219+ # mock S3 returning None (content not found)
220+ mock_get_from_s3 .return_value = None
221+
222+ request = request_factory .get ("/doc/libs/test/" )
223+ view = DocLibsTemplateView ()
224+ view .request = request
225+
226+ with pytest .raises (ContentNotFoundException , match = "Content not found" ):
227+ view .get_content ("nonexistent/path" )
228+
229+
152230def test_markdown_view_top_level_includes_extension (tp ):
153231 res = tp .get ("/markdown/foo.html" )
154232 tp .response_200 (res )
0 commit comments