Beispiel #1
0
    def test_delete__episodes_in_another_podcast__ok(
        self, client, episode_data, user, mocked_s3, dbs
    ):
        dbs = dbs
        podcast_1 = await_(Podcast.async_create(dbs, **get_podcast_data(created_by_id=user.id)))
        episode_data["status"] = Episode.Status.PUBLISHED
        episode_data["podcast_id"] = podcast_1.id
        episode_1 = await_(Episode.async_create(dbs, **episode_data))
        episode_1_1 = await_(Episode.async_create(dbs, **get_episode_data(podcast_1, "published")))

        podcast_2 = await_(Podcast.async_create(dbs, **get_podcast_data()))
        episode_data["status"] = Episode.Status.PUBLISHED
        episode_data["podcast_id"] = podcast_2.id
        # creating episode with same `source_id` in another podcast
        episode_2 = await_(Episode.async_create(dbs, **episode_data))

        await_(dbs.commit())
        client.login(user)
        url = self.url.format(id=podcast_1.id)
        response = client.delete(url)
        assert response.status_code == 200
        assert await_(Podcast.async_get(dbs, id=podcast_1.id)) is None
        assert await_(Episode.async_get(dbs, id=episode_1.id)) is None

        assert await_(Podcast.async_get(dbs, id=podcast_2.id)) is not None
        assert await_(Episode.async_get(dbs, id=episode_2.id)) is not None

        mocked_s3.delete_files_async.assert_called_with([episode_1_1.file_name])
Beispiel #2
0
    def test_delete__episodes_deleted_too__ok(self, client, podcast, user, mocked_s3, dbs):
        episode_1 = await_(Episode.async_create(dbs, **get_episode_data(podcast)))
        episode_2 = await_(Episode.async_create(dbs, **get_episode_data(podcast, "published")))
        await_(dbs.commit())

        client.login(user)
        url = self.url.format(id=podcast.id)
        response = client.delete(url)
        assert response.status_code == 200
        assert await_(Podcast.async_get(dbs, id=podcast.id)) is None
        assert await_(Episode.async_get(dbs, id=episode_1.id)) is None
        assert await_(Episode.async_get(dbs, id=episode_2.id)) is None

        mocked_s3.delete_files_async.assert_called_with([episode_2.file_name])
 def test_delete__ok(self, client, episode, user, mocked_s3, dbs):
     client.login(user)
     url = self.url.format(id=episode.id)
     response = client.delete(url)
     assert response.status_code == 204
     assert await_(Episode.async_get(dbs, id=episode.id)) is None
     mocked_s3.delete_files_async.assert_called_with([episode.file_name])
Beispiel #4
0
 def test_download_episode__unexpected_error__ok(self, episode,
                                                 mocked_youtube, dbs):
     mocked_youtube.download.side_effect = RuntimeError("Oops")
     result = await_(DownloadEpisodeTask(db_session=dbs).run(episode.id))
     episode = await_(Episode.async_get(dbs, id=episode.id))
     assert result == FinishCode.ERROR
     assert episode.status == Episode.Status.ERROR
     assert episode.published_at is None
    def test_delete__same_episode_exists__ok(
        self,
        client,
        podcast,
        episode_data,
        mocked_s3,
        same_episode_status,
        delete_called,
        dbs,
    ):
        source_id = get_video_id()

        user_1 = create_user(dbs)
        user_2 = create_user(dbs)

        podcast_1 = await_(
            Podcast.async_create(dbs,
                                 db_commit=True,
                                 **get_podcast_data(created_by_id=user_1.id)))
        podcast_2 = await_(
            Podcast.async_create(dbs,
                                 db_commit=True,
                                 **get_podcast_data(created_by_id=user_2.id)))

        episode_data["created_by_id"] = user_1.id
        _ = create_episode(dbs,
                           episode_data,
                           podcast_1,
                           status=same_episode_status,
                           source_id=source_id)

        episode_data["created_by_id"] = user_2.id
        episode_2 = create_episode(dbs,
                                   episode_data,
                                   podcast_2,
                                   status=Episode.Status.NEW,
                                   source_id=source_id)

        url = self.url.format(id=episode_2.id)
        client.login(user_2)
        response = client.delete(url)
        assert response.status_code == 204, f"Delete API is not available: {response.text}"
        assert await_(Episode.async_get(dbs, id=episode_2.id)) is None
        if delete_called:
            mocked_s3.delete_files_async.assert_called_with(
                [episode_2.file_name])
        else:
            assert not mocked_s3.delete_files_async.called
Beispiel #6
0
    def test_download_episode__upload_to_s3_failed__fail(
            self, episode, mocked_youtube, mocked_ffmpeg, mocked_s3,
            mocked_generate_rss_task, dbs):
        file_path = settings.TMP_AUDIO_PATH / episode.file_name
        with open(file_path, "wb") as file:
            file.write(b"EpisodeData")

        mocked_s3.upload_file.side_effect = lambda *_, **__: ""

        result = await_(DownloadEpisodeTask(db_session=dbs).run(episode.id))
        assert result == FinishCode.ERROR

        mocked_generate_rss_task.run.assert_not_called()
        episode = await_(Episode.async_get(dbs, id=episode.id))
        assert episode.status == Episode.Status.ERROR
        assert episode.published_at is None
Beispiel #7
0
    def test_download_episode__downloading_failed__roll_back_changes__ok(
            self, episode, mocked_youtube, mocked_ffmpeg, mocked_s3,
            mocked_generate_rss_task, dbs):
        file_path = settings.TMP_AUDIO_PATH / episode.file_name
        with open(file_path, "wb") as file:
            file.write(b"EpisodeData")

        mocked_youtube.download.side_effect = DownloadError(
            "Video is not available")

        result = await_(DownloadEpisodeTask(db_session=dbs).run(episode.id))

        episode = await_(Episode.async_get(dbs, id=episode.id))
        mocked_youtube.download.assert_called_with([episode.watch_url])
        mocked_s3.upload_file.assert_not_called()
        mocked_generate_rss_task.run.assert_not_called()

        assert result == FinishCode.ERROR
        assert episode.status == Episode.Status.ERROR
        assert episode.published_at is None
Beispiel #8
0
    def test_download_episode__ok(self, episode, mocked_youtube, mocked_ffmpeg,
                                  mocked_s3, mocked_generate_rss_task, dbs):
        file_path = settings.TMP_AUDIO_PATH / episode.file_name
        with open(file_path, "wb") as file:
            file.write(b"EpisodeData")

        result = await_(DownloadEpisodeTask(db_session=dbs).run(episode.id))
        episode = await_(Episode.async_get(dbs, id=episode.id))

        mocked_youtube.download.assert_called_with([episode.watch_url])
        mocked_ffmpeg.assert_called_with(episode.file_name)
        self.assert_called_with(
            mocked_s3.upload_file,
            src_path=str(file_path),
            dst_path=settings.S3_BUCKET_AUDIO_PATH,
        )
        mocked_generate_rss_task.run.assert_called_with(episode.podcast_id)

        assert result == FinishCode.OK
        assert episode.status == Episode.Status.PUBLISHED
        assert episode.published_at == episode.created_at