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