# Path: iprestrict/models.py
# TYPE_LOCATION = 'location'
# TYPE_RANGE = 'range'
#     TYPE_CHOICES = ((TYPE_LOCATION, 'Location based'),
#                     (TYPE_RANGE, 'Range based'))
#     TYPE = None
#     TYPE = TYPE_RANGE
#     TYPE = TYPE_LOCATION
#     ACTION_CHOICES = (
#         ('A', 'ALLOW'),
#         ('D', 'DENY')
#     )
# class IPGroupManager(models.Manager):
# class IPGroup(models.Model):
#     class Meta:
# class RangeBasedIPGroup(IPGroup):
#     class Meta:
# class LocationBasedIPGroup(IPGroup):
#     class Meta:
# class IPRange(models.Model):
#     class Meta:
# class IPLocation(models.Model):
#     class Meta:
# class Rule(models.Model):
#     class Meta:
# class ReloadRulesRequest(models.Model):
#     def get_queryset(self):
#     def __init__(self, *args, **kwargs):
#     def load(self):
#     def save(self, *args, **kwargs):
#     def __str__(self):
# def typed_ip_group(ip_group):
#     def load_ranges(self):
#     def ranges(self, ip_type=None):
#     def matches(self, ip):
#     def details_str(self):
#     def load_locations(self):
#     def matches(self, ip):
#     def details_str(self):
#     def start(self):
#     def end(self):
#     def ip_type(self):
#     def __contains__(self, ip):
#     def __str__(self):
#     def __contains__(self, country_code):
#     def __str__(self):
#     def __init__(self, *args, **kwargs):
#     def regex(self):
#     def matches_url(self, url):
#     def matches_ip(self, ip):
#     def is_restricted(self):
#     def is_allowed(self):
#     def action_str(self):
#     def swap_with_rule(self, other):
#     def move_up(self):
#     def move_up_url(self):
#     def move_down_url(self):
#     def move_down(self):
#     def save(self, *args, **kwargs):
#     def request_reload(cls):
#     def last_request():


from django.test import TestCase
from iprestrict import models
import mock
import iprestrict


class IPRestrictorOneUrlAllowedFromOneIpTest(TestCase):
    def setUp(self):
        localip = models.RangeBasedIPGroup.objects.create(name='localip')
        models.IPRange.objects.create(ip_group=localip, first_ip=IP)
        models.Rule.objects.create(url_pattern=SOME_URL, ip_group=localip, action='A')
        self.restrictor = iprestrict.IPRestrictor()

    def test_ip_matches_url_does_not(self):
        self.assertTrue(self.restrictor.is_restricted('', IP))

    def test_url_matches_ip_does_not(self):
        self.assertTrue(self.restrictor.is_restricted(SOME_URL, '192.168.1.1'))

    def test_both_match(self):
        self.assertFalse(self.restrictor.is_restricted(SOME_URL, IP))

    def test_reload_if_ipgroup_changed(self):
        self.assertTrue(self.restrictor.is_restricted(SOME_URL, '10.10.10.10'))
        localhost = models.RangeBasedIPGroup.objects.get(name='localhost')
        models.IPRange.objects.create(ip_group=localhost, first_ip='10.10.10.10')

        self.assertTrue(self.restrictor.is_restricted(SOME_URL, '10.10.10.10'))
        self.restrictor.reload_rules()
        self.assertFalse(self.restrictor.is_restricted(SOME_URL, '10.10.10.10'))


class IPRestrictorOneIpAllowedIfNotFromOneCountryTest(TestCase):
    def setUp(self):

# Path: iprestrict/models.py
# TYPE_LOCATION = 'location'
# TYPE_RANGE = 'range'
#     TYPE_CHOICES = ((TYPE_LOCATION, 'Location based'),
#                     (TYPE_RANGE, 'Range based'))
#     TYPE = None
#     TYPE = TYPE_RANGE
#     TYPE = TYPE_LOCATION
#     ACTION_CHOICES = (
#         ('A', 'ALLOW'),
#         ('D', 'DENY')
#     )
# class IPGroupManager(models.Manager):
# class IPGroup(models.Model):
#     class Meta:
# class RangeBasedIPGroup(IPGroup):
#     class Meta:
# class LocationBasedIPGroup(IPGroup):
#     class Meta:
# class IPRange(models.Model):
#     class Meta:
# class IPLocation(models.Model):
#     class Meta:
# class Rule(models.Model):
#     class Meta:
# class ReloadRulesRequest(models.Model):
#     def get_queryset(self):
#     def __init__(self, *args, **kwargs):
#     def load(self):
#     def save(self, *args, **kwargs):
#     def __str__(self):
# def typed_ip_group(ip_group):
#     def load_ranges(self):
#     def ranges(self, ip_type=None):
#     def matches(self, ip):
#     def details_str(self):
#     def load_locations(self):
#     def matches(self, ip):
#     def details_str(self):
#     def start(self):
#     def end(self):
#     def ip_type(self):
#     def __contains__(self, ip):
#     def __str__(self):
#     def __contains__(self, country_code):
#     def __str__(self):
#     def __init__(self, *args, **kwargs):
#     def regex(self):
#     def matches_url(self, url):
#     def matches_ip(self, ip):
#     def is_restricted(self):
#     def is_allowed(self):
#     def action_str(self):
#     def swap_with_rule(self, other):
#     def move_up(self):
#     def move_up_url(self):
#     def move_down_url(self):
#     def move_down(self):
#     def save(self, *args, **kwargs):
#     def request_reload(cls):
#     def last_request():


# Path: tests/test_restrictor.py
from django.test import TestCase
from iprestrict import models
import mock
import iprestrict



class IPRestrictorOneUrlAllowedFromOneIpTest(TestCase):
    def setUp(self):
        localip = models.RangeBasedIPGroup.objects.create(name='localip')
        models.IPRange.objects.create(ip_group=localip, first_ip=IP)
        models.Rule.objects.create(url_pattern=SOME_URL, ip_group=localip, action='A')
        self.restrictor = iprestrict.IPRestrictor()

    def test_ip_matches_url_does_not(self):
        self.assertTrue(self.restrictor.is_restricted('', IP))

    def test_url_matches_ip_does_not(self):
        self.assertTrue(self.restrictor.is_restricted(SOME_URL, '192.168.1.1'))

    def test_both_match(self):
        self.assertFalse(self.restrictor.is_restricted(SOME_URL, IP))

    def test_reload_if_ipgroup_changed(self):
        self.assertTrue(self.restrictor.is_restricted(SOME_URL, '10.10.10.10'))
        localhost = models.RangeBasedIPGroup.objects.get(name='localhost')
        models.IPRange.objects.create(ip_group=localhost, first_ip='10.10.10.10')

        self.assertTrue(self.restrictor.is_restricted(SOME_URL, '10.10.10.10'))
        self.restrictor.reload_rules()
        self.assertFalse(self.restrictor.is_restricted(SOME_URL, '10.10.10.10'))


class IPRestrictorOneIpAllowedIfNotFromOneCountryTest(TestCase):
    def setUp(self):

        us = models.LocationBasedIPGroup.objects.create(name='US')