Skip to content

Commit

Permalink
Test on travis
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolas committed Jul 12, 2019
1 parent f1bee75 commit fc878dd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 68 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: python
dist: xenial
python:
- '2.7'
- '3.6'
env:
- DJANGO="Django>=1.11,<1.12.0"
install:
- pip install -q $DJANGO
script:
- make test
141 changes: 75 additions & 66 deletions audit_log/tests/audit_log_tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
EmployeeCreateView, EmployeeUpdateView)
from django.test.client import Client

from django.conf.urls import patterns, include, url
from django.conf.urls import url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
urlpatterns = [
'',
url(r'^/$', index),
url(r'^rate/(\d)/$', rate_product),
url(r'^category/create/$', CategoryCreateView.as_view()),
Expand All @@ -27,7 +28,7 @@
url(r'^property/update/(?P<pk>\d+)/$', PropertyUpdateView.as_view()),
url(r'^employee/create/$', EmployeeCreateView.as_view()),
url(r'^employee/update/(?P<pk>\d+)/$', EmployeeUpdateView.as_view()),
)
]



Expand Down Expand Up @@ -82,26 +83,26 @@ def setUp(self):
def test_logging_user(self):
_setup_admin()
product = Product.objects.get(pk = 1)
self.assertEqual(product.productrating_set.all().count(), 0)
c = Client()
self.assertEqual(product.productrating_set.count(), 0)
c = self.client
c.login(username = "[email protected]", password = "admin")
c.post('/rate/1/', {'rating': 4})
self.assertEqual(product.productrating_set.all().count(), 1)
self.assertEqual(product.productrating_set.all()[0].user.username, "[email protected]")
self.assertEqual(product.productrating_set.count(), 1)
self.assertEqual(product.productrating_set.first().user.username, "[email protected]")

def test_logging_session(self):
_setup_admin()
product = Product.objects.get(pk = 1)
self.assertEqual(product.productrating_set.all().count(), 0)
c = Client()
self.assertEqual(product.productrating_set.count(), 0)
c = self.client
c.login(username = "[email protected]", password = "admin")
c.get('/rate/1/',)
key = c.session.session_key
resp = c.post('/rate/1/', {'rating': 4})
self.assertEqual(resp.status_code, 200)
self.assertEqual(product.productrating_set.all().count(), 1)
self.assertIsNotNone(product.productrating_set.all()[0].session)
self.assertEqual(product.productrating_set.all()[0].session, key)
self.assertEqual(product.productrating_set.count(), 1)
self.assertIsNotNone(product.productrating_set.first().session)
self.assertEqual(product.productrating_set.first().session, key)

def test_logging_anon_session(self):
pass
Expand All @@ -110,11 +111,11 @@ def test_logging_anon_session(self):

def test_logging_user_none(self):
product = Product.objects.get(pk = 1)
self.assertEqual(product.productrating_set.all().count(), 0)
c = Client()
self.assertEqual(product.productrating_set.count(), 0)
c = self.client
c.post('/rate/1/', {'rating': 4})
self.assertEqual(product.productrating_set.all().count(), 1)
self.assertEqual(product.productrating_set.all()[0].user, None)
self.assertEqual(product.productrating_set.count(), 1)
self.assertEqual(product.productrating_set.first().user, None)


class TrackingChangesTest(TestCase):
Expand All @@ -128,82 +129,83 @@ def run_client(self, client):


def test_logging_insert_anon(self):
c = Client()
c = self.client
self.run_client(c)
category = ProductCategory.objects.get(name = 'Test Category')
self.assertEqual(category.audit_log.all()[0].name, category.name)
self.assertEqual(category.audit_log.all()[0].description, category.description)
self.assertEqual(category.audit_log.all()[0].action_type, "I")
self.assertEqual(category.audit_log.all()[0].action_user, None)
self.assertEqual(category.audit_log.first().name, category.name)
self.assertEqual(category.audit_log.first().description, category.description)
self.assertEqual(category.audit_log.first().action_type, "I")
self.assertEqual(category.audit_log.first().action_user, None)


