Skip to content

Commit

Permalink
Update: Add tests for controller
Browse files Browse the repository at this point in the history
Signed-off-by: Preetham Kamidi <kamidipreetham@gmail.com>
  • Loading branch information
preetham committed Jul 8, 2019
1 parent 7c4b3c1 commit 97dc1fc
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
install_requires=[
"click>=5.1", "Pillow==6.0.0", "pytesseract==0.2.6",
"requests==2.22.0", "scikit-learn==0.21.2", "nltk>=3.4.3",
"python-dateutil==2.8.0",
"python-dateutil==2.8.0", "werkzeug==0.15.4",
"twint @ git+https://github.com/twintproject/twint.git"
],
entry_points={
Expand Down
Binary file modified tests/static/real-tweet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Verify Tweet verifies tweets of a public user
# from tweet screenshots: real or generated from
# tweet generators.
# Copyright (C) 2019 Preetham Kamidi

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import pytest

from verifytweet import controller
from verifytweet import result

non_api_approach = controller.NonAPIApproach()


def test_exec_empty_input():
"""Test exec for empty input
"""
with pytest.raises(TypeError):
non_api_approach.exec()


def test_exec_invalid_type_input():
"""Test exec for invalid type input
"""
with pytest.raises(TypeError):
non_api_approach.exec(None)
non_api_approach.exec(123)
non_api_approach.exec(['/home/'])


def test_exec_invalid_input():
"""Test exec for valid type invalid input
"""
with pytest.raises(ValueError):
non_api_approach.exec('')
assert non_api_approach.exec(
'123')[1] == result.ResultStatus.MODULE_FAILURE
assert non_api_approach.exec(
'/home')[1] == result.ResultStatus.MODULE_FAILURE
assert non_api_approach.exec(
'tmp.png')[1] == result.ResultStatus.MODULE_FAILURE


def test_exec_valid_input(file_path):
"""Test exec for valid input
"""
from twint.tweet import tweet

test_result_tweet = 'ey send me stolen pdp wave designs'
test_result_username = 'pewdiepie'
module_result, module_status = non_api_approach.exec(file_path)
assert module_status == result.ResultStatus.ALL_OKAY
assert isinstance(module_result, tweet)
assert test_result_tweet in module_result.tweet
assert test_result_username == module_result.username
4 changes: 4 additions & 0 deletions tests/test_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from werkzeug.datastructures import FileStorage

from verifytweet import uploader
from verifytweet import settings


def test_save_to_disk_empty_input():
Expand Down Expand Up @@ -51,6 +52,9 @@ def test_save_to_disk_invalid_input():
def test_save_to_disk_valid_input(file_path):
"""Test save to disk for valid file object
"""
app_config = settings.app_config
app_config.ALLOWED_EXTENSIONS = set(["png", "jpg", "jpeg"])

with open(file_path, 'rb') as f:
test_file_obj = FileStorage(f)
test_file_name = uploader.save_to_disk(test_file_obj)
Expand Down
3 changes: 0 additions & 3 deletions verifytweet/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os

import click

os.environ["VERIFYTWEET_RUN_FROM_CLI"] = "true"

from .services import controller
from .config.settings import app_config
from .util.logging import logger
Expand Down
4 changes: 2 additions & 2 deletions verifytweet/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Config(object):
FILE_DIRECTORY = tempfile.mkdtemp()
TWEET_MAX_STORE = 150
RUN_METHOD = "cli"
LOG_LEVEL = logging.DEBUG if os.getenv('VERBOSE_LOGS') else logging.INFO
LOG_LEVEL = logging.DEBUG if os.getenv('DEBUG') else logging.INFO


class TwitterAPIConfig(Config):
Expand Down Expand Up @@ -77,7 +77,7 @@ class WebConfig(Config):
ALLOWED_EXTENSIONS = set(["png", "jpg", "jpeg"])


run_method = "cli" if "VERIFYTWEET_RUN_FROM_CLI" in os.environ else "web"
run_method = "web" if "VERIFYTWEET_RUN_FOR_WEB" in os.environ else "cli"
Config.RUN_METHOD = run_method
configurations = {"web": WebConfig, "cli": Config}

Expand Down
3 changes: 2 additions & 1 deletion verifytweet/services/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,19 @@ def search(self, user_id: str, date: datetime.datetime,
)
if not user_id or not date or not tweet_snippet:
raise ValueError('User ID, Tweet or Date cannot be empty')
results = list()
twint_config = twint.Config()
twint_config.Username = user_id
twint_config.Search = tweet_snippet
twint_config.Since = date_checker.format_for_date(date)
twint_config.Limit = app_config.TWEET_MAX_STORE
twint_config.Store_object = True
twint_config.Store_object_tweets_list = results
try:
twint.run.Search(twint_config)
except Exception as e:
logger.exception(e)
return (None, ResultStatus.MODULE_FAILURE)
results = twint.output.tweets_object
if not results:
return (results, ResultStatus.NO_RESULT)
logger.debug(f'Search results: {results}\n')
Expand Down
2 changes: 1 addition & 1 deletion verifytweet/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from verifytweet.config.settings import app_config

logger = logging.getLogger()
logger = logging.getLogger('verify_logger')
logger.setLevel(app_config.LOG_LEVEL)

handler = logging.StreamHandler(sys.stdout)
Expand Down
2 changes: 2 additions & 0 deletions wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import gunicorn.app.base
from gunicorn.six import iteritems

os.environ["VERIFYTWEET_RUN_FOR_WEB"] = "true"

from verifytweet.config.settings import app_config
from verifytweet.app import router

Expand Down

0 comments on commit 97dc1fc

Please sign in to comment.