Fixed flake8 errors for tests/test_api.py

Since I am adding a test I also took time to fix all flake8 errors in
this test file, since I may add more tests in there and I prefer to work
on a coherent code base.
This commit is contained in:
Romain Porte 2017-11-11 18:29:56 +01:00 committed by Boris Bobrov
parent f2b32fbf6a
commit 8b48db61d1

View File

@ -25,11 +25,11 @@ from webtest import AppError
from .resources import GOOD_JPG
from mediagoblin import mg_globals
from mediagoblin.db.models import User, Activity, MediaEntry, TextComment
from mediagoblin.tools.routing import extract_url_arguments
from mediagoblin.db.models import User, MediaEntry, TextComment
from mediagoblin.tests.tools import fixture_add_user
from mediagoblin.moderation.tools import take_away_privileges
class TestAPI(object):
""" Test mediagoblin's pump.io complient APIs """
@ -38,7 +38,8 @@ class TestAPI(object):
self.test_app = test_app
self.db = mg_globals.database
self.user = fixture_add_user(privileges=[u'active', u'uploader', u'commenter'])
self.user = fixture_add_user(privileges=[u'active', u'uploader',
u'commenter'])
self.other_user = fixture_add_user(
username="otheruser",
privileges=[u'active', u'uploader', u'commenter']
@ -134,7 +135,7 @@ class TestAPI(object):
assert data["object"]["image"]["url"].endswith("unknown.thumbnail.jpe")
def test_can_post_image_custom_filename(self, test_app):
""" Tests that an image can be posted to the API with custom filename """
""" Tests an image can be posted to the API with custom filename """
# First request we need to do is to upload the image
response, image = self._upload_image(test_app, GOOD_JPG,
custom_filename="hello.jpg")
@ -200,7 +201,7 @@ class TestAPI(object):
assert "403 FORBIDDEN" in excinfo.value.args[0]
def test_only_able_to_update_own_image(self, test_app):
""" Test's that the uploader is the only person who can update an image """
""" Test uploader is the only person who can update an image """
response, data = self._upload_image(test_app, GOOD_JPG)
response, data = self._post_image_to_feed(test_app, data)
@ -214,13 +215,16 @@ class TestAPI(object):
}
# Lets change the image uploader to be self.other_user, this is easier
# than uploading the image as someone else as the way self.mocked_oauth_required
# and self._upload_image.
media = MediaEntry.query.filter_by(public_id=data["object"]["id"]).first()
# than uploading the image as someone else as the way
# self.mocked_oauth_required and self._upload_image.
media = MediaEntry.query \
.filter_by(public_id=data["object"]["id"]) \
.first()
media.actor = self.other_user.id
media.save()
# Now lets try and edit the image as self.user, this should produce a 403 error.
# Now lets try and edit the image as self.user, this should produce a
# 403 error.
with self.mock_oauth():
with pytest.raises(AppError) as excinfo:
test_app.post(
@ -270,7 +274,6 @@ class TestAPI(object):
assert image["content"] == description
assert image["license"] == license
def test_only_uploaders_post_image(self, test_app):
""" Test that only uploaders can upload images """
# Remove uploader permissions from user
@ -316,6 +319,8 @@ class TestAPI(object):
image = json.loads(request.body.decode())
entry = MediaEntry.query.filter_by(public_id=image["id"]).first()
assert entry is not None
assert request.status_code == 200
assert "image" in image
@ -344,7 +349,9 @@ class TestAPI(object):
assert response.status_code == 200
# Find the objects in the database
media = MediaEntry.query.filter_by(public_id=data["object"]["id"]).first()
media = MediaEntry.query \
.filter_by(public_id=data["object"]["id"]) \
.first()
comment = media.get_comments()[0].comment()
# Tests that it matches in the database
@ -406,7 +413,9 @@ class TestAPI(object):
response, comment_data = self._activity_to_feed(test_app, activity)
# change who uploaded the comment as it's easier than changing
comment = TextComment.query.filter_by(public_id=comment_data["object"]["id"]).first()
comment = TextComment.query \
.filter_by(public_id=comment_data["object"]["id"]) \
.first()
comment.actor = self.other_user.id
comment.save()
@ -460,7 +469,7 @@ class TestAPI(object):
def test_whoami_without_login(self, test_app):
""" Test that whoami endpoint returns error when not logged in """
with pytest.raises(AppError) as excinfo:
response = test_app.get("/api/whoami")
test_app.get("/api/whoami")
assert "401 UNAUTHORIZED" in excinfo.value.args[0]
@ -649,8 +658,11 @@ class TestAPI(object):
delete = self._activity_to_feed(test_app, activity)[1]
# Verify the comment no longer exists
assert TextComment.query.filter_by(public_id=comment["object"]["id"]).first() is None
comment_id = comment["object"]["id"]
assert TextComment.query \
.filter_by(public_id=comment["object"]["id"]) \
.first() is None
assert "id" in comment["object"]
# Check we've got a delete activity back
assert "id" in delete
@ -690,6 +702,8 @@ class TestAPI(object):
comment = self._activity_to_feed(test_app, activity)[1]
# Verify the comment reflects the changes
model = TextComment.query.filter_by(public_id=comment["object"]["id"]).first()
model = TextComment.query \
.filter_by(public_id=comment["object"]["id"]) \
.first()
assert model.content == activity["object"]["content"]