From 92aa3db846d852a083ef3828e27cb22cb8313f22 Mon Sep 17 00:00:00 2001 From: Mike Williamson Date: Wed, 22 Apr 2020 17:08:30 -0400 Subject: [PATCH] Run pytest as a module This commit modifies the "test" script to run pytest as a module which adds the current directory to the module search path. This allows the removal of a bunch of path munging code sprinkled throughout the tests. --- api/Pipfile | 2 +- api/tests/test_auth_functions.py | 7 ------- api/tests/test_auth_wrapper.py | 8 -------- api/tests/test_cost_check.py | 10 ---------- api/tests/test_db_migrate.py | 9 --------- api/tests/test_depth_check.py | 10 ---------- api/tests/test_domains_mutations.py | 15 ++------------- api/tests/test_domains_resolver_access_control.py | 10 ---------- api/tests/test_domains_resolver_values.py | 13 ++----------- api/tests/test_email_address_scalar.py | 14 ++------------ api/tests/test_notification_emails.py | 8 -------- api/tests/test_organization_acronym_scalar.py | 13 ++----------- api/tests/test_organization_mutations.py | 14 ++------------ .../test_organization_resolver_access_control.py | 10 ---------- api/tests/test_organization_resolver_values.py | 10 ---------- ...that_migrations_have_a_single_revision_head.py | 1 - api/tests/test_url_scalar.py | 9 --------- api/tests/test_user_access_control.py | 10 ---------- api/tests/test_user_mutations.py | 9 --------- api/tests/test_user_roles.py | 7 ------- api/tests/test_user_sign_in.py | 8 -------- api/tests/test_user_values.py | 10 ---------- api/tests/test_users.py | 10 ++++++++++ api/tests/test_users_access_control.py | 9 --------- api/tests/test_users_values.py | 10 ---------- 25 files changed, 21 insertions(+), 215 deletions(-) create mode 100644 api/tests/test_users.py diff --git a/api/Pipfile b/api/Pipfile index 08cc99e048..106066ecd9 100644 --- a/api/Pipfile +++ b/api/Pipfile @@ -31,7 +31,7 @@ pyotp = "*" pyjwt = "*" [scripts] -test = "pytest" +test = "python -m pytest" server = "python application.py" schema-export = "python schema_export.py" db-init = 'python manage.py db init' diff --git a/api/tests/test_auth_functions.py b/api/tests/test_auth_functions.py index 0b0c6b10e6..2c0762dc62 100644 --- a/api/tests/test_auth_functions.py +++ b/api/tests/test_auth_functions.py @@ -2,14 +2,7 @@ import os from os.path import dirname, join, expanduser, normpath, realpath from flask_bcrypt import Bcrypt - import pytest - -# 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 app import app from db import db_session from models import Users, User_affiliations, Organizations diff --git a/api/tests/test_auth_wrapper.py b/api/tests/test_auth_wrapper.py index a4751264e8..a77da50b6a 100644 --- a/api/tests/test_auth_wrapper.py +++ b/api/tests/test_auth_wrapper.py @@ -5,22 +5,14 @@ from flask_bcrypt import Bcrypt from flask import Request from werkzeug.test import create_environ - import pytest from unittest import TestCase - -# 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 app import app from db import db_session from queries import schema from models import Users, User_affiliations, Organizations from backend.security_check import SecurityAnalysisBackend - @pytest.fixture(scope='class') def user_role_test_db_init(): bcrypt = Bcrypt(app) diff --git a/api/tests/test_cost_check.py b/api/tests/test_cost_check.py index 484433f5a7..fa84185d57 100644 --- a/api/tests/test_cost_check.py +++ b/api/tests/test_cost_check.py @@ -1,28 +1,18 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from graphene.test import Client - from werkzeug.test import create_environ from flask import Request from flask_bcrypt import Bcrypt - from unittest import TestCase - -# 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 db import db_session from app import app from queries import schema from models import Users from backend.security_check import SecurityAnalysisBackend - @pytest.fixture(scope='class') def user_schema_test_db_init(): bcrypt = Bcrypt(app) diff --git a/api/tests/test_db_migrate.py b/api/tests/test_db_migrate.py index 426333c1c5..152c746f81 100644 --- a/api/tests/test_db_migrate.py +++ b/api/tests/test_db_migrate.py @@ -1,20 +1,11 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest - from sqlalchemy import create_engine - -# 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 manage import * from db import db_session, Base, engine - class TestDBCreation: def test_created_schema_contains_expected_tables(self): """This test determines that the postgres db is created with the expected tables""" diff --git a/api/tests/test_depth_check.py b/api/tests/test_depth_check.py index 137eecc514..f0edcd3bb2 100644 --- a/api/tests/test_depth_check.py +++ b/api/tests/test_depth_check.py @@ -1,28 +1,18 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - from werkzeug.test import create_environ from flask import Request - import pytest from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - -# 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 app import app from db import db_session from queries import schema from models import Users from backend.security_check import SecurityAnalysisBackend - @pytest.fixture(scope='class') def user_schema_test_db_init(): bcrypt = Bcrypt(app) diff --git a/api/tests/test_domains_mutations.py b/api/tests/test_domains_mutations.py index 24d203245e..bf722f5cff 100644 --- a/api/tests/test_domains_mutations.py +++ b/api/tests/test_domains_mutations.py @@ -1,22 +1,15 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from flask import Request from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - from werkzeug.test import create_environ - -# 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 app import app +from queries import schema +from backend.security_check import SecurityAnalysisBackend from db import db_session from models import ( Organizations, @@ -32,10 +25,6 @@ Ssl_scans ) -from queries import schema -from backend.security_check import SecurityAnalysisBackend - - @pytest.fixture(scope='class') def domain_test_db_init(): bcrypt = Bcrypt(app) diff --git a/api/tests/test_domains_resolver_access_control.py b/api/tests/test_domains_resolver_access_control.py index 116d5151b1..5ad9f143ea 100644 --- a/api/tests/test_domains_resolver_access_control.py +++ b/api/tests/test_domains_resolver_access_control.py @@ -1,28 +1,18 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from flask import Request from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - from werkzeug.test import create_environ - -# 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 app import app from db import db_session from models import Organizations, Domains, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend - @pytest.fixture(scope='class') def domain_test_db_init(): bcrypt = Bcrypt(app) diff --git a/api/tests/test_domains_resolver_values.py b/api/tests/test_domains_resolver_values.py index 29c62a808e..ea632356cd 100644 --- a/api/tests/test_domains_resolver_values.py +++ b/api/tests/test_domains_resolver_values.py @@ -1,23 +1,16 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from flask import Request from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - from werkzeug.test import create_environ - -# 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 app import app from db import db_session +from queries import schema +from backend.security_check import SecurityAnalysisBackend from models import ( Organizations, Domains, @@ -32,8 +25,6 @@ Spf_scans, Ssl_scans ) -from queries import schema -from backend.security_check import SecurityAnalysisBackend @pytest.fixture(scope='class') diff --git a/api/tests/test_email_address_scalar.py b/api/tests/test_email_address_scalar.py index deb44c49bb..6c54066693 100644 --- a/api/tests/test_email_address_scalar.py +++ b/api/tests/test_email_address_scalar.py @@ -1,28 +1,18 @@ import sys import os - import pytest from graphene.test import Client from graphql import GraphQLScalarType 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 unittest import TestCase from scalars.email_address import ( EmailAddress, scalar_error_type, scalar_error_only_types ) - -class TestEmailAddressScalar(unittest.TestCase): +class TestEmailAddressScalar(TestCase): def test_valid_email_serialize(self): test_email = 'test.account@canada.ca' diff --git a/api/tests/test_notification_emails.py b/api/tests/test_notification_emails.py index acf680f0f6..e984224e8f 100644 --- a/api/tests/test_notification_emails.py +++ b/api/tests/test_notification_emails.py @@ -1,20 +1,12 @@ import os import sys from os.path import dirname, join, expanduser, normpath, realpath - 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 app import app - from queries import schema from backend.security_check import SecurityAnalysisBackend diff --git a/api/tests/test_organization_acronym_scalar.py b/api/tests/test_organization_acronym_scalar.py index ea5a0b28e2..fb508ff09b 100644 --- a/api/tests/test_organization_acronym_scalar.py +++ b/api/tests/test_organization_acronym_scalar.py @@ -1,17 +1,8 @@ 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 unittest import TestCase from scalars.organization_acronym import ( scalar_error_only_types, scalar_error_type, @@ -19,7 +10,7 @@ ) -class TestOrganizationScalar(unittest.TestCase): +class TestOrganizationScalar(TestCase): def test_valid_acronym_serialize(self): test_value = 'TEST_ORG' diff --git a/api/tests/test_organization_mutations.py b/api/tests/test_organization_mutations.py index 5d7fb89580..bc4fe7c607 100644 --- a/api/tests/test_organization_mutations.py +++ b/api/tests/test_organization_mutations.py @@ -1,23 +1,16 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from flask import Request from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - from werkzeug.test import create_environ - -# 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 app import app from db import db_session +from queries import schema +from backend.security_check import SecurityAnalysisBackend from models import ( Organizations, Domains, @@ -31,9 +24,6 @@ Spf_scans, Ssl_scans ) -from queries import schema -from backend.security_check import SecurityAnalysisBackend - @pytest.fixture(scope='class') def organization_mutation_db_init(): diff --git a/api/tests/test_organization_resolver_access_control.py b/api/tests/test_organization_resolver_access_control.py index 3285e628a2..425fce8858 100644 --- a/api/tests/test_organization_resolver_access_control.py +++ b/api/tests/test_organization_resolver_access_control.py @@ -1,28 +1,18 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from flask import Request from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - from werkzeug.test import create_environ - -# 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 app import app from db import db_session from models import Organizations, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend - @pytest.fixture(scope='class') def org_perm_test_db_init(): bcrypt = Bcrypt(app) diff --git a/api/tests/test_organization_resolver_values.py b/api/tests/test_organization_resolver_values.py index 52f4dcb831..d2074960b3 100644 --- a/api/tests/test_organization_resolver_values.py +++ b/api/tests/test_organization_resolver_values.py @@ -1,28 +1,18 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from flask import Request from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - from werkzeug.test import create_environ - -# 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 app import app from db import db_session from models import Organizations, Domains, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend - @pytest.fixture(scope='class') def org_perm_test_db_init(): bcrypt = Bcrypt(app) diff --git a/api/tests/test_that_migrations_have_a_single_revision_head.py b/api/tests/test_that_migrations_have_a_single_revision_head.py index a2a750eaff..28df768d89 100644 --- a/api/tests/test_that_migrations_have_a_single_revision_head.py +++ b/api/tests/test_that_migrations_have_a_single_revision_head.py @@ -1,7 +1,6 @@ from alembic.config import Config from alembic.script import ScriptDirectory - # From here: # https://blog.jerrycodes.com/multiple-heads-in-alembic-migrations/ def test_only_single_head_revision_in_migrations(): diff --git a/api/tests/test_url_scalar.py b/api/tests/test_url_scalar.py index 9e66daa4e3..c84d2e6bf2 100644 --- a/api/tests/test_url_scalar.py +++ b/api/tests/test_url_scalar.py @@ -1,17 +1,8 @@ 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.url import ( URL, scalar_error_type, diff --git a/api/tests/test_user_access_control.py b/api/tests/test_user_access_control.py index 40af014b65..cb1ab9fa6c 100644 --- a/api/tests/test_user_access_control.py +++ b/api/tests/test_user_access_control.py @@ -1,28 +1,18 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from flask import Request from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - from werkzeug.test import create_environ - -# 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 app import app from db import db_session from models import Organizations, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend - @pytest.fixture(scope='class') def user_resolver_ac_test_db_init(): bcrypt = Bcrypt(app) diff --git a/api/tests/test_user_mutations.py b/api/tests/test_user_mutations.py index 8af0151c58..ab35560b50 100644 --- a/api/tests/test_user_mutations.py +++ b/api/tests/test_user_mutations.py @@ -1,18 +1,10 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pyotp import pytest from flask_bcrypt import Bcrypt 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 app import app from db import db_session from queries import schema @@ -20,7 +12,6 @@ from backend.security_check import SecurityAnalysisBackend from functions.error_messages import * - @pytest.fixture(scope='class') def user_schema_test_db_init(): bcrypt = Bcrypt(app) diff --git a/api/tests/test_user_roles.py b/api/tests/test_user_roles.py index 7b2901a230..8e2946fc73 100644 --- a/api/tests/test_user_roles.py +++ b/api/tests/test_user_roles.py @@ -5,15 +5,8 @@ from flask_bcrypt import Bcrypt from flask import Request from werkzeug.test import create_environ - import pytest from unittest import TestCase - -# 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 app import app from db import db_session from queries import schema diff --git a/api/tests/test_user_sign_in.py b/api/tests/test_user_sign_in.py index 769e04fc55..9eac7be081 100644 --- a/api/tests/test_user_sign_in.py +++ b/api/tests/test_user_sign_in.py @@ -2,18 +2,10 @@ import os from os.path import dirname, join, expanduser, normpath, realpath import datetime - import pyotp import pytest from flask_bcrypt import Bcrypt 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 db import db_session from app import app from queries import schema diff --git a/api/tests/test_user_values.py b/api/tests/test_user_values.py index b8356e399c..af7e42dc1a 100644 --- a/api/tests/test_user_values.py +++ b/api/tests/test_user_values.py @@ -1,28 +1,18 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from flask import Request from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - from werkzeug.test import create_environ - -# 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 app import app from db import db_session from models import Organizations, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend - @pytest.fixture(scope='class') def user_resolver_test_db_init(): bcrypt = Bcrypt(app) diff --git a/api/tests/test_users.py b/api/tests/test_users.py new file mode 100644 index 0000000000..0d9c4dd044 --- /dev/null +++ b/api/tests/test_users.py @@ -0,0 +1,10 @@ +from unittest import TestCase +from app import app +from db import db_session +from models import Organizations, Users, User_affiliations + +class TestUsersModel(TestCase): + # Super Admin Tests + def test_user(self): + + assert True == True diff --git a/api/tests/test_users_access_control.py b/api/tests/test_users_access_control.py index 0ab0d0e344..77b3583020 100644 --- a/api/tests/test_users_access_control.py +++ b/api/tests/test_users_access_control.py @@ -1,21 +1,12 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from flask import Request from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - from werkzeug.test import create_environ - -# 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 app import app from db import db_session from models import Organizations, Users, User_affiliations diff --git a/api/tests/test_users_values.py b/api/tests/test_users_values.py index b6bc6e43dc..cc6ac2a261 100644 --- a/api/tests/test_users_values.py +++ b/api/tests/test_users_values.py @@ -1,28 +1,18 @@ import sys import os from os.path import dirname, join, expanduser, normpath, realpath - import pytest from flask import Request from graphene.test import Client from flask_bcrypt import Bcrypt - from unittest import TestCase - from werkzeug.test import create_environ - -# 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 app import app from db import db_session from models import Organizations, Users, User_affiliations from queries import schema from backend.security_check import SecurityAnalysisBackend - @pytest.fixture(scope='class') def users_resolver_test_db_init(): bcrypt = Bcrypt(app)