def setUp(self, mock_get_workflow): self.video = VideoFactory() self.language = SubtitleLanguageFactory(video=self.video, language_code='en') self.workflow = mock.Mock() mock_get_workflow.return_value = self.workflow self.workflow.user_can_view_private_subtitles.return_value = True self.user = UserFactory() self.viewset = SubtitleLanguageViewSet(kwargs={ 'video_id': self.video.video_id, 'language_code': 'en', }, request=mock.Mock(user=self.user))
class SubtitleLanguageViewset(TestCase): @test_utils.patch_for_test('subtitles.workflows.get_workflow') def setUp(self, mock_get_workflow): self.video = VideoFactory() self.language = SubtitleLanguageFactory(video=self.video, language_code='en') self.workflow = mock.Mock() mock_get_workflow.return_value = self.workflow self.workflow.user_can_view_private_subtitles.return_value = True self.user = UserFactory() self.viewset = SubtitleLanguageViewSet(kwargs={ 'video_id': self.video.video_id, 'language_code': 'en', }, request=mock.Mock(user=self.user)) def test_check_user_can_view_video(self): # test successful permissions check self.workflow.user_can_view_video.return_value = True self.viewset.get_queryset() self.viewset.get_object() # test failed permissions check self.workflow.user_can_view_video.return_value = False with assert_raises(PermissionDenied): self.viewset.get_queryset() with assert_raises(PermissionDenied): self.viewset.get_object() # check the arguments for the permissions check assert_equal(self.workflow.user_can_view_video.call_args_list, [ mock.call(self.user), mock.call(self.user), mock.call(self.user), mock.call(self.user), ]) def test_show_private_versions(self): # test successful permissions check self.workflow.user_can_view_private_subtitles.return_value = True assert_equal(self.viewset.show_private_versions('en'), True) # test failed permissions check self.workflow.user_can_view_private_subtitles.return_value = False assert_equal(self.viewset.show_private_versions('en'), False) # check the arguments for the permissions check assert_equal( self.workflow.user_can_view_private_subtitles.call_args_list, [ mock.call(self.user, 'en'), mock.call(self.user, 'en'), ]) def test_serializer_context(self): self.viewset.action = 'retrieve' serializer_context = self.viewset.get_serializer_context() assert_equal(serializer_context['show_private_versions'], self.viewset.show_private_versions) assert_equal(serializer_context['video'], self.video)