Skip to content

Commit

Permalink
Merge pull request #646 from fecgov/release/sprint-35
Browse files Browse the repository at this point in the history
Release/sprint 35
  • Loading branch information
mjtravers authored Jan 3, 2024
2 parents ec3eb35 + 1697b97 commit a9b15c6
Show file tree
Hide file tree
Showing 42 changed files with 995 additions and 537 deletions.
8 changes: 4 additions & 4 deletions Dockerfile-e2e
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
FROM python:3.10
ENV PYTHONUNBUFFERED=1

RUN mkdir /opt/nxg_fec_e2e
WORKDIR /opt/nxg_fec_e2e
RUN mkdir /opt/nxg_fec
WORKDIR /opt/nxg_fec
ADD requirements.txt /opt
ADD django-backend /opt/nxg_fec_e2e/
ADD django-backend /opt/nxg_fec/
RUN pip3 install -r /opt/requirements.txt

RUN mv /etc/localtime /etc/localtime.backup && ln -s /usr/share/zoneinfo/EST5EDT /etc/localtime

RUN useradd nxgu --no-create-home --home /opt/nxg_fec_e2e && chown -R nxgu:nxgu /opt/nxg_fec_e2e
RUN useradd nxgu --no-create-home --home /opt/nxg_fec && chown -R nxgu:nxgu /opt/nxg_fec
USER nxgu

EXPOSE 8080
Expand Down
42 changes: 27 additions & 15 deletions django-backend/fecfiler/contacts/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
from .models import Contact
from .views import ContactViewSet, DeletedContactsViewSet

mock_results = {
"results": [
{"name": "LNAME, FNAME I", "id": "P60012143", "office_sought": "P"},
{
"name": "LNAME, FNAME",
"id": "P60012465",
"office_sought": "P",
},
]
}


def mocked_requests_get_candidates(*args, **kwargs):
class MockResponse:
Expand All @@ -18,19 +29,7 @@ def __init__(self, json_data, status_code):
def json(self):
return self.json_data

return MockResponse(
{
"results": [
{"name": "BIDEN, JOE R", "id": "P60012143", "office_sought": "P"},
{
"name": "BIDEN, JR., JOSEPH R.",
"id": "P60012465",
"office_sought": "P",
},
]
},
200,
)
return MockResponse(mock_results, 200)


def mocked_requests_get_committees(*args, **kwargs):
Expand Down Expand Up @@ -60,6 +59,19 @@ def setUp(self):
self.user = Account.objects.get(cmtee_id="C12345678")
self.factory = RequestFactory()

@mock.patch("requests.get", side_effect=mocked_requests_get_candidates)
def test_candidate(self, mock_get):
request = self.factory.get(
"/api/v1/contacts/candidate?"
"candidate_id=P60012143"
)
request.user = self.user
response = ContactViewSet.as_view({"get": "candidate"})(request)

expected_json = mock_results
self.assertEqual(response.status_code, 200)
self.assertJSONEqual(str(response.content, encoding="utf8"), expected_json)

def test_candidate_lookup_no_auth(self):
request = self.factory.get("/api/v1/contacts/candidate_lookup")

Expand Down Expand Up @@ -88,9 +100,9 @@ def test_candidate_lookup_happy_path(self, mock_get):

