88from contentcuration .constants import (
99 community_library_submission as community_library_submission_constants ,
1010)
11+ from contentcuration .models import Change
1112from contentcuration .models import CommunityLibrarySubmission
1213from contentcuration .tests import testdata
1314from contentcuration .tests .base import StudioAPITestCase
15+ from contentcuration .viewsets .sync .constants import ADDED_TO_COMMUNITY_LIBRARY
1416
1517
1618def reverse_with_query (
@@ -59,21 +61,21 @@ def setUp(self):
5961 self .country2 = testdata .country (name = "Country 2" , code = "C2" )
6062
6163 self .channel_with_submission1 = testdata .channel ()
62- self .channel_with_submission1 .public = True
64+ self .channel_with_submission1 .public = False
6365 self .channel_with_submission1 .version = 1
6466 self .channel_with_submission1 .editors .add (self .author_user )
6567 self .channel_with_submission1 .editors .add (self .editor_user )
6668 self .channel_with_submission1 .save ()
6769
6870 self .channel_with_submission2 = testdata .channel ()
69- self .channel_with_submission2 .public = True
71+ self .channel_with_submission2 .public = False
7072 self .channel_with_submission2 .version = 1
7173 self .channel_with_submission2 .editors .add (self .author_user )
7274 self .channel_with_submission2 .editors .add (self .editor_user )
7375 self .channel_with_submission2 .save ()
7476
7577 self .channel_without_submission = testdata .channel ()
76- self .channel_without_submission .public = True
78+ self .channel_without_submission .public = False
7779 self .channel_without_submission .version = 1
7880 self .channel_without_submission .editors .add (self .author_user )
7981 self .channel_without_submission .editors .add (self .editor_user )
@@ -86,6 +88,13 @@ def setUp(self):
8688 self .unpublished_channel .editors .add (self .editor_user )
8789 self .unpublished_channel .save ()
8890
91+ self .public_channel = testdata .channel ()
92+ self .public_channel .public = True
93+ self .public_channel .version = 1
94+ self .public_channel .editors .add (self .author_user )
95+ self .public_channel .editors .add (self .editor_user )
96+ self .public_channel .save ()
97+
8998 self .existing_submission1 = testdata .community_library_submission ()
9099 self .existing_submission1 .channel = self .channel_with_submission1
91100 self .existing_submission1 .author = self .author_user
@@ -132,6 +141,18 @@ def test_create_submission__unpublished_channel(self):
132141 )
133142 self .assertEqual (response .status_code , 400 , response .content )
134143
144+ def test_create_submission__public_channel (self ):
145+ self .client .force_authenticate (user = self .editor_user )
146+ submission = self .new_submission_metadata
147+ submission ["channel" ] = self .public_channel .id
148+
149+ response = self .client .post (
150+ reverse ("community-library-submission-list" ),
151+ submission ,
152+ format = "json" ,
153+ )
154+ self .assertEqual (response .status_code , 400 , response .content )
155+
135156 def test_create_submission__explicit_channel_version (self ):
136157 self .client .force_authenticate (user = self .editor_user )
137158 submission_metadata = self .new_submission_metadata
@@ -317,7 +338,7 @@ def test_update_submission__is_editor(self):
317338 )
318339 self .assertEqual (response .status_code , 404 , response .content )
319340
320- def test_update_submission__is_admin (self ):
341+ def test_update_submission__is_admin__change_countries (self ):
321342 self .client .force_authenticate (user = self .admin_user )
322343 response = self .client .put (
323344 reverse (
@@ -335,6 +356,28 @@ def test_update_submission__is_admin(self):
335356 self .assertEqual (updated_submission .countries .count (), 1 )
336357 self .assertEqual (updated_submission .countries .first ().code , "C2" )
337358
359+ def test_update_submission__is_admin__keep_countries (self ):
360+ self .client .force_authenticate (user = self .admin_user )
361+
362+ updated_submission_metadata = self .updated_submission_metadata .copy ()
363+ updated_submission_metadata .pop ("countries" )
364+
365+ response = self .client .put (
366+ reverse (
367+ "community-library-submission-detail" ,
368+ args = [self .existing_submission1 .id ],
369+ ),
370+ updated_submission_metadata ,
371+ format = "json" ,
372+ )
373+ self .assertEqual (response .status_code , 200 , response .content )
374+
375+ updated_submission = CommunityLibrarySubmission .objects .get (
376+ id = self .existing_submission1 .id
377+ )
378+ self .assertEqual (updated_submission .countries .count (), 1 )
379+ self .assertEqual (updated_submission .countries .first ().code , "C1" )
380+
338381 def test_update_submission__change_channel (self ):
339382 self .client .force_authenticate (user = self .admin_user )
340383 submission_metadata = self .updated_submission_metadata
@@ -628,7 +671,10 @@ def test_resolve_submission__editor(self):
628671 )
629672 self .assertEqual (response .status_code , 403 , response .content )
630673
631- def test_resolve_submission__accept_correct (self ):
674+ @mock .patch (
675+ "contentcuration.viewsets.community_library_submission.apply_channel_changes_task"
676+ )
677+ def test_resolve_submission__accept_correct (self , apply_task_mock ):
632678 self .client .force_authenticate (user = self .admin_user )
633679 response = self .client .post (
634680 reverse (
@@ -652,7 +698,21 @@ def test_resolve_submission__accept_correct(self):
652698 self .assertEqual (resolved_submission .resolved_by , self .admin_user )
653699 self .assertEqual (resolved_submission .date_resolved , self .resolved_time )
654700
655- def test_resolve_submission__reject_correct (self ):
701+ self .assertTrue (
702+ Change .objects .filter (
703+ channel = self .submission .channel ,
704+ change_type = ADDED_TO_COMMUNITY_LIBRARY ,
705+ ).exists ()
706+ )
707+ apply_task_mock .fetch_or_enqueue .assert_called_once_with (
708+ self .admin_user ,
709+ channel_id = self .submission .channel .id ,
710+ )
711+
712+ @mock .patch (
713+ "contentcuration.viewsets.community_library_submission.apply_channel_changes_task"
714+ )
715+ def test_resolve_submission__reject_correct (self , apply_task_mock ):
656716 self .client .force_authenticate (user = self .admin_user )
657717 response = self .client .post (
658718 reverse (
@@ -680,6 +740,14 @@ def test_resolve_submission__reject_correct(self):
680740 self .assertEqual (resolved_submission .resolved_by , self .admin_user )
681741 self .assertEqual (resolved_submission .date_resolved , self .resolved_time )
682742
743+ self .assertFalse (
744+ Change .objects .filter (
745+ channel = self .submission .channel ,
746+ change_type = ADDED_TO_COMMUNITY_LIBRARY ,
747+ ).exists ()
748+ )
749+ apply_task_mock .fetch_or_enqueue .assert_not_called ()
750+
683751 def test_resolve_submission__reject_missing_resolution_reason (self ):
684752 self .client .force_authenticate (user = self .admin_user )
685753 metadata = self .resolve_reject_metadata .copy ()
0 commit comments