예제 #1
0
    def setUp(self):
        self.location_model_patcher = patch.object(
            settings, 'GEOIP_LOCATION_MODEL',
            'test_app.models.MyCustomLocation')
        self.location_model = self.location_model_patcher.start()

        self.locator = Locator(RequestFactory().get('/'))
예제 #2
0
def get_location(request):
    from django_geoip.base import Locator
    if not hasattr(request, '_cached_location'):
        request._cached_location = Locator(request).locate()
    return request._cached_location
예제 #3
0
 def test_get_stored_location_none(self):
     request = self.factory.get('/')
     self.assertEqual(Locator(request)._get_stored_location(), None)
예제 #4
0
 def test_get_stored_location_ok(self):
     self.factory.cookies[settings.GEOIP_COOKIE_NAME] = 200
     request = self.factory.get('/')
     self.assertEqual(
         Locator(request)._get_stored_location(), self.my_location)
예제 #5
0
class LocatorTest(TestCase):
    def setUp(self):
        self.location_model_patcher = patch.object(
            settings, 'GEOIP_LOCATION_MODEL',
            'test_app.models.MyCustomLocation')
        self.location_model = self.location_model_patcher.start()

        self.locator = Locator(RequestFactory().get('/'))

    def tearDown(self):
        self.location_model_patcher.stop()

    def test_get_stored_location_none(self):
        self.assertEqual(self.locator._get_stored_location(), None)

        self.locator.request.COOKIES['geoip_location_id'] = 1
        self.assertEqual(self.locator._get_stored_location(), None)

    def test_get_stored_location_ok(self):
        location = create_custom_location(MyCustomLocation, name='location1')
        self.locator.request.COOKIES['geoip_location_id'] = location.id
        self.assertEqual(self.locator._get_stored_location(), location)

    @patch('django_geoip.base.Locator._get_real_ip')
    def test_get_ip_range_none(self, mock_get_ip):
        mock_get_ip.return_value = '1.2.3.4'
        self.assertEqual(self.locator._get_ip_range(), None)

    @patch('django_geoip.base.Locator._get_real_ip')
    @patch('django_geoip.models.IpRange.objects.by_ip')
    def test_get_ip_range_ok(self, by_ip, mock_get_ip):
        mock_get_ip.return_value = '1.2.3.4'

        self.assertEqual(self.locator._get_ip_range(), by_ip.return_value)
        by_ip.assert_called_once_with('1.2.3.4')

    @patch('django_geoip.base.Locator._get_stored_location')
    def test_is_store_empty(self, mock_get_stored):
        mock_get_stored.return_value = None
        self.assertTrue(self.locator.is_store_empty())
        mock_get_stored.return_value = 1
        self.assertFalse(self.locator.is_store_empty())

    @patch('test_app.models.MyCustomLocation.get_by_ip_range')
    @patch('test_app.models.MyCustomLocation.get_default_location')
    def test_get_corresponding_location_doesnotexists(
            self, mock_get_default_location, mock_get_by_ip_range):
        mock_get_by_ip_range.side_effect = MyCustomLocation.DoesNotExist
        ip_range = Mock()
        self.locator._get_corresponding_location(ip_range)
        mock_get_by_ip_range.assert_called_once_with(ip_range)
        mock_get_default_location.assert_called_once()

    @patch('test_app.models.MyCustomLocation.get_by_ip_range')
    @patch('test_app.models.MyCustomLocation.get_default_location')
    def test_get_corresponding_location_exception(self,
                                                  mock_get_default_location,
                                                  mock_get_by_ip_range):
        mock_get_by_ip_range.side_effect = None
        ip_range = Mock()
        self.locator._get_corresponding_location(ip_range)
        mock_get_by_ip_range.assert_called_once_with(ip_range)
        mock_get_default_location.assert_called_once()

    @patch('test_app.models.MyCustomLocation.get_by_ip_range')
    @patch('test_app.models.MyCustomLocation.get_default_location')
    def test_get_corresponding_location_ok(self, mock_get_default_location,
                                           mock_get_by_ip_range):
        range = create_ip_range()
        self.locator._get_corresponding_location(range)
        mock_get_by_ip_range.assert_called_once_with(range)
        self.assertFalse(mock_get_default_location.called)

    @patch('django_geoip.base.Locator._get_stored_location')
    def test_locate_from_stored(self, mock_stored):
        self.assertEqual(self.locator.locate(), mock_stored.return_value)

    @patch('django_geoip.base.Locator._get_stored_location')
    @patch('django_geoip.base.Locator._get_corresponding_location')
    def test_locate_not_stored(self, mock_corresponding, mock_stored):
        mock_stored.return_value = None
        self.assertEqual(self.locator.locate(),
                         mock_corresponding.return_value)