expected_json = {
"fec_api_candidates": [
{"name": "BIDEN, JOE R", "id": "P60012143", "office_sought": "P"},
{"name": "LNAME, FNAME I", "id": "P60012143", "office_sought": "P"},
{
"name": "BIDEN, JR., JOSEPH R.",
"name": "LNAME, FNAME",
"id": "P60012465",
"office_sought": "P",
},
Expand Down
20 changes: 19 additions & 1 deletion django-backend/fecfiler/contacts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from fecfiler.settings import (
FEC_API_CANDIDATE_LOOKUP_ENDPOINT,
FEC_API_COMMITTEE_LOOKUP_ENDPOINT,
FEC_API_CANDIDATE_ENDPOINT,
FEC_API_KEY,
)
from rest_framework.decorators import action
Expand All @@ -32,6 +33,12 @@
)


def validate_and_sanitize_candidate(candidate_id):
if candidate_id is None:
raise AssertionError("No Candidate ID provided")
return candidate_id


class ContactViewSet(CommitteeOwnedViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
Expand Down Expand Up @@ -72,6 +79,17 @@ class ContactViewSet(CommitteeOwnedViewSet):
]
ordering = ["-created"]

@action(detail=False)
def candidate(self, request):
candidate_id = request.query_params.get("candidate_id")
try:
url = (FEC_API_CANDIDATE_ENDPOINT
+ validate_and_sanitize_candidate(candidate_id) + "/")
return JsonResponse(requests.get(url, params=urlencode(
{"api_key": FEC_API_KEY})).json())
except AssertionError:
return HttpResponseBadRequest()

@action(detail=False)
def candidate_lookup(self, request):
q = request.GET.get("q")
Expand Down Expand Up @@ -226,7 +244,7 @@ def update(self, request, *args, **kwargs):
return super().update(request, *args, **kwargs)

def get_int_param_value(
self, request, param_name: str, default_param_value: int, max_param_value: int
self, request, param_name: str, default_param_value: int, max_param_value: int
):
if request:
param_val = request.GET.get(param_name, "")
Expand Down
1 change: 0 additions & 1 deletion django-backend/fecfiler/f3x_summaries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class F3XSummaryViewSet(CommitteeOwnedViewSet):
"report_code_label",
"coverage_through_date",
"upload_submission__fec_status",
"submission_status",
]
ordering = ["form_type"]