def test_logging_insert_auth(self):
_setup_admin()
c = Client()
c = self.client
c.login(username = "[email protected]", password = "admin")
self.run_client(c)
category = ProductCategory.objects.get(name = 'Test Category 2')
self.assertEqual(category.audit_log.all()[0].name, category.name)
self.assertEqual(category.audit_log.all()[0].description, category.description)
self.assertEqual(category.audit_log.all()[0].action_type, "I")
self.assertNotEqual(category.audit_log.all()[0].action_user, None)
self.assertEqual(category.audit_log.all()[0].action_user.username, '[email protected]')
self.assertEqual(category.audit_log.first().name, category.name)
self.assertEqual(category.audit_log.first().description, category.description)
self.assertEqual(category.audit_log.first().action_type, "I")
self.assertNotEqual(category.audit_log.first().action_user, None)
self.assertEqual(category.audit_log.first().action_user.username, '[email protected]')

def test_loggin_update_anon(self):
c = Client()
c = self.client
self.run_client(c)
product= Product.objects.get(name = 'Test Product')
self.assertGreater(product.audit_log.all()[0].action_date, product.audit_log.all()[1].action_date)
self.assertEqual(Product.objects.count(), 1)
product = Product.objects.get(name='Test Product')
self.assertGreater(product.audit_log.first().action_date, product.audit_log.all()[1].action_date)
self.assertEqual(product.audit_log.all()[1].action_type, 'I')
self.assertEqual(product.audit_log.all()[0].action_type, 'U')
self.assertEqual(product.audit_log.all()[0].description, 'Test description new')
self.assertEqual(product.audit_log.all()[0].price, 5.00)
self.assertEqual(product.audit_log.all()[0].action_user, None)
self.assertEqual(product.audit_log.first().action_type, 'U')
self.assertEqual(product.audit_log.first().description, 'Test description new')
self.assertEqual(product.audit_log.first().price, 5.00)
self.assertEqual(product.audit_log.first().action_user, None)

def test_loging_update_auth(self):
_setup_admin()
c = Client()
c = self.client
c.login(username = '[email protected]', password = 'admin')
self.run_client(c)
product= Product.objects.get(name = 'Test Product')
self.assertNotEqual(product.audit_log.all()[0].action_user, None)
self.assertEqual(product.audit_log.all()[0].action_user.username, '[email protected]')
self.assertNotEqual(product.audit_log.first().action_user, None)
self.assertEqual(product.audit_log.first().action_user.username, '[email protected]')

def test_logging_delete_anon(self):
c = Client()
c = self.client
self.run_client(c)
c.post('/product/delete/1/')
self.assertEqual(Product.objects.all().count(), 0)
self.assertEqual(Product.audit_log.all()[0].action_type, 'D')
self.assertEqual(Product.audit_log.all()[0].name, 'Test Product')
self.assertEqual(Product.audit_log.all()[0].action_user, None)
self.assertEqual(Product.objects.count(), 0)
self.assertEqual(Product.audit_log.first().action_type, 'D')
self.assertEqual(Product.audit_log.first().name, 'Test Product')
self.assertEqual(Product.audit_log.first().action_user, None)

def test_logging_delete_auth(self):
_setup_admin()
c = Client()
c = self.client
c.login(username = '[email protected]', password = 'admin')
self.run_client(c)
self.assertEqual(Product.objects.all().count(), 1)
self.assertEqual(Product.objects.count(), 1)
c.post('/product/delete/1/')
self.assertEqual(Product.objects.all().count(), 0)
self.assertEqual(Product.audit_log.all()[0].action_type, 'D')
self.assertEqual(Product.audit_log.all()[0].name, 'Test Product')
self.assertNotEqual(Product.audit_log.all()[0].action_user, None)
self.assertEqual(Product.audit_log.all()[0].action_user.username, '[email protected]')
self.assertEqual(Product.objects.count(), 0)
self.assertEqual(Product.audit_log.first().action_type, 'D')
self.assertEqual(Product.audit_log.first().name, 'Test Product')
self.assertNotEqual(Product.audit_log.first().action_user, None)
self.assertEqual(Product.audit_log.first().action_user.username, '[email protected]')

