<commit_before>from django.test.client import Client

from nose.tools import eq_

import cyder.base.tests
from cyder.base.tests.test_views_template import build, random_label
from cyder.core.ctnr.models import Ctnr
from cyder.cydns.address_record.models import AddressRecord
from cyder.cydns.cname.models import CNAME
from cyder.cydns.domain.models import Domain
from cyder.cydns.ip.utils import ip_to_domain_name
from cyder.cydns.mx.models import MX
from cyder.cydns.nameserver.models import Nameserver
from cyder.cydns.ptr.models import PTR
from cyder.cydns.srv.models import SRV
from cyder.cydns.txt.models import TXT
from cyder.cydns.sshfp.models import SSHFP
from cyder.cydns.view.models import View

from cyder.cydns.tests.utils import create_fake_zone


def do_setUp(self, test_class, test_data, use_ctnr=True,
             use_domain=True, use_rdomain=False):
    self.ctnr = Ctnr.objects.get(name='test_ctnr')
    if use_ctnr:
        test_data['ctnr'] = self.ctnr

    self.client = Client()
    self.client.login(username='test_superuser', password='password')
    self.test_class = test_class
    self.public_view = View.objects.get_or_create(name='public')[0]
    self.private_view = View.objects.get_or_create(name='private')[0]

    # Create forward zone.
    self.domain = create_fake_zone(random_label(), suffix='.oregonstate.edu')
    self.ctnr.domains.add(self.domain)
    self.soa = self.domain.soa
    self.subdomain = Domain.objects.create(
        name=random_label() + '.' + self.domain.name, soa=self.soa)
    self.ctnr.domains.add(self.subdomain)

    self.reverse_domain = create_fake_zone('196.in-addr.arpa', suffix='')
    self.ctnr.domains.add(self.reverse_domain)
    self.soa2 = self.reverse_domain.soa

    # Create test object.
    test_data = dict(test_data.items())
    if use_domain:
        test_data['domain'] = self.domain
    if use_rdomain:
        test_data['reverse_domain'] = self.reverse_domain
    self.test_obj, create = test_class.objects.get_or_create(**test_data)


class NoNSTests(object):

    def get_domain_and_post_data(self):
        # This is different for classes that have ips instead of fqdns
        domain_name = "{0}.{1}.{2}.{3}.com".format(
            random_label(), random_label(), random_label(), random_label()
        )
        root_domain = create_fake_zone(domain_name, suffix="")
        post_data = self.post_data()
        # Get the '_' in SRV records
        post_data['fqdn'] = post_data['fqdn'][0] + "asdf.asdf." + domain_name
        return root_domain, post_data

    def test_no_ns_in_view(self):
        root_domain, post_data = self.get_domain_and_post_data()
        ns = root_domain.nameserver_set.all()[0]
        ns.views.remove(self.public_view)
        ns.views.remove(self.private_view)
        # We now have a zone with nameservers that aren't in any views. No
        # record should be allowed to be in the view

        start_obj_count = self.test_class.objects.all().count()
        post_data['views'] = [self.public_view.pk]

        # Create the object then get the object
        resp = self.client.post(self.test_class.get_create_url(),
                                post_data, follow=True)
        self.assertEqual(resp.status_code, 200)
        new_obj_count = self.test_class.objects.all().count()

        # Nothing should have been created
        self.assertEqual(start_obj_count, new_obj_count)

        ns.views.add(self.public_view)

        # Okay, we should be able to add to the public view now
        start_obj_count = self.test_class.objects.all().count()
        resp = self.client.post(self.test_class.get_create_url(),
                                post_data, follow=True)
        new_obj_count = self.test_class.objects.all().count()
        self.assertEqual(start_obj_count + 1, new_obj_count)


class AddressRecordViewTests(cyder.base.tests.TestCase, NoNSTests):
    fixtures = ['test_users/test_users.json']
    name = 'address_record'

    def setUp(self):
        test_data = {
            'label': random_label(),
            'ip_type': '4',
            'ip_str': '172.16.58.3',
        }
        do_setUp(self, AddressRecord, test_data)

    def post_data(self):
        return {
            'fqdn': self.domain.name,
            'ip_type': '4',
            'ip_str': '192.168.3.11',
            'ttl': '400',
            'description': 'yo',
            'ctnr': self.ctnr.pk,
        }


class CNAMEViewTests(cyder.base.tests.TestCase, NoNSTests):
    fixtures = ['test_users/test_users.json']
    name = 'cname'

    def setUp(self):
        test_data = {
            'label': random_label(),
            'target': random_label()
        }
        do_setUp(self, CNAME, test_data)

    def post_data(self):
        return {
            'fqdn': self.subdomain.name,
            'label': random_label(),
            'target': random_label(),
            'ctnr': self.ctnr.pk,
        }


class NSViewTests(cyder.base.tests.TestCase):
    fixtures = ['test_users/test_users.json']
    name = 'nameserver'

    def setUp(self):
        self.domain = create_fake_zone("foobarbaz.com")
        test_data = {
            'server': self.domain.name
        }
        do_setUp(self, Nameserver, test_data)

    def post_data(self):
        return {
            'fqdn': self.domain.name,
            'server': self.domain.name
        }

    def test_no_ns_in_view(self):
        root_domain = create_fake_zone("asdfdjhjd")
        ns = root_domain.nameserver_set.all()[0]
<commit_msg>add domain to ctnr in broken NSViewTest<commit_after>from django.test.client import Client

from nose.tools import eq_