Expand Down
138 changes: 138 additions & 0 deletions django-backend/fecfiler/reports/fixtures/test_f3x_reports.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,144 @@
"updated": "2022-02-09T00:00:00.000Z"
}
},
{
"model": "reports.Form3X",
"fields": {
"id": "a6d60d2d-d926-4e89-ad4b-000000000002",
"change_of_address": true,
"street_1": "test_string_value",
"street_2": "test_string_value",
"city": "test_string_value",
"state": "test_string_value",
"zip": "test_string_value",
"election_code": "test_string_value",
"date_of_election": "2005-01-30",
"state_of_election": "test_string_value",
"qualified_committee": false,
"L6b_cash_on_hand_beginning_period": 6,
"L6c_total_receipts_period": 60,
"L6d_subtotal_period": 600,
"L7_total_disbursements_period": 7,
"L8_cash_on_hand_at_close_period": 8,
"L9_debts_to_period": 9,
"L10_debts_by_period": 10,
"L11ai_itemized_period": 11,
"L11aii_unitemized_period": 110,
"L11aiii_total_period": 1100,
"L11b_political_party_committees_period": 11000,
"L11c_other_political_committees_pacs_period": 110000,
"L11d_total_contributions_period": 110000,
"L12_transfers_from_affiliated_other_party_cmtes_period": 12,
"L13_all_loans_received_period": 13,
"L14_loan_repayments_received_period": 14,
"L15_offsets_to_operating_expenditures_refunds_period": 15,
"L16_refunds_of_federal_contributions_period": 16,
"L17_other_federal_receipts_dividends_period": 17,
"L18a_transfers_from_nonfederal_account_h3_period": 18,
"L18b_transfers_from_nonfederal_levin_h5_period": 180,
"L18c_total_nonfederal_transfers_18a_18b_period": 1800,
"L19_total_receipts_period": 19,
"L20_total_federal_receipts_period": 20,
"L21ai_federal_share_period": 21,
"L21aii_nonfederal_share_period": 21,
"L21b_other_federal_operating_expenditures_period": 21,
"L21c_total_operating_expenditures_period": 21,
"L22_transfers_to_affiliated_other_party_cmtes_period": 22,
"L23_contributions_to_federal_candidates_cmtes_period": 23,
"L24_independent_expenditures_period": 24,
"L25_coordinated_expend_made_by_party_cmtes_period": 25,
"L26_loan_repayments_period": 26,
"L27_loans_made_period": 27,
"L28a_individuals_persons_period": 28,
"L28b_political_party_committees_period": 28,
"L28c_other_political_committees_period": 28,
"L28d_total_contributions_refunds_period": 28,
"L29_other_disbursements_period": 29,
"L30ai_shared_federal_activity_h6_fed_share_period": 30,
"L30aii_shared_federal_activity_h6_nonfed_period": 30,
"L30b_nonallocable_fed_election_activity_period": 30,
"L30c_total_federal_election_activity_period": 30,
"L31_total_disbursements_period": 31,
"L32_total_federal_disbursements_period": 32,
"L33_total_contributions_period": 33,
"L34_total_contribution_refunds_period": 34,
"L35_net_contributions_period": 35,
"L36_total_federal_operating_expenditures_period": 36,
"L37_offsets_to_operating_expenditures_period": 37,
"L38_net_operating_expenditures_period": 38,
"L6a_cash_on_hand_jan_1_ytd": 61,
"L6a_year_for_above_ytd": "2005",
"L6c_total_receipts_ytd": 611,
"L6d_subtotal_ytd": 6111,
"L7_total_disbursements_ytd": 71,
"L8_cash_on_hand_close_ytd": 81,
"L11ai_itemized_ytd": 111,
"L11aii_unitemized_ytd": 1111,
"L11aiii_total_ytd": 11111,
"L11b_political_party_committees_ytd": 111111,
"L11c_other_political_committees_pacs_ytd": 1111111,
"L11d_total_contributions_ytd": 11111111,
"L12_transfers_from_affiliated_other_party_cmtes_ytd": 121,
"L13_all_loans_received_ytd": 131,
"L14_loan_repayments_received_ytd": 141,
"L15_offsets_to_operating_expenditures_refunds_ytd": 151,
"L16_refunds_of_federal_contributions_ytd": 161,
"L17_other_federal_receipts_dividends_ytd": 171,
"L18a_transfers_from_nonfederal_account_h3_ytd": 181,
"L18b_transfers_from_nonfederal_levin_h5_ytd": 181,
"L18c_total_nonfederal_transfers_18a_18b_ytd": 181,
"L19_total_receipts_ytd": 191,
"L20_total_federal_receipts_ytd": 201,
"L21ai_federal_share_ytd": 211,
"L21aii_nonfederal_share_ytd": 211,
"L21b_other_federal_operating_expenditures_ytd": 211,
"L21c_total_operating_expenditures_ytd": 211,
"L22_transfers_to_affiliated_other_party_cmtes_ytd": 221,
"L23_contributions_to_federal_candidates_cmtes_ytd": 231,
"L24_independent_expenditures_ytd": 241,
"L25_coordinated_expend_made_by_party_cmtes_ytd": 251,
"L26_loan_repayments_made_ytd": 261,
"L27_loans_made_ytd": 271,
"L28a_individuals_persons_ytd": 281,
"L28b_political_party_committees_ytd": 281,
"L28c_other_political_committees_ytd": 281,
"L28d_total_contributions_refunds_ytd": 281,
"L29_other_disbursements_ytd": 291,
"L30ai_shared_federal_activity_h6_fed_share_ytd": 301,
"L30aii_shared_federal_activity_h6_nonfed_ytd": 301,
"L30b_nonallocable_fed_election_activity_ytd": 301,
"L30c_total_federal_election_activity_ytd": 301,
"L31_total_disbursements_ytd": 311,
"L32_total_federal_disbursements_ytd": 321,
"L33_total_contributions_ytd": 331,
"L34_total_contribution_refunds_ytd": 341,
"L35_net_contributions_ytd": 351,
"L36_total_federal_operating_expenditures_ytd": 361,
"L37_offsets_to_operating_expenditures_ytd": 371,
"L38_net_operating_expenditures_ytd": 381
}
},
{
"model": "reports.report",
"fields": {
"id": "b6d60d2d-d926-4e89-ad4b-000000000002",
"form_3x_id": "a6d60d2d-d926-4e89-ad4b-000000000002",
"committee_account_id": "735db943-9446-462a-9be0-c820baadb622",
"form_type": "F3XN",
"report_code": "M4",
"calculation_status": null,
"coverage_from_date": "2005-03-01",
"coverage_through_date": "2005-03-31",
"treasurer_last_name": "Lastname",
"treasurer_first_name": "Firstname",
"treasurer_middle_name": "test_string_value",
"treasurer_prefix": "test_string_value",
"treasurer_suffix": "test_string_value",
"date_signed": "2004-07-29",
"created": "2022-02-09T00:00:00.000Z",
"updated": "2022-02-09T00:00:00.000Z"
}
},
{
"model": "reports.Form3X",
"fields": {
Expand Down
9 changes: 0 additions & 9 deletions django-backend/fecfiler/reports/form_1m/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from rest_framework import filters
from rest_framework.viewsets import ModelViewSet
from fecfiler.reports.models import Report
from fecfiler.reports.managers import ReportType
Expand All @@ -20,14 +19,6 @@ class Form1MViewSet(ReportViewSet):
)

serializer_class = Form1MSerializer
filter_backends = [filters.OrderingFilter]
ordering_fields = [
"form_type",
"report_code_label",
"upload_submission__fec_status",
"submission_status",
]
ordering = ["form_type"]

def create(self, request):
return super(ModelViewSet, self).create(request)
Expand Down
9 changes: 0 additions & 9 deletions django-backend/fecfiler/reports/form_24/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from rest_framework import filters
from rest_framework.viewsets import ModelViewSet
from fecfiler.reports.models import Report
from fecfiler.reports.managers import ReportType
Expand All @@ -20,14 +19,6 @@ class Form24ViewSet(ReportViewSet):
)

