-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move wasteline app * move root confest to server dir * move org app * move profile app * move handler app * move manifest app * move rcrasite app * rename site app to orgsite app * move core app * move orgsite app
- Loading branch information
1 parent
00124bf
commit 0b41b20
Showing
185 changed files
with
1,457 additions
and
917 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,377 +0,0 @@ | ||
import datetime | ||
import json | ||
import os | ||
import random | ||
import string | ||
from enum import Enum | ||
from typing import Dict, Literal, Optional | ||
|
||
import pytest | ||
import pytest_mock | ||
import responses | ||
from django.contrib.auth.models import User | ||
from django.db import IntegrityError | ||
from faker import Faker | ||
from faker.providers import BaseProvider | ||
from rest_framework.test import APIClient | ||
|
||
from apps.core.models import ( | ||
TrakUser, | ||
) | ||
from apps.org.models import Org, OrgAccess | ||
from apps.profile.models import Profile, RcrainfoProfile | ||
from apps.rcrasite.models import ( | ||
Address, | ||
Contact, | ||
RcraPhone, | ||
RcraSite, | ||
) | ||
from apps.site.models import Site, SiteAccess | ||
|
||
|
||
class SiteIDProvider(BaseProvider): | ||
PREFIXES = ["VAT", "VAD", "TXD", "TXR", "TND", "TNR", "LAD", "LAR", "CAD", "CAR", "MAD", "MAR"] | ||
NUMBERS = ["".join(random.choices(string.digits, k=9)) for _ in range(100)] | ||
|
||
def site_id(self): | ||
return f"{self.random_element(self.PREFIXES)}{self.random_element(self.NUMBERS)}" | ||
|
||
|
||
@pytest.fixture | ||
def haztrak_json(): | ||
"""Fixture with JSON data""" | ||
json_dir = os.path.dirname(os.path.abspath(__file__)) + "/../fixtures/json" | ||
|
||
def read_file(path: str) -> Dict: | ||
with open(path) as f: | ||
return json.load(f) | ||
|
||
class Json(Enum): | ||
CONTACT = read_file(f"{json_dir}/contact/good_contact.json") | ||
PHONE = read_file(f"{json_dir}/contact/phone.json") | ||
WASTELINE_1 = read_file(f"{json_dir}/test_wasteline1.json") | ||
MANIFEST = read_file(f"{json_dir}/test_manifest_100033134ELC.json") | ||
SITE_PERMISSION = read_file(f"{json_dir}/site_permission.json") | ||
EPA_PERMISSION = read_file(f"{json_dir}/epa_permission.json") | ||
HANDLER = read_file(f"{json_dir}/test_handler.json") | ||
PAPER_MANIFEST_HANDLER = read_file(f"{json_dir}/paper_manifest_handler.json") | ||
E_SIGNATURE = read_file(f"{json_dir}/test_e_signature.json") | ||
|
||
return Json | ||
|
||
|
||
@pytest.fixture | ||
def user_factory(db, faker): | ||
"""Abstract factory for Django's User model""" | ||
|
||
def create_user( | ||
username: Optional[str] = None, | ||
first_name: Optional[str] = None, | ||
last_name: Optional[str] = None, | ||
email: Optional[str] = None, | ||
password: Optional[str] = None, | ||
) -> TrakUser: | ||
return TrakUser.objects.create_user( | ||
username=username or faker.user_name(), | ||
first_name=first_name or faker.first_name(), | ||
last_name=last_name or faker.last_name(), | ||
email=email or faker.email(), | ||
password=password or faker.password(), | ||
) | ||
|
||
return create_user | ||
|
||
|
||
@pytest.fixture | ||
def rcrainfo_profile_factory(db, user_factory, faker: Faker): | ||
"""Abstract factory for Haztrak RcrainfoProfile model""" | ||
|
||
def create_profile( | ||
rcra_api_id: Optional[str] = str(faker.uuid4()), | ||
rcra_api_key: Optional[str] = faker.pystr(min_chars=15), | ||
rcra_username: Optional[str] = faker.pystr(min_chars=12), | ||
) -> RcrainfoProfile: | ||
return RcrainfoProfile.objects.create( | ||
rcra_api_id=rcra_api_id, | ||
rcra_api_key=rcra_api_key, | ||
rcra_username=rcra_username, | ||
) | ||
|
||
return create_profile | ||
|
||
|
||
@pytest.fixture | ||
def profile_factory(db, user_factory, rcrainfo_profile_factory, org_factory): | ||
"""Abstract factory for Haztrak RcrainfoProfile model""" | ||
|
||
def create_profile( | ||
user: Optional[User] = None, | ||
rcrainfo_profile: Optional[RcrainfoProfile] = rcrainfo_profile_factory(), | ||
) -> Profile: | ||
return Profile.objects.create( | ||
user=user or user_factory(), | ||
rcrainfo_profile=rcrainfo_profile, | ||
) | ||
|
||
return create_profile | ||
|
||
|
||
@pytest.fixture | ||
def address_factory(db, faker: Faker): | ||
"""Abstract factory for Haztrak Address model""" | ||
|
||
def create_address( | ||
address1: Optional[str] = None, | ||
street_number: Optional[str] = None, | ||
country: Optional[str] = "US", | ||
city: Optional[str] = None, | ||
) -> Address: | ||
return Address.objects.create( | ||
address1=address1 or faker.street_name(), | ||
street_number=street_number or faker.building_number(), | ||
country=country, | ||
city=city or faker.city(), | ||
) | ||
|
||
return create_address | ||
|
||
|
||
@pytest.fixture | ||
def rcra_phone_factory(db, faker: Faker): | ||
"""Abstract factory for Haztrak ManifestPhone model""" | ||
|
||
def create_site_phone( | ||
number: Optional[str] = "202-505-5500", | ||
extension: Optional[str] = "1234", | ||
) -> RcraPhone: | ||
return RcraPhone.objects.create( | ||
number=number, | ||
extension=extension, | ||
) | ||
|
||
return create_site_phone | ||
|
||
|
||
@pytest.fixture | ||
def contact_factory(db, rcra_phone_factory, faker: Faker): | ||
"""Abstract factory for Haztrak Contact model""" | ||
|
||
def create_contact( | ||
first_name: Optional[str] = None, | ||
middle_initial: Optional[str] = None, | ||
last_name: Optional[str] = None, | ||
email: Optional[str] = None, | ||
phone: Optional[RcraPhone] = None, | ||
) -> Contact: | ||
contact = Contact.objects.create( | ||
first_name=first_name or faker.first_name(), | ||
middle_initial=middle_initial or faker.pystr(max_chars=1), | ||
last_name=last_name or faker.last_name(), | ||
email=email or faker.email(), | ||
phone=phone or rcra_phone_factory(), | ||
) | ||
return contact | ||
|
||
return create_contact | ||
|
||
|
||
@pytest.fixture | ||
def rcra_site_factory(db, address_factory, contact_factory): | ||
"""Abstract factory for Haztrak RcraSite model""" | ||
|
||
def create_rcra_site( | ||
epa_id: Optional[str] = None, | ||
name: Optional[str] = None, | ||
site_type: Optional[Literal["Generator", "Transporter", "Tsdf"]] = "Generator", | ||
site_address: Optional[Address] = None, | ||
mail_address: Optional[Address] = None, | ||
) -> RcraSite: | ||
fake = Faker() | ||
fake.add_provider(SiteIDProvider) | ||
while True: | ||
try: | ||
return RcraSite.objects.create( | ||
epa_id=epa_id or fake.site_id(), | ||
name=name or fake.name(), | ||
site_type=site_type, | ||
site_address=site_address or address_factory(), | ||
mail_address=mail_address or address_factory(), | ||
contact=contact_factory(), | ||
) | ||
except IntegrityError: | ||
return RcraSite.objects.create( | ||
epa_id=fake.site_id(), | ||
name=name or fake.name(), | ||
site_type=site_type, | ||
site_address=site_address or address_factory(), | ||
mail_address=mail_address or address_factory(), | ||
contact=contact_factory(), | ||
) | ||
|
||
return create_rcra_site | ||
|
||
|
||
@pytest.fixture | ||
def validated_data_factory(): | ||
def _create_data_dict(*, instance, serializer) -> Dict: | ||
data = serializer(instance).data | ||
new_serializer = serializer(data=data) | ||
new_serializer.is_valid(raise_exception=True) | ||
return new_serializer.validated_data | ||
|
||
return _create_data_dict | ||
|
||
|
||
@pytest.fixture | ||
def org_factory(db, rcrainfo_profile_factory, user_factory, faker): | ||
"""Abstract factory for Haztrak Org model""" | ||
|
||
def create_org( | ||
org_id: Optional[str] = None, | ||
name: Optional[str] = None, | ||
admin: Optional[TrakUser] = None, | ||
) -> Org: | ||
return Org.objects.create( | ||
id=org_id or faker.uuid4(), | ||
name=name or faker.company(), | ||
admin=admin or user_factory(), | ||
) | ||
|
||
return create_org | ||
|
||
|
||
@pytest.fixture | ||
def site_factory(db, rcra_site_factory, org_factory, faker): | ||
"""Abstract factory for Haztrak Site model""" | ||
|
||
def create_site( | ||
rcra_site: Optional[RcraSite] = None, | ||
name: Optional[str] = None, | ||
org: Optional[Org] = None, | ||
last_rcrainfo_manifest_sync: Optional[datetime.datetime] = None, | ||
) -> Site: | ||
return Site.objects.create( | ||
rcra_site=rcra_site or rcra_site_factory(), | ||
name=name or faker.name(), | ||
org=org or org_factory(), | ||
last_rcrainfo_manifest_sync=last_rcrainfo_manifest_sync | ||
or datetime.datetime.now(datetime.UTC), | ||
) | ||
|
||
return create_site | ||
|
||
|
||
@pytest.fixture | ||
def api_client_factory(db, user_factory): | ||
"""Abstract factory for DRF APIClient testing class""" | ||
|
||
def create_client( | ||
user: Optional[User] = None, | ||
) -> APIClient: | ||
client = APIClient() | ||
client.force_authenticate( | ||
user=user or user_factory(), | ||
) | ||
return client | ||
|
||
return create_client | ||
|
||
|
||
@pytest.fixture | ||
def mock_responses(): | ||
""" | ||
fixture for mocking external http request responses | ||
see Responses docs | ||
https://github.com/getsentry/responses#responses-as-a-pytest-fixture | ||
""" | ||
with responses.RequestsMock() as mock_responses: | ||
yield mock_responses | ||
|
||
|
||
@pytest.fixture() | ||
def mock_emanifest_auth_response(request, mock_responses): | ||
api_id, api_key = request.param | ||
mock_responses.get( | ||
f"https://rcrainfopreprod.epa.gov/rcrainfo/rest/api/v1/auth/{api_id}/{api_key}", | ||
body='{"token": "mocK_token", "expiration": "2021-01-01T00:00:00.000000Z"}', | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def mocker(mocker: pytest_mock.MockerFixture): | ||
""" | ||
wrapper fixture pytest-mock's mocker fixture for easy type annotations | ||
https://github.com/pytest-dev/pytest-mock | ||
""" | ||
return mocker | ||
|
||
|
||
@pytest.fixture | ||
def site_access_factory(db, faker, site_factory, profile_factory): | ||
"""Abstract factory for Haztrak RcraSitePermissions model""" | ||
|
||
def create_permission( | ||
site: Optional[Site] = None, | ||
user: Optional[TrakUser] = None, | ||
emanifest: Optional[Literal["viewer", "signer", "editor"]] = "viewer", | ||
) -> SiteAccess: | ||
"""Returns testuser1 RcraSitePermissions model to site_generator""" | ||
return SiteAccess.objects.create( | ||
site=site or site_factory(), | ||
user=user or user_factory(), | ||
emanifest=emanifest, | ||
) | ||
|
||
return create_permission | ||
|
||
|
||
@pytest.fixture | ||
def user_with_org_factory( | ||
db, | ||
user_factory, | ||
org_factory, | ||
rcrainfo_profile_factory, | ||
profile_factory, | ||
org_access_factory, | ||
): | ||
"""Fixture for creating a user with an org that has set up RCRAInfo integration""" | ||
|
||
def create_fixtures( | ||
user: Optional[User] = None, | ||
org: Optional[Org] = None, | ||
admin_rcrainfo_profile: Optional[RcrainfoProfile] = None, | ||
is_rcrainfo_enabled: Optional[bool] = True, | ||
): | ||
if is_rcrainfo_enabled: | ||
rcra_profile_data = { | ||
"rcra_api_id": "mock_api_id", | ||
"rcra_api_key": "mock_api_key", | ||
"rcra_username": "mock_username", | ||
} | ||
else: | ||
rcra_profile_data = {"rcra_api_id": None, "rcra_api_key": None, "rcra_username": None} | ||
user = user or user_factory() | ||
admin = user_factory(username="admin") | ||
admin_rcrainfo_profile or rcrainfo_profile_factory(**rcra_profile_data) | ||
org = org or org_factory(admin=admin) | ||
org_access_factory(org=org, user=user) | ||
profile_factory(user=user) | ||
return user, org | ||
|
||
return create_fixtures | ||
|
||
|
||
@pytest.fixture | ||
def org_access_factory(db, user_factory, org_factory): | ||
"""Abstract factory for creating a model that represents a user's access to an organization""" | ||
|
||
def create_permission( | ||
org: Optional[Org] = None, | ||
user: Optional[TrakUser] = None, | ||
) -> OrgAccess: | ||
return OrgAccess.objects.create( | ||
org=org or org_factory(), | ||
user=user or user_factory(), | ||
) | ||
|
||
return create_permission | ||
Oops, something went wrong.