# Path: open_connect/accounts/models.py
# class Visit(TimestampModel):
#     """Store information about a user's visits to the site."""
#     user = models.ForeignKey(User, blank=True, null=True)
#     ip_address = models.GenericIPAddressField(blank=True, null=True)
#     user_agent = models.TextField(blank=True)


from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test import TestCase, RequestFactory
from django.test.utils import override_settings
from mock import Mock
from model_mommy import mommy
from open_connect.middleware.visit_tracking import VisitTrackingMiddleware
from open_connect.accounts.models import Visit
from open_connect.connect_core.utils.basetests import ConnectTestMixin
        request1 = request_factory.get('/')
        response1 = Mock()
        self.assertFalse(hasattr(request1, 'user'))
        result1 = visit_tracking_mw.process_response(request1, response1)
        self.assertEqual(response1, result1)
        self.assertEqual(visit_count, Visit.objects.count())

        request2 = request_factory.get('/')
        response2 = Mock()
        request2.user = user
        self.assertTrue(hasattr(request2, 'user'))
        visit_tracking_mw.process_response(request2, response2)
        self.assertEqual(Visit.objects.count(), visit_count + 1)

    def test_unauthenticated_requests_not_logged(self):
        """Anonymous users shouldn't have their visits logged."""
        visit_count = Visit.objects.count()
        self.client.get('/')
        self.assertIsNone(self.client.cookies.get('visit_logged'))
        self.assertEqual(Visit.objects.count(), visit_count)

    def test_authenticated_requests_logged(self):
        """An authenticated user should have their visit logged."""
        visit_count = Visit.objects.count()
        User.objects.create_user(username='a@b.local', password='moo')
        self.client.post(
            reverse('account_login'),
            {'login': 'a@b.local', 'password': 'moo'})
        self.client.get('/')
        self.assertEqual(self.client.cookies.get('visit_logged').value, '1')

# Path: open_connect/accounts/models.py
# class Visit(TimestampModel):
#     """Store information about a user's visits to the site."""
#     user = models.ForeignKey(User, blank=True, null=True)
#     ip_address = models.GenericIPAddressField(blank=True, null=True)
#     user_agent = models.TextField(blank=True)


# Path: open_connect/middleware/tests/test_visit_tracking.py
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test import TestCase, RequestFactory
from django.test.utils import override_settings
from mock import Mock
from model_mommy import mommy
from open_connect.middleware.visit_tracking import VisitTrackingMiddleware
from open_connect.accounts.models import Visit
from open_connect.connect_core.utils.basetests import ConnectTestMixin

        request1 = request_factory.get('/')
        response1 = Mock()
        self.assertFalse(hasattr(request1, 'user'))
        result1 = visit_tracking_mw.process_response(request1, response1)
        self.assertEqual(response1, result1)
        self.assertEqual(visit_count, Visit.objects.count())

        request2 = request_factory.get('/')
        response2 = Mock()
        request2.user = user
        self.assertTrue(hasattr(request2, 'user'))
        visit_tracking_mw.process_response(request2, response2)
        self.assertEqual(Visit.objects.count(), visit_count + 1)

    def test_unauthenticated_requests_not_logged(self):
        """Anonymous users shouldn't have their visits logged."""
        visit_count = Visit.objects.count()
        self.client.get('/')
        self.assertIsNone(self.client.cookies.get('visit_logged'))
        self.assertEqual(Visit.objects.count(), visit_count)

    def test_authenticated_requests_logged(self):
        """An authenticated user should have their visit logged."""
        visit_count = Visit.objects.count()
        User.objects.create_user(username='a@b.local', password='moo')
        self.client.post(
            reverse('account_login'),
            {'login': 'a@b.local', 'password': 'moo'})
        self.client.get('/')
        self.assertEqual(self.client.cookies.get('visit_logged').value, '1')

        self.assertEqual(Visit.objects.count(), visit_count + 1)