Beispiel #1
0
    def test_update_subscriptions_with_last_pk(self):
        last_week = timezone.now() - timedelta(7)
        sub1 = SubscriptionFactory.create(user=self.user, channel_id="123", last_update=last_week)
        sub2 = SubscriptionFactory.create(user=self.user, channel_id="456", last_update=last_week)
        first, second = sorted([sub1, sub2], key=lambda x: x.pk)

        self.subscription_mock.return_value.execute.return_value['items'] = [{
            'snippet': {
                'resourceId': {'channelId': second.channel_id},
                'thumbnails': {},
            },
        }]

        self.channel_mock.return_value.execute.return_value['items'] = [{
            'id': second.channel_id,
            'contentDetails': {
                'relatedPlaylists': {'uploads': 'upload%s' % second.channel_id},
            },
        }]

        subscriptions(self.user.id, last_pk=first.id)
        self.assertEqual(self.subscription_mock.call_count, 1)
        self.assertEqual(self.channel_mock.call_count, 1)

        first.refresh_from_db()
        self.assertEqual(first.last_update, last_week)

        second.refresh_from_db()
        self.assertNotEqual(second.last_update, last_week)

        # the update task end by deferring itself with the last PK, plus one
        # import_video calls
        self.assertNumTasksEquals(2)
        # make sure it doens't infinitely loop
        self.process_task_queues()
Beispiel #2
0
    def test_subscriptions_with_runtime_exceeded_error(self, defer_mock):
        defer_mock.defer.side_effect = MockExecute([RuntimeExceededError(), None])

        last_week = timezone.now() - timedelta(7)
        sub1 = SubscriptionFactory.create(user=self.user, channel_id="123", last_update=last_week)
        sub2 = SubscriptionFactory.create(user=self.user, channel_id="456", last_update=last_week)

        subscriptions(self.user.id)
        self.assertEqual(self.subscription_mock.call_count, 1)
        self.assertEqual(self.channel_mock.call_count, 1)
        self.assertEqual(defer_mock.defer.call_count, 2)

        sub1.refresh_from_db()
        sub2.refresh_from_db()

        self.assertEqual(
            defer_mock.defer.call_args_list[0],
            ((import_videos, self.user.id, sub1.pk, sub1.upload_playlist, []), {"only_first_page": False})
        )
        self.assertEqual(defer_mock.defer.call_args_list[1],
                         ((subscriptions, self.user.id, None), {}))

        self.assertNotEqual(sub1.last_update, last_week)

        self.assertEqual(sub2.last_update, last_week)
Beispiel #3
0
    def test_missing_oauth_token(self):
        OauthToken.objects.get(user_id=self.user.id).delete()
        last_week = timezone.now() - timedelta(7)
        SubscriptionFactory.create(user=self.user, channel_id="123", last_update=last_week)
        SubscriptionFactory.create(user=self.user, channel_id="456", last_update=last_week)
        self.service_patch.stop()

        # should raise no exceptions
        subscriptions(self.user.id)
Beispiel #4
0
    def test_model_method(self):
        sub = SubscriptionFactory(channel_id="123")
        with self.assertRaises(AttributeError):
            sub.title
        with self.assertRaises(AttributeError):
            sub.description

        sub.add_titles()

        self.assertEqual(sub.title, "henlo")
        self.assertEqual(sub.description, "bluh bluh")
Beispiel #5
0
    def test_bucket_edit_subs_qs(self):
        bucket = BucketFactory()
        other_user = UserFactory()

        my_subs = SubscriptionFactory.create_batch(2, user=bucket.user)
        SubscriptionFactory.create_batch(2, user=other_user)  # other subs

        form = BucketEditForm(instance=bucket)
        subs = list(form.fields["subs"].queryset)

        self.assertEqual(len(subs), 2)
        self.assertCountEqual(subs, list(my_subs))
Beispiel #6
0
    def test_video_from_subscription(self):
        user = UserFactory()
        sub1 = SubscriptionFactory(user=user)
        sub2 = SubscriptionFactory(user=user)
        video = VideoFactory(user=user, subscription=sub2)

        videos1 = Video.objects.from_subscription(user=user, subscription=sub1)
        videos2 = Video.objects.from_subscription(user=user, subscription=sub2)

        self.assertEqual(len(videos1), 0)
        self.assertEqual(len(videos2), 1)
        self.assertEqual(videos2[0], video)
