@@ -93,7 +93,7 @@ def test_create_assessmentitem_no_node_permission(self):
9393 except models .AssessmentItem .DoesNotExist :
9494 pass
9595
96- def test_create_assessmentitem_with_file_question (self ):
96+ def test_create_assessmentitem_with_incorrect_file_placeholder_in_question (self ):
9797 self .client .force_authenticate (user = self .user )
9898 assessmentitem = self .assessmentitem_metadata
9999 image_file = testdata .fileobj_exercise_image ()
@@ -103,6 +103,43 @@ def test_create_assessmentitem_with_file_question(self):
103103 exercises .IMG_PLACEHOLDER , image_file .checksum , image_file .file_format_id
104104 )
105105
106+ assessmentitem ["question" ] = question
107+ response = self .sync_changes (
108+ [
109+ generate_create_event (
110+ [assessmentitem ["contentnode" ], assessmentitem ["assessment_id" ]],
111+ ASSESSMENTITEM ,
112+ assessmentitem ,
113+ channel_id = self .channel .id ,
114+ )
115+ ],
116+ )
117+ self .assertEqual (response .status_code , 200 , response .content )
118+ try :
119+ ai = models .AssessmentItem .objects .get (
120+ assessment_id = assessmentitem ["assessment_id" ]
121+ )
122+ except models .AssessmentItem .DoesNotExist :
123+ self .fail ("AssessmentItem was not created" )
124+ try :
125+ file = ai .files .get ()
126+ self .assertEqual (file .id , image_file .id )
127+ self .fail ("File was updated" )
128+ except models .File .DoesNotExist :
129+ pass
130+
131+ def test_create_assessmentitem_with_file_question (self ):
132+ self .client .force_authenticate (user = self .user )
133+ assessmentitem = self .assessmentitem_metadata
134+ image_file = testdata .fileobj_exercise_image ()
135+ image_file .uploaded_by = self .user
136+ image_file .save ()
137+ question = "" .format (
138+ exercises .CONTENT_STORAGE_PLACEHOLDER ,
139+ image_file .checksum ,
140+ image_file .file_format_id ,
141+ )
142+
106143 assessmentitem ["question" ] = question
107144 response = self .sync_changes (
108145 [
@@ -127,14 +164,84 @@ def test_create_assessmentitem_with_file_question(self):
127164 except models .File .DoesNotExist :
128165 self .fail ("File was not updated" )
129166
167+ def test_create_assessmentitem_with_file_in_question_no_file_object (self ):
168+ self .client .force_authenticate (user = self .user )
169+ assessmentitem = self .assessmentitem_metadata
170+ image_file = testdata .fileobj_exercise_image ()
171+ image_file .uploaded_by = self .user
172+ image_file .save ()
173+ question = "" .format (
174+ exercises .CONTENT_STORAGE_PLACEHOLDER ,
175+ image_file .checksum ,
176+ image_file .file_format_id ,
177+ )
178+
179+ image_file .delete ()
180+
181+ assessmentitem ["question" ] = question
182+ response = self .sync_changes (
183+ [
184+ generate_create_event (
185+ [assessmentitem ["contentnode" ], assessmentitem ["assessment_id" ]],
186+ ASSESSMENTITEM ,
187+ assessmentitem ,
188+ channel_id = self .channel .id ,
189+ )
190+ ],
191+ )
192+ self .assertEqual (response .status_code , 200 , response .content )
193+ try :
194+ ai = models .AssessmentItem .objects .get (
195+ assessment_id = assessmentitem ["assessment_id" ]
196+ )
197+ except models .AssessmentItem .DoesNotExist :
198+ self .fail ("AssessmentItem was not created" )
199+ try :
200+ file = ai .files .get ()
201+ self .assertEqual (file .assessment_item_id , ai .id )
202+ except models .File .DoesNotExist :
203+ self .fail ("File was not created" )
204+
205+ def test_create_assessmentitem_with_file_in_question_no_file_uploaded (self ):
206+ self .client .force_authenticate (user = self .user )
207+ assessmentitem = self .assessmentitem_metadata
208+ question = "" .format (
209+ exercises .CONTENT_STORAGE_PLACEHOLDER ,
210+ "123456789012345678901234567890ab" ,
211+ "jpg" ,
212+ )
213+
214+ assessmentitem ["question" ] = question
215+ response = self .sync_changes (
216+ [
217+ generate_create_event (
218+ [assessmentitem ["contentnode" ], assessmentitem ["assessment_id" ]],
219+ ASSESSMENTITEM ,
220+ assessmentitem ,
221+ channel_id = self .channel .id ,
222+ )
223+ ],
224+ )
225+ self .assertEqual (response .status_code , 200 , response .content )
226+ self .assertEqual (len (response .data ["errors" ]), 1 )
227+ try :
228+ models .AssessmentItem .objects .get (
229+ assessment_id = assessmentitem ["assessment_id" ]
230+ )
231+ self .fail ("AssessmentItem was created" )
232+ except models .AssessmentItem .DoesNotExist :
233+ pass
234+
130235 def test_create_assessmentitem_with_file_answers (self ):
131236 self .client .force_authenticate (user = self .user )
132237 assessmentitem = self .assessmentitem_metadata
133238 image_file = testdata .fileobj_exercise_image ()
134239 image_file .uploaded_by = self .user
135240 image_file .save ()
136241 answer = "" .format (
137- exercises .IMG_PLACEHOLDER , image_file .checksum , image_file .file_format_id
242+ exercises .CONTENT_STORAGE_PLACEHOLDER ,
243+ image_file .checksum ,
244+ image_file .file_format_id ,
138245 )
139246
140247 answers = [{"answer" : answer , "correct" : False , "order" : 1 }]
@@ -171,7 +278,9 @@ def test_create_assessmentitem_with_file_hints(self):
171278 image_file .uploaded_by = self .user
172279 image_file .save ()
173280 hint = "" .format (
174- exercises .IMG_PLACEHOLDER , image_file .checksum , image_file .file_format_id
281+ exercises .CONTENT_STORAGE_PLACEHOLDER ,
282+ image_file .checksum ,
283+ image_file .file_format_id ,
175284 )
176285 hints = [
177286 {"hint" : hint , "order" : 1 },
@@ -209,7 +318,9 @@ def test_create_assessmentitem_with_file_no_permission(self):
209318 assessmentitem = self .assessmentitem_metadata
210319 image_file = testdata .fileobj_exercise_image ()
211320 question = "" .format (
212- exercises .IMG_PLACEHOLDER , image_file .checksum , image_file .file_format_id
321+ exercises .CONTENT_STORAGE_PLACEHOLDER ,
322+ image_file .checksum ,
323+ image_file .file_format_id ,
213324 )
214325 assessmentitem ["question" ] = question
215326 response = self .sync_changes (
@@ -223,14 +334,17 @@ def test_create_assessmentitem_with_file_no_permission(self):
223334 ],
224335 )
225336 self .assertEqual (response .status_code , 200 , response .content )
226- self .assertEqual (len (response .json ()["errors" ]), 1 )
227337 try :
228- models .AssessmentItem .objects .get (
338+ ai = models .AssessmentItem .objects .get (
229339 assessment_id = assessmentitem ["assessment_id" ]
230340 )
231- self .fail ("AssessmentItem was created" )
232341 except models .AssessmentItem .DoesNotExist :
233- pass
342+ self .fail ("AssessmentItem was not created" )
343+ try :
344+ file = ai .files .get ()
345+ self .assertEqual (file .assessment_item_id , ai .id )
346+ except models .File .DoesNotExist :
347+ self .fail ("File was not created" )
234348
235349 self .assertIsNone (image_file .assessment_item )
236350
@@ -324,7 +438,9 @@ def test_update_assessmentitem_with_file(self):
324438 image_file .uploaded_by = self .user
325439 image_file .save ()
326440 question = "" .format (
327- exercises .IMG_PLACEHOLDER , image_file .checksum , image_file .file_format_id
441+ exercises .CONTENT_STORAGE_PLACEHOLDER ,
442+ image_file .checksum ,
443+ image_file .file_format_id ,
328444 )
329445
330446 self .client .force_authenticate (user = self .user )
@@ -352,7 +468,9 @@ def test_update_assessmentitem_with_file_no_permissions(self):
352468 )
353469 image_file = testdata .fileobj_exercise_image ()
354470 question = "" .format (
355- exercises .IMG_PLACEHOLDER , image_file .checksum , image_file .file_format_id
471+ exercises .CONTENT_STORAGE_PLACEHOLDER ,
472+ image_file .checksum ,
473+ image_file .file_format_id ,
356474 )
357475
358476 self .client .force_authenticate (user = self .user )
@@ -367,13 +485,13 @@ def test_update_assessmentitem_with_file_no_permissions(self):
367485 ],
368486 )
369487 self .assertEqual (response .status_code , 200 , response .content )
370- self .assertEqual (len (response .json ()["errors" ]), 1 )
371488 try :
372489 file = assessmentitem .files .get ()
373- self .assertNotEqual (file .id , image_file .id )
374- self .fail ("File was updated" )
490+ self .assertEqual (file .assessment_item_id , assessmentitem .id )
375491 except models .File .DoesNotExist :
376- pass
492+ self .fail ("File was not created" )
493+
494+ self .assertIsNone (image_file .assessment_item )
377495
378496 def test_update_assessmentitem_remove_file (self ):
379497
0 commit comments