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

Remove settings and invoices internal endpoints #654

Merged
merged 4 commits into from
Jul 10, 2024
Merged
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
8 changes: 2 additions & 6 deletions api/internal/enterprise_urls.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from django.urls import include, path

from api.internal.self_hosted.views import SettingsViewSet, UserViewSet
from utils.routers import OptionalTrailingSlashRouter, RetrieveUpdateDestroyRouter
from api.internal.self_hosted.views import UserViewSet
from utils.routers import OptionalTrailingSlashRouter

self_hosted_router = OptionalTrailingSlashRouter()
self_hosted_router.register(r"users", UserViewSet, basename="selfhosted-users")

settings_router = RetrieveUpdateDestroyRouter()
settings_router.register(r"settings", SettingsViewSet, basename="selfhosted-settings")

urlpatterns = [
path("license/", include("api.internal.license.urls")),
path("", include(settings_router.urls)),
path("", include(self_hosted_router.urls)),
]
25 changes: 0 additions & 25 deletions api/internal/owner/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,6 @@ class OwnerViewSet(OwnerViewSetMixin, mixins.RetrieveModelMixin):
serializer_class = OwnerSerializer


class InvoiceViewSet(
viewsets.GenericViewSet,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
OwnerPropertyMixin,
):
serializer_class = StripeInvoiceSerializer
permission_classes = [MemberOfOrgPermissions]
pagination_class = None

def get_queryset(self):
return BillingService(
requesting_user=self.request.current_owner
).list_filtered_invoices(self.owner, 100)

def get_object(self):
invoice_id = self.kwargs.get("pk")
invoice = BillingService(
requesting_user=self.request.current_owner
).get_invoice(self.owner, invoice_id)
if not invoice:
raise NotFound(f"Invoice {invoice_id} does not exist for that account")
return invoice


class AccountDetailsViewSet(
viewsets.GenericViewSet,
mixins.UpdateModelMixin,
Expand Down
17 changes: 0 additions & 17 deletions api/internal/self_hosted/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,3 @@ def update(self, instance, validated_data):

# re-query for object to get updated `activated` value
return self.context["view"].get_queryset().filter(pk=instance.pk).first()


class SettingsSerializer(serializers.Serializer):
# this name is used to be consistent with org-level auto activation
plan_auto_activate = serializers.BooleanField()

seats_used = serializers.IntegerField()
seats_limit = serializers.IntegerField()

def update(self, instance, validated_data):
if "plan_auto_activate" in validated_data:
if validated_data["plan_auto_activate"] is True:
self_hosted.enable_autoactivation()
else:
self_hosted.disable_autoactivation()

return self.context["view"]._get_settings()
30 changes: 1 addition & 29 deletions api/internal/self_hosted/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .filters import UserFilters
from .permissions import AdminPermissions
from .serializers import SettingsSerializer, UserSerializer
from .serializers import UserSerializer


class UserViewSet(
Expand Down Expand Up @@ -63,31 +63,3 @@ def current_update(self, request):
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)


class SettingsViewSet(
viewsets.GenericViewSet,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
):
serializer_class = SettingsSerializer
permission_classes = [AdminPermissions]

def retrieve(self, request, *args, **kwargs):
serializer = self.get_serializer(self._get_settings())
return Response(serializer.data)

def update(self, request, *args, **kwargs):
serializer = self.get_serializer(
self._get_settings(), data=request.data, partial=True
)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)

def _get_settings(self):
return {
"plan_auto_activate": self_hosted.is_autoactivation_enabled(),
"seats_used": self_hosted.activated_owners().count(),
"seats_limit": self_hosted.license_seats(),
}
132 changes: 0 additions & 132 deletions api/internal/tests/views/test_invoice_viewset.py

This file was deleted.

61 changes: 0 additions & 61 deletions api/internal/tests/views/test_self_hosted_settings_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,64 +28,3 @@ def test_settings(self):
res = self.client.get(reverse("selfhosted-users-list"))
# not authenticated
assert res.status_code == 403


@override_settings(IS_ENTERPRISE=True, ROOT_URLCONF="api.internal.enterprise_urls")
class SettingsViewsetTestCase(TestCase):
def setUp(self):
self.current_owner = OwnerFactory()
self.client = APIClient()
self.client.force_login_owner(self.current_owner)

@patch("services.self_hosted.license_seats")
@patch("services.self_hosted.is_autoactivation_enabled")
@patch("services.self_hosted.admin_owners")
def test_settings(self, admin_owners, is_autoactivation_enabled, license_seats):
admin_owners.return_value = Owner.objects.filter(pk__in=[self.current_owner.pk])

is_autoactivation_enabled.return_value = False
license_seats.return_value = 5

res = self.client.get(reverse("selfhosted-settings-detail"))
assert res.status_code == 200
assert res.json() == {
"plan_auto_activate": False,
"seats_used": 0,
"seats_limit": 5,
}

is_autoactivation_enabled.return_value = True

org = OwnerFactory()
owner = OwnerFactory(organizations=[org.pk])
activate_owner(owner)

res = self.client.get(reverse("selfhosted-settings-detail"))
assert res.status_code == 200
assert res.json() == {
"plan_auto_activate": True,
"seats_used": 1,
"seats_limit": 5,
}

@patch("services.self_hosted.admin_owners")
def test_settings_update(self, admin_owners):
admin_owners.return_value = Owner.objects.filter(pk__in=[self.current_owner.pk])

res = self.client.patch(
reverse("selfhosted-settings-detail"),
data={"plan_auto_activate": True},
format="json",
)
assert res.status_code == 200
assert res.json()["plan_auto_activate"] == True
assert is_autoactivation_enabled() == True

res = self.client.patch(
reverse("selfhosted-settings-detail"),
data={"plan_auto_activate": False},
format="json",
)
assert res.status_code == 200
assert res.json()["plan_auto_activate"] == False
assert is_autoactivation_enabled() == False
2 changes: 0 additions & 2 deletions api/internal/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from api.internal.feature.views import FeaturesView
from api.internal.owner.views import (
AccountDetailsViewSet,
InvoiceViewSet,
OwnerViewSet,
UserViewSet,
)
Expand All @@ -29,7 +28,6 @@

owner_artifacts_router = OptionalTrailingSlashRouter()
owner_artifacts_router.register(r"users", UserViewSet, basename="users")
owner_artifacts_router.register(r"invoices", InvoiceViewSet, basename="invoices")

account_details_router = RetrieveUpdateDestroyRouter()
account_details_router.register(
Expand Down
Loading