def test_subscribe_user_by_urls_already_subscribed(db): """Test that the user isn't subscribed twice to a podcast.""" url = "http://example.com/feed" podcast = get_valid_podcast_model(url) podcast.save() user = get_valid_user() user.save() user.subscription_objs.create(podcast=podcast) services.subscribe_user_by_urls(user, [url]) assert len(user.subscription_objs.all()) == 1
def mutate_and_get_payload(cls, input, info): if not info.request_context.user.is_authenticated(): raise Exception("You need to be logged in to subscribe to podcasts.") urls = input["feed_urls"] result_dict = subscribe_user_by_urls(info.request_context.user, urls) for i in xrange(len(urls)): urls[i] = result_dict[urls[i]] return Subscribe(user=UserNode(info.request_context.user), success=urls)
def test_subscribe_user_by_urls(monkeypatch): url1 = "http://example1.com/feed" url2 = "http://example2.com/feed" podcast1 = get_valid_podcast_model(url1) podcast2 = get_valid_podcast_model(url2) get_multi_podcast_mock = Mock(return_value={ url1: podcast1, url2: podcast2 }) user_model = get_valid_user() subscription_mock = Mock() monkeypatch.setattr(services, "get_multi_podcasts_by_url", get_multi_podcast_mock) monkeypatch.setattr(services, "Subscription", subscription_mock) monkeypatch.setattr(models,"Subscription", subscription_mock) services.subscribe_user_by_urls(user_model, [url1, url2]) get_multi_podcast_mock.assert_called_with([url1, url2]) assert subscription_mock.objects.bulk_create.call_count == 1 subscription_mock.objects.bulk_create.assert_called_with([models.Subscription(podcast=podcast1), models.Subscription(podcast=podcast2)])