Skip to content
1 change: 0 additions & 1 deletion cyder/api/v1/endpoints/core/system/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@ class SystemViewSet(CommonAPIViewSet):
serializer_class = SystemSerializer
avmodel = SystemAV
model = System

1 change: 0 additions & 1 deletion cyder/api/v1/endpoints/dhcp/dynamic_interface/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Meta:
model = DynamicInterfaceAV



class DynamicInterfaceAVViewSet(api.CommonDHCPViewSet):
model = DynamicInterfaceAV
serializer_class = DynamicInterfaceAVSerializer
Expand Down
13 changes: 7 additions & 6 deletions cyder/api/v1/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def namehack(field):
This function is the meat of a hack to handle an issue where trying to
filter by attribute or view led to a very strange-seeming error. The issue
is that attribute names in records with attributes and view names in DNS
records are represented using SlugRelatedFields so they appear with friendly
names (like "Hardware type" in the former case and "public" in the latter
case), but because of how this filter module is written, it will just
attempt to search by the related field, confusing Django when it gets a
query requesting a field with a string primary key like "public".
records are represented using SlugRelatedFields so they appear with
friendly names (like "Hardware type" in the former case and "public" in the
latter case), but because of how this filter module is written, it will
just attempt to search by the related field, confusing Django when it gets
a query requesting a field with a string primary key like "public".
"""
if field.endswith(("attribute", "views")):
return field + "__name"
Expand Down Expand Up @@ -93,7 +93,8 @@ def filter_queryset(self, request, queryset, view):
parent_ids = reduce((lambda x, y: x & y), parent_set_list)
kv_queryset = queryset.filter(id__in=parent_ids)
else:
raise InvalidQuery("This record type does not have attributes.")
raise InvalidQuery(
"This record type does not have attributes.")

if q_include or q_exclude:
f_queryset = queryset.filter(**q_include).exclude(**q_exclude)
Expand Down
4 changes: 1 addition & 3 deletions cyder/api/v1/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import json
from django.contrib.auth.models import User
from django.test.client import Client
from django.test import TestCase
from rest_framework.test import APIClient

from cyder.api.authtoken.models import Token
from cyder.base.eav.models import Attribute, EAVBase
from cyder.base.eav.models import Attribute
from cyder.base.eav.constants import ATTRIBUTE_INVENTORY
from cyder.base.eav.validators import VALUE_TYPES
from cyder.cydns.domain.models import Domain
from cyder.cydns.nameserver.models import Nameserver
from cyder.cydns.soa.models import SOA
Expand Down
18 changes: 9 additions & 9 deletions cyder/base/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def get_klasses(obj_type):
from cyder.cydns.cname.forms import CNAMEForm
from cyder.core.ctnr.forms import CtnrForm
from cyder.cydns.domain.forms import DomainForm
from cyder.cydhcp.interface.dynamic_intr.forms import (DynamicInterfaceForm,
DynamicInterfaceAVForm)
from cyder.cydhcp.interface.dynamic_intr.forms import (
DynamicInterfaceForm, DynamicInterfaceAVForm)
from cyder.cydns.mx.forms import MXForm
from cyder.cydns.nameserver.forms import NameserverForm
from cyder.cydhcp.network.forms import NetworkForm, NetworkAVForm
Expand All @@ -53,21 +53,21 @@ def get_klasses(obj_type):
from cyder.cydns.srv.forms import SRVForm
from cyder.cydns.sshfp.forms import SSHFPForm
from cyder.core.system.forms import SystemForm, SystemAVForm
from cyder.cydhcp.interface.static_intr.forms import (StaticInterfaceForm,
StaticInterfaceAVForm)
from cyder.cydhcp.interface.static_intr.forms import (
StaticInterfaceForm, StaticInterfaceAVForm)
from cyder.cydns.txt.forms import TXTForm
from cyder.cydhcp.vlan.forms import VlanForm, VlanAVForm
from cyder.cydhcp.vrf.forms import VrfForm, VrfAVForm
from cyder.cydhcp.workgroup.forms import WorkgroupForm, WorkgroupAVForm

from cyder.models import (
AddressRecord, CNAME, Ctnr, Domain, DynamicInterface, DynamicInterfaceAV,
MX, Nameserver, Network, NetworkAV, PTR, Range, RangeAV, Site, SiteAV, SOA,
SOAAV, SRV, SSHFP, StaticInterface, StaticInterfaceAV, System, SystemAV,
TXT, Vlan, VlanAV, Vrf, VrfAV, Workgroup, WorkgroupAV
AddressRecord, CNAME, Ctnr, Domain, DynamicInterface,
DynamicInterfaceAV, MX, Nameserver, Network, NetworkAV, PTR, Range,
RangeAV, Site, SiteAV, SOA, SOAAV, SRV, SSHFP, StaticInterface,
StaticInterfaceAV, System, SystemAV, TXT, Vlan, VlanAV, Vrf, VrfAV,
Workgroup, WorkgroupAV
)


klasses = {
'address_record': (AddressRecord, AddressRecordForm),
'cname': (CNAME, CNAMEForm),
Expand Down
24 changes: 12 additions & 12 deletions cyder/base/eav/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, *args, **kwargs):
if self.attribute_type_field is None:
raise Exception("The 'attribute_type_field' argument is required")

kwargs['blank'] = False # always run validate()
kwargs['blank'] = False # always run validate()

super(AttributeValueTypeField, self).__init__(*args, **kwargs)

Expand Down Expand Up @@ -79,7 +79,7 @@ def __init__(self, *args, **kwargs):

def to_python(self, value):
if not isinstance(value, unicode):
value = value.decode() # force it to unicode; assume ASCII
value = value.decode() # force it to unicode; assume ASCII

value = value.strip()

Expand All @@ -93,7 +93,7 @@ def validate(self, value, model_instance):

if attribute.attribute_type in (ATTRIBUTE_OPTION, ATTRIBUTE_STATEMENT):
try:
value.encode('ascii') # ignore the return value
value.encode('ascii') # ignore the return value
except UnicodeEncodeError:
raise ValidationError('DHCP option or statement value can '
'only contain ASCII characters')
Expand Down Expand Up @@ -183,19 +183,19 @@ def to_python(self, value):
add_introspection_rules(
[
(
[AttributeValueTypeField], # model
[], # args
{'attribute_type_field': ('', {'is_value': True})} # kwargs
[AttributeValueTypeField], # model
[], # args
{'attribute_type_field': ('', {'is_value': True})} # kwargs
),
(
[EAVValueField], # model
[], # args
{'attribute_field': ('', {'is_value': True})} # kwargs
[EAVValueField], # model
[], # args
{'attribute_field': ('', {'is_value': True})} # kwargs
),
(
[EAVAttributeField], # model
[], # args
{} # kwargs
[EAVAttributeField], # model
[], # args
{} # kwargs
)
],
[
Expand Down
4 changes: 0 additions & 4 deletions cyder/base/eav/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from django import forms
from django.core.exceptions import ValidationError

from cyder.base.eav.constants import ATTRIBUTE_TYPES
from cyder.base.eav.models import Attribute


def get_eav_form(eav_model, entity_model):
Expand Down
21 changes: 9 additions & 12 deletions cyder/base/eav/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.core.exceptions import ValidationError
from django.test import TestCase

from cyder.base.eav import utils as u
Expand All @@ -9,28 +8,26 @@ def _valid(self, func, value_list):
for value in value_list:
self.assertTrue(func(value))


def _invalid(self, func, value_list):
for value in value_list:
self.assertFalse(func(value))


def test_is_hex_byte(self):
self._valid(u.is_hex_byte, ('01', '23', '45', '67', '89', 'ab', 'cd',
'ef', 'AB', 'CD', 'EF'))
self._invalid(u.is_hex_byte, (
'012', # too many digits
'no', # invalid byte
'0x01', # '0x' not allowed (we already know it's hex)
'-1a', # negative bytes not allowed
'012', # too many digits
'no', # invalid byte
'0x01', # '0x' not allowed (we already know it's hex)
'-1a', # negative bytes not allowed
))

def test_is_hex_byte_sequence(self):
self._valid(u.is_hex_byte_sequence, ('01', '01:23:45:67:89:ab:cd:ef'))
self._invalid(u.is_hex_byte_sequence, (
'012:34', # invalid byte
'01::23', # too many consecutive colons
'01:', # trailing colon
':01', # leading colon
'yes:no', # invalid bytes
'012:34', # invalid byte
'01::23', # too many consecutive colons
'01:', # trailing colon
':01', # leading colon
'yes:no', # invalid bytes
))
83 changes: 35 additions & 48 deletions cyder/base/eav/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,76 +9,67 @@ def _valid(self, validator, value_list):
for value in value_list:
validator(value)


def _invalid(self, validator, value_list):
for value in value_list:
self.assertRaises(ValidationError, lambda: validator(value))


def test_flag(self):
self._valid(v.flag, ('on', 'off', 'true', 'false', 'oN', 'True'))
self._invalid(v.flag, ('truth', 'yes', 'no', '0', '1'))


def test_uint8(self):
self._valid(v.uint8, ('0', '10', '255',
'0x0', '0xa', '0xFF'))
'0x0', '0xa', '0xFF'))
self._invalid(v.uint8, ('-1', 'a', '256',
'-0x1', '0x-1', '0x100'))

'-0x1', '0x-1', '0x100'))

def test_int8(self):
self._valid(v.int8, ('-128', '-10', '0', '10', '127',
'0x0', '0xa', '0x7F'))
'0x0', '0xa', '0x7F'))
self._invalid(v.int8, ('-129', '1a', '128'))


def test_domain(self):
self._valid(v.domain, ('com', 'example.com', 'example.com.'))
self._invalid(v.domain, (
'.com', # missing label
'example.com..', # too many dots
'example..com', # too many dots
'.com', # missing label
'example.com..', # too many dots
'example..com', # too many dots
))


def test_host(self):
self._valid(v.host, (
'foo', 'example.com', 'example.com.', '1.2.3.4', '127.0.0.1',
))
self._invalid(v.host, (
'.com', # missing label
'example.com..', # too many dots
'example..com', # too many dots
'.com', # missing label
'example.com..', # too many dots
'example..com', # too many dots
))


def test_domain_list(self):
self._valid(v.domain_list, (
'"example.com"',
'"example.com", "example.org." ,"example.edu"',
))
self._invalid(v.domain_list, (
'example.com', # no quotes
'"example.com" "example.org." "example.edu"', # no commas
'example.com', # no quotes
'"example.com" "example.org." "example.edu"', # no commas
))


def test_host_pair(self):
self._valid(v.host_pair, (
'example.com example.org',
'1.2.3.4 example.com',
'example.com 1.2.3.4', # multiple spaces
'example.com 1.2.3.4', # multiple spaces
'1.2.3.4 127.0.0.1',
))
self._invalid(v.host_pair, (
'example.com, example.org', # comma
'example..com example.org', # invalid host
'example.com', # too few hosts
'1.2.3.4 127.0.0.1 192.168.0.1', # too many hosts
'example.com, example.org', # comma
'example..com example.org', # invalid host
'example.com', # too few hosts
'1.2.3.4 127.0.0.1 192.168.0.1', # too many hosts
))


def test_host_pair_list(self):
self._valid(v.host_pair_list, (
'example.com example.org',
Expand All @@ -87,63 +78,59 @@ def test_host_pair_list(self):
'example.com 1.2.3.4, example.org 127.0.0.1',
))
self._invalid(v.host_pair_list, (
'example.com', # too few hosts in one pair
'example.com, example.org example.edu', # too few hosts in one pair
'1.2.3.4 127.0.0.1 192.168.0.1', # too many hosts in one pair
'example.com', # too few hosts in one pair
'example.com, example.org example.edu', # too few hosts in one
# pair
'1.2.3.4 127.0.0.1 192.168.0.1', # too many hosts in one pair
))


def test_flag_host_list(self):
self._valid(v.flag_host_list, (
'true example.com, example.org',
'false example.com, example.org, 127.0.0.1',
'on 127.0.0.1',
))
self._invalid(v.flag_host_list, (
'true', # no hosts
'false example.com example.org', # no space between hosts
'yes, 127.0.0.1', # comma after flag
'true', # no hosts
'false example.com example.org', # no space between hosts
'yes, 127.0.0.1', # comma after flag
))


def test_text(self):
self._valid(v.text, (
'foo', 'foo bar', "'foo bar'", '!@#$%^&*()_',
# hex byte sequence in text is valid but not special
'3A:B0', 'DE:AD:be:ef',
))
self._invalid(v.text, (
'"foo"', # double quote
'foo"bar', # double quote
'"foo"', # double quote
'foo"bar', # double quote
))


def test_string(self):
self._valid(v.string, (
'foo', 'foo bar', 'foo bar baz', "'foo bar baz'",
# hex byte sequence in string is valid and special
'3A:B0', 'DE:AD:be:ef',
))
self._invalid(v.string, (
'"foo"', # double quotes
'foo"bar', # double quote
'"foo"', # double quotes
'foo"bar', # double quote
))


def test_identifier(self):
self._valid(v.identifier, (
'f', 'foobar', 'FooBar', 'Foo_Bar_', 'Foo-Bar-', 'foo9bar',
'9foobar', 'foobar9',
'9-9', '9_9', # don't ask
'9-9', '9_9', # don't ask
))
self._invalid(v.identifier, (
'99', # all digits
'foo:bar', # ':'
'"foobar"', # double quote
"'foobar", # single quote
'99', # all digits
'foo:bar', # ':'
'"foobar"', # double quote
"'foobar", # single quote
))


def test_flag_optional_text(self):
self._valid(v.flag_optional_text, (
'true',
Expand All @@ -153,7 +140,7 @@ def test_flag_optional_text(self):
'On "!@#$%^&*_"',
))
self._invalid(v.flag_optional_text, (
'truth', # invalid flag
'true foobar', # no quotes around text
'false, "foobar"', # comma between flag and text
'truth', # invalid flag
'true foobar', # no quotes around text
'false, "foobar"', # comma between flag and text
))
3 changes: 2 additions & 1 deletion cyder/base/eav/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from django.core.exceptions import ValidationError


default_validator = lambda x: x != '' # FIXME: Do we need this?
default_validator = lambda x: x != '' # FIXME: Do we need this?


def validate_list(value, validator=default_validator, separator=',',
strip_whitespace=True, min_length=0, die=False):
Expand Down
Loading