diff --git a/api/functions/generate_enums.py b/api/functions/generate_enums.py deleted file mode 100644 index aa576a782c..0000000000 --- a/api/functions/generate_enums.py +++ /dev/null @@ -1,85 +0,0 @@ -import graphene -from enum import Enum -from sqlalchemy.orm import load_only -from model_enums import create_enum_app, create_enum_db - - -def create_enums(Table, enum_name, column): - """ - Function to allow the creation of enums for various uses. Function takes in SQLAlchemy Model, and the - Column that you would like your enums to be created. The value of the enum is equal to that of the name - """ - app = create_enum_app() - db = create_enum_db(app) - - with app.app_context(): - query = db.session.query(Table).options(load_only(column)) - rows = db.session.execute(query).fetchall() - - enum_dict = dict() - - for select in rows: - enum_dict.update({select[1]: select[1]}) - - if not len(enum_dict): - ret_enum = Enum(enum_name, 'EMPTY') - else: - ret_enum = Enum(enum_name, enum_dict) - - return graphene.Enum.from_enum(ret_enum) - - -def create_enum_with_descriptions(Table, enum_name, enum_column, description_column): - """ - Function to allow the creation of enums with descriptions for various uses. Function takes in SQLAlchemy Model, - and the Column that you would like your enums to be created. The value of the enum is equal to that of the name - """ - - app = create_enum_app() - db = create_enum_db(app) - - with app.app_context(): - query = db.session.query(Table).options(load_only(enum_column)) - rows = db.session.execute(query).fetchall() - - enum_dict = dict() - - for select in rows: - enum_dict.update({select[1]: select[1]}) - - if not len(enum_dict): - ret_enum = Enum(enum_name, 'EMPTY') - else: - ret_enum = Enum(enum_name, enum_dict) - - description_dict = dict() - - for enum in ret_enum: - with app.app_context(): - query = db.session.query(Table).filter( - getattr(Table, enum_column) == enum.name).options( - load_only(description_column)) - row = db.session.execute(query).first() - - try: - description_dict.update({enum.name: row[1]["description"]}) - except: - description_dict.update({enum.name: "Error loading description"}) - - @property - def description(self): - return description_dict.get(self.name) - - setattr(ret_enum, 'description', description) - - return graphene.Enum.from_enum(ret_enum) - -# Example of how to create enums for a given table -# from functions.generate_enums import create_enums -# from models import Groups -# -# GroupEnums = create_enums(Groups, 'GroupEnums', 's_group') - -# Put this above other file import in the test files -# import model_enums -# model_enums._called_from_test = True diff --git a/api/model_enums/organiztions.py b/api/model_enums/organiztions.py deleted file mode 100644 index 4026f66208..0000000000 --- a/api/model_enums/organiztions.py +++ /dev/null @@ -1,4 +0,0 @@ -from models import Organizations -from functions.generate_enums import create_enum_with_descriptions - -OrganizationsEnum = create_enum_with_descriptions(Organizations, 'OrganizationsEnum', 'acronym', 'org_tags') diff --git a/api/scalars/organization_acronym.py b/api/scalars/organization_acronym.py index 9b09a85051..d77f853b61 100644 --- a/api/scalars/organization_acronym.py +++ b/api/scalars/organization_acronym.py @@ -29,7 +29,7 @@ def parse_value(value): raise GraphQLError(scalar_error_type('String', value)) if not ACRONYM_REGEX.search(value): - raise GraphQLError(scalar_error_type("String", value)) + raise GraphQLError(scalar_error_type("Acronym", value)) return value @staticmethod diff --git a/api/schemas/organizations_mutations.py b/api/schemas/organizations_mutations.py index 0b9f8172f0..397c9c6bf8 100644 --- a/api/schemas/organizations_mutations.py +++ b/api/schemas/organizations_mutations.py @@ -9,8 +9,6 @@ from functions.auth_functions import is_super_admin from functions.input_validators import cleanse_input -from model_enums.organiztions import OrganizationsEnum - from models import ( Organizations, User_affiliations, diff --git a/api/tests/test_auth_functions.py b/api/tests/test_auth_functions.py index 2102f2e1db..d9feabd814 100644 --- a/api/tests/test_auth_functions.py +++ b/api/tests/test_auth_functions.py @@ -10,8 +10,6 @@ SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -from manage import seed, remove_seed -seed() from app import app from db import db from models import Users, User_affiliations, Organizations @@ -21,7 +19,6 @@ is_user_write, is_user_read ) -remove_seed() @pytest.fixture(scope='class') diff --git a/api/tests/test_auth_wrapper.py b/api/tests/test_auth_wrapper.py index f932386d13..0e49ca3fb6 100644 --- a/api/tests/test_auth_wrapper.py +++ b/api/tests/test_auth_wrapper.py @@ -14,14 +14,11 @@ SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -from manage import seed, remove_seed -seed() from app import app from db import db from queries import schema from models import Users, User_affiliations, Organizations from backend.security_check import SecurityAnalysisBackend -remove_seed() @pytest.fixture(scope='class') diff --git a/api/tests/test_cost_check.py b/api/tests/test_cost_check.py index 534d62890b..aa91b607c5 100644 --- a/api/tests/test_cost_check.py +++ b/api/tests/test_cost_check.py @@ -16,14 +16,11 @@ SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -from manage import seed, remove_seed -seed() from db import db from app import app from queries import schema from models import Users from backend.security_check import SecurityAnalysisBackend -remove_seed() @pytest.fixture(scope='class') diff --git a/api/tests/test_depth_check.py b/api/tests/test_depth_check.py index c584a1ecf5..fffeb49d30 100644 --- a/api/tests/test_depth_check.py +++ b/api/tests/test_depth_check.py @@ -16,14 +16,11 @@ SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -from manage import seed, remove_seed -seed() from app import app from db import db from queries import schema from models import Users from backend.security_check import SecurityAnalysisBackend -remove_seed() @pytest.fixture(scope='class') diff --git a/api/tests/test_domains_resolver_access_control.py b/api/tests/test_domains_resolver_access_control.py index 744d7bfbd1..2a41be983a 100644 --- a/api/tests/test_domains_resolver_access_control.py +++ b/api/tests/test_domains_resolver_access_control.py @@ -11,20 +11,16 @@ from werkzeug.test import create_environ -from manage import seed, remove_seed +# This is the only way I could get imports to work for unit testing. +PACKAGE_PARENT = '..' +SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) +sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -seed() from app import app from db import db from models import Organizations, Domains, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend -remove_seed() - -# This is the only way I could get imports to work for unit testing. -PACKAGE_PARENT = '..' -SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) -sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) @pytest.fixture(scope='class') diff --git a/api/tests/test_notification_emails.py b/api/tests/test_notification_emails.py index a0472f672c..acf680f0f6 100644 --- a/api/tests/test_notification_emails.py +++ b/api/tests/test_notification_emails.py @@ -4,22 +4,19 @@ from graphene.test import Client +# This is the only way I could get imports to work for unit testing. +PACKAGE_PARENT = '..' +SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) +sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) + from functions.email_templates import ( email_verification_template, password_reset_template) from functions.error_messages import scalar_error_type -from manage import seed, remove_seed from app import app -seed() from queries import schema from backend.security_check import SecurityAnalysisBackend -remove_seed() - -# This is the only way I could get imports to work for unit testing. -PACKAGE_PARENT = '..' -SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) -sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) class TestPasswordReset: diff --git a/api/tests/test_organization_acronym_scalar.py b/api/tests/test_organization_acronym_scalar.py new file mode 100644 index 0000000000..86fd078fe5 --- /dev/null +++ b/api/tests/test_organization_acronym_scalar.py @@ -0,0 +1,63 @@ +import sys +import os + +from graphql.language import ast +from graphql import GraphQLError + +import unittest + +# This is the only way I could get imports to work for unit testing. +PACKAGE_PARENT = '..' +SCRIPT_DIR = os.path.dirname( + os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) +sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) + +from scalars.organization_acronym import ( + scalar_error_only_types, + scalar_error_type, + Acronym +) + + +class TestOrganizationScalar(unittest.TestCase): + + def test_valid_acronym_serialize(self): + test_value = 'TEST_ORG' + assert Acronym.serialize(test_value) + + def test_invalid_acronym_serialize(self): + test_value = 'fdsaf' + with self.assertRaisesRegex(GraphQLError, scalar_error_type("Acronym", test_value)): + Acronym.serialize(test_value) + + def test_invalid_value_acronym_serialize(self): + test_value = 3213 + with self.assertRaisesRegex(GraphQLError, scalar_error_type("String", test_value)): + Acronym.serialize(test_value) + + def test_valid_acronym_parse_value(self): + test_value = 'TEST_ORG' + assert Acronym.parse_value(test_value) + + def test_invalid_acronym_parse_value(self): + test_value = 'fdasf' + with self.assertRaisesRegex(GraphQLError, scalar_error_type("Acronym", test_value)): + Acronym.parse_value(test_value) + + def test_invalid_value_acronym_parse_value(self): + test_value = 654645 + with self.assertRaisesRegex(GraphQLError, scalar_error_type("String", test_value)): + Acronym.serialize(test_value) + + def test_valid_acronym_parse_literal(self): + test_value = ast.StringValue( + value='TEST_ORG' + ) + assert Acronym.parse_literal(test_value) + + def test_wrong_ast_type_url_parse_literal(self): + test_value = ast.IntValue( + value="1234" + ) + with self.assertRaisesRegex(GraphQLError, scalar_error_only_types("strings", "acronym", str(ast.Type))): + Acronym.parse_literal(test_value) diff --git a/api/tests/test_organization_resolver_access_control.py b/api/tests/test_organization_resolver_access_control.py index 7bf510aea2..6dd3090674 100644 --- a/api/tests/test_organization_resolver_access_control.py +++ b/api/tests/test_organization_resolver_access_control.py @@ -11,20 +11,16 @@ from werkzeug.test import create_environ -from manage import seed, remove_seed +# This is the only way I could get imports to work for unit testing. +PACKAGE_PARENT = '..' +SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) +sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -seed() from app import app from db import db -from models import Organizations, Domains, Users, User_affiliations +from models import Organizations, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend -remove_seed() - -# This is the only way I could get imports to work for unit testing. -PACKAGE_PARENT = '..' -SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) -sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) @pytest.fixture(scope='class') diff --git a/api/tests/test_organization_resolver_values.py b/api/tests/test_organization_resolver_values.py index 5850fffe3e..ef1e377253 100644 --- a/api/tests/test_organization_resolver_values.py +++ b/api/tests/test_organization_resolver_values.py @@ -11,20 +11,16 @@ from werkzeug.test import create_environ -from manage import seed, remove_seed +# This is the only way I could get imports to work for unit testing. +PACKAGE_PARENT = '..' +SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) +sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -seed() from app import app from db import db from models import Organizations, Domains, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend -remove_seed() - -# This is the only way I could get imports to work for unit testing. -PACKAGE_PARENT = '..' -SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) -sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) @pytest.fixture(scope='class') diff --git a/api/tests/test_url_scalar.py b/api/tests/test_url_scalar.py index 04118019ae..169e0ab85b 100644 --- a/api/tests/test_url_scalar.py +++ b/api/tests/test_url_scalar.py @@ -6,7 +6,7 @@ import unittest -# This is the only way I could get imports to work for unit testing. TODO: See if there is a better way! +# This is the only way I could get imports to work for unit testing. PACKAGE_PARENT = '..' SCRIPT_DIR = os.path.dirname( os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) diff --git a/api/tests/test_user_access_control.py b/api/tests/test_user_access_control.py index e5fa046777..a7f6c054a3 100644 --- a/api/tests/test_user_access_control.py +++ b/api/tests/test_user_access_control.py @@ -11,20 +11,16 @@ from werkzeug.test import create_environ -from manage import seed, remove_seed +# This is the only way I could get imports to work for unit testing. +PACKAGE_PARENT = '..' +SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) +sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -seed() from app import app from db import db -from models import Organizations, Domains, Users, User_affiliations +from models import Organizations, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend -remove_seed() - -# This is the only way I could get imports to work for unit testing. -PACKAGE_PARENT = '..' -SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) -sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) @pytest.fixture(scope='class') diff --git a/api/tests/test_user_mutations.py b/api/tests/test_user_mutations.py index 0faac40ec6..39c636844e 100644 --- a/api/tests/test_user_mutations.py +++ b/api/tests/test_user_mutations.py @@ -13,15 +13,12 @@ SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -from manage import seed, remove_seed -seed() from app import app from db import db from queries import schema from models import Users from backend.security_check import SecurityAnalysisBackend from functions.error_messages import * -remove_seed() @pytest.fixture(scope='class') diff --git a/api/tests/test_user_roles.py b/api/tests/test_user_roles.py index 8c5b5571f9..6e553f7abd 100644 --- a/api/tests/test_user_roles.py +++ b/api/tests/test_user_roles.py @@ -14,15 +14,12 @@ SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -from manage import seed, remove_seed -seed() from app import app from db import db from queries import schema from models import Users, User_affiliations, Organizations from functions.error_messages import error_not_an_admin from backend.security_check import SecurityAnalysisBackend -remove_seed() @pytest.fixture(scope='class') diff --git a/api/tests/test_user_sign_in.py b/api/tests/test_user_sign_in.py index e4a729fdcb..840c43cc4c 100644 --- a/api/tests/test_user_sign_in.py +++ b/api/tests/test_user_sign_in.py @@ -14,9 +14,7 @@ SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -from manage import seed, remove_seed -seed() -from db import * +from db import db from app import app from queries import schema from models import Users @@ -26,7 +24,6 @@ error_too_many_failed_login_attempts, error_user_does_not_exist ) -remove_seed() @pytest.fixture(scope='class') diff --git a/api/tests/test_user_values.py b/api/tests/test_user_values.py index 8daa18685e..e40e6c8d4f 100644 --- a/api/tests/test_user_values.py +++ b/api/tests/test_user_values.py @@ -11,20 +11,16 @@ from werkzeug.test import create_environ -from manage import seed, remove_seed +# This is the only way I could get imports to work for unit testing. +PACKAGE_PARENT = '..' +SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) +sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -seed() from app import app from db import db -from models import Organizations, Domains, Users, User_affiliations +from models import Organizations, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend -remove_seed() - -# This is the only way I could get imports to work for unit testing. -PACKAGE_PARENT = '..' -SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) -sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) @pytest.fixture(scope='class') diff --git a/api/tests/test_users_access_control.py b/api/tests/test_users_access_control.py index 8b5eaab8ec..e7b0269629 100644 --- a/api/tests/test_users_access_control.py +++ b/api/tests/test_users_access_control.py @@ -11,20 +11,16 @@ from werkzeug.test import create_environ -from manage import seed, remove_seed +# This is the only way I could get imports to work for unit testing. +PACKAGE_PARENT = '..' +SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) +sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -seed() from app import app from db import db -from models import Organizations, Domains, Users, User_affiliations +from models import Organizations, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend -remove_seed() - -# This is the only way I could get imports to work for unit testing. -PACKAGE_PARENT = '..' -SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) -sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) @pytest.fixture(scope='class') diff --git a/api/tests/test_users_values.py b/api/tests/test_users_values.py index bb122ad5db..e1ffdd3fad 100644 --- a/api/tests/test_users_values.py +++ b/api/tests/test_users_values.py @@ -11,20 +11,16 @@ from werkzeug.test import create_environ -from manage import seed, remove_seed +# This is the only way I could get imports to work for unit testing. +PACKAGE_PARENT = '..' +SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) +sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) -seed() from app import app from db import db -from models import Organizations, Domains, Users, User_affiliations +from models import Organizations, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend -remove_seed() - -# This is the only way I could get imports to work for unit testing. -PACKAGE_PARENT = '..' -SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) -sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) @pytest.fixture(scope='class')