from django.test import RequestFactory, TestCase, override_settings

from gem.context_processors import (
    compress_settings,
    detect_bbm,
    detect_freebasics,
)


class TestDetectBbm(TestCase):
    def setUp(self):
        self.request_factory = RequestFactory()

    def test_returns_false_for_requests_without_bbm_cookie(self):
        request = self.request_factory.get('/')
        self.assertEqual(detect_bbm(request), {'is_via_bbm': False})

    def test_returns_true_for_requests_with_bbm_host(self):
        request = self.request_factory.get('/', HTTP_HOST='bbm.localhost:80')
        self.assertEqual(detect_bbm(request), {'is_via_bbm': True})

    def test_returns_true_for_requests_with_bbm_cookie(self):
        request = self.request_factory.get('/')
        request.COOKIES['bbm'] = 'true'
        self.assertEqual(detect_bbm(request), {'is_via_bbm': True})


class TestDetectFreebasics(TestCase):
    def setUp(self):
        self.request_factory = RequestFactory()

    def test_returns_false_by_default(self):
        request = self.request_factory.get('/')
        self.assertEqual(
            detect_freebasics(request),
            {'is_via_freebasics': False},
        )

    def test_returns_true_if_internetorg_in_httpvia(self):
        request = self.request_factory.get('/', HTTP_VIA='Internet.org')
        self.assertEqual(
            detect_freebasics(request),
            {'is_via_freebasics': True},
        )

    def test_returns_true_if_internetorgapp_in_user_agent(self):
        request = self.request_factory.get(
            '/',
            HTTP_USER_AGENT='InternetOrgApp',
        )
        self.assertEqual(
            detect_freebasics(request),
            {'is_via_freebasics': True},
        )

    def test_returns_true_if_true_in_xiorgsfbs(self):
        request = self.request_factory.get('/', HTTP_X_IORG_FBS='true')
        self.assertEqual(
            detect_freebasics(request),
            {'is_via_freebasics': True},
        )


class TestCompressSettings(TestCase):
    @override_settings(ENV='test_env', STATIC_URL='test_static_url')
    def test_returns_settings(self):
        request = RequestFactory().get('/')
        self.assertEqual(
            compress_settings(request),
            {
                'LOGIN_URL': 'molo.profiles:auth_login',
                'VIEW_PROFILE_URL': u'/profiles/view/myprofile/',
                'EDIT_PROFILE_URL': u'/profiles/edit/myprofile/',
                'REGISTRATION_URL': u'/profiles/register/',
                'LOGOUT_URL': 'molo.profiles:auth_logout',
                'ENV': 'test_env',
                'STATIC_URL': 'test_static_url',
            }
        )

@@ -163,16 +163,96 @@
 sics,%0A)%0A
+from gem.models import OIDCSettings%0Afrom gem.tests.base import GemTestCaseMixin%0A
 %0A%0Aclass 
@@ -2103,26 +2103,178 @@
 ngs(TestCase
-):
+, GemTestCaseMixin):%0A    def setUp(self):%0A        self.main = self.mk_main(%0A            title='main1', slug='main1', path='00010002', url_path='/main1/')%0A
 %0A    @overri
@@ -2898,16 +2898,16 @@
       %7D%0A
-
         
@@ -2900,16 +2900,1514 @@
     %7D%0A        )%0A
+%0A    @override_settings(ENV='test_env', STATIC_URL='test_static_url',%0A                       USE_OIDC_AUTHENTICATION='True')%0A    def test_returns_oidc_settings(self):%0A        request = RequestFactory().get('/')%0A        site = self.main.get_site()%0A        site.oidcsettings = OIDCSettings.objects.create(%0A            oidc_rp_client_id='client_id',%0A            oidc_rp_client_secret='client_secret', oidc_rp_scopes='scopes',%0A            wagtail_redirect_url='http://example.url', site_id=site.id)%0A        request.site = site%0A%0A        settings = compress_settings(request)%0A        self.assertEqual(settings%5B'LOGIN_URL'%5D, 'molo.profiles:auth_login')%0A        self.assertEqual(%0A            settings%5B'VIEW_PROFILE_URL'%5D,%0A            u'/profile/edit/?theme=springster&redirect_uri='%0A            'http://example.url&client_id=client_id&language=en')%0A        self.assertEqual(settings%5B'EDIT_PROFILE_URL'%5D,%0A                         u'/profile/edit/?theme=springster&redirect_uri='%0A                         'http://example.url&client_id=client_id&language=en')%0A        self.assertEqual(%0A            settings%5B'REGISTRATION_URL'%5D,%0A            u'/registration/?theme=springster&hide=end-user&redirect_uri='%0A            'http://main1-1.localhost/oidc/authenticate/&client_id=client_id&'%0A            'language=en')%0A        self.assertEqual(settings%5B'LOGOUT_URL'%5D, 'molo.profiles:auth_logout')%0A        self.assertEqual(settings%5B'ENV'%5D, 'test_env')%0A        self.assertEqual(settings%5B'STATIC_URL'%5D, 'test_static_url')%0A

Test the oidc settings are added by the context processor