serializer_class = Form24Serializer
filter_backends = [filters.OrderingFilter]
ordering_fields = [
"form_type",
"report_code_label",
"upload_submission__fec_status",
"submission_status",
]
ordering = ["form_type"]

def create(self, request):
return super(ModelViewSet, self).create(request)
Expand Down
8 changes: 5 additions & 3 deletions django-backend/fecfiler/reports/form_3x/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
class Form3XSerializer(ReportSerializer):
schema_name = "F3X"

is_first = BooleanField(read_only=True, allow_null=True)
change_of_address = BooleanField(required=False, allow_null=True)
street_1 = CharField(required=False, allow_null=True)
street_2 = CharField(required=False, allow_null=True)
Expand Down Expand Up @@ -345,8 +346,9 @@ def to_internal_value(self, data):
def create(self, validated_data: dict):
with transaction.atomic():
form_3x_data = get_model_data(validated_data, Form3X)
form_3x = Form3X.objects.create(**form_3x_data)
report_data = get_model_data(validated_data, Report)
form_3x_data["L6a_year_for_above_ytd"] = report_data["coverage_from_date"].year # noqa: E501
form_3x = Form3X.objects.create(**form_3x_data)
report_data["form_3x_id"] = form_3x.id
report = super().create(report_data)
return report
Expand Down Expand Up @@ -392,7 +394,7 @@ class Meta(ReportSerializer.Meta):
for f in Form3X._meta.get_fields()
if f.name not in ["committee_name", "report"]
]
+ ["fields_to_validate"]
+ ["fields_to_validate", "is_first"]
)

read_only_fields = ["id"]
read_only_fields = ["id", "deleted", "created", "updated", "is_first"]
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def setUp(self):
)

def test_get_f3x_summary(self):
f3x_summary = Form3X.objects.get(L6a_year_for_above_ytd="2005")
f3x_summary = Form3X.objects.get(id="a6d60d2d-d926-4e89-ad4b-c47d152a66ae")
self.assertEquals(f3x_summary.election_code, "test_string_value")

def test_save_and_delete(self):
Expand Down
5 changes: 5 additions & 0 deletions django-backend/fecfiler/reports/form_3x/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def test_coverage_dates_happy_path(self):
"coverage_through_date": "2005-02-28",
"report_code": "Q1",
},
{
"coverage_from_date": "2005-03-01",
"coverage_through_date": "2005-03-31",
"report_code": "M4",
},
{
"coverage_from_date": "2006-01-30",
"coverage_through_date": "2006-02-28",
Expand Down
Loading

0 comments on commit a9b15c6

Please sign in to comment.