Add mocked test data for Fimfiction APIv1

This commit is contained in:
Joakim Soderlund 2018-09-08 16:55:11 +02:00
parent d9a0a78c79
commit b7c7fcc780
4 changed files with 179 additions and 6 deletions

View file

@ -0,0 +1,145 @@
{
"responses": [
{
"method": "GET",
"status_code": 404,
"text": "<html><body>REDACTED</body></html>",
"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": "<html><body>REDACTED</body></html>",
"url": "https://www.fimfiction.net/story/download/208799/html"
},
{
"method": "GET",
"status_code": 403,
"text": "<html><body>REDACTED</body></html>",
"url": "https://www.fimfiction.net/story/download/8/html"
},
{
"method": "GET",
"status_code": 200,
"text": "<html><body><h1><a name='1'></a>REDACTED</h1></body></html>",
"url": "https://www.fimfiction.net/story/download/9/html"
}
]
}

View file

@ -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"<html><body>{text}</body></html>"
def handle(self, data, key, value) -> None:
if key in ('description', 'short_description'):
data[key] = "REDACTED"
elif key == 'text' and "<h1><a name='1'></a>" in value:
data[key] = self.wrap("<h1><a name='1'></a>REDACTED</h1>")
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.

View file

@ -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]]:

View file

@ -18,7 +18,6 @@ commands =
[pytest]
addopts =
--ignore tests/fetchers/test_fimfarchive.py
--ignore tests/fetchers/test_fimfiction.py
tests
[flake8]