-
-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83ac4de
commit e0698b2
Showing
13 changed files
with
190 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
from flask import Blueprint, current_app as app, send_file | ||
from flask import Blueprint, current_app as app, redirect, url_for, send_file | ||
|
||
from .. import domain | ||
|
||
blueprint = Blueprint('image', __name__, url_prefix="/") | ||
|
||
|
||
@blueprint.route("<key>/<top>/<bottom>.<kind>") | ||
def get(key, top, bottom, kind): | ||
def get_visible(key, top, bottom, kind): | ||
template = domain.Template(key) | ||
text = domain.Text(top, bottom) | ||
_path = app.image_service.create_image(template, text, kind) | ||
return send_file(_path, mimetype='image/png') | ||
return send_file(_path, mimetype='image/jpeg') | ||
|
||
|
||
@blueprint.route("<code>.<kind>") | ||
def get_hidden(code, kind): | ||
key, top, bottom = app.link_service.decode(code) | ||
# TODO: maybe this shouldn't redirect | ||
# url = url_for('.get_visible', key=key, top=top, bottom=bottom, kind=kind) | ||
# return redirect(url) | ||
template = domain.Template(key) | ||
text = domain.Text(top, bottom) | ||
_path = app.image_service.create_image(template, text, kind) | ||
return send_file(_path, mimetype='image/jpeg') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
from flask import Blueprint | ||
from collections import OrderedDict | ||
|
||
from flask import Blueprint, current_app as app, url_for | ||
|
||
from ..domain import Image | ||
|
||
|
||
blueprint = Blueprint('link', __name__, url_prefix="/") | ||
|
||
|
||
@blueprint.route("<key>/<top>/<bottom>") | ||
def get(key, top, bottom): | ||
return {} | ||
def get(**kwargs): | ||
data = OrderedDict() | ||
data['visible'] = OrderedDict() | ||
data['hidden'] = OrderedDict() | ||
for kind in Image.KINDS: | ||
url = url_for('image.get_visible', kind=kind, _external=True, **kwargs) | ||
data['visible'][kind] = url | ||
code = app.link_service.encode(**kwargs) | ||
url = url_for('image.get_hidden', kind=kind, _external=True, code=code) | ||
data['hidden'][kind] = url | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,2 @@ | ||
from abc import ABCMeta | ||
|
||
|
||
class Exceptions: | ||
|
||
def __init__(self, missing=KeyError): | ||
self.missing = missing | ||
|
||
|
||
class Service(metaclass=ABCMeta): | ||
|
||
"""Base class for domain services.""" | ||
|
||
def __init__(self, exceptions=None): | ||
self.exceptions = exceptions or Exceptions() | ||
|
||
|
||
from ._base import Exceptions, Service | ||
from . import image, link, template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from abc import ABCMeta | ||
|
||
|
||
class Exceptions: | ||
|
||
def __init__(self, not_found=KeyError, bad_code=ValueError): | ||
self.not_found = not_found | ||
self.bad_code = bad_code | ||
|
||
|
||
class Service(metaclass=ABCMeta): | ||
|
||
"""Base class for domain services.""" | ||
|
||
def __init__(self, exceptions=None): | ||
self.exceptions = exceptions or Exceptions() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from memegen import services | ||
|
||
|
||
@pytest.fixture | ||
def link_service(): | ||
return services.link.LinkService(template_store=Mock()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
|
||
|
||
class TestLinkService: | ||
|
||
def test_decode_encoded_parts(self, link_service): | ||
code = link_service.encode("a", "b", "c") | ||
parts = link_service.decode(code) | ||
assert ("a", "b", "c") == parts | ||
|
||
def test_decode_invalid_code(self, link_service): | ||
with pytest.raises(ValueError): | ||
link_service.decode("bad_code") | ||
|
||
def test_decode_empty_code(self, link_service): | ||
with pytest.raises(ValueError): | ||
link_service.decode(b"") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
|
||
from memegen.settings import Config, get_config | ||
|
||
|
||
class TestGetConfig: | ||
|
||
def test_get_valid(self): | ||
config = get_config('prod') | ||
assert issubclass(config, Config) | ||
|
||
def test_get_none(self): | ||
with pytest.raises(AssertionError): | ||
get_config('') | ||
|
||
def test_get_unknown(self): | ||
with pytest.raises(AssertionError): | ||
get_config('not_a_valid_config') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import json | ||
import logging | ||
|
||
import pytest | ||
|
||
from memegen.app import create_app | ||
from memegen.settings import get_config | ||
|
||
|
||
def load(response): | ||
"""Convert a response's binary data (JSON) to a dictionary.""" | ||
text = response.data.decode('utf-8') | ||
if text: | ||
data = json.loads(text) | ||
else: | ||
data = None | ||
logging.debug("response: %r", data) | ||
return data | ||
|
||
|
||
|
||
@pytest.fixture | ||
def app(): | ||
return create_app(get_config('test')) | ||
|
||
|
||
@pytest.fixture | ||
def client(app): | ||
return app.test_client() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from .conftest import load | ||
|
||
|
||
class TestMeme: | ||
|
||
def test_get_jpg(self, client): | ||
response = client.get("/iw/hello/world.jpg") | ||
assert response.status_code == 200 | ||
assert response.mimetype == 'image/jpeg' | ||
|
||
# TODO: add more image types | ||
|
||
# def test_get_jpeg(self, client): | ||
# response = client.get("/iw/hello/world.jpeg") | ||
# assert response.status_code == 200 | ||
# assert response.mimetype == 'image/jpeg' | ||
|
||
# def test_get_png(self, client): | ||
# response = client.get("/iw/hello/world.png") | ||
# assert response.status_code == 200 | ||
# assert response.mimetype == 'image/png' | ||
|
||
# def test_get_gif(self, client): | ||
# response = client.get("/iw/hello/world.gif") | ||
# assert response.status_code == 200 | ||
# assert response.mimetype == 'image/gif' | ||
|
||
|
||
class TestLink: | ||
|
||
def test_get_links(self, client): | ||
response = client.get("/iw/hello/world") | ||
assert response.status_code == 200 | ||
assert load(response) == dict( | ||
visible=dict( | ||
jpg="http://localhost/iw/hello/world.jpg", | ||
), | ||
hidden=dict( | ||
jpg="http://localhost/aXcJaGVsbG8Jd29ybGQg.jpg", | ||
), | ||
) |