def test_compare_default_site_id(self): self.site_id = SiteID(default=self.site.id) self.assertEqual(self.site_id, self.site.id) self.assertFalse(self.site_id != self.site.id) self.assertFalse(self.site_id < self.site.id) self.assertTrue(self.site_id <= self.site.id) self.assertFalse(self.site_id > self.site.id) self.assertTrue(self.site_id >= self.site.id)
from django.conf import settings from django.test.runner import DiscoverRunner from edc_test_utils import DefaultTestSettings from os.path import abspath, dirname, join from edc_utils import get_datetime_from_env from multisite import SiteID app_name = "mapitio_edc" base_dir = dirname(abspath(__file__)) DEFAULT_SETTINGS = DefaultTestSettings( calling_file=__file__, BASE_DIR=base_dir, APP_NAME=app_name, SITE_ID=SiteID(default=10), MAPITIO_SCREENING_DISABLED=True, EDC_SITES_MODULE_NAME="mapitio_edc.sites", SUBJECT_VISIT_MODEL="mapitio_subject.subjectvisit", SUBJECT_CONSENT_MODEL="mapitio_consent.subjectconsent", SUBJECT_REQUISITION_MODEL=f"mapitio_subject.subjectrequisition", EDC_PROTOCOL_STUDY_OPEN_DATETIME=get_datetime_from_env(2020, 1, 30, 0, 0, 0, "UTC"), EDC_PROTOCOL_STUDY_CLOSE_DATETIME=get_datetime_from_env( 2024, 12, 31, 23, 59, 59, "UTC" ), ADVERSE_EVENT_ADMIN_SITE="mapitio_ae_admin", ADVERSE_EVENT_APP_LABEL="mapitio_ae", EDC_NAVBAR_DEFAULT="mapitio_dashboard", DASHBOARD_BASE_TEMPLATES=dict( edc_base_template="edc_dashboard/base.html", listboard_base_template="mapitio_edc/base.html",
def setUp(self): Site.objects.all().delete() self.site = Site.objects.create(domain='example.com') self.site_id = SiteID()
class TestSiteID(TestCase): def setUp(self): Site.objects.all().delete() self.site = Site.objects.create(domain='example.com') self.site_id = SiteID() def test_invalid_default(self): self.assertRaises(ValueError, SiteID, default='a') self.assertRaises(ValueError, SiteID, default=self.site_id) def test_compare_default_site_id(self): self.site_id = SiteID(default=self.site.id) self.assertEqual(self.site_id, self.site.id) self.assertFalse(self.site_id != self.site.id) self.assertFalse(self.site_id < self.site.id) self.assertTrue(self.site_id <= self.site.id) self.assertFalse(self.site_id > self.site.id) self.assertTrue(self.site_id >= self.site.id) def test_compare_site_ids(self): self.site_id.set(1) self.assertEqual(self.site_id, self.site_id) self.assertFalse(self.site_id != self.site_id) self.assertFalse(self.site_id < self.site_id) self.assertTrue(self.site_id <= self.site_id) self.assertFalse(self.site_id > self.site_id) self.assertTrue(self.site_id >= self.site_id) def test_compare_differing_types(self): self.site_id.set(1) # SiteIDHook <op> int self.assertNotEqual(self.site_id, '1') self.assertFalse(self.site_id == '1') self.assertTrue(self.site_id < '1') self.assertTrue(self.site_id <= '1') self.assertFalse(self.site_id > '1') self.assertFalse(self.site_id >= '1') # int <op> SiteIDHook self.assertNotEqual('1', self.site_id) self.assertFalse('1' == self.site_id) self.assertFalse('1' < self.site_id) self.assertFalse('1' <= self.site_id) self.assertTrue('1' > self.site_id) self.assertTrue('1' >= self.site_id) def test_set(self): self.site_id.set(10) self.assertEqual(int(self.site_id), 10) self.site_id.set(20) self.assertEqual(int(self.site_id), 20) self.site_id.set(self.site) self.assertEqual(int(self.site_id), self.site.id) def test_hash(self): self.site_id.set(10) self.assertEqual(hash(self.site_id), 10) self.site_id.set(20) self.assertEqual(hash(self.site_id), 20) def test_str_repr(self): self.site_id.set(10) self.assertEqual(str(self.site_id), '10') self.assertEqual(repr(self.site_id), '10') def test_context_manager(self): self.assertEqual(self.site_id.site_id, None) with self.site_id.override(1): self.assertEqual(self.site_id.site_id, 1) with self.site_id.override(2): self.assertEqual(self.site_id.site_id, 2) self.assertEqual(self.site_id.site_id, 1) self.assertEqual(self.site_id.site_id, None)
self.host = host def get(self, path, data={}, host=None, **extra): if host is None: host = self.host return super(RequestFactory, self).get(path=path, data=data, HTTP_HOST=host, **extra) @pytest.mark.django_db @skipUnless(Site._meta.installed, 'django.contrib.sites is not in settings.INSTALLED_APPS') @override_settings( SITE_ID=SiteID(), CACHE_SITES_KEY_PREFIX='__test__', ) class TestContribSite(TestCase): def setUp(self): Site.objects.all().delete() self.site = Site.objects.create(domain='example.com') settings.SITE_ID.set(self.site.id) def test_get_current_site(self): current_site = Site.objects.get_current() self.assertEqual(current_site, self.site) self.assertEqual(current_site.id, settings.SITE_ID) # Because we are a middleware package, we have no views available to test with easily
import os # noqa from multisite import SiteID from .defaults import * # noqa print(f"Settings file {__file__}") # noqa SITE_ID = SiteID(default=122) EDC_SITES_UAT_DOMAIN = False DEBUG = True ALLOWED_HOSTS = [ # "amana.tz.mocca.clinicedc.org", # 220 # "bunju.tz.mocca.clinicedc.org", # 250 # "kisugu.ug.mocca.clinicedc.org", # 119 # "kiswa.ug.mocca.clinicedc.org", # 120 "localhost", "127.0.0.1", ] # comment: comment out if using runserver and folders read from .env # ETC_DIR = os.path.join(BASE_DIR, "tests", "etc") # noqa # KEY_PATH = os.path.join(ETC_DIR, "crypto_fields") # noqa # end comment AUTO_CREATE_KEYS = False if os.path.exists(BASE_DIR) and not os.path.exists(KEY_PATH): # noqa os.makedirs(KEY_PATH) # noqa AUTO_CREATE_KEYS = True
class TestSiteID(TestCase): def setUp(self): Site.objects.all().delete() self.site = Site.objects.create(domain='example.com') self.site_id = SiteID() def test_invalid_default(self): self.assertRaises(ValueError, SiteID, default='a') self.assertRaises(ValueError, SiteID, default=self.site_id) def test_compare_default_site_id(self): self.site_id = SiteID(default=self.site.id) self.assertEqual(self.site_id, self.site.id) self.assertFalse(self.site_id != self.site.id) self.assertFalse(self.site_id < self.site.id) self.assertTrue(self.site_id <= self.site.id) self.assertFalse(self.site_id > self.site.id) self.assertTrue(self.site_id >= self.site.id) def test_compare_site_ids(self): self.site_id.set(1) self.assertEqual(self.site_id, self.site_id) self.assertFalse(self.site_id != self.site_id) self.assertFalse(self.site_id < self.site_id) self.assertTrue(self.site_id <= self.site_id) self.assertFalse(self.site_id > self.site_id) self.assertTrue(self.site_id >= self.site_id) def test_compare_differing_types(self): self.site_id.set(1) self.assertNotEqual(self.site_id, '1') self.assertFalse(self.site_id == '1') self.assertTrue(self.site_id < '1') self.assertTrue(self.site_id <= '1') self.assertFalse(self.site_id > '1') self.assertFalse(self.site_id >= '1') self.assertNotEqual('1', self.site_id) self.assertFalse('1' == self.site_id) self.assertFalse('1' < self.site_id) self.assertFalse('1' <= self.site_id) self.assertTrue('1' > self.site_id) self.assertTrue('1' >= self.site_id) def test_set(self): self.site_id.set(10) self.assertEqual(int(self.site_id), 10) self.site_id.set(20) self.assertEqual(int(self.site_id), 20) self.site_id.set(self.site) self.assertEqual(int(self.site_id), self.site.id) def test_hash(self): self.site_id.set(10) self.assertEqual(hash(self.site_id), 10) self.site_id.set(20) self.assertEqual(hash(self.site_id), 20) def test_str_repr(self): self.site_id.set(10) self.assertEqual(str(self.site_id), '10') self.assertEqual(repr(self.site_id), '10') def test_context_manager(self): self.assertEqual(self.site_id.site_id, None) with self.site_id.override(1): self.assertEqual(self.site_id.site_id, 1) with self.site_id.override(2): self.assertEqual(self.site_id.site_id, 2) self.assertEqual(self.site_id.site_id, 1) self.assertEqual(self.site_id.site_id, None)
""" A settings file with the bare minimum attributes. """ import os from pathlib import Path import environ from multisite import SiteID APP_NAME = "inte_edc" BASE_DIR = str(Path(os.path.dirname(os.path.abspath(__file__))).parent.parent) ENV_DIR = str(Path(os.path.dirname(os.path.abspath(__file__))).parent.parent) env = environ.Env() env.read_env(os.path.join(ENV_DIR, ".env")) EDC_SITES_MODULE_NAME = env.str("EDC_SITES_MODULE_NAME") ETC_DIR = env.str("DJANGO_ETC_FOLDER") SECRET_KEY = "blahblahblah" ALLOWED_HOSTS = ["*"] SITE_ID = SiteID(default=1) # 1 is not a site
# Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.environ.get("SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = int(os.environ.get("DEBUG", default=0)) # 'DJANGO_ALLOWED_HOSTS' should be a single string of hosts with a space between each. # For example: 'DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]' ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ") AUTH_USER_MODEL = "accounts.BlinkUser" SITE_ID = SiteID(default=2) # Application definition INSTALLED_APPS = [ "blink", "bettertexts", "email_registration", "bootstrap3", "bootstrap_admin", "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles",
def _update_defaults(self): """Assumes BASE_DIR, APP_NAME are in kwargs.""" self.settings.update( ALLOWED_HOSTS=["localhost"], STATIC_URL="/static/", DATABASES={ # required for tests when acting as a server that deserializes "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": os.path.join(self.base_dir, "db.sqlite3"), }, "client": { "ENGINE": "django.db.backends.sqlite3", "NAME": os.path.join(self.base_dir, "db.sqlite3"), }, }, TEMPLATES=[{ "BACKEND": "django.template.backends.django.DjangoTemplates", "APP_DIRS": True, "OPTIONS": { "context_processors": [ "django.contrib.auth.context_processors.auth", "django.contrib.messages.context_processors.messages", "django.template.context_processors.request", ] }, }], MIDDLEWARE=[ "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", "django.contrib.sites.middleware.CurrentSiteMiddleware", ], LANGUAGE_CODE="en-us", TIME_ZONE="UTC", USE_I18N=True, USE_L10N=True, USE_TZ=True, DEFAULT_AUTO_FIELD="django.db.models.BigAutoField", EDC_BOOTSTRAP=3, GIT_DIR=self.base_dir, LIVE_SYSTEM=False, REVIEWER_SITE_ID=0, SITE_ID=SiteID(default=1) if SiteID else 1, SILENCED_SYSTEM_CHECKS=[ "sites.E101" ], # The SITE_ID setting must be an integer SECRET_KEY=uuid4().hex, HOLIDAY_FILE=os.path.join(self.base_dir, self.app_name, "tests", "holidays.csv"), INDEX_PAGE_LABEL="", DASHBOARD_URL_NAMES={}, DASHBOARD_BASE_TEMPLATES={}, EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend", EMAIL_CONTACTS={ "data_request": "*****@*****.**", "data_manager": "*****@*****.**", "tmg": "*****@*****.**", }, EMAIL_ENABLED=True, INDEX_PAGE="http://localhost:8000", SENTRY_ENABLED=False, TWILIO_ENABLED=False, TWILIO_TEST_RECIPIENT="+15555555555", SUBJECT_SCREENING_MODEL=f"{self.app_name}.subjectscreening", SUBJECT_CONSENT_MODEL=f"{self.app_name}.subjectconsent", SUBJECT_VISIT_MODEL=f"{self.app_name}.subjectvisit", SUBJECT_VISIT_MISSED_MODEL=f"{self.app_name}.subjectvisitmissed", SUBJECT_REQUISITION_MODEL=f"{self.app_name}.subjectrequisition", SUBJECT_APP_LABEL=f"{self.app_name}", ADVERSE_EVENT_ADMIN_SITE="adverse_event_app_admin", ADVERSE_EVENT_APP_LABEL="adverse_event_app", DJANGO_COLLECT_OFFLINE_ENABLED=False, DJANGO_COLLECT_OFFLINE_FILES_REMOTE_HOST=None, DJANGO_COLLECT_OFFLINE_FILES_USB_VOLUME=None, DJANGO_COLLECT_OFFLINE_FILES_USER=None, DJANGO_COLLECT_OFFLINE_SERVER_IP=None, EDC_NAVBAR_DEFAULT=self.app_name, EDC_PROTOCOL_PROJECT_NAME="EDC TEST PROJECT", EDC_PROTOCOL_STUDY_OPEN_DATETIME=arrow.utcnow().datetime - relativedelta(years=1), EDC_PROTOCOL_STUDY_CLOSE_DATETIME=arrow.utcnow().datetime + relativedelta(years=1), EDC_PROTOCOL_NUMBER="101", EDC_FACILITY_USE_DEFAULTS=True, EDC_FACILITY_DEFAULT_FACILITY_NAME="7-day-clinic", LIST_MODEL_APP_LABEL=self.app_name.replace("edc", "lists"), EDC_RANDOMIZATION_LIST_PATH=os.path.join(self.base_dir, self.app_name, "tests", "etc"), EDC_RANDOMIZATION_REGISTER_DEFAULT_RANDOMIZER=True, EDC_RANDOMIZATION_SKIP_VERIFY_CHECKS=True, EDC_SITES_MODULE_NAME=None, DATA_DICTIONARY_APP_LABELS=[], DEFAULT_FILE_STORAGE="inmemorystorage.InMemoryStorage", MIGRATION_MODULES=get_migrations_module(), PASSWORD_HASHERS=( "django.contrib.auth.hashers.MD5PasswordHasher", ), )