Beispiel #7
0
    def test_subscription(self):
        response = self.client.get(
            reverse('subscription', kwargs={'subscription': 1}))
        self.assertEqual(response.status_code, 404)

        subscription = SubscriptionFactory()
        response = self.client.get(
            reverse('subscription', kwargs={'subscription': subscription.pk}))
        self.assertEqual(response.status_code, 404)

        subscription.user = self.user
        subscription.save()
        response = self.client.get(
            reverse('subscription', kwargs={'subscription': subscription.pk}))
        self.assertEqual(response.status_code, 200)
Beispiel #8
0
    def test_queryset_method(self):
        SubscriptionFactory(channel_id="123")
        SubscriptionFactory(channel_id="456")

        subs = Subscription.objects.all()
        for obj in subs:
            with self.assertRaises(AttributeError):
                obj.title
            with self.assertRaises(AttributeError):
                obj.description

        titled_subs = subs.add_titles()
        for obj in titled_subs:
            self.assertNotEqual(obj.title, None)
            self.assertNotEqual(obj.description, None)
Beispiel #9
0
    def test_get(self):
        subscription = SubscriptionFactory(user=self.user)
        video = VideoFactory(user=self.user, subscription=subscription)
        video = video.add_titles()

        response = self.client.get(
            reverse("subscription-video-api",
                    kwargs={"subscription": subscription.pk}))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(
            data, {
                "next":
                "{}?after={}".format(
                    reverse("subscription-video-api",
                            kwargs={"subscription": subscription.pk}),
                    video.ordering_key),
                "videos": [{
                    "id": video.youtube_id,
                    "title": video.title,
                    "description": video.description,
                    "published": datetime_to_js_iso(video.published_at),
                    "html_snippet": video.html_snippet,
                    "ordering_key": video.ordering_key,
                }],
            })
Beispiel #10
0
 def test_get_empty(self):
     subscription = SubscriptionFactory()
     response = self.client.get(
         reverse("subscription-video-api",
                 kwargs={"subscription": subscription.id}))
     self.assertEqual(response.status_code, 200)
     data = json.loads(response.content)
     self.assertEqual(data, {"videos": []})
Beispiel #11
0
 def test_bad_subscription_id(self):
     video = VideoFactory(user=self.user)
     data = {"id": video.youtube_id}
     response = self.client.post(reverse(
         "subscription-video-viewed-api",
         kwargs={"subscription": SubscriptionFactory(user=self.user).pk}),
                                 data=data)
     self.assertEqual(response.status_code, 404)
Beispiel #12
0
    def test_subscriptions(self):
        last_week = timezone.now() - timedelta(7)
        sub1 = SubscriptionFactory.create(user=self.user, channel_id="123", last_update=last_week)
        sub2 = SubscriptionFactory.create(user=self.user, channel_id="456", last_update=last_week)

        subscriptions(self.user.id)
        self.assertEqual(self.subscription_mock.call_count, 1)
        self.assertEqual(self.channel_mock.call_count, 1)

        sub1.refresh_from_db()
        self.assertNotEqual(sub1.last_update, last_week)

        sub2.refresh_from_db()
        self.assertNotEqual(sub2.last_update, last_week)

        # two import_video calls
        self.assertNumTasksEquals(2)
        # make sure it doens't infinitely loop
        self.process_task_queues()
Beispiel #13
0
    def test_overview_has_items(self):
        subscriptions = SubscriptionFactory.create_batch(2, user=self.user)
        buckets = [
            BucketFactory(user=self.user),
            BucketFactory(user=self.user, subs=[subscriptions[0]]),
            BucketFactory(),
        ]

        response = self.client.get(reverse('overview'))
        self.assertEqual(response.status_code, 200)
        self.assertIn(buckets[0].title, response.content)
        self.assertIn(buckets[1].title, response.content)
        self.assertNotIn(buckets[2].title, response.content)