def test_logging_inherited(self):
_setup_admin()
c = Client()
c = self.client
c.login(username = "[email protected]", password = "admin")
c.post('/extremewidget/create/', {'name': 'Test name', 'special_power': 'Testpower'})
widget = ExtremeWidget.objects.all()[0]
self.failUnlessEqual(widget.audit_log.all()[0].name, 'Test name')
self.failUnlessEqual(hasattr(widget.audit_log.all()[0], 'special_power'), True)
self.failUnlessEqual(widget.audit_log.all()[0].special_power, "Testpower")
widget = ExtremeWidget.objects.first()
self.failUnlessEqual(widget.audit_log.first().name, 'Test name')
self.failUnlessEqual(hasattr(widget.audit_log.first(), 'special_power'), True)
self.failUnlessEqual(widget.audit_log.first().special_power, "Testpower")

def test_logging_custom_user(self):
_setup_admin()
c = Client()
c = self.client
c.login(username = "[email protected]", password = "admin")
c.post('/employee/create/', {'email': '[email protected]', 'password': 'testpass'})

Expand All @@ -218,20 +220,27 @@ def run_client(self, client):
client.post('/property/update/1/', {'name': 'Property2', 'owned_by': '2'})

def test_fields(self):
c = Client()
c = self.client
self.run_client(c)
owner = PropertyOwner.objects.get(pk = 1)
prop = Property.objects.get(pk = 1)
self.assertEqual(prop.audit_log.all()[0]._meta.get_field('owned_by').__class__, models.ForeignKey)
self.assertEqual(PropertyOwner.objects.count(), 2)
self.assertEqual(Property.objects.count(), 1)

owner = PropertyOwner.objects.first()
new_owner = PropertyOwner.objects.all()[1]
prop = Property.objects.first()
self.assertEqual(prop.owned_by, new_owner.pk)

def test_logging(self):
c = Client()
c = self.client
self.run_client(c)
owner1 = PropertyOwner.objects.get(pk = 1)
owner2 = PropertyOwner.objects.get(pk = 2)
prop = Property.objects.get(pk = 1)
self.assertEqual(prop.audit_log.all().count(), 2)
self.assertEqual(prop.audit_log.all()[0].action_type, 'U')
self.assertEqual(PropertyOwner.objects.count(), 2)
self.assertEqual(Property.objects.count(), 1)

owner1 = PropertyOwner.objects.first()
owner2 = PropertyOwner.objects.all()[1]
prop = Property.objects.first()
self.assertEqual(prop.audit_log.count(), 2)
self.assertEqual(prop.audit_log.first().action_type, 'U')
self.assertEqual(prop.audit_log.all()[1].action_type, 'I')
self.assertEqual(prop.audit_log.all()[0].owned_by, owner2)
self.assertEqual(prop.audit_log.first().owned_by, owner2)
self.assertEqual(prop.audit_log.all()[1].owned_by, owner1)
5 changes: 3 additions & 2 deletions audit_log/tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.abspath(os.path.join(BASE_DIR, '..')))
sys.path.append(os.path.abspath(os.path.join(BASE_DIR, '../..')))

# Unfortunately, apps can not be installed via ``modify_settings``
# decorator, because it would miss the database setup.
Expand All @@ -24,7 +25,7 @@
'django.contrib.staticfiles',
)

ALWAYS_MIDDLEWARE_CLASSES = (
ALWAYS_MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand All @@ -41,7 +42,7 @@
TEMPLATE_DEBUG=True,
ALLOWED_HOSTS=[],
INSTALLED_APPS=ALWAYS_INSTALLED_APPS + CUSTOM_INSTALLED_APPS,
MIDDLEWARE_CLASSES=ALWAYS_MIDDLEWARE_CLASSES,
MIDDLEWARE=ALWAYS_MIDDLEWARE,
ROOT_URLCONF='tests.urls',
DATABASES={
'default': {
Expand Down

0 comments on commit fc878dd

Please sign in to comment.