Esempio n. 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])
Esempio n. 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])
Esempio n. 3
0
    def test_generate__single_podcast__ok(self, user, mocked_s3, dbs):

        podcast_1: Podcast = await_(
            Podcast.async_create(dbs, **get_podcast_data()))
        podcast_2: Podcast = await_(
            Podcast.async_create(dbs, **get_podcast_data()))

        episode_data = get_episode_data(podcast_1, creator=user)
        episode_data["status"] = Episode.Status.NEW
        episode_new = await_(Episode.async_create(dbs, **episode_data))

        episode_data = get_episode_data(podcast_1, creator=user)
        episode_data["status"] = Episode.Status.DOWNLOADING
        episode_downloading = await_(Episode.async_create(dbs, **episode_data))

        episode_data = get_episode_data(podcast_1, creator=user)
        episode_data["status"] = Episode.Status.PUBLISHED
        episode_data["published_at"] = datetime.now()
        episode_published = await_(Episode.async_create(dbs, **episode_data))

        episode_data = get_episode_data(podcast_2, creator=user)
        episode_data["status"] = Episode.Status.PUBLISHED
        episode_podcast_2 = await_(Episode.async_create(dbs, **episode_data))
        await_(dbs.commit())

        expected_file_path = mocked_s3.tmp_upload_dir / f"{podcast_1.publish_id}.xml"
        generate_rss_task = tasks.GenerateRSSTask(db_session=dbs)
        result_code = await_(generate_rss_task.run(podcast_1.id))
        assert result_code == FinishCode.OK

        assert os.path.exists(
            expected_file_path), f"File {expected_file_path} didn't uploaded"
        with open(expected_file_path) as file:
            generated_rss_content = file.read()

        assert episode_published.title in generated_rss_content
        assert episode_published.description in generated_rss_content
        assert episode_published.file_name in generated_rss_content

        for episode in [episode_new, episode_downloading, episode_podcast_2]:
            assert episode.source_id not in generated_rss_content, f"{episode} in RSS {podcast_1}"

        podcast_1 = await_(Podcast.async_get(dbs, id=podcast_1.id))
        assert podcast_1.rss_link == str(expected_file_path)
Esempio n. 4
0
    def test_download_episode__file_correct__ignore(
        self,
        episode_data,
        podcast_data,
        mocked_youtube,
        mocked_ffmpeg,
        mocked_s3,
        mocked_generate_rss_task,
        dbs,
    ):
        podcast_1 = await_(Podcast.async_create(dbs, **get_podcast_data()))
        podcast_2 = await_(Podcast.async_create(dbs, **get_podcast_data()))

        episode_data.update({
            "status": "published",
            "source_id": mocked_youtube.video_id,
            "watch_url": mocked_youtube.watch_url,
            "file_size": 1024,
            "podcast_id": podcast_1.id,
        })
        await_(Episode.async_create(dbs, **episode_data))
        episode_data["status"] = "new"
        episode_data["podcast_id"] = podcast_2.id
        episode_2 = await_(Episode.async_create(dbs, **episode_data))
        await_(dbs.commit())

        mocked_s3.get_file_size.return_value = episode_2.file_size
        result = await_(DownloadEpisodeTask(db_session=dbs).run(episode_2.id))
        await_(dbs.refresh(episode_2))
        mocked_generate_rss_task.run.assert_called_with(
            podcast_1.id, podcast_2.id)
        assert result == FinishCode.SKIP
        assert not mocked_youtube.download.called
        assert not mocked_ffmpeg.called
        assert episode_2.status == Episode.Status.PUBLISHED
        assert episode_2.published_at == episode_2.created_at
Esempio n. 5
0
def create_episode(
    db_session: AsyncSession,
    episode_data: dict,
    podcast: Podcast,
    status: Episode.Status = Episode.Status.NEW,
    file_size: int = 0,
    source_id: str = None,
) -> Episode:
    src_id = source_id or get_video_id()
    episode_data.update(
        {
            "podcast_id": podcast.id,
            "source_id": src_id,
            "file_name": f"file_name_{src_id}.mp3",
            "status": status,
            "file_size": file_size,
        }
    )
    return await_(Episode.async_create(db_session, db_commit=True, **episode_data))
Esempio n. 6
0
    def test_download_episode__file_bad_size__ignore(
        self,
        episode_data,
        mocked_youtube,
        mocked_ffmpeg,
        mocked_s3,
        mocked_generate_rss_task,
        dbs,
    ):

        episode_data.update({
            "status": "published",
            "source_id": mocked_youtube.video_id,
            "watch_url": mocked_youtube.watch_url,
            "file_size": 1024,
        })
        episode = await_(
            Episode.async_create(dbs, db_commit=True, **episode_data))

        file_path = settings.TMP_AUDIO_PATH / episode.file_name
        with open(file_path, "wb") as file:
            file.write(b"EpisodeData")

        mocked_s3.get_file_size.return_value = 32

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

        await_(dbs.refresh(episode))
        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
Esempio n. 7
0
def episode(podcast, user, loop, dbs) -> Episode:
    episode_data = get_episode_data(podcast, creator=user)
    episode = loop.run_until_complete(Episode.async_create(
        dbs, **episode_data))
    loop.run_until_complete(dbs.commit())
    return episode