def test_suspended_until_is_set_if_was_suspended( self, channel_factory, user ): channel = channel_factory(user=user) moderation_services.suspend_channel( channel_id=channel.id, reason='reason', for_days=1 ) channel.refresh_from_db() assert isinstance(channel.suspended_until, datetime)
def test_is_suspended_returns_false_if_was_suspended_but_expired( self, channel_factory, user ): channel = channel_factory(user=user) moderation_services.suspend_channel( channel_id=channel.id, reason='reason', until=timezone.now() - timedelta(seconds=1), ) channel.refresh_from_db() assert channel.is_suspended is False
def test_is_suspended_returns_true_if_was_suspended( self, channel_factory, user ): channel = channel_factory(user=user) moderation_services.suspend_channel( channel_id=channel.id, reason='reason', until=timezone.now() + timedelta(days=1), ) channel.refresh_from_db() assert channel.is_suspended is True
def test_raises_if_destination_channel_is_suspended(self, channel): moderation_services.suspend_channel(channel_id=channel.id, reason='x', for_days=7) with pytest.raises(PermissionDenied): services.create_channel_sync( external_source='youtube', external_source_channel_url=( f'https://youtube.com/channel/{EXTERNAL_SOURCE_CHANNEL_ID}' ), destination_channel_id=channel.id, user_id=channel.user_id, )
def test(self, channel, mocker, settings): mock_send_mail = mocker.patch(f'{MODULE}.send_mail') channel_suspension = services.suspend_channel( channel_id=channel.id, reason='broke rule X', for_days=7, notify=False, ) tasks.task_notify_channel_owner_channel_is_suspended( channel_suspension_id=channel_suspension.id) assert mock_send_mail.called exp_until = time.datetime_to_human(channel_suspension.suspended_until) exp_subject = 'Your Veems channel has been suspended' exp_message = ( f'Your channel "{channel.name}" ' f'has been suspended until {exp_until}.\n' "Until then, you won't be able to upload new videos.\n\n" f'Reason: {channel_suspension.reason}\n' f'Channel link: http://localhost:8000/c/{channel.id}/\n' f'Suspension end date: {exp_until}\n\n' 'If you think this decision has been made in error, ' 'please reply to this email.' '\n\n- Veems Support Team\n') mock_send_mail.assert_called_once_with( subject=exp_subject, message=exp_message, from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=[channel.user.email], fail_silently=False, )
def test_skips_syncing_if_channel_is_suspended(self, channel_sync): moderation_services.suspend_channel( channel_id=channel_sync.destination_channel.id, reason='x', for_days=1, ) ( synced_external_source_video_ids, new_video_records_ids, ) = services.sync(channel_sync_id=channel_sync.id) assert synced_external_source_video_ids == () assert new_video_records_ids == () channel_sync.refresh_from_db() assert channel_sync.is_syncing is False
def test_suspended_until_is_null_if_suspension_record_deleted( self, channel_factory, user ): channel = channel_factory(user=user) channel_suspension = moderation_services.suspend_channel( channel_id=channel.id, reason='reason', for_days=1 ) channel_suspension.delete() channel.refresh_from_db() assert channel.suspended_until is None
def test_returns_403_when_channel_is_suspended(self, api_client, channel_factory): api_client, user = api_client channel = channel_factory(user=user) moderation_services.suspend_channel(channel_id=channel.id, reason='reason', for_days=7) body = json.dumps({ 'filename': 'MyFile.mp4', 'channel_id': channel.id, 'num_parts': 3, }) response = api_client.put(self.URL, body, content_type='application/json') assert response.status_code == FORBIDDEN assert response.json() == { 'detail': ERRORS['cant_upload_channel_suspended'] } assert models.Upload.objects.count() == 0 assert models.Video.objects.count() == 0
def test_returns_403_if_destination_channel_is_suspended( self, api_client, channel_factory): api_client, user = api_client channel = channel_factory(user=user) moderation_services.suspend_channel(channel_id=channel.id, reason='x', for_days=1) body = { 'destination_channel_id': channel.id, 'external_source': 'youtube', 'external_source_channel_url': (f'https://youtube.com/channel/{EXTERNAL_SOURCE_CHANNEL_ID}'), } response = api_client.post('/api/v1/channel/sync/', body, format='json') assert response.status_code == FORBIDDEN assert response.json() == { 'detail': 'Cannot create a sync for a Channel which is suspended' }
def test(self, channel, mocker): mock_task_notify_channel_owner_channel_is_suspended = mocker.patch( f'{MODULE}.tasks.task_notify_channel_owner_channel_is_suspended') assert services.suspend_channel( channel_id=channel.id, reason='broke rule X', ) suspensions = channel.suspensions.all() assert len(suspensions) == 1 suspension = suspensions[0] assert suspension.channel_id == channel.id assert suspension.reason == 'broke rule X' assert suspension.suspended_on assert suspension.suspended_until is None assert mock_task_notify_channel_owner_channel_is_suspended.delay.called
def test_with_suspension_end_date(self, channel, mocker): mock_task_notify_channel_owner_channel_is_suspended = mocker.patch( f'{MODULE}.tasks.task_notify_channel_owner_channel_is_suspended') assert services.suspend_channel( channel_id=channel.id, reason='broke rule X', for_days=7, ) suspensions = channel.suspensions.all() assert len(suspensions) == 1 suspension = suspensions[0] assert suspension.channel_id == channel.id assert suspension.reason == 'broke rule X' assert suspension.suspended_on assert suspension.suspended_until delta = suspension.suspended_until - suspension.suspended_on assert delta.days == 7 assert mock_task_notify_channel_owner_channel_is_suspended.delay.called
def make(channel=None, **kwargs): channel = channel or request.getfixturevalue('channel') return moderation_services.suspend_channel(channel_id=channel.id, reason='broke rule X', **kwargs)