@@ -42,7 +42,7 @@ def test_auth_for_create_edit_unit_feedback_chips
4242 }
4343
4444 data_to_put = {
45- chip_text : 'Updated text' ,
45+ chip_text : 'Updated text'
4646 }
4747
4848 learning_outcomes . each do |lo |
@@ -243,6 +243,79 @@ def test_auth_for_get_global_feedback_chips
243243 end
244244 end
245245
246+ def test_auth_for_delete_feedback_chips
247+ start_inside = DateTime . now - Doubtfire ::Application . config . auditor_unit_access_years + 1 . week
248+ end_inside = DateTime . now - 1 . week
249+
250+ unit = FactoryBot . create ( :unit , student_count : 1 , start_date : start_inside , end_date : end_inside )
251+ admin = FactoryBot . create ( :user , :admin )
252+ tutor = FactoryBot . create ( :user , :tutor )
253+ auditor = FactoryBot . create ( :user , :auditor )
254+ student = unit . students . first
255+
256+
257+ learning_outcomes = [
258+ {
259+ learning_outcome : unit . learning_outcomes . first ,
260+ context : 'ULO'
261+ } ,
262+ {
263+ learning_outcome : unit . task_definitions . first . learning_outcomes . first ,
264+ context : 'TLO'
265+ }
266+ ]
267+
268+ unit . employ_staff ( tutor , Role . tutor )
269+
270+ users_can = [
271+ unit . main_convenor_user ,
272+ admin
273+ ]
274+ users_cant = [
275+ tutor ,
276+ auditor ,
277+ FactoryBot . create ( :user , :student ) ,
278+ FactoryBot . create ( :user , :tutor ) ,
279+ FactoryBot . create ( :user , :convenor ) ,
280+ student . user
281+ ]
282+
283+ assert_equal Role . auditor , unit . role_for ( auditor )
284+
285+ users_can . each do |user |
286+ add_auth_header_for user : user
287+ learning_outcomes . each do |lo_data |
288+ lo = lo_data [ :learning_outcome ]
289+ context = lo_data [ :context ]
290+
291+ chip = FactoryBot . create ( :feedback_template_chip , learning_outcome_id : lo . id )
292+
293+ chip_count = lo . feedback_chips . count
294+
295+ delete "api/feedback_chips/#{ chip . id } "
296+ assert_equal 204 , last_response . status , "User #{ user . role . name } should be able to delete #{ context } feedback chips"
297+ assert_equal chip_count - 1 , lo . feedback_chips . count
298+ end
299+ end
300+
301+ users_cant . each do |user |
302+ add_auth_header_for user : user
303+
304+ learning_outcomes . each do |lo_data |
305+ lo = lo_data [ :learning_outcome ]
306+ context = lo_data [ :context ]
307+
308+ chip = FactoryBot . create ( :feedback_template_chip , learning_outcome_id : lo . id )
309+
310+ chip_count = lo . feedback_chips . count
311+
312+ delete "api/feedback_chips/#{ chip . id } "
313+ assert_equal 403 , last_response . status , "User #{ user . role . name } should not be able to delete #{ context } feedback chips"
314+ assert_equal chip_count , lo . feedback_chips . count
315+ end
316+ end
317+ end
318+
246319 def test_create_feedback_group_chip
247320 learning_outcome = FactoryBot . create ( :learning_outcome )
248321 data_to_post = {
@@ -323,7 +396,7 @@ def test_create_feedback_template_chip_with_parent
323396 end
324397
325398 def test_get_all_feedback_chips_for_a_context
326- unit = Unit . first
399+ unit = FactoryBot . create ( :unit , with_students : false , task_count : 0 , outcome_count : 0 )
327400 learning_outcome = FactoryBot . create ( :learning_outcome , context_id : unit . id , context_type : 'Unit' )
328401 learning_outcome2 = FactoryBot . create ( :learning_outcome , context_id : unit . id , context_type : 'Unit' )
329402 group_chip_lo1 = FactoryBot . create ( :feedback_group_chip , learning_outcome_id : learning_outcome . id )
@@ -335,70 +408,98 @@ def test_get_all_feedback_chips_for_a_context
335408 add_auth_header_for user : User . first
336409 get "api/units/#{ unit . id } /feedback_chips"
337410 assert_equal 200 , last_response . status
411+
412+ assert_equal 6 , last_response_body . count , last_response_body
413+
414+ chips = [ learning_outcome . feedback_chips . pluck ( :id ) , learning_outcome2 . feedback_chips . pluck ( :id ) ] . flatten
415+
416+ last_response_body . each do |line |
417+ assert chips . include? ( line [ 'id' ] ) , "Found unknown chip #{ line } "
418+ end
338419 end
339420
340421 def test_update_feedback_template_chip
341422 learning_outcome = FactoryBot . create ( :learning_outcome )
342423 group_chip = FactoryBot . create ( :feedback_group_chip , learning_outcome_id : learning_outcome . id )
343424 template_chip = FactoryBot . create ( :feedback_template_chip , chip_text : 'chippy' , description : 'blah blah' , comment_text : 'your work is horrible' , summary_text : 'just plain bad' , task_status : TaskStatus . complete . name , learning_outcome_id : learning_outcome . id , parent_chip_id : group_chip . id )
344- data_to_post = {
425+ data_to_put = {
345426 chip_text : 'updated chip' ,
346427 description : 'updated description'
347428 }
348429 add_auth_header_for user : User . first
349- put_json "api/feedback_chips/#{ template_chip . id } " , data_to_post
430+ put_json "api/feedback_chips/#{ template_chip . id } " , data_to_put
350431 assert_equal 200 , last_response . status
432+
433+ template_chip . reload
434+ assert_json_matches_model template_chip , last_response_body , %w[ id chip_text description ]
435+ assert_json_matches_model template_chip , data_to_put , %w[ chip_text description ]
351436 end
352437
353438 def test_update_feedback_group_chip
354439 learning_outcome = FactoryBot . create ( :learning_outcome )
355440 group_chip = FactoryBot . create ( :feedback_group_chip , learning_outcome_id : learning_outcome . id )
356- data_to_post = {
441+ data_to_put = {
357442 chip_text : 'updated chip text' ,
358443 description : 'updated description'
359444 }
360445 add_auth_header_for user : User . first
361- put_json "api/feedback_chips/#{ group_chip . id } " , data_to_post
446+ put_json "api/feedback_chips/#{ group_chip . id } " , data_to_put
362447 assert_equal 200 , last_response . status
448+
449+ group_chip . reload
450+ assert_json_matches_model group_chip , last_response_body , %w[ id chip_text description ]
451+ assert_json_matches_model group_chip , data_to_put , %w[ chip_text description ]
363452 end
364453
365454 def test_delete_feedback_template_chip
366455 learning_outcome = FactoryBot . create ( :learning_outcome )
367456 group_chip = FactoryBot . create ( :feedback_group_chip , learning_outcome_id : learning_outcome . id )
368457 template_chip = FactoryBot . create ( :feedback_template_chip , chip_text : 'chippy' , description : 'blah blah' , comment_text : 'your work is horrible' , summary_text : 'just plain bad' , task_status : TaskStatus . complete . name , learning_outcome_id : learning_outcome . id , parent_chip_id : group_chip . id )
369458 add_auth_header_for user : User . first
459+
460+ chip_count = learning_outcome . feedback_chips . count
461+
370462 delete "api/feedback_chips/#{ template_chip . id } "
371463 assert_equal 204 , last_response . status
464+
465+ assert_equal chip_count - 1 , learning_outcome . feedback_chips . count
372466 end
373467
374468 def test_delete_feedback_group_chip
375469 learning_outcome = FactoryBot . create ( :learning_outcome )
376470 group_chip = FactoryBot . create ( :feedback_group_chip , learning_outcome_id : learning_outcome . id )
377471 add_auth_header_for user : User . first
472+
473+ chip_count = learning_outcome . feedback_chips . count
474+
378475 delete "api/feedback_chips/#{ group_chip . id } "
379476 assert_equal 204 , last_response . status
477+
478+ assert_equal chip_count - 1 , learning_outcome . feedback_chips . count
380479 end
381480
382- def test_change_chip_type_t_to_g
481+ def test_cannot_change_chip_type_t_to_g
383482 learning_outcome = FactoryBot . create ( :learning_outcome )
384483 group_chip = FactoryBot . create ( :feedback_group_chip , learning_outcome_id : learning_outcome . id )
385484 template_chip = FactoryBot . create ( :feedback_template_chip , chip_text : 'chippy' , description : 'blah blah' , comment_text : 'your work is horrible' , summary_text : 'just plain bad' , task_status : TaskStatus . complete . name , learning_outcome_id : learning_outcome . id , parent_chip_id : group_chip . id )
386- # puts template_chip.inspect
387- data_to_post = {
485+
486+ data_to_put = {
388487 type : 'group' ,
389488 chip_text : 'Sample chip text' ,
390489 description : 'Sample description'
391490 }
392491 add_auth_header_for user : User . first
393- put_json "api/feedback_chips/#{ template_chip . id } " , data_to_post
394- # puts last_response.body
492+ put_json "api/feedback_chips/#{ template_chip . id } " , data_to_put
395493 assert_equal 200 , last_response . status
494+
495+ template_chip . reload
496+ assert_equal 'Feedback::FeedbackTemplateChip' , template_chip . type
396497 end
397498
398- def test_change_chip_type_g_to_t
499+ def test_cannot_change_chip_type_g_to_t
399500 learning_outcome = FactoryBot . create ( :learning_outcome )
400501 group_chip = FactoryBot . create ( :feedback_group_chip , learning_outcome_id : learning_outcome . id )
401- # puts group_chip.inspect
502+
402503 data_to_post = {
403504 type : 'template' ,
404505 chip_text : 'Sample chip text' ,
@@ -411,21 +512,11 @@ def test_change_chip_type_g_to_t
411512 }
412513 add_auth_header_for user : User . first
413514 put_json "api/feedback_chips/#{ group_chip . id } " , data_to_post
414- # puts last_response.body
415- assert_equal 200 , last_response . status
416- end
417515
418- def test_update_without_type
419- learning_outcome = FactoryBot . create ( :learning_outcome )
420- group_chip = FactoryBot . create ( :feedback_group_chip , learning_outcome_id : learning_outcome . id )
421- data_to_post = {
422- chip_text : 'updated chip text' ,
423- description : 'updated description'
424- }
425- add_auth_header_for user : User . first
426- put_json "api/feedback_chips/#{ group_chip . id } " , data_to_post
427- # puts last_response.body
428516 assert_equal 200 , last_response . status
517+
518+ group_chip . reload
519+ assert_equal 'Feedback::FeedbackGroupChip' , group_chip . type
429520 end
430521
431522 def test_get_global_context_chips
@@ -437,7 +528,16 @@ def test_get_global_context_chips
437528 feedback_template_chip2 = FactoryBot . create ( :feedback_template_chip , learning_outcome_id : learning_outcome2 . id , parent_chip_id : feedback_group_chip2 . id )
438529 add_auth_header_for user : User . first
439530 get 'api/global/feedback_chips'
440- # puts last_response.body
531+
532+ global_chips = Feedback ::FeedbackChip . global_chips
533+ chip_count = global_chips . count
534+
441535 assert_equal 200 , last_response . status
536+ assert_equal chip_count , last_response_body . count
537+
538+ ids = global_chips . pluck ( :id )
539+ last_response_body . each do |line |
540+ assert ids . include? ( line [ 'id' ] )
541+ end
442542 end
443543end
0 commit comments