unit tests and integration tests

Signed-off-by: CocoByte <nicolle.leclair@gmail.com>
This commit is contained in:
CocoByte 2023-10-27 11:22:47 -06:00
parent 15262f3747
commit 72ae138991
No known key found for this signature in database
GPG key ID: BBFAA2526384C97F
4 changed files with 229 additions and 105 deletions

View file

@ -1,3 +1,4 @@
from unittest.mock import patch
from django.test import TestCase
from registrar.models import (
@ -10,21 +11,22 @@ from registrar.models import (
)
from registrar.management.commands.master_domain_migrations import Command as master_migration_command
from registrar.management.commands.utility.terminal_helper import (
TerminalHelper,
)
from django.core.management import call_command
class TestLogins(TestCase):
"""Test ......"""
""" """
def setUp(self):
""" """
# self.user, _ = User.objects.get_or_create(email=self.email)
# self.load_transition_domain_script = "load_transition_domain",
# self.transfer_script = "transfer_transition_domains_to_domains",
# self.master_script = "load_transition_domain",
# # clean out the roles each time
# UserDomainRole.objects.all().delete()
self.test_data_file_location = "/app/registrar/tests/data"
self.test_domain_contact_filename = "test_domain_contacts.txt"
self.test_contact_filename = "test_contacts.txt"
self.test_domain_status_filename = "test_domain_statuses.txt"
def tearDown(self):
super().tearDown()
@ -32,6 +34,29 @@ class TestLogins(TestCase):
Domain.objects.all().delete()
DomainInvitation.objects.all().delete()
DomainInformation.objects.all().delete()
User.objects.all().delete()
UserDomainRole.objects.all().delete()
def run_load_domains(self):
call_command(
"load_transition_domain",
f"{self.test_data_file_location}/{self.test_domain_contact_filename}",
f"{self.test_data_file_location}/{self.test_contact_filename}",
f"{self.test_data_file_location}/{self.test_domain_status_filename}",
)
def run_transfer_domains(self):
call_command("transfer_transition_domains_to_domains")
def run_master_script(self):
command = call_command(
"master_domain_migrations",
runMigrations=True,
migrationDirectory=f"{self.test_data_file_location}",
migrationFilenames=(f"{self.test_domain_contact_filename},"
f"{self.test_contact_filename},"
f"{self.test_domain_status_filename}"),
)
def compare_tables(self,
expected_total_transition_domains,
@ -102,6 +127,7 @@ class TestLogins(TestCase):
self.assertTrue(total_domains == expected_total_domains)
self.assertTrue(total_domain_informations == expected_total_domain_informations)
self.assertTrue(total_domain_invitations == expected_total_domain_invitations)
def test_master_migration_functions(self):
""" Run the full master migration script using local test data.
@ -110,28 +136,23 @@ class TestLogins(TestCase):
But for now, this will double-check that the script
works as intended. """
migration_directory = "/app/registrar/tests/data/"
contacts_filename = "test_contacts.txt"
domain_contacts_filename = "test_domain_contacts.txt"
domain_statuses_filename = "test_domain_statuses.txt"
self.run_master_script()
# STEP 1: Run the master migration script using local test data
master_migration_command.run_load_transition_domain_script(master_migration_command(),
migration_directory,
domain_contacts_filename,
contacts_filename,
domain_statuses_filename,
"|",
False,
False,
False,
0)
# run_master_script_command = "./manage.py master_domain_migrations"
# run_master_script_command += " --runMigrations"
# run_master_script_command += " --migrationDirectory /app/registrar/tests/data"
# run_master_script_command += " --migrationFilenames test_contacts.txt,test_domain_contacts.txt,test_domain_statuses.txt"
# TerminalHelper.execute_command(run_master_script_command)
# TODO: instead of patching....there has got to be a way of making sure subsequent commands use the django database
# Patch subroutines for migrations
def side_effect():
self.run_load_domains()
self.run_transfer_domains()
patcher = patch("registrar.management.commands.master_domain_migrations.Command.run_migration_scripts")
mocked_get = patcher.start()
mocked_get.side_effect = side_effect
# Patch subroutines for sending invitations
def side_effect():
# TODO: what should happen here?
return
patcher = patch("registrar.management.commands.master_domain_migrations.Command.run_send_invites_script")
mocked_get = patcher.start()
mocked_get.side_effect = side_effect
# STEP 2: (analyze the tables just like the migration script does, but add assert statements)
expected_total_transition_domains = 8
@ -154,11 +175,85 @@ class TestLogins(TestCase):
expected_missing_domain_informations,
expected_missing_domain_invitations,
)
def test_load_transition_domain(self):
self.run_load_domains()
def test_load_transition_domains():
""" """
# STEP 2: (analyze the tables just like the migration script does, but add assert statements)
expected_total_transition_domains = 8
expected_total_domains = 0
expected_total_domain_informations = 0
expected_total_domain_invitations = 0
def test_user_logins(self):
"""A new user's first_login callback retrieves their invitations."""
# self.user.first_login()
# self.assertTrue(UserDomainRole.objects.get(user=self.user, domain=self.domain))
expected_missing_domains = 8
expected_duplicate_domains = 0
expected_missing_domain_informations = 8
expected_missing_domain_invitations = 8
self.compare_tables(expected_total_transition_domains,
expected_total_domains,
expected_total_domain_informations,
expected_total_domain_invitations,
expected_missing_domains,
expected_duplicate_domains,
expected_missing_domain_informations,
expected_missing_domain_invitations,
)
def test_transfer_transition_domains_to_domains(self):
# TODO: setup manually instead of calling other script
self.run_load_domains()
self.run_transfer_domains()
# Analyze the tables
expected_total_transition_domains = 8
expected_total_domains = 4
expected_total_domain_informations = 0
expected_total_domain_invitations = 7
expected_missing_domains = 0
expected_duplicate_domains = 0
expected_missing_domain_informations = 8
expected_missing_domain_invitations = 1
self.compare_tables(expected_total_transition_domains,
expected_total_domains,
expected_total_domain_informations,
expected_total_domain_invitations,
expected_missing_domains,
expected_duplicate_domains,
expected_missing_domain_informations,
expected_missing_domain_invitations,
)
def test_logins(self):
# TODO: setup manually instead of calling other scripts
self.run_load_domains()
self.run_transfer_domains()
# Simluate Logins
for invite in DomainInvitation.objects.all():
# get a user with this email address
user, user_created = User.objects.get_or_create(email=invite.email, username=invite.email)
user.first_login()
# Analyze the tables
expected_total_transition_domains = 8
expected_total_domains = 4
expected_total_domain_informations = 3
expected_total_domain_invitations = 7
expected_missing_domains = 0
expected_duplicate_domains = 0
expected_missing_domain_informations = 1
expected_missing_domain_invitations = 1
self.compare_tables(expected_total_transition_domains,
expected_total_domains,
expected_total_domain_informations,
expected_total_domain_invitations,
expected_missing_domains,
expected_duplicate_domains,
expected_missing_domain_informations,
expected_missing_domain_invitations,
)