def mutate_and_get_payload(cls, input, info): user = info.request_context.user if not user.is_authenticated(): raise Exception("You need to be logged in to unsubscribe from podcasts.") podcast = get_podcast_by_url(input["podcast_url"]) result = unsubscribe_user_from_podcast(user, podcast) return Unsubscribe(user=user, podcast=podcast, success=result)
def test_get_podcast_by_url_retrieves_model(monkeypatch): """Test that get_podcast_by_url returns the podcast model from the database if it exists.""" podcast_model = get_valid_podcast_model() objects_get_mock = Mock(return_value=podcast_model) monkeypatch.setattr(models.Podcast.objects, "get", objects_get_mock) result = services.get_podcast_by_url(FEED_URL) assert result == podcast_model models.Podcast.objects.get.assert_called_with(url=FEED_URL)
def test_get_podcast_by_url_fetches_podcast_if_not_in_db(monkeypatch): """Test that get_podcast_by_url fetches the podcast if it isn't in the database""" # We want to force the podcast to be fetched, so Podcast.objects.get needs to raise an error. objects_get_mock = Mock(side_effect=models.models.ObjectDoesNotExist()) # Set up the mock value for the fetcher to return. podcast_model = get_valid_podcast_model() parsed_podcast_mock = Mock(spec=parsed_podcasts.ParsedPodcast()) parsed_podcast_mock.save_to_db.return_value = podcast_model fetch_mock = Mock(return_value=parsed_podcast_mock) monkeypatch.setattr(models.Podcast.objects, "get", objects_get_mock) monkeypatch.setattr(fetcher, "fetch", fetch_mock) result = services.get_podcast_by_url(FEED_URL) # Assert that it checks the db first, models.Podcast.objects.get.assert_called_with(url=FEED_URL) # Assert that it calls the fetcher. fetcher.fetch.asert_called_with(FEED_URL) # Assert that it calls the save_to_db of the parsed podcast. parsed_podcast_mock.save_to_db.assert_called_with() # Assert that it returns the model, as returned from save_to_db. assert result == podcast_model
def resolve_podcast_by_url(self, args, info): return PodcastNode(get_podcast_by_url(args.get("url")))