Skip to content

Commit

Permalink
Fixed resource_url to work with non-id models (jazzband#147)
Browse files Browse the repository at this point in the history
* Fixed resource_url to work with non-id models

* Add tests for auditlog admin
  • Loading branch information
delneg authored and audiolion committed Jan 4, 2018
1 parent 4dee034 commit 96f2f3d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/auditlog/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def resource_url(self, obj):
app_label, model = obj.content_type.app_label, obj.content_type.model
viewname = 'admin:%s_%s_change' % (app_label, model)
try:
link = urlresolvers.reverse(viewname, args=[obj.object_id])
args = [obj.object_pk] if obj.object_id is None else [obj.object_id]
link = urlresolvers.reverse(viewname, args=args)
except NoReverseMatch:
return obj.object_repr
else:
Expand Down
30 changes: 27 additions & 3 deletions src/auditlog_tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
"""
Settings file for the Auditlog test suite.
"""
import os
import django

SECRET_KEY = 'test'

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.admin',
'auditlog',
'auditlog_tests',
'multiselectfield',
]

MIDDLEWARE_CLASSES = (
middlewares = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware'
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'auditlog.middleware.AuditlogMiddleware',
)

if django.VERSION < (1, 10):
MIDDLEWARE_CLASSES = middlewares
else:
MIDDLEWARE = middlewares

if django.VERSION <= (1, 9):
POSTGRES_DRIVER = 'django.db.backends.postgresql_psycopg2'
else:
Expand All @@ -42,6 +52,20 @@
}
}

ROOT_URLCONF = []
TEMPLATES = [
{
'APP_DIRS': True,
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
]
},
},
]

ROOT_URLCONF = 'auditlog_tests.urls'

USE_TZ = True
28 changes: 28 additions & 0 deletions src/auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,31 @@ def test_is_authenticated(self):
else:
assert not self.user.is_anonymous
assert compat.is_authenticated(self.user)


class AdminPanelTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.username = "test_admin"
cls.password = User.objects.make_random_password()
cls.user, created = User.objects.get_or_create(username=cls.username)
cls.user.set_password(cls.password)
cls.user.is_staff = True
cls.user.is_superuser = True
cls.user.is_active = True
cls.user.save()
cls.obj = SimpleModel.objects.create(text='For admin logentry test')

def test_auditlog_admin(self):
self.client.login(username=self.username, password=self.password)
log_pk = self.obj.history.latest().pk
res = self.client.get("/admin/auditlog/logentry/")
assert res.status_code == 200
res = self.client.get("/admin/auditlog/logentry/add/")
assert res.status_code == 200
res = self.client.get("/admin/auditlog/logentry/{}/".format(log_pk), follow=True)
assert res.status_code == 200
res = self.client.get("/admin/auditlog/logentry/{}/delete/".format(log_pk))
assert res.status_code == 200
res = self.client.get("/admin/auditlog/logentry/{}/history/".format(log_pk))
assert res.status_code == 200
13 changes: 13 additions & 0 deletions src/auditlog_tests/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import django
from django.conf.urls import include, url
from django.contrib import admin


if django.VERSION < (1, 9):
admin_urls = include(admin.site.urls)
else:
admin_urls = admin.site.urls

urlpatterns = [
url(r'^admin/', admin_urls),
]

0 comments on commit 96f2f3d

Please sign in to comment.