Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1875 - Add CommitteeOwnedMixin tests for both Models and Views #1257

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions django-backend/fecfiler/cash_on_hand/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from fecfiler.routers import register_router
from .views import CashOnHandYearlyViewSet

# Create a router and register our viewsets with it.
router = DefaultRouter()
router = register_router()
router.register(r"cash_on_hand", CashOnHandYearlyViewSet, basename="cash_on_hand")

# The API URLs are now determined automatically by the router.
Expand Down
42 changes: 38 additions & 4 deletions django-backend/fecfiler/committee_accounts/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.apps import apps
from django.test import TestCase
from .models import CommitteeAccount
from .models import CommitteeAccount, CommitteeOwnedModel


class CommitteeAccountTestCase(TestCase):
Expand All @@ -16,9 +17,7 @@ def test_get_contact(self):

def test_save_and_delete(self):
self.valid_committee_account.save()
committee_account_from_db = CommitteeAccount.objects.get(
committee_id="C87654321"
)
committee_account_from_db = CommitteeAccount.objects.get(committee_id="C87654321")
self.assertIsInstance(committee_account_from_db, CommitteeAccount)
self.assertEquals(committee_account_from_db.committee_id, "C87654321")
committee_account_from_db.delete()
Expand All @@ -39,3 +38,38 @@ def test_save_and_delete(self):
CommitteeAccount.all_objects.get,
committee_id="C87654321",
)

def test_all_models_include_committee_owned_mixin(self):
ignore_list = [
"Permission",
"Group",
"ContentType",
"Session",
"CommitteeAccount",
"Membership",
"Form3X",
"Form24",
"Form99",
"Form1M",
"ReportTransaction",
"ScheduleA",
"ScheduleB",
"ScheduleC",
"ScheduleC1",
"ScheduleC2",
"ScheduleD",
"ScheduleE",
"DotFEC",
"UploadSubmission",
"WebPrintSubmission",
"User",
]
app_models = apps.get_models()

for model in app_models:
with self.subTest(model=model):
if model.__name__ not in ignore_list:
self.assertTrue(
issubclass(model, CommitteeOwnedModel),
f"{model.__name__} does not include CommitteeOwnedModel mixin.",
)
49 changes: 47 additions & 2 deletions django-backend/fecfiler/committee_accounts/test_views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from uuid import UUID
from django.test import RequestFactory, TestCase
from fecfiler.committee_accounts.models import Membership
from fecfiler.committee_accounts.views import CommitteeMembershipViewSet, CommitteeViewSet

from fecfiler.committee_accounts.views import (
CommitteeMembershipViewSet,
CommitteeOwnedViewMixin,
CommitteeViewSet,
)
from fecfiler.user.models import User
from unittest.mock import Mock, patch
from fecfiler.routers import get_all_routers
import structlog

logger = structlog.get_logger(__name__)


class CommitteeMemberViewSetTest(TestCase):
Expand Down Expand Up @@ -221,3 +228,41 @@ def test_get_committee_account_data_from_redis(self):
self.assertNotEqual(len(was_called_with), 0)
self.assertIn("C12345678", was_called_with)
self.assertEqual(response.data["name"], "TEST")

def test_viewsets_have_committee_owned_mixin(self):
exclude_list = [
"CommitteeViewSet",
"UserViewSet",
"SystemStatusViewSet",
"SummaryViewSet",
"FeedbackViewSet",
]
routers = get_all_routers()
missing_mixin = []

for router in routers:
for prefix, viewset, basename in router.registry:
if (
not issubclass(viewset, CommitteeOwnedViewMixin)
and viewset.__name__ not in exclude_list
):
missing_mixin.append(
{
"viewset": viewset.__name__,
"prefix": prefix,
"basename": basename,
}
)

if missing_mixin:
error_message = "\n".join(
[
f"ViewSet '{entry['viewset']}' "
f"does not inherit from CommitteeOwnedViewMixin."
for entry in missing_mixin
]
)
self.fail(
f"The following ViewSets are missing CommitteeOwnedViewMixin:\n"
f"{error_message}"
)
10 changes: 5 additions & 5 deletions django-backend/fecfiler/committee_accounts/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from rest_framework.routers import DefaultRouter
from fecfiler.routers import register_router
from .views import CommitteeMembershipViewSet, CommitteeViewSet

# Create a router and register our viewsets with it.
router = DefaultRouter()
router = register_router()
router.register(r"committees", CommitteeViewSet, basename="committees")
router.register(r"committee-members", CommitteeMembershipViewSet,
basename="committee-members")
router.register(
r"committee-members", CommitteeMembershipViewSet, basename="committee-members"
)

# The API URLs are now determined automatically by the router.
urlpatterns = []
Expand Down
9 changes: 3 additions & 6 deletions django-backend/fecfiler/contacts/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from fecfiler.routers import register_router
from .views import ContactViewSet, DeletedContactsViewSet

# Create a router and register our viewsets with it.
router = DefaultRouter()
router = register_router()
router.register(r"contacts", ContactViewSet, basename="contacts")
router.register(
r"contacts-deleted", DeletedContactsViewSet, basename="contacts-deleted"
)
router.register(r"contacts-deleted", DeletedContactsViewSet, basename="contacts-deleted")

