Skip to content

Commit

Permalink
v9.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronny Vedrilla committed Nov 3, 2023
1 parent 0ef6305 commit 1204e53
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ambient_toolbox/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AbstractUserSpecificManager(Manager, AbstractPermissionMixin):
"""
The UserSpecificQuerySet has a method 'as_manger', which can be used for creating a default manager,
which inherits all methods of the queryset and invokes the respective method of it's queryset, respectively.
If the manager has to be declared separately for some reasons, all queryset methods, have to be declared twice,
If the manager has to be declared separately for some reason, all queryset methods, have to be declared twice,
once in the QuerySet, once in the manager class.
For consistency reasons, both inherit from the same mixin, to ensure the equality of the method's names.
"""
Expand Down
2 changes: 2 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# Routing
ROOT_URLCONF = "testapp.urls"

STATIC_URL = "/static/"

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

DATABASES = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from unittest import mock

from django.contrib import admin
from django.contrib.admin import AdminSite, ModelAdmin
from django.contrib.auth.models import User
from django.test import TestCase
from django.core.handlers.wsgi import WSGIRequest
from django.test import RequestFactory, TestCase

from ambient_toolbox.admin.model_admins.classes import EditableOnlyAdmin, ReadOnlyAdmin
from ambient_toolbox.tests.mixins import RequestProviderMixin
Expand Down Expand Up @@ -69,3 +73,52 @@ def test_editable_only_admin_no_change_permissions(self):

self.assertFalse(admin_class.has_add_permission(request))
self.assertFalse(admin_class.has_delete_permission(request))


class ReadOnlyAdminTest(TestCase):
user: User
request: WSGIRequest

@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(username="testuser", password="testpassword", is_superuser=True)
cls.model_admin = ReadOnlyAdmin(User, AdminSite())

factory = RequestFactory()
cls.request = factory.get(f"/admin/auth/user/{cls.user.id}/change/")
cls.request.user = cls.user

def test_changeform_view_regular(self):
response = self.model_admin.changeform_view(self.request, str(self.user.id))
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, "Save and continue editing")
self.assertNotContains(response, "Save")

def test_has_add_permission_regular(self):
self.assertFalse(self.model_admin.has_add_permission(self.request))

def test_has_change_permission_regular(self):
self.assertFalse(self.model_admin.has_change_permission(self.request))

def test_has_delete_permission_regular(self):
self.assertFalse(self.model_admin.has_delete_permission(self.request))


class EditableOnlyAdminTest(TestCase):
user: User
request: WSGIRequest

@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(username="testuser", password="testpassword", is_superuser=True)
cls.model_admin = EditableOnlyAdmin(User, AdminSite())

factory = RequestFactory()
cls.request = factory.get(f"/admin/auth/user/{cls.user.id}/change/")
cls.request.user = cls.user

@mock.patch.object(ModelAdmin, "get_actions", return_value={"delete_selected": 1})
def test_get_actions_regular(self, *args):
actions = self.model_admin.get_actions(self.request)
self.assertIsInstance(actions, dict)
self.assertNotIn("delete_selected", actions)
7 changes: 6 additions & 1 deletion tests/test_admin_forms.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout
from django import forms
from django.test import TestCase

from ambient_toolbox.admin.views.forms import AdminCrispyForm


class TestForm(AdminCrispyForm):
my_field = forms.Field()


class AdminFormTest(TestCase):
def test_admin_crispy_form_regular(self):
# Form provides mostly styling, so we just validate that it renders
form = AdminCrispyForm()
form = TestForm()

self.assertIsInstance(form.helper, FormHelper)
self.assertIsInstance(form.helper.layout, Layout)
Expand Down
71 changes: 68 additions & 3 deletions tests/test_managers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
from django.contrib.auth.models import User
from django.test import TestCase

from ambient_toolbox.managers import (
AbstractUserSpecificManager,
AbstractUserSpecificQuerySet,
)
from testapp.models import MySingleSignalModel


