mirror of
https://github.com/Casvt/MIND.git
synced 2026-04-03 03:00:22 -04:00
Refactored backend (Fixes #87)
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from frontend.api import api
|
||||
from frontend.ui import ui
|
||||
from backend.server import SERVER
|
||||
|
||||
class Test_MIND(unittest.TestCase):
|
||||
def test_create_app(self):
|
||||
SERVER.create_app()
|
||||
self.assertTrue(hasattr(SERVER, 'app'))
|
||||
app = SERVER.app
|
||||
self.assertIsInstance(app, Flask)
|
||||
|
||||
self.assertEqual(app.blueprints.get('ui'), ui)
|
||||
self.assertEqual(app.blueprints.get('api'), api)
|
||||
|
||||
handlers = app.error_handler_spec[None].keys()
|
||||
required_handlers = 400, 405, 500
|
||||
for handler in required_handlers:
|
||||
self.assertIn(handler, handlers)
|
||||
24
tests/Tbackend/MIND_test.py
Normal file
24
tests/Tbackend/MIND_test.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import unittest
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from backend.internals.server import Server
|
||||
from frontend.api import api
|
||||
from frontend.ui import ui
|
||||
|
||||
|
||||
class Test_MIND(unittest.TestCase):
|
||||
def test_create_app(self):
|
||||
SERVER = Server()
|
||||
SERVER.create_app()
|
||||
self.assertTrue(hasattr(SERVER, 'app'))
|
||||
app = SERVER.app
|
||||
self.assertIsInstance(app, Flask)
|
||||
|
||||
self.assertEqual(app.blueprints.get('ui'), ui)
|
||||
self.assertEqual(app.blueprints.get('api'), api)
|
||||
|
||||
handlers = app.error_handler_spec[None].keys()
|
||||
required_handlers = 400, 405, 500
|
||||
for handler in required_handlers:
|
||||
self.assertIn(handler, handlers)
|
||||
0
tests/Tbackend/__init__.py
Normal file
0
tests/Tbackend/__init__.py
Normal file
5
tests/Tbackend/custom_exceptions_test.py
Normal file
5
tests/Tbackend/custom_exceptions_test.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import unittest
|
||||
|
||||
|
||||
class Test_Custom_Exceptions(unittest.TestCase):
|
||||
pass
|
||||
26
tests/Tbackend/db_test.py
Normal file
26
tests/Tbackend/db_test.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import unittest
|
||||
from os.path import join
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from backend.base.helpers import folder_path
|
||||
from backend.internals.db import Constants, DBConnection, close_db
|
||||
|
||||
|
||||
class Test_DB(unittest.TestCase):
|
||||
def test_foreign_key_and_wal(self):
|
||||
app = Flask(__name__)
|
||||
app.teardown_appcontext(close_db)
|
||||
|
||||
DBConnection.file = join(
|
||||
folder_path(*Constants.DB_FOLDER),
|
||||
Constants.DB_NAME
|
||||
)
|
||||
with app.app_context():
|
||||
instance = DBConnection(timeout=Constants.DB_TIMEOUT)
|
||||
self.assertEqual(
|
||||
instance.cursor().execute(
|
||||
"PRAGMA foreign_keys;"
|
||||
).fetchone()[0],
|
||||
1
|
||||
)
|
||||
19
tests/Tbackend/reminders_test.py
Normal file
19
tests/Tbackend/reminders_test.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import unittest
|
||||
|
||||
from backend.base.definitions import GeneralReminderData
|
||||
from backend.base.helpers import search_filter
|
||||
|
||||
|
||||
class Test_Reminder_Handler(unittest.TestCase):
|
||||
def test_filter_function(self):
|
||||
p = GeneralReminderData(
|
||||
id=1,
|
||||
title='TITLE',
|
||||
text='TEXT',
|
||||
color=None,
|
||||
notification_services=[]
|
||||
)
|
||||
for test_case in ('', 'title', 'ex'):
|
||||
self.assertTrue(search_filter(test_case, p))
|
||||
for test_case in (' ', 'Hello'):
|
||||
self.assertFalse(search_filter(test_case, p))
|
||||
10
tests/Tbackend/security_test.py
Normal file
10
tests/Tbackend/security_test.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import unittest
|
||||
|
||||
from backend.base.helpers import generate_salt_hash, get_hash
|
||||
|
||||
|
||||
class Test_Security(unittest.TestCase):
|
||||
def test_hash(self):
|
||||
for test_case in ('test', ''):
|
||||
result = generate_salt_hash(test_case)
|
||||
self.assertEqual(result[1], get_hash(result[0], test_case))
|
||||
21
tests/Tbackend/ui_test.py
Normal file
21
tests/Tbackend/ui_test.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import unittest
|
||||
|
||||
from flask import Blueprint, Flask
|
||||
|
||||
from frontend.ui import methods, ui
|
||||
|
||||
|
||||
class Test_UI(unittest.TestCase):
|
||||
def test_methods(self):
|
||||
self.assertEqual(len(methods), 1)
|
||||
self.assertEqual(methods[0], 'GET')
|
||||
|
||||
def test_blueprint(self):
|
||||
self.assertIsInstance(ui, Blueprint)
|
||||
|
||||
def test_route_methods(self):
|
||||
temp_app = Flask(__name__)
|
||||
temp_app.register_blueprint(ui)
|
||||
for rule in temp_app.url_map.iter_rules():
|
||||
self.assertEqual(len(rule.methods or []), 3)
|
||||
self.assertIn(methods[0], rule.methods or [])
|
||||
15
tests/Tbackend/users_test.py
Normal file
15
tests/Tbackend/users_test.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import unittest
|
||||
|
||||
from backend.base.custom_exceptions import UsernameInvalid
|
||||
from backend.base.definitions import Constants
|
||||
from backend.implementations.users import is_valid_username
|
||||
|
||||
|
||||
class Test_Users(unittest.TestCase):
|
||||
def test_username_check(self):
|
||||
for test_case in ('', 'test'):
|
||||
is_valid_username(test_case)
|
||||
|
||||
for test_case in (' ', ' ', '0', 'api', *Constants.INVALID_USERNAMES):
|
||||
with self.assertRaises(UsernameInvalid):
|
||||
is_valid_username(test_case)
|
||||
@@ -1,25 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from flask import Blueprint
|
||||
|
||||
from backend.custom_exceptions import *
|
||||
from frontend.api import api, return_api
|
||||
|
||||
class Test_API(unittest.TestCase):
|
||||
def test_blueprint(self):
|
||||
self.assertIsInstance(api, Blueprint)
|
||||
|
||||
def test_return_api(self):
|
||||
for case in ({'result': {}, 'error': 'Error', 'code': 201},
|
||||
{'result': ''}):
|
||||
result = return_api(**case)
|
||||
self.assertEqual(result[0]['result'], case['result'])
|
||||
if case.get('error'):
|
||||
self.assertEqual(result[0]['error'], case['error'])
|
||||
else:
|
||||
self.assertIsNone(result[0]['error'])
|
||||
if case.get('code'):
|
||||
self.assertEqual(result[1], case['code'])
|
||||
else:
|
||||
self.assertEqual(result[1], 200)
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
import unittest
|
||||
from inspect import getmembers, getmro, isclass
|
||||
from sys import modules
|
||||
from typing import List
|
||||
|
||||
import backend.custom_exceptions
|
||||
|
||||
class Test_Custom_Exceptions(unittest.TestCase):
|
||||
def test_type(self):
|
||||
defined_exceptions: List[Exception] = filter(
|
||||
lambda c: c.__module__ == 'backend.custom_exceptions'
|
||||
and c is not backend.custom_exceptions.CustomException,
|
||||
map(
|
||||
lambda c: c[1],
|
||||
getmembers(modules['backend.custom_exceptions'], isclass)
|
||||
)
|
||||
)
|
||||
|
||||
for defined_exception in defined_exceptions:
|
||||
self.assertIn(
|
||||
getmro(defined_exception)[1],
|
||||
(
|
||||
backend.custom_exceptions.CustomException,
|
||||
Exception
|
||||
)
|
||||
)
|
||||
try:
|
||||
result = defined_exception().api_response
|
||||
except TypeError:
|
||||
try:
|
||||
result = defined_exception('1').api_response
|
||||
except TypeError:
|
||||
result = defined_exception('1', '2').api_response
|
||||
|
||||
self.assertIsInstance(result, dict)
|
||||
result['error']
|
||||
result['result']
|
||||
result['code']
|
||||
self.assertIsInstance(result['code'], int)
|
||||
@@ -1,11 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from backend.db import DB_FILENAME, DBConnection
|
||||
from backend.helpers import folder_path
|
||||
|
||||
|
||||
class Test_DB(unittest.TestCase):
|
||||
def test_foreign_key_and_wal(self):
|
||||
DBConnection.file = folder_path(*DB_FILENAME)
|
||||
instance = DBConnection(timeout=20.0)
|
||||
self.assertEqual(instance.cursor().execute("PRAGMA foreign_keys;").fetchone()[0], 1)
|
||||
@@ -1,14 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from backend.helpers import search_filter
|
||||
|
||||
class Test_Reminder_Handler(unittest.TestCase):
|
||||
def test_filter_function(self):
|
||||
p = {
|
||||
'title': 'TITLE',
|
||||
'text': 'TEXT'
|
||||
}
|
||||
for test_case in ('', 'title', 'ex'):
|
||||
self.assertTrue(search_filter(test_case, p))
|
||||
for test_case in (' ', 'Hello'):
|
||||
self.assertFalse(search_filter(test_case, p))
|
||||
@@ -1,10 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from backend.security import generate_salt_hash, get_hash
|
||||
|
||||
class Test_Security(unittest.TestCase):
|
||||
def test_hash(self):
|
||||
for test_case in ('test', ''):
|
||||
result = generate_salt_hash(test_case)
|
||||
self.assertEqual(result[1], get_hash(result[0], test_case))
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from flask import Blueprint, Flask
|
||||
|
||||
from frontend.ui import methods, ui
|
||||
|
||||
class Test_UI(unittest.TestCase):
|
||||
def test_methods(self):
|
||||
self.assertEqual(len(methods), 1)
|
||||
self.assertEqual(methods[0], 'GET')
|
||||
|
||||
def test_blueprint(self):
|
||||
self.assertIsInstance(ui, Blueprint)
|
||||
|
||||
def test_route_methods(self):
|
||||
temp_app = Flask(__name__)
|
||||
temp_app.register_blueprint(ui)
|
||||
for rule in temp_app.url_map.iter_rules():
|
||||
self.assertEqual(len(rule.methods), 3)
|
||||
self.assertIn(methods[0], rule.methods)
|
||||
@@ -1,14 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from backend.custom_exceptions import UsernameInvalid
|
||||
from backend.users import ONEPASS_INVALID_USERNAMES, Users
|
||||
|
||||
class Test_Users(unittest.TestCase):
|
||||
def test_username_check(self):
|
||||
users = Users()
|
||||
for test_case in ('', 'test'):
|
||||
users._check_username(test_case)
|
||||
|
||||
for test_case in (' ', ' ', '0', 'api', *ONEPASS_INVALID_USERNAMES):
|
||||
with self.assertRaises(UsernameInvalid):
|
||||
users._check_username(test_case)
|
||||
Reference in New Issue
Block a user