Beispiel #14
0
    def test_post(self):
        video = VideoFactory(user=self.user,
                             subscription=SubscriptionFactory(user=self.user))
        data = {"id": video.youtube_id}
        response = self.client.post(reverse(
            "subscription-video-viewed-api",
            kwargs={"subscription": video.subscription_id}),
                                    data=data)
        self.assertEqual(response.status_code, 200)

        video.refresh_from_db()
        video.subscription.refresh_from_db()
        self.assertEqual(video.viewed, True)
        self.assertEqual(video.subscription.last_watched_video,
                         video.ordering_key)
Beispiel #15
0
    def test_cache_populated(self):
        sub = SubscriptionFactory.build(channel_id="123")

        cache.set(SUBSCRIPTION_TITLE_CACHE_PREFIX + "123", {
            "title": "henlo",
            "description": "bluh bluh"
        })

        results = list(subscription_add_titles([sub]))
        self.assertEqual(results, [sub])

        self.assertEqual(sub.title, "henlo")
        self.assertEqual(sub.description, "bluh bluh")

        self.assertEqual(self.channel_mock.call_count, 0)
Beispiel #16
0
    def test_clean_cache(self):
        sub = SubscriptionFactory.build(channel_id="123")

        cached_data = cache.get(SUBSCRIPTION_TITLE_CACHE_PREFIX + "123")
        self.assertEqual(cached_data, None)

        results = list(subscription_add_titles([sub]))
        self.assertEqual(results, [sub])

        self.assertEqual(sub.title, "henlo")
        self.assertEqual(sub.description, "bluh bluh")

        self.assertEqual(self.channel_mock.call_count, 1)
        self.assertEqual(self.channel_mock.call_args[1]["id"], "123")

        cached_data = cache.get(SUBSCRIPTION_TITLE_CACHE_PREFIX + "123")
        self.assertEqual(cached_data, {
            "title": "henlo",
            "description": "bluh bluh"
        })
Beispiel #17
0
    def test_get_with_after(self):
        subscription = SubscriptionFactory(user=self.user)
        VideoFactory.create_batch(3, user=self.user, subscription=subscription)
        videos = list(
            Video.objects.all().order_by("ordering_key").add_titles())

        response = self.client.get("{}?after={}".format(
            reverse("subscription-video-api",
                    kwargs={"subscription": subscription.pk}),
            videos[0].ordering_key))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(len(data["videos"]), 2)
        self.assertEqual(
            data, {
                "next":
                "{}?after={}".format(
                    reverse("subscription-video-api",
                            kwargs={"subscription": subscription.pk}),
                    videos[2].ordering_key),
                "videos": [
                    {
                        "id": videos[1].youtube_id,
                        "title": videos[1].title,
                        "description": videos[1].description,
                        "published": datetime_to_js_iso(
                            videos[1].published_at),
                        "html_snippet": videos[1].html_snippet,
                        "ordering_key": videos[1].ordering_key,
                    },
                    {
                        "id": videos[2].youtube_id,
                        "title": videos[2].title,
                        "description": videos[2].description,
                        "published": datetime_to_js_iso(
                            videos[2].published_at),
                        "html_snippet": videos[2].html_snippet,
                        "ordering_key": videos[2].ordering_key,
                    },
                ],
            })
Beispiel #18
0
    def test_bucket_edit(self):
        subscription = SubscriptionFactory(user=self.user)
        bucket = BucketFactory(user=self.user, title='Cheese')
        self.assertEqual(len(bucket.subs_ids), 0)
        data = {
            'title': 'Games',
            'subs': [subscription.pk],
        }
        response = self.client.post(
            reverse('bucket-edit', kwargs={'bucket': bucket.pk}), data)

        bucket.refresh_from_db()

        self.assertRedirects(response,
                             reverse('bucket', kwargs={'bucket': bucket.pk}))
        self.assertEqual(bucket.title, 'Games')
        self.assertEqual(bucket.subs_ids, set([subscription.pk]))

        new_bucket = BucketFactory(title='Cheese', user=self.user)
        response = self.client.post(
            reverse('bucket-edit', kwargs={'bucket': new_bucket.pk}), data)
        self.assertEqual(response.status_code, 200)