class AbstractUserSpecificQuerySetTest(TestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()

cls.cqs = AbstractUserSpecificQuerySet()
cls.user = User.objects.create(username="my-username")

def test_default(self):
self.assertEqual(self.cqs.default(self.user), self.cqs)

def test_visible_for_regular(self):
with self.assertRaisesMessage(NotImplementedError, "Please implement this method"):
self.cqs.visible_for(self.user)

def test_editable_for_regular(self):
with self.assertRaisesMessage(NotImplementedError, "Please implement this method"):
self.cqs.editable_for(self.user)

def test_deletable_for_regular(self):
with self.assertRaisesMessage(NotImplementedError, "Please implement this method"):
self.cqs.deletable_for(self.user)


class TestUserSpecificManager(AbstractUserSpecificManager):
pass


TestUserSpecificManager = TestUserSpecificManager.from_queryset(AbstractUserSpecificQuerySet)


class AbstractUserSpecificManagerTest(TestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()

cls.manager = TestUserSpecificManager()
cls.user = User.objects.create(username="my-username")

def test_visible_for_regular(self):
with self.assertRaisesMessage(NotImplementedError, "Please implement this method"):
self.manager.visible_for(self.user)

def test_editable_for_regular(self):
with self.assertRaisesMessage(NotImplementedError, "Please implement this method"):
self.manager.editable_for(self.user)

def test_deletable_for_regular(self):
with self.assertRaisesMessage(NotImplementedError, "Please implement this method"):
self.manager.deletable_for(self.user)


class GloballyVisibleQuerySetTest(TestCase):
@classmethod
def setUpTestData(cls):
Expand All @@ -20,12 +76,21 @@ def setUpTestData(cls):

def test_visible_for_regular(self):
self.assertGreater(len(self.object_list), 0)
self.assertEqual(MySingleSignalModel.objects.visible_for(self.user).count(), len(self.object_list))
self.assertEqual(
MySingleSignalModel.objects.visible_for(self.user).count(),
len(self.object_list),
)

def test_editable_for_regular(self):
self.assertGreater(len(self.object_list), 0)
self.assertEqual(MySingleSignalModel.objects.editable_for(self.user).count(), len(self.object_list))
self.assertEqual(
MySingleSignalModel.objects.editable_for(self.user).count(),
len(self.object_list),
)

def test_deletable_for_regular(self):
self.assertGreater(len(self.object_list), 0)
self.assertEqual(MySingleSignalModel.objects.deletable_for(self.user).count(), len(self.object_list))
self.assertEqual(
MySingleSignalModel.objects.deletable_for(self.user).count(),
len(self.object_list),
)
57 changes: 57 additions & 0 deletions tests/tests/mixins/test_mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django.contrib.auth.models import AnonymousUser, User
from django.http import HttpResponse
from django.test import RequestFactory, TestCase
from django.views import generic

from ambient_toolbox.tests.mixins import ClassBasedViewTestMixin


class TestView(generic.TemplateView):
def get(self, request, *args, **kwargs):
return HttpResponse(status=202)

def post(self, request, *args, **kwargs):
return HttpResponse(status=203)

def delete(self, request, *args, **kwargs):
return HttpResponse(status=204)


class ClassBasedViewTestMixinTest(ClassBasedViewTestMixin, TestCase):
view_class = TestView

@classmethod
def setUpTestData(cls):
super().setUpTestData()

factory = RequestFactory()
cls.request = factory.get("/admin")
cls.user = User.objects.create(username="my-username")

def test_authentication_user_given(self):
self._authentication(self.request, self.user), self.user
self.assertEqual(self.request.user, self.user)

def test_authentication_no_user_given(self):
self._authentication(self.request, None), self.user
self.assertEqual(self.request.user, AnonymousUser())

def test_get_response_regular(self):
response = self._get_response(method="get", user=self.user, data={"my_data": 42})
self.assertIsInstance(response, HttpResponse)
self.assertEqual(response.status_code, 202)

def test_get_regular(self):
response = self.get()
self.assertIsInstance(response, HttpResponse)
self.assertEqual(response.status_code, 202)

def test_post_regular(self):
response = self.post()
self.assertIsInstance(response, HttpResponse)
self.assertEqual(response.status_code, 203)

def test_delete_regular(self):
response = self.delete()
self.assertIsInstance(response, HttpResponse)
self.assertEqual(response.status_code, 204)

0 comments on commit 1204e53

Please sign in to comment.