From 46d8bbd7fcd695650a12b83fb7ba6b5bfcf61a31 Mon Sep 17 00:00:00 2001 From: Nahue Date: Mon, 25 May 2020 21:48:51 -0300 Subject: [PATCH] Hacky test for checking correct tweeting directly from the project --- .env.sample | 5 +++++ config-test.yaml | 7 +++++++ diffengine/twitter.py | 5 +++++ test_diffengine.py | 30 +++++++++++++++++++++++++++++- 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 .env.sample create mode 100644 config-test.yaml diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..33b38ca --- /dev/null +++ b/.env.sample @@ -0,0 +1,5 @@ +TEST_DATABASE_URL=postgres://nsotelo@localhost/diffengine_test?autorollback=true +TEST_CONSUMER_KEY=XOPVgJv5pWFVzru2QekheieNE +TEST_CONSUMER_SECRET=xKJgA6yCn8KR9Xn5EZ8AskW8Ki3DaADEkmOrHpQJrf7icMLvfD +TEST_ACCESS_TOKEN=1251342388813090816-boWeRcbEjQpkKXVxNSOHeQtrJCcWZH +TEST_ACCESS_TOKEN_SECRET=18SVyF10ZxbqXMJIUj63aH9FeGlPURIie1aGIehFvzByp diff --git a/config-test.yaml b/config-test.yaml new file mode 100644 index 0000000..9b963ae --- /dev/null +++ b/config-test.yaml @@ -0,0 +1,7 @@ +db: "${TEST_DATABASE_URL}" +twitter: + consumer_key: "${TEST_CONSUMER_KEY}" + consumer_secret: "${TEST_CONSUMER_SECRET}" + token: + access_token: "${TEST_ACCESS_TOKEN}" + access_token_secret: "${TEST_ACCESS_TOKEN_SECRET}" diff --git a/diffengine/twitter.py b/diffengine/twitter.py index 1e29674..e886aff 100644 --- a/diffengine/twitter.py +++ b/diffengine/twitter.py @@ -98,3 +98,8 @@ def tweet_diff(self, diff, token=None, lang={}): diff.save() except Exception as e: logging.error("unable to tweet: %s", e) + + def delete_diff(self, diff, token=None): + twitter = self.api(token) + twitter.destroy_status(diff.old.tweet_status_id_str) + twitter.destroy_status(diff.new.tweet_status_id_str) diff --git a/test_diffengine.py b/test_diffengine.py index 6947e04..3da01e9 100644 --- a/test_diffengine.py +++ b/test_diffengine.py @@ -2,6 +2,8 @@ import os import re import yaml +from envyaml import EnvYAML + import setup import pytest import shutil @@ -43,6 +45,7 @@ ) test_home = "test" +test_config = EnvYAML("config-test.yaml", env_file=".env") if os.path.isdir(test_home): shutil.rmtree(test_home) @@ -66,7 +69,7 @@ class FeedTest(TestCase): version = None def setUp(self) -> None: - generate_config(test_home, {"db": "sqlite:///:memory:"}) + generate_config(test_home, {"db": test_config.get("db", "sqlite:///:memory:")}) # set things up but disable prompting for initial feed init(test_home, prompt=False) self.feed = Feed.create(name="Test", url="https://inkdroid.org/feed.xml") @@ -111,6 +114,31 @@ def test_diff(self): "^https://web.archive.org/web/diff/\\d+/\\d+/https.+$", diff.url ) + def test_tweet_diff(self): + e = self.entry + v1 = e.versions[0] + + # remove some characters from the version + v1.summary = v1.summary[0:-20] + v1.save() + + v2 = e.get_latest() + + # Actual tweeting purposes only + # run this alone for checking correct tweeting behavior + if v2 is not None: + diff = v2.diff + try: + token = test_config.get("twitter.token") + twitter_handler = TwitterHandler( + test_config.get("twitter.consumer_key"), + test_config.get("twitter.consumer_secret"), + ) + twitter_handler.tweet_diff(diff, token) + twitter_handler.delete_diff(diff, token) + except Exception: + logging.debug("no tweet configured for test. Doing nothing") + def test_html_diff(self): e = self.entry