# The API URLs are now determined automatically by the router.
urlpatterns = [
Expand Down
6 changes: 2 additions & 4 deletions django-backend/fecfiler/devops/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from fecfiler.routers import register_router
from fecfiler.devops.views import SystemStatusViewSet


# Create a router and register our viewsets with it.
router = DefaultRouter()
router = register_router()
router.register(r"devops", SystemStatusViewSet, basename="devops")

# The API URLs are now determined automatically by the router.
Expand Down
5 changes: 2 additions & 3 deletions django-backend/fecfiler/feedback/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from fecfiler.routers import register_router

from .views import FeedbackViewSet

# Create a router and register our viewsets with it.
router = DefaultRouter()
router = register_router()
router.register(r"feedback", FeedbackViewSet, basename="feedback")

# The API URLs are now determined automatically by the router.
Expand Down
8 changes: 3 additions & 5 deletions django-backend/fecfiler/memo_text/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from fecfiler.routers import register_router
from .views import MemoTextViewSet

# Create a router and register our viewsets with it.
router = DefaultRouter()
router.register(r"memo-text", MemoTextViewSet,
basename="memo-text")
router = register_router()
router.register(r"memo-text", MemoTextViewSet, basename="memo-text")

# The API URLs are now determined automatically by the router.
urlpatterns = [
Expand Down
3 changes: 1 addition & 2 deletions django-backend/fecfiler/memo_text/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from .models import MemoText
from .serializers import MemoTextSerializer
from fecfiler.committee_accounts.views import CommitteeOwnedViewMixin
from fecfiler.reports.views import ReportViewMixin
from rest_framework.viewsets import ModelViewSet


class MemoTextViewSet(CommitteeOwnedViewMixin, ReportViewMixin, ModelViewSet):
class MemoTextViewSet(ReportViewMixin, ModelViewSet):
def get_queryset(self):
memos = super().get_queryset()
query_params = self.request.query_params.keys()
Expand Down
5 changes: 2 additions & 3 deletions django-backend/fecfiler/reports/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from django.urls import path, include
from fecfiler.reports.form_99.views import Form99ViewSet
from rest_framework.routers import DefaultRouter
from fecfiler.routers import register_router
from .form_3x.views import Form3XViewSet
from .form_24.views import Form24ViewSet
from .form_1m.views import Form1MViewSet
from .views import ReportViewSet

# Create a router and register our viewsets with it.
router = DefaultRouter()
router = register_router()
router.register(r"form-3x", Form3XViewSet, basename="form-3x")
router.register(r"form-24", Form24ViewSet, basename="form-24")
router.register(r"form-99", Form99ViewSet, basename="form-99")
Expand Down
2 changes: 1 addition & 1 deletion django-backend/fecfiler/reports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def list(self, request, *args, **kwargs):
return super().list(request, args, kwargs)


class ReportViewMixin(GenericViewSet):
class ReportViewMixin(CommitteeOwnedViewMixin, GenericViewSet):
def get_queryset(self):
return filter_by_report(super().get_queryset(), self)

Expand Down
14 changes: 14 additions & 0 deletions django-backend/fecfiler/routers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from rest_framework.routers import DefaultRouter

# Global list to store all routers
_all_routers = []


def register_router():
router = DefaultRouter()
_all_routers.append(router)
return router


def get_all_routers():
return _all_routers
5 changes: 2 additions & 3 deletions django-backend/fecfiler/transactions/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from fecfiler.routers import register_router
from .views import TransactionViewSet

# Create a router and register our viewsets with it.
router = DefaultRouter()
router = register_router()
router.register(r"", TransactionViewSet, basename="transactions")

# The API URLs are now determined automatically by the router.
Expand Down
5 changes: 2 additions & 3 deletions django-backend/fecfiler/user/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from fecfiler.routers import register_router
from .views import UserViewSet

# Create a router and register our viewsets with it.
router = DefaultRouter()
router = register_router()
router.register(r"users", UserViewSet, basename="users")

# The API URLs are now determined automatically by the router.
Expand Down
5 changes: 2 additions & 3 deletions django-backend/fecfiler/web_services/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from fecfiler.routers import register_router
from .views import WebServicesViewSet
from .summary.views import SummaryViewSet

# Create a router and register our viewsets with it.
router = DefaultRouter()
router = register_router()
router.register(r"web-services", WebServicesViewSet, basename="web-services")
router.register(r"web-services/summary", SummaryViewSet, basename="summary")

Expand Down
4 changes: 2 additions & 2 deletions django-backend/fecfiler/web_services/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
from .models import DotFEC, UploadSubmission, WebPrintSubmission
from fecfiler.reports.models import Report, FORMS_TO_CALCULATE
from celery.result import AsyncResult

from fecfiler.committee_accounts.views import CommitteeOwnedViewMixin
import structlog

logger = structlog.get_logger(__name__)


class WebServicesViewSet(viewsets.ViewSet):
class WebServicesViewSet(CommitteeOwnedViewMixin, viewsets.ViewSet):
"""
A viewset that provides actions to start web service tasks and
retrieve thier statuses and results
Expand Down