From dc4d60fba2c86b04195eee94259c14b362f181ec Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Wed, 25 Aug 2021 21:49:51 -0400 Subject: [PATCH] testing --- ceo/utils.py | 2 +- ceo_common/interfaces/IDatabaseService.py | 1 - ceo_common/interfaces/__init__.py | 1 + ceod/api/app_factory.py | 14 ++++++--- ceod/api/database.py | 14 ++++----- ceod/db/__init__.py | 2 +- tests/ceod/db/test_mysql.py | 35 +++++++++++++++++++++++ tests/ceod/db/test_postgres.py | 1 - tests/ceod_dev.ini | 1 + tests/ceod_test_local.ini | 1 + 10 files changed, 57 insertions(+), 15 deletions(-) diff --git a/ceo/utils.py b/ceo/utils.py index fc84265..b8bb194 100644 --- a/ceo/utils.py +++ b/ceo/utils.py @@ -10,7 +10,7 @@ def http_request(method: str, path: str, **kwargs) -> requests.Response: client = component.getUtility(IHTTPClient) cfg = component.getUtility(IConfig) if path.startswith('/api/db'): - host = cfg.get('ceod_db_host') + host = cfg.get('ceod_database_host') need_cred = False else: host = cfg.get('ceod_admin_host') diff --git a/ceo_common/interfaces/IDatabaseService.py b/ceo_common/interfaces/IDatabaseService.py index fcfbd7d..1062df2 100644 --- a/ceo_common/interfaces/IDatabaseService.py +++ b/ceo_common/interfaces/IDatabaseService.py @@ -1,5 +1,4 @@ from zope.interface import Attribute, Interface -from .IUser import IUser class IDatabaseService(Interface): diff --git a/ceo_common/interfaces/__init__.py b/ceo_common/interfaces/__init__.py index 56f2203..226b90f 100644 --- a/ceo_common/interfaces/__init__.py +++ b/ceo_common/interfaces/__init__.py @@ -8,3 +8,4 @@ from .IUWLDAPService import IUWLDAPService from .IMailService import IMailService from .IMailmanService import IMailmanService from .IHTTPClient import IHTTPClient +from .IDatabaseService import IDatabaseService diff --git a/ceod/api/app_factory.py b/ceod/api/app_factory.py index fa0a424..2dae0ed 100644 --- a/ceod/api/app_factory.py +++ b/ceod/api/app_factory.py @@ -38,6 +38,10 @@ def create_app(flask_config={}): from ceod.api import mailman app.register_blueprint(mailman.bp, url_prefix='/api/mailman') + if hostname == cfg.get('ceod_database_host'): + from ceod.api import database + app.register_blueprint(database.bp, url_prefix='/api/db') + from ceod.api import groups app.register_blueprint(groups.bp, url_prefix='/api/groups') @@ -109,9 +113,11 @@ def register_services(app): component.provideUtility(uwldap_srv, IUWLDAPService) # MySQLService - mysql_srv = MySQLService() - component.provideUtility(mysql_srv, IDatabaseService, 'mysql') + if hostname == cfg.get('ceod_database_host'): + mysql_srv = MySQLService() + component.provideUtility(mysql_srv, IDatabaseService, 'mysql') # PostgreSQLService - psql_srv = PostgreSQLService() - component.provideUtility(psql_srv, IDatabaseService, 'postgresql') + if hostname == cfg.get('ceod_database_host'): + psql_srv = PostgreSQLService() + component.provideUtility(psql_srv, IDatabaseService, 'postgresql') diff --git a/ceod/api/database.py b/ceod/api/database.py index 402a8ab..ff31b41 100644 --- a/ceod/api/database.py +++ b/ceod/api/database.py @@ -55,13 +55,6 @@ def create_mysql_db(auth_user: str, username: str): return create_db_from_type('mysql', username) -@bp.route('/mysql/', methods=['DELETE']) -@authz_restrict_to_syscom -@development_only -def delete_mysql_db(username: str): - delete_db_from_type('mysql', username) - - @bp.route('/postgresql/', methods=['POST']) @requires_authentication_no_realm def create_postgresql_db(auth_user: str, username: str): @@ -70,6 +63,13 @@ def create_postgresql_db(auth_user: str, username: str): return create_db_from_type('postgresql', username) +@bp.route('/mysql/', methods=['DELETE']) +@authz_restrict_to_syscom +@development_only +def delete_mysql_db(username: str): + delete_db_from_type('mysql', username) + + @bp.route('/postgresql/', methods=['DELETE']) @authz_restrict_to_syscom @development_only diff --git a/ceod/db/__init__.py b/ceod/db/__init__.py index b5373bd..6e93e5c 100644 --- a/ceod/db/__init__.py +++ b/ceod/db/__init__.py @@ -1,2 +1,2 @@ from .MySQLService import MySQLService -from .PostgreSQLService import PostgreSQLService \ No newline at end of file +from .PostgreSQLService import PostgreSQLService diff --git a/tests/ceod/db/test_mysql.py b/tests/ceod/db/test_mysql.py index ce1c76f..4c7da26 100644 --- a/tests/ceod/db/test_mysql.py +++ b/tests/ceod/db/test_mysql.py @@ -1,5 +1,40 @@ import pytest +from ceod.db.MySQLService import MySQLService +from ceo_common.errors import DatabaseConnectionError, DatabasePermissionError +from mysql.connector import connect +from mysql.connector.errors import InterfaceError, ProgrammingError + + +def test_mysql_db_create(cfg): + mysql_srv = MySQLService() + password = mysql_srv.create_db('test_jdoe') + + with connect( + host=cfg.get('ceod_database_host'), + user='test_jdoe', + password=password, + ) as con: + with con.cursor() as cur: + cur.execute("SHOW DATABASES") + response = cur.fetchall() + assert len(response) == 2 + + mysql_srv.delete_db('test_jdoe') + + # user should be deleted + with pytest.raises(InterfaceError): + con = connect( + host=cfg.get('ceod_database_host'), + user='test_jdoe', + password=password, + ) + +# except InterfaceError: +# raise DatabaseConnectionError() +# except ProgrammingError: +# raise DatabasePermissionError() + # ask for mysql and postgres with proper postgres configs and no public schema # tests are stateless diff --git a/tests/ceod/db/test_postgres.py b/tests/ceod/db/test_postgres.py index a1ace31..8b13789 100644 --- a/tests/ceod/db/test_postgres.py +++ b/tests/ceod/db/test_postgres.py @@ -1,2 +1 @@ -import pytest diff --git a/tests/ceod_dev.ini b/tests/ceod_dev.ini index 33f2f3a..36ae91b 100644 --- a/tests/ceod_dev.ini +++ b/tests/ceod_dev.ini @@ -7,6 +7,7 @@ admin_host = phosphoric-acid # this is the host with NFS no_root_squash fs_root_host = phosphoric-acid mailman_host = mail +database_host = coffee krb5_cache_dir = /run/ceod/krb5_cache use_https = false port = 9987 diff --git a/tests/ceod_test_local.ini b/tests/ceod_test_local.ini index 9e04403..07d0221 100644 --- a/tests/ceod_test_local.ini +++ b/tests/ceod_test_local.ini @@ -7,6 +7,7 @@ uw_domain = uwaterloo.internal admin_host = phosphoric-acid fs_root_host = phosphoric-acid mailman_host = phosphoric-acid +database_host = phosphoric-acid krb5_cache_dir = /tmp/ceod_test_krb5_cache use_https = false port = 9987