def test_user_request(self): request = self.factory.get('/', HTTP_USER_AGENT=self.user_agent) # anon TrackMiddleware.process_request(request) # authenticated request.user = self.user TrackMiddleware.process_request(request) self.assertNotEqual(cache.get(self.key), None) active_user = UserSession.objects.first() self.assertEqual(active_user.ip, '127.0.0.1') if parse: self.assertEqual(active_user.user_agent.ua_string, self.user_agent) else: self.assertEqual(active_user.user_agent, self.user_agent) self.assertEqual(active_user.city(), 'unknown') self.assertEqual(active_user.country(), 'unknown') self.assertEqual(UserSession.active_user_ids(), [1]) self.assertEqual(UserSession.active_users()[0], self.user) self.assertEqual(UserSession.active_user_count(), 1)
def test_user_request(self): request = self.factory.get("/", HTTP_USER_AGENT=self.user_agent) # anon TrackMiddleware.process_request(request) # authenticated request.user = self.user TrackMiddleware.process_request(request) self.assertNotEqual(cache.get(self.key), None) active_user = UserSession.objects.first() self.assertEqual(active_user.ip, "127.0.0.1") if parse: self.assertEqual(active_user.user_agent.ua_string, self.user_agent) else: self.assertEqual(active_user.user_agent, self.user_agent) self.assertEqual(active_user.city(), "unknown") self.assertEqual(active_user.country(), "unknown") self.assertEqual(UserSession.active_user_ids(), [1]) self.assertEqual(UserSession.active_users()[0], self.user) self.assertEqual(UserSession.active_user_count(), 1)
def get_queryset(self, request): """Only show users that have tracking info""" qs = super(UserSessionAdmin, self).get_queryset(request) return qs.filter(pk__in=UserSession.active_user_ids())