예제 #6
0
    def setUp(self):
        self.location_model_patcher = patch.object(settings, 'GEOIP_LOCATION_MODEL', 'test_app.models.MyCustomLocation')
        self.location_model = self.location_model_patcher.start()

        self.locator = Locator(RequestFactory().get('/'))
예제 #7
0
class LocatorTest(TestCase):
    def setUp(self):
        self.location_model_patcher = patch.object(settings, 'GEOIP_LOCATION_MODEL', 'test_app.models.MyCustomLocation')
        self.location_model = self.location_model_patcher.start()

        self.locator = Locator(RequestFactory().get('/'))

    def tearDown(self):
        self.location_model_patcher.stop()

    def test_get_stored_location_none(self):
        self.assertEqual(self.locator._get_stored_location(), None)

        self.locator.request.COOKIES['geoip_location_id'] = 1
        self.assertEqual(self.locator._get_stored_location(), None)

    def test_get_stored_location_ok(self):
        location = create_custom_location(MyCustomLocation, name='location1')
        self.locator.request.COOKIES['geoip_location_id'] = location.id
        self.assertEqual(self.locator._get_stored_location(), location)

    @patch('django_geoip.base.Locator._get_real_ip')
    def test_get_ip_range_none(self, mock_get_ip):
        mock_get_ip.return_value = '1.2.3.4'
        self.assertEqual(self.locator._get_ip_range(), None)

    @patch('django_geoip.base.Locator._get_real_ip')
    @patch('django_geoip.models.IpRange.objects.by_ip')
    def test_get_ip_range_ok(self, by_ip, mock_get_ip):
        mock_get_ip.return_value = '1.2.3.4'

        self.assertEqual(self.locator._get_ip_range(), by_ip.return_value)
        by_ip.assert_called_once_with('1.2.3.4')

    @patch('django_geoip.base.Locator._get_stored_location')
    def test_is_store_empty(self, mock_get_stored):
        mock_get_stored.return_value = None
        self.assertTrue(self.locator.is_store_empty())
        mock_get_stored.return_value = 1
        self.assertFalse(self.locator.is_store_empty())

    @patch('test_app.models.MyCustomLocation.get_by_ip_range')
    @patch('test_app.models.MyCustomLocation.get_default_location')
    def test_get_corresponding_location_doesnotexists(self, mock_get_default_location, mock_get_by_ip_range):
        mock_get_by_ip_range.side_effect = MyCustomLocation.DoesNotExist
        ip_range = Mock()
        self.locator._get_corresponding_location(ip_range)
        mock_get_by_ip_range.assert_called_once_with(ip_range)
        mock_get_default_location.assert_called_once()

    @patch('test_app.models.MyCustomLocation.get_by_ip_range')
    @patch('test_app.models.MyCustomLocation.get_default_location')
    def test_get_corresponding_location_exception(self, mock_get_default_location, mock_get_by_ip_range):
        mock_get_by_ip_range.side_effect = None
        ip_range = Mock()
        self.locator._get_corresponding_location(ip_range)
        mock_get_by_ip_range.assert_called_once_with(ip_range)
        mock_get_default_location.assert_called_once()

    @patch('test_app.models.MyCustomLocation.get_by_ip_range')
    @patch('test_app.models.MyCustomLocation.get_default_location')
    def test_get_corresponding_location_ok(self, mock_get_default_location, mock_get_by_ip_range):
        range = create_ip_range()
        self.locator._get_corresponding_location(range)
        mock_get_by_ip_range.assert_called_once_with(range)
        self.assertFalse(mock_get_default_location.called)

    @patch('django_geoip.base.Locator._get_stored_location')
    def test_locate_from_stored(self, mock_stored):
        self.assertEqual(self.locator.locate(), mock_stored.return_value)

    @patch('django_geoip.base.Locator._get_stored_location')
    @patch('django_geoip.base.Locator._get_corresponding_location')
    def test_locate_not_stored(self, mock_corresponding, mock_stored):
        mock_stored.return_value = None
        self.assertEqual(self.locator.locate(), mock_corresponding.return_value)