From b7c7fcc78028a0d324b3617c0dd195c7c4dae781 Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Sat, 8 Sep 2018 16:55:11 +0200 Subject: [PATCH] Add mocked test data for Fimfiction APIv1 --- tests/fetchers/test_fimfiction.json | 145 ++++++++++++++++++++++++++++ tests/fetchers/test_fimfiction.py | 35 ++++++- tests/fixtures/responses.py | 4 +- tox.ini | 1 - 4 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 tests/fetchers/test_fimfiction.json diff --git a/tests/fetchers/test_fimfiction.json b/tests/fetchers/test_fimfiction.json new file mode 100644 index 0000000..de6bd34 --- /dev/null +++ b/tests/fetchers/test_fimfiction.json @@ -0,0 +1,145 @@ +{ + "responses": [ + { + "method": "GET", + "status_code": 404, + "text": "REDACTED", + "url": "http://example.com/-" + }, + { + "json": { + "story": { + "author": { + "id": 197482, + "name": "That Naga Pone" + }, + "chapter_count": 1, + "chapters": [ + { + "date_modified": 1417226139, + "id": 666935, + "link": "https://www.fimfiction.net/story/208799/1/forgotten-ponies-of-equestria/chapter-1-celestias-and-lunas-3rd-sister-turnout", + "title": "Chapter 1: Celestia's and Luna's 3rd Sister Turnout", + "views": 37, + "words": 1893 + } + ], + "comments": 6, + "content_rating": 0, + "content_rating_text": "Everyone", + "date_modified": 1415461804, + "description": "REDACTED", + "dislikes": -1, + "id": 208799, + "likes": -1, + "short_description": "REDACTED", + "status": "Incomplete", + "title": "Forgotten Ponies of Equestria", + "total_views": 37, + "url": "https://www.fimfiction.net/story/208799/forgotten-ponies-of-equestria", + "views": 37, + "words": 1893 + } + }, + "method": "GET", + "status_code": 200, + "url": "https://www.fimfiction.net/api/story.php?story=208799" + }, + { + "json": { + "error": "Invalid story id" + }, + "method": "GET", + "status_code": 200, + "url": "https://www.fimfiction.net/api/story.php?story=7" + }, + { + "json": { + "story": { + "author": { + "id": 17, + "name": "Cheddah" + }, + "chapter_count": 0, + "comments": 1, + "content_rating": 0, + "content_rating_text": "Everyone", + "date_modified": 0, + "description": "REDACTED", + "dislikes": 8, + "id": 8, + "likes": 111, + "short_description": "REDACTED", + "status": "Incomplete", + "title": "The Reports of Agent X", + "total_views": 0, + "url": "https://www.fimfiction.net/story/8/the-reports-of-agent-x", + "views": 0, + "words": 0 + } + }, + "method": "GET", + "status_code": 200, + "url": "https://www.fimfiction.net/api/story.php?story=8" + }, + { + "json": { + "story": { + "author": { + "id": 18, + "name": "Sethisto" + }, + "chapter_count": 1, + "chapters": [ + { + "date_modified": 1390908352, + "id": 10, + "link": "https://www.fimfiction.net/story/9/1/the-greatest-equine-who-has-ever-lived/chapter-1", + "title": "Chapter 1", + "views": 10362, + "words": 321 + } + ], + "comments": 231, + "content_rating": 0, + "content_rating_text": "Everyone", + "date_modified": 1309035953, + "description": "REDACTED", + "dislikes": 52, + "full_image": "https://cdn-img.fimfiction.net/story/vr3n-1432418803-9-full", + "id": 9, + "image": "https://cdn-img.fimfiction.net/story/vr3n-1432418803-9-medium", + "likes": 393, + "short_description": "REDACTED", + "status": "Incomplete", + "title": "The Greatest Equine Who has Ever Lived!", + "total_views": 10362, + "url": "https://www.fimfiction.net/story/9/the-greatest-equine-who-has-ever-lived", + "views": 10362, + "words": 321 + } + }, + "method": "GET", + "status_code": 200, + "url": "https://www.fimfiction.net/api/story.php?story=9" + }, + { + "method": "GET", + "status_code": 403, + "text": "REDACTED", + "url": "https://www.fimfiction.net/story/download/208799/html" + }, + { + "method": "GET", + "status_code": 403, + "text": "REDACTED", + "url": "https://www.fimfiction.net/story/download/8/html" + }, + { + "method": "GET", + "status_code": 200, + "text": "

REDACTED

", + "url": "https://www.fimfiction.net/story/download/9/html" + } + ] +} \ No newline at end of file diff --git a/tests/fetchers/test_fimfiction.py b/tests/fetchers/test_fimfiction.py index 0d80a73..b05297c 100644 --- a/tests/fetchers/test_fimfiction.py +++ b/tests/fetchers/test_fimfiction.py @@ -5,7 +5,7 @@ Fimfiction fetcher tests. # # Fimfarchive, preserves stories from Fimfiction. -# Copyright (C) 2015 Joakim Soderlund +# Copyright (C) 2018 Joakim Soderlund # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -26,6 +26,9 @@ import pytest from fimfarchive.exceptions import InvalidStoryError, StorySourceError from fimfarchive.fetchers import FimfictionFetcher +from fimfarchive.utils import JayWalker + +from tests.fixtures.responses import Recorder VALID_STORY_KEY = 9 @@ -34,17 +37,42 @@ EMPTY_STORY_KEY = 8 PROTECTED_STORY_KEY = 208799 +class Redactor(JayWalker): + """ + Redacts recorded responses. + """ + + def wrap(self, text: str) -> str: + """ + Wraps text in a valid HTML body. + """ + return f"{text}" + + def handle(self, data, key, value) -> None: + if key in ('description', 'short_description'): + data[key] = "REDACTED" + elif key == 'text' and "

" in value: + data[key] = self.wrap("

REDACTED

") + elif key == 'text': + data[key] = self.wrap("REDACTED") + else: + self.walk(value) + + class TestFimfictionFetcher: """ FimfictionFetcher tests. """ @pytest.fixture - def fetcher(self): + def fetcher(self, responses): """ Returns the fetcher instance to test. """ - return FimfictionFetcher() + if isinstance(responses, Recorder): + responses.walker = Redactor() + + yield FimfictionFetcher() def test_with_statment(self): """ @@ -53,6 +81,7 @@ class TestFimfictionFetcher: with FimfictionFetcher() as fetcher: fetcher.get('http://example.com/') + @pytest.mark.xfail(reason="Recorder issue") def test_get_for_invalid_host(self, fetcher): """ Tests `StorySourceError` is raised if server is unreachable. diff --git a/tests/fixtures/responses.py b/tests/fixtures/responses.py index 9c38d83..9d51b1f 100644 --- a/tests/fixtures/responses.py +++ b/tests/fixtures/responses.py @@ -5,7 +5,7 @@ Requests mocking fixture. # # Fimfarchive, preserves stories from Fimfiction. -# Copyright (C) 2015 Joakim Soderlund +# Copyright (C) 2018 Joakim Soderlund # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -60,7 +60,7 @@ class Recorder(ContextManager['Recorder']): """ self.original = Session.send self.responses: Dict = dict() - self.walker: Optional[JayWalker] + self.walker: Optional[JayWalker] = None self.path = path def __iter__(self) -> Iterator[Dict[str, Any]]: diff --git a/tox.ini b/tox.ini index 9d3cd8e..61f00f8 100644 --- a/tox.ini +++ b/tox.ini @@ -18,7 +18,6 @@ commands = [pytest] addopts = --ignore tests/fetchers/test_fimfarchive.py - --ignore tests/fetchers/test_fimfiction.py tests [flake8]