# Path: yithlibraryserver/user/analytics.py
# class GoogleAnalytics(object):
# 
#     def __init__(self, request):
#         self.request = request
# 
#     @property
#     def enabled(self):
#         code = self.request.registry.settings['google_analytics_code']
#         return code is not None
# 
#     @property
#     def first_time(self):
#         if self.request.user is None:
#             return USER_ATTR not in self.request.session
#         else:
#             return USER_ATTR not in self.request.user
# 
#     def show_in_session(self):
#         return self.request.session.get(USER_ATTR, False)
# 
#     def show_in_user(self, user):
#         return user.get(USER_ATTR, False)
# 
#     def is_in_session(self):
#         return USER_ATTR in self.request.session
# 
#     def is_stored_in_user(self, user):
#         return USER_ATTR in user
# 
#     @property
#     def show(self):
#         user = self.request.user
#         if user is None:
#             return self.show_in_session()
#         else:
#             return self.show_in_user(user)
# 
#     def clean_session(self):
#         if USER_ATTR in self.request.session:
#             del self.request.session[USER_ATTR]
# 
#     def get_user_attr(self, value):
#         return {USER_ATTR: value}
# 
# Path: yithlibraryserver/user/analytics.py
# def get_google_analytics(request):
#     return GoogleAnalytics(request)
# 
# Path: yithlibraryserver/user/analytics.py
# USER_ATTR = 'allow_google_analytics'


import unittest
from pyramid.testing import DummyRequest
from yithlibraryserver.user.analytics import GoogleAnalytics
from yithlibraryserver.user.analytics import get_google_analytics
from yithlibraryserver.user.analytics import USER_ATTR
    def test_is_stored_in_user(self):
        request = DummyRequest()
        ga = GoogleAnalytics(request)
        self.assertFalse(ga.is_stored_in_user({}))

        self.assertTrue(ga.is_stored_in_user({USER_ATTR: True}))

        self.assertTrue(ga.is_stored_in_user({USER_ATTR: False}))

    def test_show(self):
        request = DummyRequest()
        request.session = {}
        request.user = None
        ga = GoogleAnalytics(request)
        self.assertFalse(ga.show)

        request = DummyRequest()
        request.session = {USER_ATTR: True}
        request.user = None
        ga = GoogleAnalytics(request)
        self.assertTrue(ga.show)

        request = DummyRequest()
        request.session = {}
        request.user = {USER_ATTR: True}
        ga = GoogleAnalytics(request)
        self.assertTrue(ga.show)

        request = DummyRequest()
        request.session = {USER_ATTR: True}

# Path: yithlibraryserver/user/analytics.py
# class GoogleAnalytics(object):
# 
#     def __init__(self, request):
#         self.request = request
# 
#     @property
#     def enabled(self):
#         code = self.request.registry.settings['google_analytics_code']
#         return code is not None
# 
#     @property
#     def first_time(self):
#         if self.request.user is None:
#             return USER_ATTR not in self.request.session
#         else:
#             return USER_ATTR not in self.request.user
# 
#     def show_in_session(self):
#         return self.request.session.get(USER_ATTR, False)
# 
#     def show_in_user(self, user):
#         return user.get(USER_ATTR, False)
# 
#     def is_in_session(self):
#         return USER_ATTR in self.request.session
# 
#     def is_stored_in_user(self, user):
#         return USER_ATTR in user
# 
#     @property
#     def show(self):
#         user = self.request.user
#         if user is None:
#             return self.show_in_session()
#         else:
#             return self.show_in_user(user)
# 
#     def clean_session(self):
#         if USER_ATTR in self.request.session:
#             del self.request.session[USER_ATTR]
# 
#     def get_user_attr(self, value):
#         return {USER_ATTR: value}
# 
# Path: yithlibraryserver/user/analytics.py
# def get_google_analytics(request):
#     return GoogleAnalytics(request)
# 
# Path: yithlibraryserver/user/analytics.py
# USER_ATTR = 'allow_google_analytics'


# Path: yithlibraryserver/user/tests/test_analytics.py
import unittest
from pyramid.testing import DummyRequest
from yithlibraryserver.user.analytics import GoogleAnalytics
from yithlibraryserver.user.analytics import get_google_analytics
from yithlibraryserver.user.analytics import USER_ATTR

    def test_is_stored_in_user(self):
        request = DummyRequest()
        ga = GoogleAnalytics(request)
        self.assertFalse(ga.is_stored_in_user({}))

        self.assertTrue(ga.is_stored_in_user({USER_ATTR: True}))

        self.assertTrue(ga.is_stored_in_user({USER_ATTR: False}))

    def test_show(self):
        request = DummyRequest()
        request.session = {}
        request.user = None
        ga = GoogleAnalytics(request)
        self.assertFalse(ga.show)

        request = DummyRequest()
        request.session = {USER_ATTR: True}
        request.user = None
        ga = GoogleAnalytics(request)
        self.assertTrue(ga.show)

        request = DummyRequest()
        request.session = {}
        request.user = {USER_ATTR: True}
        ga = GoogleAnalytics(request)
        self.assertTrue(ga.show)

        request = DummyRequest()
        request.session = {USER_ATTR: True}

        request.user = {USER_ATTR: False}