# Path: audit/models.py
# class PageviewRecord(models.Model):
# 
#     HTTP_METHODS = ['GET', 'POST', 'HEAD', 'PUT', 'PATCH', 'DELETE',
#                     'CONNECT', 'OPTIONS', 'TRACE']
# 
#     user = models.ForeignKey(User, blank=True, null=True)
#     user_ip = models.GenericIPAddressField()
#     role = models.ForeignKey(core_models.ProviderType,
#                              blank=True, null=True)
# 
#     method = models.CharField(
#         max_length=max(len(v) for v in HTTP_METHODS),
#         choices=[(v, v) for v in HTTP_METHODS])
# 
#     url = models.URLField(max_length=256)
#     referrer = models.URLField(max_length=256, blank=True, null=True)
# 
#     status_code = models.PositiveSmallIntegerField()
# 
#     timestamp = models.DateTimeField(auto_now_add=True)
# 
#     def __str__(self):
#         return '%s by %s to %s at %s' % (self.method, self.user, self.url,
#                                          self.timestamp)


from builtins import str
from django.test import TestCase, override_settings
from django.test import Client
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from pttrack.models import ProviderType
from pttrack.test_views import build_provider, log_in_provider
from .models import PageviewRecord
from .middleware import AuditMiddleware
        record = PageviewRecord.objects.first()
        self.assertEquals(record.user, expected_user)
        self.assertEquals(record.user_ip, USER_IP)
        self.assertEquals(record.role.short_name, 'Attending')
        self.assertEquals(record.method, 'GET')

    def test_audit_admin(self):
        p = log_in_provider(self.client, build_provider(["Coordinator"]))
        p.associated_user.is_staff = True
        p.associated_user.is_superuser = True
        p.associated_user.save()

        r = self.client.get(reverse('admin:audit_pageviewrecord_changelist'))
        self.assertEquals(r.status_code, 200)

    @override_settings(OSLER_AUDIT_BLACK_LIST=['0.0.0.1'])
    def test_create_on_view_if_USER_IP_is_not_in_BLACKLIST(self):

        provider = log_in_provider(self.client, build_provider(["Attending"]))
        expected_user = provider.associated_user

        # format is: {X-Forwarded-For: client, proxy1, proxy2}
        USER_IP = '0.0.0.0'
        self.client.get(
            reverse('home'),
            HTTP_X_FORWARDED_FOR=USER_IP + ',,')

        n_records = PageviewRecord.objects.count()
        self.assertEquals(n_records, 1)


# Path: audit/models.py
# class PageviewRecord(models.Model):
# 
#     HTTP_METHODS = ['GET', 'POST', 'HEAD', 'PUT', 'PATCH', 'DELETE',
#                     'CONNECT', 'OPTIONS', 'TRACE']
# 
#     user = models.ForeignKey(User, blank=True, null=True)
#     user_ip = models.GenericIPAddressField()
#     role = models.ForeignKey(core_models.ProviderType,
#                              blank=True, null=True)
# 
#     method = models.CharField(
#         max_length=max(len(v) for v in HTTP_METHODS),
#         choices=[(v, v) for v in HTTP_METHODS])
# 
#     url = models.URLField(max_length=256)
#     referrer = models.URLField(max_length=256, blank=True, null=True)
# 
#     status_code = models.PositiveSmallIntegerField()
# 
#     timestamp = models.DateTimeField(auto_now_add=True)
# 
#     def __str__(self):
#         return '%s by %s to %s at %s' % (self.method, self.user, self.url,
#                                          self.timestamp)


# Path: audit/tests.py
from builtins import str
from django.test import TestCase, override_settings
from django.test import Client
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from pttrack.models import ProviderType
from pttrack.test_views import build_provider, log_in_provider
from .models import PageviewRecord
from .middleware import AuditMiddleware

        record = PageviewRecord.objects.first()
        self.assertEquals(record.user, expected_user)
        self.assertEquals(record.user_ip, USER_IP)
        self.assertEquals(record.role.short_name, 'Attending')
        self.assertEquals(record.method, 'GET')

    def test_audit_admin(self):
        p = log_in_provider(self.client, build_provider(["Coordinator"]))
        p.associated_user.is_staff = True
        p.associated_user.is_superuser = True
        p.associated_user.save()

        r = self.client.get(reverse('admin:audit_pageviewrecord_changelist'))
        self.assertEquals(r.status_code, 200)

    @override_settings(OSLER_AUDIT_BLACK_LIST=['0.0.0.1'])
    def test_create_on_view_if_USER_IP_is_not_in_BLACKLIST(self):

        provider = log_in_provider(self.client, build_provider(["Attending"]))
        expected_user = provider.associated_user

        # format is: {X-Forwarded-For: client, proxy1, proxy2}
        USER_IP = '0.0.0.0'
        self.client.get(
            reverse('home'),
            HTTP_X_FORWARDED_FOR=USER_IP + ',,')

        n_records = PageviewRecord.objects.count()
        self.assertEquals(n_records, 1)


        record = PageviewRecord.objects.first()