Skip to content

Commit

Permalink
Add unit test for customizable filters; normalize settings
Browse files Browse the repository at this point in the history
  • Loading branch information
morlandi committed Apr 8, 2019
1 parent 056afd0 commit 15ccd16
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 28 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,37 @@ Below are some of the settings you may want to use. These should be defined in y
will be matched against the URL path.
[Check our wiki](https://github.com/soynatan/django-easy-audit/wiki/Settings#request-auditing)
for more details on how to use it.

* `DJANGO_EASY_AUDIT_CRUD_DIFFERENCE_CALLBACKS`

May point to a list of callables/string-paths-to-functions-classes in which the application code can determine
on a per CRUDEvent whether or not the application chooses to create the CRUDEvent or not. This is different
from the registered/unregistered settings (e.g. `DJANGO_EASY_AUDIT_UNREGISTERED_CLASSES_EXTRA`).
from the registered/unregistered settings (e.g. `DJANGO_EASY_AUDIT_UNREGISTERED_CLASSES_EXTRA`).
This is meant to be for dynamic configurations where the application
may inspect the current save/create/delete and choose whether or not to save that into the database or ignore it.

* `DJANGO_EASY_AUDIT_USER_DB_CONSTRAINT`

Default is `True`. This is reserved for future use (does not do anything yet). The functionality provided by the
setting (whether enabled or disabled) could be handled more explicitly in certain
Default is `True`. This is reserved for future use (does not do anything yet). The functionality provided by the
setting (whether enabled or disabled) could be handled more explicitly in certain
code paths (or even internally as custom model managers). For projects that separate the easyaudit database, such
that the tables are not on the same database as the user table, this could help with making certain queries easier.
Again, this doesn't do anything yet, and if it ever does, the version will be increased and the README will be
updated accordingly. If you keep your database together (the standard usage), you have nothing to worry about.

* `DJANGO_EASY_AUDIT_CRUD_EVENT_LIST_FILTER`

* `DJANGO_EASY_AUDIT_LOGIN_EVENT_LIST_FILTER`

* `DJANGO_EASY_AUDIT_REQUEST_EVENT_LIST_FILTER`

Changeview filters configuration.
Used to remove filters when the corresponding list of data would be too long.
Defaults are:
- ['event_type', 'content_type', 'user', 'datetime', ] for CRUDEventAdmin
- ['login_type', 'user', 'datetime', ] for LoginEventAdmin
- ['method', 'user', 'datetime', ] for RequestEventAdmin

## What does it do

Django Easy Audit uses [Django signals](https://docs.djangoproject.com/en/dev/topics/signals/)
Expand Down
13 changes: 0 additions & 13 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,3 @@ Quick start
3. Run 'python manage.py migrate easyaudit' to create the audit models.

4. That's it! Now every CRUD event on your whole project will be registered in the audit models, which you will be able to query from the Django admin app. Additionally, this app will also log all authentication events and all URLs requested.


App settings
------------

EASY_AUDIT_CRUD_EVENT_LIST_FILTER
list_filter for CRUDEventAdmin

EASY_AUDIT_LOGIN_EVENT_LIST_FILTER
list_filter for LoginEventAdmin

EASY_AUDIT_REQUEST_EVENT_LIST_FILTER
list_filter for RequestEventAdmin
4 changes: 1 addition & 3 deletions easyaudit/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
from . import settings
from .models import CRUDEvent, LoginEvent, RequestEvent
from .admin_helpers import prettify_json, EasyAuditModelAdmin
from .settings import CRUD_EVENT_LIST_FILTER, LOGIN_EVENT_LIST_FILTER, REQUEST_EVENT_LIST_FILTER

from .app_settings import CRUD_EVENT_LIST_FILTER
from .app_settings import LOGIN_EVENT_LIST_FILTER
from .app_settings import REQUEST_EVENT_LIST_FILTER

# CRUD events
class CRUDEventAdmin(EasyAuditModelAdmin):
Expand Down
6 changes: 0 additions & 6 deletions easyaudit/app_settings.py

This file was deleted.

5 changes: 5 additions & 0 deletions easyaudit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@ def get_model_list(class_list):
# model.objects.all().delete()
# which is however much costly when many rows are involved
TRUNCATE_TABLE_SQL_STATEMENT = getattr(settings, 'DJANGO_EASY_AUDIT_TRUNCATE_TABLE_SQL_STATEMENT', '')

# Changeview filters configuration
CRUD_EVENT_LIST_FILTER = getattr(settings, 'DJANGO_EASY_AUDIT_CRUD_EVENT_LIST_FILTER', ['event_type', 'content_type', 'user', 'datetime', ])
LOGIN_EVENT_LIST_FILTER = getattr(settings, 'DJANGO_EASY_AUDIT_LOGIN_EVENT_LIST_FILTER', ['login_type', 'user', 'datetime', ])
REQUEST_EVENT_LIST_FILTER = getattr(settings, 'DJANGO_EASY_AUDIT_REQUEST_EVENT_LIST_FILTER', ['method', 'user', 'datetime', ])
3 changes: 2 additions & 1 deletion easyaudit/tests/test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class TestModel(models.Model):

class TestForeignKey(models.Model):
name = models.CharField(max_length=50)
test_fk = models.ForeignKey(TestModel)
test_fk = models.ForeignKey(TestModel, on_delete=models.CASCADE)


class TestM2M(models.Model):
name = models.CharField(max_length=50)
Expand Down
44 changes: 44 additions & 0 deletions easyaudit/tests/test_app/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import json
import re
from django.test import TestCase
try: # Django 2.0
from django.urls import reverse
Expand All @@ -14,6 +15,9 @@

TEST_USER_EMAIL = 'joe@example.com'
TEST_USER_PASSWORD = 'password'
TEST_ADMIN_EMAIL = 'admin@example.com'
TEST_ADMIN_PASSWORD = 'password'


class TestAuditModels(TestCase):

Expand Down Expand Up @@ -93,3 +97,43 @@ def test_manual_set_user(self):
self.assertEqual(crud_event_qs.count(), 1)
crud_event = crud_event_qs[0]
self.assertEqual(crud_event.user, None)


class TestAuditAdmin(TestCase):

def _setup_superuser(self, email, password):
admin = User.objects.create_superuser(email, email, TEST_ADMIN_PASSWORD)
admin.save()
return admin

def _log_in_user(self, email, password):
login = self.client.login(username=email, password=password)
self.assertTrue(login)

def _list_filters(self, content):
"""
Extract filters from response content;
example:
<div id="changelist-filter">
<h2>Filter</h2>
<h3> By method </h3>
...
<h3> By datetime </h3>
...
</div>
returns:
['method', 'datetime', ]
"""
html = re.search('<div\s*id="changelist-filter">(.*?)</div>', str(content)).group(0)
filters = re.findall('<h3>\s*By\s*(.*?)\s*</h3>', html)
return filters

def test_request_event_admin_no_users(self):
self._setup_superuser(TEST_ADMIN_EMAIL, TEST_ADMIN_PASSWORD)
self._log_in_user(TEST_ADMIN_EMAIL, TEST_ADMIN_PASSWORD)
response = self.client.get(reverse('admin:easyaudit_requestevent_changelist'))
self.assertEqual(200, response.status_code)
filters = self._list_filters(response.content)
print(filters)
1 change: 1 addition & 0 deletions easyaudit/tests/test_app/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.conf.urls import url
from test_app import views

app_name = 'test_easyaudit'

urlpatterns = [
url("create-obj", views.create_obj_view, name="create-obj"),
Expand Down
2 changes: 2 additions & 0 deletions easyaudit/tests/test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

DJANGO_EASY_AUDIT_REQUEST_EVENT_LIST_FILTER = ['method', 'datetime', ]

0 comments on commit 15ccd16

Please sign in to comment.