2011-01-30 01:50:49 +00:00
|
|
|
"""
|
|
|
|
|
Tests for the analytical.utils module.
|
|
|
|
|
"""
|
2016-05-18 12:51:52 +00:00
|
|
|
# import django
|
2011-01-30 09:06:01 +00:00
|
|
|
|
2021-02-20 12:41:17 +00:00
|
|
|
import pytest
|
2016-05-18 12:51:52 +00:00
|
|
|
from django.contrib.auth.models import AbstractBaseUser
|
2015-12-20 17:40:08 +00:00
|
|
|
from django.db import models
|
2011-01-30 09:06:01 +00:00
|
|
|
from django.http import HttpRequest
|
|
|
|
|
from django.template import Context
|
2014-09-05 19:26:08 +00:00
|
|
|
from django.test.utils import override_settings
|
2021-02-20 12:41:17 +00:00
|
|
|
from utils import TestCase
|
2011-01-30 09:06:01 +00:00
|
|
|
|
2011-09-12 16:25:46 +00:00
|
|
|
from analytical.utils import (
|
2016-05-18 12:51:52 +00:00
|
|
|
AnalyticalException,
|
|
|
|
|
get_domain,
|
|
|
|
|
get_identity,
|
|
|
|
|
get_required_setting,
|
|
|
|
|
is_internal_ip,
|
|
|
|
|
)
|
2020-12-09 21:14:39 +00:00
|
|
|
|
2011-09-12 16:25:46 +00:00
|
|
|
|
|
|
|
|
class SettingDeletedTestCase(TestCase):
|
2014-09-05 19:26:08 +00:00
|
|
|
@override_settings(USER_ID=None)
|
2011-09-12 16:25:46 +00:00
|
|
|
def test_get_required_setting(self):
|
|
|
|
|
"""
|
|
|
|
|
Make sure using get_required_setting fails in the right place.
|
|
|
|
|
"""
|
2015-04-18 04:42:54 +00:00
|
|
|
|
2025-04-04 11:25:00 +00:00
|
|
|
with pytest.raises(AnalyticalException, match='USER_ID setting is not set'):
|
|
|
|
|
get_required_setting('USER_ID', r'\d+', 'invalid USER_ID')
|
2011-01-30 09:06:01 +00:00
|
|
|
|
2015-09-08 20:35:06 +00:00
|
|
|
|
2015-12-20 17:40:08 +00:00
|
|
|
class MyUser(AbstractBaseUser):
|
2016-01-06 14:22:38 +00:00
|
|
|
identity = models.CharField(max_length=50)
|
2015-12-20 17:40:08 +00:00
|
|
|
USERNAME_FIELD = 'identity'
|
|
|
|
|
|
2019-04-11 23:45:45 +00:00
|
|
|
class Meta:
|
2021-09-27 18:36:13 +00:00
|
|
|
abstract = False
|
2019-04-11 23:45:45 +00:00
|
|
|
app_label = 'testapp'
|
|
|
|
|
|
2015-12-20 17:40:08 +00:00
|
|
|
|
|
|
|
|
class GetIdentityTestCase(TestCase):
|
|
|
|
|
def test_custom_username_field(self):
|
|
|
|
|
get_id = get_identity(Context({}), user=MyUser(identity='fake_id'))
|
2020-12-09 21:14:39 +00:00
|
|
|
assert get_id == 'fake_id'
|
2015-12-20 17:40:08 +00:00
|
|
|
|
2022-07-14 09:56:27 +00:00
|
|
|
def test_custom_identity_specific_provider(self):
|
2025-04-04 11:25:00 +00:00
|
|
|
get_id = get_identity(
|
|
|
|
|
Context(
|
|
|
|
|
{
|
|
|
|
|
'foo_provider_identity': 'bar',
|
|
|
|
|
'analytical_identity': 'baz',
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
prefix='foo_provider',
|
|
|
|
|
)
|
2022-07-14 09:56:27 +00:00
|
|
|
assert get_id == 'bar'
|
|
|
|
|
|
|
|
|
|
def test_custom_identity_general(self):
|
2025-04-04 11:25:00 +00:00
|
|
|
get_id = get_identity(
|
|
|
|
|
Context(
|
|
|
|
|
{
|
|
|
|
|
'analytical_identity': 'baz',
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
prefix='foo_provider',
|
|
|
|
|
)
|
2022-07-14 09:56:27 +00:00
|
|
|
assert get_id == 'baz'
|
|
|
|
|
|
2015-12-20 17:40:08 +00:00
|
|
|
|
2025-04-04 11:25:00 +00:00
|
|
|
@override_settings(ANALYTICAL_DOMAIN='example.org')
|
2011-09-17 21:13:25 +00:00
|
|
|
class GetDomainTestCase(TestCase):
|
|
|
|
|
def test_get_service_domain_from_context(self):
|
|
|
|
|
context = Context({'test_domain': 'example.com'})
|
2020-12-09 21:14:39 +00:00
|
|
|
assert get_domain(context, 'test') == 'example.com'
|
2011-09-17 21:13:25 +00:00
|
|
|
|
|
|
|
|
def test_get_analytical_domain_from_context(self):
|
|
|
|
|
context = Context({'analytical_domain': 'example.com'})
|
2020-12-09 21:14:39 +00:00
|
|
|
assert get_domain(context, 'test') == 'example.com'
|
2011-09-17 21:13:25 +00:00
|
|
|
|
2025-04-04 11:25:00 +00:00
|
|
|
@override_settings(TEST_DOMAIN='example.net')
|
2011-09-17 21:13:25 +00:00
|
|
|
def test_get_service_domain_from_settings(self):
|
|
|
|
|
context = Context()
|
2020-12-09 21:14:39 +00:00
|
|
|
assert get_domain(context, 'test') == 'example.net'
|
2011-09-17 21:13:25 +00:00
|
|
|
|
|
|
|
|
def test_get_analytical_domain_from_settings(self):
|
|
|
|
|
context = Context()
|
2020-12-09 21:14:39 +00:00
|
|
|
assert get_domain(context, 'test') == 'example.org'
|
2011-09-17 21:13:25 +00:00
|
|
|
|
|
|
|
|
|
2014-04-28 23:45:47 +00:00
|
|
|
# FIXME: enable Django apps dynamically and enable test again
|
2017-10-15 17:46:44 +00:00
|
|
|
# @with_apps('django.contrib.sites')
|
|
|
|
|
# @override_settings(TEST_DOMAIN=SETTING_DELETED, ANALYTICAL_DOMAIN=SETTING_DELETED)
|
|
|
|
|
# class GetDomainTestCaseWithSites(TestCase):
|
2014-04-28 23:45:47 +00:00
|
|
|
# def test_get_domain_from_site(self):
|
|
|
|
|
# site = Site.objects.create(domain="example.com", name="test")
|
|
|
|
|
# with override_settings(SITE_ID=site.id):
|
|
|
|
|
# context = Context()
|
|
|
|
|
# self.assertEqual(get_domain(context, 'test'), 'example.com')
|
2011-09-17 21:13:25 +00:00
|
|
|
|
|
|
|
|
|
2011-01-30 09:06:01 +00:00
|
|
|
class InternalIpTestCase(TestCase):
|
2011-07-05 05:12:58 +00:00
|
|
|
@override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
2011-01-30 09:06:01 +00:00
|
|
|
def test_render_no_internal_ip(self):
|
|
|
|
|
context = Context()
|
2020-12-09 21:14:39 +00:00
|
|
|
assert not is_internal_ip(context)
|
2011-01-30 09:06:01 +00:00
|
|
|
|
2015-12-17 21:48:24 +00:00
|
|
|
@override_settings(INTERNAL_IPS=['1.1.1.1'])
|
|
|
|
|
@override_settings(ANALYTICAL_INTERNAL_IPS=[])
|
|
|
|
|
def test_render_analytical_internal_ips_override_when_empty(self):
|
|
|
|
|
req = HttpRequest()
|
|
|
|
|
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
|
|
|
|
context = Context({'request': req})
|
2020-12-09 21:14:39 +00:00
|
|
|
assert not is_internal_ip(context)
|
2015-12-17 21:48:24 +00:00
|
|
|
|
2011-07-05 05:12:58 +00:00
|
|
|
@override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
2011-01-30 09:06:01 +00:00
|
|
|
def test_render_internal_ip(self):
|
|
|
|
|
req = HttpRequest()
|
|
|
|
|
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
|
|
|
|
context = Context({'request': req})
|
2020-12-09 21:14:39 +00:00
|
|
|
assert is_internal_ip(context)
|
2011-01-30 09:06:01 +00:00
|
|
|
|
2011-07-05 05:12:58 +00:00
|
|
|
@override_settings(TEST_INTERNAL_IPS=['1.1.1.1'])
|
2011-01-30 09:06:01 +00:00
|
|
|
def test_render_prefix_internal_ip(self):
|
|
|
|
|
req = HttpRequest()
|
|
|
|
|
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
|
|
|
|
context = Context({'request': req})
|
2020-12-09 21:14:39 +00:00
|
|
|
assert is_internal_ip(context, 'TEST')
|
2011-01-30 09:06:01 +00:00
|
|
|
|
2011-07-05 05:12:58 +00:00
|
|
|
@override_settings(INTERNAL_IPS=['1.1.1.1'])
|
2011-01-30 09:06:01 +00:00
|
|
|
def test_render_internal_ip_fallback(self):
|
|
|
|
|
req = HttpRequest()
|
|
|
|
|
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
|
|
|
|
context = Context({'request': req})
|
2020-12-09 21:14:39 +00:00
|
|
|
assert is_internal_ip(context)
|
2011-01-30 09:06:01 +00:00
|
|
|
|
2011-07-05 05:12:58 +00:00
|
|
|
@override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
2011-01-30 09:06:01 +00:00
|
|
|
def test_render_internal_ip_forwarded_for(self):
|
|
|
|
|
req = HttpRequest()
|
|
|
|
|
req.META['HTTP_X_FORWARDED_FOR'] = '1.1.1.1'
|
|
|
|
|
context = Context({'request': req})
|
2020-12-09 21:14:39 +00:00
|
|
|
assert is_internal_ip(context)
|
2011-01-30 09:06:01 +00:00
|
|
|
|
2011-07-05 05:12:58 +00:00
|
|
|
@override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
2011-01-30 09:06:01 +00:00
|
|
|
def test_render_different_internal_ip(self):
|
|
|
|
|
req = HttpRequest()
|
|
|
|
|
req.META['REMOTE_ADDR'] = '2.2.2.2'
|
|
|
|
|
context = Context({'request': req})
|
2020-12-09 21:14:39 +00:00
|
|
|
assert not is_internal_ip(context)
|