import cyder.base.tests
from cyder.base.tests.test_views_template import build, random_label
from cyder.core.ctnr.models import Ctnr
from cyder.cydns.address_record.models import AddressRecord
from cyder.cydns.cname.models import CNAME
from cyder.cydns.domain.models import Domain
from cyder.cydns.ip.utils import ip_to_domain_name
from cyder.cydns.mx.models import MX
from cyder.cydns.nameserver.models import Nameserver
from cyder.cydns.ptr.models import PTR
from cyder.cydns.srv.models import SRV
from cyder.cydns.txt.models import TXT
from cyder.cydns.sshfp.models import SSHFP
from cyder.cydns.view.models import View

from cyder.cydns.tests.utils import create_fake_zone


def do_setUp(self, test_class, test_data, use_ctnr=True,
             use_domain=True, use_rdomain=False):
    self.ctnr = Ctnr.objects.get(name='test_ctnr')
    if use_ctnr:
        test_data['ctnr'] = self.ctnr

    self.client = Client()
    self.client.login(username='test_superuser', password='password')
    self.test_class = test_class
    self.public_view = View.objects.get_or_create(name='public')[0]
    self.private_view = View.objects.get_or_create(name='private')[0]

    # Create forward zone.
    self.domain = create_fake_zone(random_label(), suffix='.oregonstate.edu')
    self.ctnr.domains.add(self.domain)
    self.soa = self.domain.soa
    self.subdomain = Domain.objects.create(
        name=random_label() + '.' + self.domain.name, soa=self.soa)
    self.ctnr.domains.add(self.subdomain)

    self.reverse_domain = create_fake_zone('196.in-addr.arpa', suffix='')
    self.ctnr.domains.add(self.reverse_domain)
    self.soa2 = self.reverse_domain.soa

    # Create test object.
    test_data = dict(test_data.items())
    if use_domain:
        test_data['domain'] = self.domain
    if use_rdomain:
        test_data['reverse_domain'] = self.reverse_domain
    self.test_obj, create = test_class.objects.get_or_create(**test_data)


class NoNSTests(object):

    def get_domain_and_post_data(self):
        # This is different for classes that have ips instead of fqdns
        domain_name = "{0}.{1}.{2}.{3}.com".format(
            random_label(), random_label(), random_label(), random_label()
        )
        root_domain = create_fake_zone(domain_name, suffix="")
        post_data = self.post_data()
        # Get the '_' in SRV records
        post_data['fqdn'] = post_data['fqdn'][0] + "asdf.asdf." + domain_name
        return root_domain, post_data

    def test_no_ns_in_view(self):
        root_domain, post_data = self.get_domain_and_post_data()
        ns = root_domain.nameserver_set.all()[0]
        ns.views.remove(self.public_view)
        ns.views.remove(self.private_view)
        # We now have a zone with nameservers that aren't in any views. No
        # record should be allowed to be in the view

        start_obj_count = self.test_class.objects.all().count()
        post_data['views'] = [self.public_view.pk]

        # Create the object then get the object
        resp = self.client.post(self.test_class.get_create_url(),
                                post_data, follow=True)
        self.assertEqual(resp.status_code, 200)
        new_obj_count = self.test_class.objects.all().count()

        # Nothing should have been created
        self.assertEqual(start_obj_count, new_obj_count)

        ns.views.add(self.public_view)

        # Okay, we should be able to add to the public view now
        start_obj_count = self.test_class.objects.all().count()
        resp = self.client.post(self.test_class.get_create_url(),
                                post_data, follow=True)
        new_obj_count = self.test_class.objects.all().count()
        self.assertEqual(start_obj_count + 1, new_obj_count)


class AddressRecordViewTests(cyder.base.tests.TestCase, NoNSTests):
    fixtures = ['test_users/test_users.json']
    name = 'address_record'

    def setUp(self):
        test_data = {
            'label': random_label(),
            'ip_type': '4',
            'ip_str': '172.16.58.3',
        }
        do_setUp(self, AddressRecord, test_data)

    def post_data(self):
        return {
            'fqdn': self.domain.name,
            'ip_type': '4',
            'ip_str': '192.168.3.11',
            'ttl': '400',
            'description': 'yo',
            'ctnr': self.ctnr.pk,
        }


class CNAMEViewTests(cyder.base.tests.TestCase, NoNSTests):
    fixtures = ['test_users/test_users.json']
    name = 'cname'

    def setUp(self):
        test_data = {
            'label': random_label(),
            'target': random_label()
        }
        do_setUp(self, CNAME, test_data)

    def post_data(self):
        return {
            'fqdn': self.subdomain.name,
            'label': random_label(),
            'target': random_label(),
            'ctnr': self.ctnr.pk,
        }


class NSViewTests(cyder.base.tests.TestCase):
    fixtures = ['test_users/test_users.json']
    name = 'nameserver'

    def setUp(self):
        self.domain = create_fake_zone("foobarbaz.com")
        test_data = {
            'server': self.domain.name
        }
        do_setUp(self, Nameserver, test_data)

    def post_data(self):
        return {
            'fqdn': self.domain.name,
            'server': self.domain.name
        }

    def test_no_ns_in_view(self):
        root_domain = create_fake_zone("asdfdjhjd")
        self.ctnr.domains.add(root_domain)
        ns = root_domain.nameserver_set.all()[0]

{'fraction_non_alphanumeric': 0.11540196697645042, 'fraction_numerical': 0.007037805648290174, 'mean_word_length': 2.7282206525395223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7296608', 'n_tokens_mistral': 3743, 'n_tokens_neox': 3621, 'n_words': 681}