def test_test_user_calls_get_country(rf): segment = SegmentFactory(name='Test segment') rule = OriginCountryRuleFactory(segment=segment, country='GB') request = rf.get('/') with patch.object(rule, 'get_country') as get_country_mock: rule.test_user(request) get_country_mock.assert_called_once_with(request)
def test_test_user_geoip_module_does_not_match(rf): segment = SegmentFactory(name='Test segment') rule = OriginCountryRuleFactory(segment=segment, country='nl') request = rf.get('/', REMOTE_ADDR='123.120.0.2') GeoIP2Mock = MagicMock() GeoIP2Mock().configure_mock(**{'country_code.return_value': 'SE'}) GeoIP2Mock.reset_mock() with patch('wagtail_personalisation.rules.get_geoip_module', return_value=GeoIP2Mock): assert rule.test_user(request) is False assert GeoIP2Mock.mock_calls == [ call(), call().country_code('123.120.0.2') ]
def test_test_user_does_not_use_geoip_module_if_disabled(rf): segment = SegmentFactory(name='Test segment') rule = OriginCountryRuleFactory(segment=segment, country='se') request = rf.get('/', REMOTE_ADDR='123.120.0.2') assert rule.test_user(request) is False
def test_test_user_returns_true_if_cloudfront_country_matches(rf): segment = SegmentFactory(name='Test segment') rule = OriginCountryRuleFactory(segment=segment, country='se') request = rf.get('/', HTTP_CLOUDFRONT_VIEWER_COUNTRY='SE') assert rule.test_user(request) is True
def test_test_user_returns_false_if_cloudfront_country_doesnt_match(rf): segment = SegmentFactory(name='Test segment') rule = OriginCountryRuleFactory(segment=segment, country='GB') request = rf.get('/', HTTP_CLOUDFRONT_VIEWER_COUNTRY='NL') assert rule.test_user(request) is False
def test_test_user_returns_false_if_cloudflare_country_doesnt_match(rf): segment = SegmentFactory(name='Test segment') rule = OriginCountryRuleFactory(segment=segment, country='GB') request = rf.get('/', HTTP_CF_IPCOUNTRY='NL') assert not rule.test_user(request)