From 18068d76ca304a55dffdb4fb54c674bb1dcc148f Mon Sep 17 00:00:00 2001 From: Miguel Victoria Villaquiran Date: Tue, 5 Jan 2021 22:17:26 +0100 Subject: Simplify tests (#685) Fix #501 --- ihatemoney/tests/common/help_functions.py | 5 ++ ihatemoney/tests/common/ihatemoney_testcase.py | 70 ++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 ihatemoney/tests/common/help_functions.py create mode 100644 ihatemoney/tests/common/ihatemoney_testcase.py (limited to 'ihatemoney/tests/common') diff --git a/ihatemoney/tests/common/help_functions.py b/ihatemoney/tests/common/help_functions.py new file mode 100644 index 0000000..e9c4dcd --- /dev/null +++ b/ihatemoney/tests/common/help_functions.py @@ -0,0 +1,5 @@ +def em_surround(string, regex_escape=False): + if regex_escape: + return r'%s<\/em>' % string + else: + return '%s' % string diff --git a/ihatemoney/tests/common/ihatemoney_testcase.py b/ihatemoney/tests/common/ihatemoney_testcase.py new file mode 100644 index 0000000..2e59059 --- /dev/null +++ b/ihatemoney/tests/common/ihatemoney_testcase.py @@ -0,0 +1,70 @@ +from flask_testing import TestCase +from werkzeug.security import generate_password_hash + +from ihatemoney import models +from ihatemoney.run import create_app, db + + +class BaseTestCase(TestCase): + + SECRET_KEY = "TEST SESSION" + + def create_app(self): + # Pass the test object as a configuration. + return create_app(self) + + def setUp(self): + db.create_all() + + def tearDown(self): + # clean after testing + db.session.remove() + db.drop_all() + + def login(self, project, password=None, test_client=None): + password = password or project + + return self.client.post( + "/authenticate", + data=dict(id=project, password=password), + follow_redirects=True, + ) + + def post_project(self, name, follow_redirects=True): + """Create a fake project""" + # create the project + return self.client.post( + "/create", + data={ + "name": name, + "id": name, + "password": name, + "contact_email": f"{name}@notmyidea.org", + "default_currency": "USD", + }, + follow_redirects=follow_redirects, + ) + + def create_project(self, name): + project = models.Project( + id=name, + name=str(name), + password=generate_password_hash(name), + contact_email=f"{name}@notmyidea.org", + default_currency="USD", + ) + models.db.session.add(project) + models.db.session.commit() + + +class IhatemoneyTestCase(BaseTestCase): + SQLALCHEMY_DATABASE_URI = "sqlite://" + TESTING = True + WTF_CSRF_ENABLED = False # Simplifies the tests. + + def assertStatus(self, expected, resp, url=""): + return self.assertEqual( + expected, + resp.status_code, + f"{url} expected {expected}, got {resp.status_code}", + ) -- cgit v1.1